36487dcd7fe90dd7b2c241e6f38368edc6fb0d72
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / UriWithLabelElement.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.ui.element;
12
13 import java.net.MalformedURLException;
14 import java.net.URI;
15
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Display;
24 import org.eclipse.swt.widgets.Label;
25 import org.eclipse.ui.PartInitException;
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.forms.widgets.TableWrapLayout;
28
29 import eu.etaxonomy.taxeditor.model.ImageResources;
30 import eu.etaxonomy.taxeditor.model.MessagingUtils;
31 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
32
33 /**
34 * @author n.hoffmann
35 * @created Dec 20, 2010
36 * @version 1.0
37 */
38 public class UriWithLabelElement extends TextWithLabelElement {
39
40 private final Label labelException;
41 private final Button btnOpenBrowser;
42
43 protected UriWithLabelElement(CdmFormFactory formFactory,
44 ICdmFormElement parentElement, String labelString,
45 URI initialUri, Integer textHeight, int style) {
46 super(formFactory, parentElement, false);
47
48 initLabel(formFactory, labelString, false, getLayoutComposite());
49
50 Composite textAndButton = formFactory.createComposite(getLayoutComposite(), style);
51 textAndButton.setLayout(LayoutConstants.LAYOUT(2, false));
52 textAndButton.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
53
54 btnOpenBrowser = formFactory.createButton(textAndButton, "", SWT.NONE);
55 btnOpenBrowser.setImage(ImageResources.getImage(ImageResources.WEB));
56 btnOpenBrowser.setToolTipText("Open in external browser");
57 btnOpenBrowser.addSelectionListener(new SelectionAdapter() {
58 @Override
59 public void widgetSelected(SelectionEvent e) {
60 String errorTitle = "Invalid URL";
61 String errorText = "Could not open external browser. URL is invalid";
62
63 URI uri = getUri();
64 if(uri!=null){
65 try {
66 PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(uri.toURL());
67 } catch (PartInitException pie) {
68 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, pie));
69 } catch (MalformedURLException mue) {
70 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, mue));
71 } catch (IllegalArgumentException iae) {
72 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, iae));
73 }
74 }
75 }
76 });
77 btnOpenBrowser.setLayoutData(LayoutConstants.RIGHT());
78
79 initLabel(formFactory, labelString, false, textAndButton);
80
81 labelException = formFactory.createLabel(getLayoutComposite(), "", SWT.WRAP);
82 int numColumns = AbstractFormSection.DEFAULT_NUM_COLUMNS;
83 if(getLayoutComposite().getLayout() instanceof TableWrapLayout){
84 numColumns = ((TableWrapLayout)getLayoutComposite().getLayout()).numColumns;
85 }
86 labelException.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(numColumns, 1));
87 addControl(labelException);
88 setUri(initialUri);
89 }
90
91 public void setUri(URI uri) {
92 if(uri != null){
93 super.setText(uri.toString());
94 }
95 }
96
97 public URI getUri(){
98 try {
99 labelException.setBackground(getPersistentBackground());
100 labelException.setText("");
101 return new URI(super.getText());
102 } catch (Exception e) {
103 labelException.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
104 labelException.setText(e.getMessage());
105 return null;
106 }
107 }
108
109 }