Add button to open URLs in external browser (#5062)
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / UriWithLabelElement.java
index e7ef5ec0b227a867e3d6522b53d4c36cf154acef..e42490f5f03495abb89b6400614e2d6dc5310a7c 100644 (file)
 
 package eu.etaxonomy.taxeditor.ui.element;
 
+import java.net.MalformedURLException;
 import java.net.URI;
 
 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.TableWrapData;
+
+import eu.etaxonomy.taxeditor.model.ImageResources;
 
 /**
  * @author n.hoffmann
@@ -24,14 +34,44 @@ import org.eclipse.swt.widgets.Label;
 public class UriWithLabelElement extends TextWithLabelElement {
 
     private final Label labelException;
+    private final Button btnOpenBrowser;
 
        protected UriWithLabelElement(CdmFormFactory formFactory,
                        ICdmFormElement parentElement, String labelString,
                        URI initialUri, Integer textHeight, int style) {
-               super(formFactory, parentElement, labelString, null, textHeight, style);
+               super(formFactory, parentElement, false);
+
+        Composite layoutComposite = formFactory.createComposite(getLayoutComposite());
+        layoutComposite.setLayout(LayoutConstants.LAYOUT(3, false));
+        layoutComposite.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2,1));
+        init(formFactory, labelString, null, textHeight, null, false, style, layoutComposite);
+
+
+               btnOpenBrowser = formFactory.createButton(layoutComposite, "", SWT.NONE);
+               btnOpenBrowser.setImage(ImageResources.getImage(ImageResources.ADD_CHILD_TAXON_ICON));
+               TableWrapData layoutData = LayoutConstants.RIGHT();
+               layoutData.grabHorizontal = false;
+               btnOpenBrowser.setLayoutData(layoutData);
+               btnOpenBrowser.addSelectionListener(new SelectionAdapter() {
+                   @Override
+                   public void widgetSelected(SelectionEvent e) {
+                       URI uri = getUri();
+                       if(uri!=null){
+                           try {
+                        PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(uri.toURL());
+                    } catch (PartInitException e1) {
+                        // TODO Auto-generated catch block
+                        e1.printStackTrace();
+                    } catch (MalformedURLException e1) {
+                        // TODO Auto-generated catch block
+                        e1.printStackTrace();
+                    }
+                       }
+                   }
+        });
 
-               labelException = formFactory.createLabel(getLayoutComposite(), "", SWT.WRAP);
-               labelException.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
+               labelException = formFactory.createLabel(layoutComposite, "", SWT.WRAP);
+               labelException.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(3, 1));
                addControl(labelException);
                setUri(initialUri);
        }