Merge develop into branch
[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.jface.resource.JFaceResources;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.SelectionAdapter;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.ui.PartInitException;
27 import org.eclipse.ui.PlatformUI;
28 import org.eclipse.ui.forms.widgets.TableWrapLayout;
29
30 import eu.etaxonomy.taxeditor.Messages;
31 import eu.etaxonomy.taxeditor.model.ImageResources;
32 import eu.etaxonomy.taxeditor.model.MessagingUtils;
33 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
34
35 /**
36 * @author n.hoffmann
37 * @created Dec 20, 2010
38 * @version 1.0
39 */
40 public class UriWithLabelElement extends TextWithLabelElement {
41
42 private final Label labelException;
43 private final Button btnOpenBrowser;
44
45 protected UriWithLabelElement(CdmFormFactory formFactory,
46 ICdmFormElement parentElement, String labelString,
47 URI initialUri, Integer textHeight, int style) {
48 super(formFactory, parentElement, false);
49
50 //label
51 initLabel(formFactory, labelString, false, getLayoutComposite());
52
53 //composite(uri + button)
54 Composite textAndButton = formFactory.createComposite(getLayoutComposite(), style);
55 textAndButton.setLayout(LayoutConstants.LAYOUT(2, false));
56 textAndButton.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
57
58 //uri text
59 initText(formFactory, null, textHeight, null, false, style, textAndButton);
60
61 //button
62 btnOpenBrowser = formFactory.createButton(textAndButton, "", SWT.NONE);
63 btnOpenBrowser.setImage(ImageResources.getImage(ImageResources.WEB));
64 btnOpenBrowser.setToolTipText("Open in external browser");
65 btnOpenBrowser.addSelectionListener(new SelectionAdapter() {
66 @Override
67 public void widgetSelected(SelectionEvent e) {
68 String errorTitle = "Invalid URL";
69 String errorText = "Could not open external browser. URL is invalid";
70
71 URI uri = getUri();
72 if(uri!=null){
73 try {
74 PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(uri.toURL());
75 } catch (PartInitException pie) {
76 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, pie));
77 } catch (MalformedURLException mue) {
78 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, mue));
79 } catch (IllegalArgumentException iae) {
80 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, iae));
81 }
82 }
83 }
84 });
85 btnOpenBrowser.setLayoutData(LayoutConstants.RIGHT());
86
87 labelException = formFactory.createLabel(getLayoutComposite(), "", SWT.WRAP);
88 int numColumns = AbstractFormSection.DEFAULT_NUM_COLUMNS;
89 if(getLayoutComposite().getLayout() instanceof TableWrapLayout){
90 numColumns = ((TableWrapLayout)getLayoutComposite().getLayout()).numColumns;
91 }
92 labelException.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(numColumns, 1));
93 addControl(labelException);
94 setUri(initialUri);
95 }
96
97 public void setUri(URI uri) {
98 if(uri != null){
99 super.setText(uri.toString());
100 }
101 }
102
103 public URI getUri(){
104 try {
105 labelException.setFont(JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT));
106 labelException.setForeground(getPersistentBackground());
107 labelException.setText(""); //$NON-NLS-1$
108 return new URI(super.getText());
109 } catch (Exception e) {
110 labelException.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
111 labelException.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
112 labelException.setText(Messages.UriWithLabelElement_URL_NOT_SAVED+e.getMessage());
113 return null;
114 }
115 }
116
117 }