Remove equal width from columns
[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.swt.widgets.Layout;
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.model.ImageResources;
31 import eu.etaxonomy.taxeditor.model.MessagingUtils;
32 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
33
34 /**
35 * @author n.hoffmann
36 * @created Dec 20, 2010
37 * @version 1.0
38 */
39 public class UriWithLabelElement extends TextWithLabelElement {
40
41 private final Label labelException;
42 private final Button btnOpenBrowser;
43 private static final int NUM_COLUMNS = 3;
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 Layout parentLayout = getLayoutComposite().getLayout();
51 int parentNumColumns = 2;
52 if(parentLayout instanceof TableWrapLayout){
53 parentNumColumns = ((TableWrapLayout)parentLayout).numColumns;
54 }
55 Composite layoutComposite = formFactory.createComposite(getLayoutComposite());
56 addControl(layoutComposite);
57
58 layoutComposite.setLayout(LayoutConstants.LAYOUT(3, false));
59 layoutComposite.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(parentNumColumns, 1));
60
61 init(formFactory, labelString, null, textHeight, null, false, style, layoutComposite);
62
63 btnOpenBrowser = formFactory.createButton(layoutComposite, "", SWT.NONE);
64 btnOpenBrowser.setImage(ImageResources.getImage(ImageResources.ADD_CHILD_TAXON_ICON));
65 btnOpenBrowser.setToolTipText("Open in external browser");
66 btnOpenBrowser.addSelectionListener(new SelectionAdapter() {
67 @Override
68 public void widgetSelected(SelectionEvent e) {
69 String errorTitle = "Invalid URL";
70 String errorText = "Could not open external browser. URL is invalid";
71
72 URI uri = getUri();
73 if(uri!=null){
74 try {
75 PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(uri.toURL());
76 } catch (PartInitException pie) {
77 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, pie));
78 } catch (MalformedURLException mue) {
79 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, mue));
80 } catch (IllegalArgumentException iae) {
81 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, iae));
82 }
83 }
84 }
85 });
86 btnOpenBrowser.setLayoutData(LayoutConstants.RIGHT());
87
88 labelException = formFactory.createLabel(layoutComposite, "", SWT.WRAP);
89 labelException.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(NUM_COLUMNS, 1));
90 addControl(labelException);
91 setUri(initialUri);
92 }
93
94 public void setUri(URI uri) {
95 if(uri != null){
96 super.setText(uri.toString());
97 }
98 }
99
100 public URI getUri(){
101 try {
102 labelException.setBackground(getPersistentBackground());
103 labelException.setText("");
104 return new URI(super.getText());
105 } catch (Exception e) {
106 labelException.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
107 labelException.setText(e.getMessage());
108 return null;
109 }
110 }
111
112 }