e42490f5f03495abb89b6400614e2d6dc5310a7c
[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.swt.SWT;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Display;
22 import org.eclipse.swt.widgets.Label;
23 import org.eclipse.ui.PartInitException;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.forms.widgets.TableWrapData;
26
27 import eu.etaxonomy.taxeditor.model.ImageResources;
28
29 /**
30 * @author n.hoffmann
31 * @created Dec 20, 2010
32 * @version 1.0
33 */
34 public class UriWithLabelElement extends TextWithLabelElement {
35
36 private final Label labelException;
37 private final Button btnOpenBrowser;
38
39 protected UriWithLabelElement(CdmFormFactory formFactory,
40 ICdmFormElement parentElement, String labelString,
41 URI initialUri, Integer textHeight, int style) {
42 super(formFactory, parentElement, false);
43
44 Composite layoutComposite = formFactory.createComposite(getLayoutComposite());
45 layoutComposite.setLayout(LayoutConstants.LAYOUT(3, false));
46 layoutComposite.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2,1));
47 init(formFactory, labelString, null, textHeight, null, false, style, layoutComposite);
48
49
50 btnOpenBrowser = formFactory.createButton(layoutComposite, "", SWT.NONE);
51 btnOpenBrowser.setImage(ImageResources.getImage(ImageResources.ADD_CHILD_TAXON_ICON));
52 TableWrapData layoutData = LayoutConstants.RIGHT();
53 layoutData.grabHorizontal = false;
54 btnOpenBrowser.setLayoutData(layoutData);
55 btnOpenBrowser.addSelectionListener(new SelectionAdapter() {
56 @Override
57 public void widgetSelected(SelectionEvent e) {
58 URI uri = getUri();
59 if(uri!=null){
60 try {
61 PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(uri.toURL());
62 } catch (PartInitException e1) {
63 // TODO Auto-generated catch block
64 e1.printStackTrace();
65 } catch (MalformedURLException e1) {
66 // TODO Auto-generated catch block
67 e1.printStackTrace();
68 }
69 }
70 }
71 });
72
73 labelException = formFactory.createLabel(layoutComposite, "", SWT.WRAP);
74 labelException.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(3, 1));
75 addControl(labelException);
76 setUri(initialUri);
77 }
78
79 public void setUri(URI uri) {
80 if(uri != null){
81 super.setText(uri.toString());
82 }
83 }
84
85 public URI getUri(){
86 try {
87 labelException.setBackground(getPersistentBackground());
88 labelException.setText("");
89 return new URI(super.getText());
90 } catch (Exception e) {
91 labelException.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
92 labelException.setText(e.getMessage());
93 return null;
94 }
95 }
96
97 }