Change order of controls
[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 //label
49 initLabel(formFactory, labelString, false, getLayoutComposite());
50
51 //composite(uri + button)
52 Composite textAndButton = formFactory.createComposite(getLayoutComposite(), style);
53 textAndButton.setLayout(LayoutConstants.LAYOUT(2, false));
54 textAndButton.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
55
56 //uri text
57 initText(formFactory, null, textHeight, null, false, style, textAndButton);
58
59 //button
60 btnOpenBrowser = formFactory.createButton(textAndButton, "", SWT.NONE);
61 btnOpenBrowser.setImage(ImageResources.getImage(ImageResources.WEB));
62 btnOpenBrowser.setToolTipText("Open in external browser");
63 btnOpenBrowser.addSelectionListener(new SelectionAdapter() {
64 @Override
65 public void widgetSelected(SelectionEvent e) {
66 String errorTitle = "Invalid URL";
67 String errorText = "Could not open external browser. URL is invalid";
68
69 URI uri = getUri();
70 if(uri!=null){
71 try {
72 PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(uri.toURL());
73 } catch (PartInitException pie) {
74 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, pie));
75 } catch (MalformedURLException mue) {
76 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, mue));
77 } catch (IllegalArgumentException iae) {
78 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, iae));
79 }
80 }
81 }
82 });
83 btnOpenBrowser.setLayoutData(LayoutConstants.RIGHT());
84
85 labelException = formFactory.createLabel(getLayoutComposite(), "", SWT.WRAP);
86 int numColumns = AbstractFormSection.DEFAULT_NUM_COLUMNS;
87 if(getLayoutComposite().getLayout() instanceof TableWrapLayout){
88 numColumns = ((TableWrapLayout)getLayoutComposite().getLayout()).numColumns;
89 }
90 labelException.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(numColumns, 1));
91 addControl(labelException);
92 setUri(initialUri);
93 }
94
95 public void setUri(URI uri) {
96 if(uri != null){
97 super.setText(uri.toString());
98 }
99 }
100
101 public URI getUri(){
102 try {
103 labelException.setBackground(getPersistentBackground());
104 labelException.setText("");
105 return new URI(super.getText());
106 } catch (Exception e) {
107 labelException.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
108 labelException.setText(e.getMessage());
109 return null;
110 }
111 }
112
113 }