Fix new occurrence / sequence move method calls
[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); //$NON-NLS-1$
63 btnOpenBrowser.setImage(ImageResources.getImage(ImageResources.WEB));
64 btnOpenBrowser.setToolTipText(Messages.UriWithLabelElement_OPEN_EXTERNAL_BROWSER);
65 btnOpenBrowser.addSelectionListener(new SelectionAdapter() {
66 @Override
67 public void widgetSelected(SelectionEvent e) {
68 String errorTitle = Messages.UriWithLabelElement_INVALID_URL;
69 String errorText = Messages.UriWithLabelElement_COULD_NOT_OPEN_BROWSER;
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 else{
84 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, null));
85 }
86 }
87 });
88 btnOpenBrowser.setLayoutData(LayoutConstants.RIGHT());
89
90 labelException = formFactory.createLabel(getLayoutComposite(), "", SWT.WRAP); //$NON-NLS-1$
91 int numColumns = AbstractFormSection.DEFAULT_NUM_COLUMNS;
92 if(getLayoutComposite().getLayout() instanceof TableWrapLayout){
93 numColumns = ((TableWrapLayout)getLayoutComposite().getLayout()).numColumns;
94 }
95 labelException.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(numColumns, 1));
96 addControl(labelException);
97 setUri(initialUri);
98 }
99
100 public void setUri(URI uri) {
101 if(uri != null){
102 super.setText(uri.toString());
103 }
104 }
105
106 public URI getUri(){
107 try {
108 labelException.setFont(JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT));
109 labelException.setForeground(getPersistentBackground());
110 labelException.setText(""); //$NON-NLS-1$
111 return new URI(super.getText());
112 } catch (Exception e) {
113 labelException.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
114 labelException.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
115 labelException.setText(Messages.UriWithLabelElement_URL_NOT_SAVED+e.getMessage());
116 return null;
117 }
118 }
119
120 }