fix #8988: add browse button to doi field
authorKatja Luther <k.luther@bgbm.org>
Fri, 15 May 2020 10:18:06 +0000 (12:18 +0200)
committerKatja Luther <k.luther@bgbm.org>
Fri, 15 May 2020 10:18:06 +0000 (12:18 +0200)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/element/DoiWithLabelElement.java

index c3c9d71134afa9aaff1ef57643c768d7e13fd763..7a2c027bb2b1b1d09b12e371fe4c684bc9a53cb1 100755 (executable)
@@ -8,14 +8,26 @@
 */
 package eu.etaxonomy.taxeditor.ui.element;
 
+import java.io.IOException;
+import java.net.URL;
+
+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.common.DOI;
 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
@@ -24,6 +36,8 @@ import eu.etaxonomy.taxeditor.l10n.Messages;
  */
 public class DoiWithLabelElement extends AbstractUriWithExceptionLabelElement<DOI> {
 
+    private Button btnOpenBrowser;
+
     /**
      * @param formFactory
      * @param parentElement
@@ -68,6 +82,37 @@ public class DoiWithLabelElement extends AbstractUriWithExceptionLabelElement<DO
         initLabel(formFactory, labelString, false, getLayoutComposite());
         //uri text
         initText(formFactory, null, textHeight, null, false, style, getLayoutComposite());
+
+        Composite textAndButton = formFactory.createComposite(getLayoutComposite(), style);
+        addControl(textAndButton);
+        textAndButton.setLayout(LayoutConstants.LAYOUT(2, false));
+        textAndButton.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
+      //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;
+
+                DOI doi= parseText();
+                if(doi!=null){
+                    try {
+                        WorkbenchUtility.openWebpage(new URL(doi.asURI()));
+                    } catch (IOException pie) {
+                        MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, pie));
+                    } 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);
     }