fix #9040: add open in browser button to orcid
authorKatja Luther <k.luther@bgbm.org>
Mon, 10 Aug 2020 10:39:53 +0000 (12:39 +0200)
committerKatja Luther <k.luther@bgbm.org>
Mon, 10 Aug 2020 10:39:53 +0000 (12:39 +0200)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/element/OrcidWithLabelElement.java

index 29a1f3a3270b16479c7b965d056714c87740ecef..1505ea81f5fb9accaca4c603e0eb6dad491211e1 100755 (executable)
@@ -8,14 +8,23 @@
 */
 package eu.etaxonomy.taxeditor.ui.element;
 
+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.ui.forms.widgets.TableWrapLayout;
 
 import eu.etaxonomy.cdm.model.agent.ORCID;
 import eu.etaxonomy.taxeditor.l10n.Messages;
+import eu.etaxonomy.taxeditor.model.ImageResources;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
+import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
 
 /**
  * @author k.luther
@@ -23,6 +32,8 @@ import eu.etaxonomy.taxeditor.l10n.Messages;
  */
 public class OrcidWithLabelElement extends AbstractUriWithExceptionLabelElement<ORCID> {
 
+    private Button btnOpenBrowser;
+
     /**
      * @param formFactory
      * @param parentElement
@@ -65,8 +76,37 @@ public class OrcidWithLabelElement extends AbstractUriWithExceptionLabelElement<
 
         //label
         initLabel(formFactory, labelString, false, getLayoutComposite());
+        Composite textAndButton = formFactory.createComposite(getLayoutComposite(), style);
+        addControl(textAndButton);
+        textAndButton.setLayout(LayoutConstants.LAYOUT(2, false));
+        textAndButton.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
+
         //uri text
-        initText(formFactory, null, textHeight, null, false, style, getLayoutComposite());
+        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;
+
+                ORCID orcid = parseText();
+                if(orcid!=null){
+                    try {
+                        WorkbenchUtility.openWebpage(orcid.asURI());
+                    } 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));
+                }
+            }
+        });
+        btnOpenBrowser.setLayoutData(LayoutConstants.RIGHT());
         initExceptionLabel(getLayoutComposite(), formFactory, initialObject);
     }
 
@@ -86,6 +126,7 @@ public class OrcidWithLabelElement extends AbstractUriWithExceptionLabelElement<
     @Override
     public ORCID parseText(){
         try {
+            labelException.setText("");
             labelException.setFont(JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT));
             labelException.setForeground(getPersistentBackground());
             return getParsedText();