Generalized UriWithLabelElement to use super class
authorPatrick Plitzner <p.plitzner@bgbm.org>
Mon, 21 Sep 2015 13:02:39 +0000 (15:02 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Mon, 21 Sep 2015 13:02:39 +0000 (15:02 +0200)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/element/AbstractUriWithExceptionLabelElement.java [moved from eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/element/UriWithExceptionLabelElement.java with 64% similarity]
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/element/LsidWithExceptionLabelElement.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/element/UriWithLabelElement.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/media/ImageFileElement.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/media/MediaDetailElement.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/media/MediaRepresentationPartElement.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/name/ProtologueElement.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/occurrence/dna/SequenceGeneralDetailElement.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/reference/ReferenceDetailElement.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/vocabulary/NamedAreaDetailElement.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/vocabulary/TermVocabularyDetailElement.java

@@ -12,6 +12,7 @@ package eu.etaxonomy.taxeditor.ui.element;
 
 import org.eclipse.jface.resource.JFaceResources;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.ui.forms.widgets.TableWrapLayout;
@@ -24,30 +25,34 @@ import eu.etaxonomy.taxeditor.Messages;
  * @date Sep 21, 2015
  *
  */
-public abstract class UriWithExceptionLabelElement <T> extends TextWithLabelElement {
+public abstract class AbstractUriWithExceptionLabelElement <T> extends TextWithLabelElement {
 
     protected Label labelException;
 
-       protected UriWithExceptionLabelElement(CdmFormFactory formFactory,
+       protected AbstractUriWithExceptionLabelElement(CdmFormFactory formFactory,
                        ICdmFormElement parentElement, String labelString,
                        T initialObject, Integer textHeight, int style) {
                super(formFactory, parentElement, false);
 
-               //label
+        init(formFactory, labelString, initialObject, textHeight, style);
+    }
+
+    protected void init(CdmFormFactory formFactory, String labelString, T initialObject, Integer textHeight, int style) {
+        //label
         initLabel(formFactory, labelString, false, getLayoutComposite());
 
         //uri text
         initText(formFactory, null, textHeight, null, false, style, getLayoutComposite());
 
         //exceptionLabel
-               initExceptionLabel(formFactory, initialObject);
-       }
+        initExceptionLabel(getLayoutComposite(), formFactory, initialObject);
+    }
 
-    private void initExceptionLabel(CdmFormFactory formFactory, T initialObject) {
-        labelException = formFactory.createLabel(getLayoutComposite(), "", SWT.WRAP); //$NON-NLS-1$
+    protected void initExceptionLabel(Composite parent, CdmFormFactory formFactory, T initialObject) {
+        labelException = formFactory.createLabel(parent, "", SWT.WRAP); //$NON-NLS-1$
                int numColumns = AbstractFormSection.DEFAULT_NUM_COLUMNS;
-               if(getLayoutComposite().getLayout() instanceof TableWrapLayout){
-                   numColumns = ((TableWrapLayout)getLayoutComposite().getLayout()).numColumns;
+               if(parent.getLayout() instanceof TableWrapLayout){
+                   numColumns = ((TableWrapLayout)parent.getLayout()).numColumns;
                }
         labelException.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(numColumns, 1));
                addControl(labelException);
@@ -57,18 +62,19 @@ public abstract class UriWithExceptionLabelElement <T> extends TextWithLabelElem
 
     public abstract void setParsedText(T object);
 
-    public abstract T parseText();
+    protected abstract T getParsedText() throws Exception;
 
-    protected void updateExceptionLabel(Exception e){
-        if(e!=null){
-            labelException.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
-            labelException.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
-            labelException.setText(Messages.UriWithLabelElement_URL_NOT_SAVED+e.getLocalizedMessage());
-        }
-        else{
+    public T parseText(){
+        try {
             labelException.setFont(JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT));
             labelException.setForeground(getPersistentBackground());
             labelException.setText(""); //$NON-NLS-1$
+            return getParsedText();
+        } catch (Exception e) {
+            labelException.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
+            labelException.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
+            labelException.setText(Messages.UriWithLabelElement_URL_NOT_SAVED+e.getLocalizedMessage());
+            return null;
         }
     }
 
index be9be32f621ad584a81b1c8c34c0bfcb52e16efc..1bea7349c4a3d08ff09357f8d0d8a27701e0da9e 100644 (file)
@@ -18,7 +18,7 @@ import eu.etaxonomy.cdm.model.common.LSID;
  * @date Sep 21, 2015
  *
  */
-public class LsidWithExceptionLabelElement extends UriWithExceptionLabelElement<LSID> {
+public class LsidWithExceptionLabelElement extends AbstractUriWithExceptionLabelElement<LSID> {
 
     protected LsidWithExceptionLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement,
             String labelString, LSID initialObject, Integer textHeight, int style) {
@@ -39,14 +39,8 @@ public class LsidWithExceptionLabelElement extends UriWithExceptionLabelElement<
         * {@inheritDoc}
         */
        @Override
-       public LSID parseText() {
-        try {
-            updateExceptionLabel(null);
+       public LSID getParsedText() throws Exception {
             return new LSID(super.getText());
-        } catch (Exception e) {
-            updateExceptionLabel(e);
-            return null;
-        }
        }
 
 }
index b9d6300ecb985131e868427f47e5c4db8bf4a330..f9e8788c101ce1ab5f8950b2994f7866c62e1a7f 100644 (file)
@@ -15,17 +15,13 @@ import java.net.URI;
 
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
-import org.eclipse.jface.resource.JFaceResources;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Label;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.forms.widgets.TableWrapLayout;
 
 import eu.etaxonomy.taxeditor.Messages;
 import eu.etaxonomy.taxeditor.model.ImageResources;
@@ -37,17 +33,23 @@ import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
  * @created Dec 20, 2010
  * @version 1.0
  */
-public class UriWithLabelElement extends TextWithLabelElement {
+public class UriWithLabelElement extends AbstractUriWithExceptionLabelElement<URI> {
 
-    private final Label labelException;
-    private final Button btnOpenBrowser;
+    private Button btnOpenBrowser;
 
-       protected UriWithLabelElement(CdmFormFactory formFactory,
-                       ICdmFormElement parentElement, String labelString,
-                       URI initialUri, Integer textHeight, int style) {
-               super(formFactory, parentElement, false);
+    protected UriWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
+            URI initialObject, Integer textHeight, int style) {
+        super(formFactory, parentElement, labelString, initialObject, textHeight, style);
 
-               //label
+       }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    protected void init(CdmFormFactory formFactory, String labelString, URI initialObject, Integer textHeight, int style) {
+
+        //label
         initLabel(formFactory, labelString, false, getLayoutComposite());
 
         //composite(uri + button)
@@ -60,18 +62,18 @@ public class UriWithLabelElement extends TextWithLabelElement {
         initText(formFactory, null, textHeight, null, false, style, textAndButton);
 
         //button
-               btnOpenBrowser = formFactory.createButton(textAndButton, "", SWT.NONE); //$NON-NLS-1$
-               btnOpenBrowser.setImage(ImageResources.getImage(ImageResources.WEB));
-               btnOpenBrowser.setToolTipText(Messages.UriWithLabelElement_OPEN_EXTERNAL_BROWSER);
-               btnOpenBrowser.addSelectionListener(new SelectionAdapter() {
-                   @Override
-                   public void widgetSelected(SelectionEvent e) {
-                       String errorTitle = Messages.UriWithLabelElement_INVALID_URL;
-                       String errorText = Messages.UriWithLabelElement_COULD_NOT_OPEN_BROWSER;
-
-                       URI uri = getUri();
-                       if(uri!=null){
-                           try {
+        btnOpenBrowser = formFactory.createButton(textAndButton, "", SWT.NONE); //$NON-NLS-1$
+        btnOpenBrowser.setImage(ImageResources.getImage(ImageResources.WEB));
+        btnOpenBrowser.setToolTipText(Messages.UriWithLabelElement_OPEN_EXTERNAL_BROWSER);
+        btnOpenBrowser.addSelectionListener(new SelectionAdapter() {
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                String errorTitle = Messages.UriWithLabelElement_INVALID_URL;
+                String errorText = Messages.UriWithLabelElement_COULD_NOT_OPEN_BROWSER;
+
+                URI uri = parseText();
+                if(uri!=null){
+                    try {
                         PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(uri.toURL());
                     } catch (PartInitException pie) {
                         MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, pie));
@@ -80,42 +82,35 @@ public class UriWithLabelElement extends TextWithLabelElement {
                     } catch (IllegalArgumentException iae) {
                         MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, iae));
                     }
-                       }
-                       else{
-                           MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, null));
-                       }
-                   }
+                }
+                else{
+                    MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, null));
+                }
+            }
         });
-               btnOpenBrowser.setLayoutData(LayoutConstants.RIGHT());
-
-               labelException = formFactory.createLabel(getLayoutComposite(), "", SWT.WRAP); //$NON-NLS-1$
-               int numColumns = AbstractFormSection.DEFAULT_NUM_COLUMNS;
-               if(getLayoutComposite().getLayout() instanceof TableWrapLayout){
-                   numColumns = ((TableWrapLayout)getLayoutComposite().getLayout()).numColumns;
-               }
-        labelException.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(numColumns, 1));
-               addControl(labelException);
-               setUri(initialUri);
-       }
+        btnOpenBrowser.setLayoutData(LayoutConstants.RIGHT());
+
+        initExceptionLabel(getLayoutComposite(), formFactory, initialObject);
+    }
 
-       public void setUri(URI uri) {
-               if(uri != null){
-                       super.setText(uri.toString());
-               }
-       }
 
-       public URI getUri(){
-        try {
-            labelException.setFont(JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT));
-            labelException.setForeground(getPersistentBackground());
-            labelException.setText(""); //$NON-NLS-1$
-            return new URI(super.getText());
-        } catch (Exception e) {
-            labelException.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
-            labelException.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
-            labelException.setText(Messages.UriWithLabelElement_URL_NOT_SAVED+e.getLocalizedMessage());
-            return null;
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setParsedText(URI object) {
+        if(object != null){
+            super.setText(object.toString());
         }
-       }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    protected URI getParsedText() throws Exception {
+        return new URI(super.getText());
+    }
 
 }
index 9d016cb3175be5d881d8eb238c4c495ff5b1410d..7a5618b27e2fa1d5f9f73012d710bc414a6a4a48 100644 (file)
@@ -117,7 +117,7 @@ public class ImageFileElement extends MediaRepresentationPartElement<ImageFile>
        public void handleEvent(Object eventSource) {
                if(eventSource == text_uri){
                        try {
-                           URI uri = text_uri.getUri();
+                           URI uri = text_uri.parseText();
                                getEntity().setUri(uri);
                                if(uri==null){
                                    //buffer URI if parsing error occurred
index 1c27665647713fdd53c267767588114f683fb708..750fc27cef907a0342620fea4c5718d49c8ea6f3 100644 (file)
@@ -70,7 +70,7 @@ public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
     public void handleEvent(Object eventSource){
         if(eventSource==textUri){
             textUri.setBackground(getPersistentBackground());
-            URI uri = textUri.getUri();
+            URI uri = textUri.parseText();
             singleMediaRepresentationPart.setUri(uri);
             if(uri==null){
                 uriBuffer=textUri.getText();
@@ -113,11 +113,11 @@ public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
             }
             textUri = formFactory.createUriWithLabelElement(parentFormElement, "Media URI", null, style);
             URI uri = singleMediaRepresentationPart.getUri();
-            textUri.setUri(uri);
+            textUri.setParsedText(uri);
             //set buffered uri as text if uri had parsing problems in advanced view
             if(uri==null && uriBuffer!=null){
                 textUri.setText(uriBuffer);
-                textUri.getUri();
+                textUri.parseText();
             }
             textUri.getLayoutComposite().layout();
         }
index 663848da419d2bef3ba02e2b78c73f4c0911f147..e89b514bd6c54e6bdc43d83f43ff368ae6a2a16c 100644 (file)
@@ -58,12 +58,12 @@ public class MediaRepresentationPartElement<T extends MediaRepresentationPart> e
                        text_size.setText(FileUtils.byteCountToDisplaySize(entity.getSize()));
                }
                if(entity.getUri() != null){
-                       text_uri.setUri(entity.getUri());
+                       text_uri.setParsedText(entity.getUri());
                } else {
             String uriBuffer = getParentMediaDetailElement().getUriBuffer();
             if(uriBuffer!=null){
                 text_uri.setText(uriBuffer);
-                text_uri.getUri();//just to update the error label
+                text_uri.parseText();//just to update the error label
             }
         }
        }
@@ -71,7 +71,7 @@ public class MediaRepresentationPartElement<T extends MediaRepresentationPart> e
        @Override
        public void handleEvent(Object eventSource) {
                if(eventSource == text_uri){
-                   URI uri = text_uri.getUri();
+                   URI uri = text_uri.parseText();
                    getEntity().setUri(uri);
                    if(uri==null){
                 //buffer URI if parsing error occurred
index aa7a3ef6b84a3e12599efb74d08028327ac6d30d..8b38c76dec048e084da08b0e691cae2b97033754 100644 (file)
@@ -110,7 +110,7 @@ public class ProtologueElement extends AbstractEntityCollectionElement<Descripti
        @Override
        public void handleEvent(Object eventSource) {
                if(eventSource == protologueUriText && protologueUriText.getText()!=null){
-                   mediaRepresentationPart.setUri(protologueUriText.getUri());
+                   mediaRepresentationPart.setUri(protologueUriText.parseText());
                }
        }
 
index b6d5775dbeb533802fe582525aab5252d08a2099..ad27ecee85c49cef357128213384d198b08287b1 100644 (file)
@@ -113,9 +113,9 @@ public class SequenceGeneralDetailElement extends AbstractCdmDetailElement<Seque
             getEntity().setGeneticAccessionNumber(textGeneticAccessNo.getText());
             if(textGeneticAccessNo.getText()!=null && !textGeneticAccessNo.getText().isEmpty()){
                 try {
-                    textNCBIUri.setUri(getEntity().getGenBankUri());
-                    textENAUri.setUri(getEntity().getEmblUri());
-                    textDDBJUri.setUri(getEntity().getDdbjUri());
+                    textNCBIUri.setParsedText(getEntity().getGenBankUri());
+                    textENAUri.setParsedText(getEntity().getEmblUri());
+                    textDDBJUri.setParsedText(getEntity().getDdbjUri());
                 } catch (URISyntaxException e) {
                     textGeneticAccessNo.setBackground(getColor(Resources.COLOR_PARSE_ERROR));
                 }
@@ -131,7 +131,7 @@ public class SequenceGeneralDetailElement extends AbstractCdmDetailElement<Seque
             getEntity().setBoldProcessId(textBoldProcessID.getText());
             if(textBoldProcessID.getText()!=null && !textBoldProcessID.getText().isEmpty()){
                 try {
-                    textBoldUri.setUri(getEntity().getBoldUri());
+                    textBoldUri.setParsedText(getEntity().getBoldUri());
                 } catch (URISyntaxException e) {
                     textBoldProcessID.setBackground(getColor(Resources.COLOR_PARSE_ERROR));
                 }
index bdbab1697900c68af8eed7c30ee18368aee0f574..4ca0bef9c70b7024293e25857af7227afd027b82 100644 (file)
@@ -441,7 +441,7 @@ public class ReferenceDetailElement extends AbstractIdentifiableEntityDetailElem
                    getEntity().setAbbrevTitle(text_abbrevTitle.getText());
                    toggleableAbbrevCache.setText(text_abbrevTitle.getText());
                } else if (eventSource == text_uri) {
-                       getEntity().setUri(text_uri.getUri());
+                       getEntity().setUri(text_uri.parseText());
                } else if (eventSource == text_volume) {
                        getEntity().setVolume(text_volume.getText());
                }
index 0a5a6ad37156c72fc0099fe986efc2ba1e0ebb02..f2621ebd217500019e58521035fefab3f9856c2c 100644 (file)
@@ -66,7 +66,7 @@ public class NamedAreaDetailElement extends DefinedTermDetailElement<NamedArea>
                } else if (eventSource == text_description) {\r
                        getEntity().getRepresentation(Language.getDefaultLanguage()).setText(text_description.getText());\r
                } else if (eventSource == uri_uri) {\r
-                       getEntity().setUri(uri_uri.getUri());\r
+                       getEntity().setUri(uri_uri.parseText());\r
                } else if (eventSource == text_abbreviatedLabel) {\r
                        getEntity().getRepresentation(Language.getDefaultLanguage()).setAbbreviatedLabel(text_abbreviatedLabel.getText());\r
                } else if (eventSource == timePeriod_validPeriod) {\r
index 5f62d2417e571a62672f1a149a741c17bd13c39a..281b383e86be43be5d765258aba8012ed6cda478 100644 (file)
@@ -51,9 +51,9 @@ public class TermVocabularyDetailElement extends AbstractTermBaseDetailElement<T
                handleRepresentation(eventSource);\r
 \r
                if (eventSource == uri_uri) {\r
-                       getEntity().setUri(uri_uri.getUri());\r
+                       getEntity().setUri(uri_uri.parseText());\r
                } else if (eventSource == uri_uriTermSource) {\r
-                       getEntity().setTermSourceUri(uri_uriTermSource.getUri());\r
+                       getEntity().setTermSourceUri(uri_uriTermSource.parseText());\r
                }\r
        }\r
 \r