I18n for Specimen Editor
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / UriWithLabelElement.java
index 02b61f5412b4e93561abcf8f35b0add06243798e..b9d6300ecb985131e868427f47e5c4db8bf4a330 100644 (file)
 
 package eu.etaxonomy.taxeditor.ui.element;
 
+import java.net.MalformedURLException;
 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;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
 
 /**
  * @author n.hoffmann
@@ -25,14 +40,60 @@ 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);
+
+               //label
+        initLabel(formFactory, labelString, false, getLayoutComposite());
+
+        //composite(uri + button)
+        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, textAndButton);
 
-               labelException = formFactory.createLabel(getLayoutComposite(), "", SWT.WRAP);
-               labelException.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
+        //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 {
+                        PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(uri.toURL());
+                    } catch (PartInitException pie) {
+                        MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, pie));
+                    } catch (MalformedURLException mue) {
+                        MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, mue));
+                    } 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());
+
+               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);
        }
@@ -47,12 +108,12 @@ public class UriWithLabelElement extends TextWithLabelElement {
         try {
             labelException.setFont(JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT));
             labelException.setForeground(getPersistentBackground());
-            labelException.setText("");
+            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("URL won't be saved! "+e.getMessage());
+            labelException.setText(Messages.UriWithLabelElement_URL_NOT_SAVED+e.getLocalizedMessage());
             return null;
         }
        }