1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.taxeditor.ui.element;
|
10
|
|
11
|
import java.io.IOException;
|
12
|
|
13
|
import org.apache.commons.lang.StringUtils;
|
14
|
import org.eclipse.core.runtime.IStatus;
|
15
|
import org.eclipse.core.runtime.Status;
|
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
|
|
22
|
import eu.etaxonomy.cdm.common.URI;
|
23
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
24
|
import eu.etaxonomy.taxeditor.model.ImageResources;
|
25
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
26
|
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
|
27
|
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
|
28
|
|
29
|
/**
|
30
|
* @author n.hoffmann
|
31
|
* @created Dec 20, 2010
|
32
|
*/
|
33
|
public class UriWithLabelElement extends AbstractUriWithExceptionLabelElement<URI> {
|
34
|
|
35
|
private Button btnOpenBrowser;
|
36
|
|
37
|
protected UriWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
|
38
|
URI initialObject, Integer textHeight, int style) {
|
39
|
super(formFactory, parentElement, labelString, initialObject, textHeight, style);
|
40
|
exceptionString = Messages.UriWithLabelElement_URL_NOT_SAVED;
|
41
|
}
|
42
|
|
43
|
@Override
|
44
|
protected void init(CdmFormFactory formFactory, String labelString, URI initialObject, Integer textHeight, int style) {
|
45
|
|
46
|
//label
|
47
|
initLabel(formFactory, labelString, false, getLayoutComposite());
|
48
|
|
49
|
//composite(uri + button)
|
50
|
Composite textAndButton = formFactory.createComposite(getLayoutComposite(), style);
|
51
|
addControl(textAndButton);
|
52
|
textAndButton.setLayout(LayoutConstants.LAYOUT(2, false));
|
53
|
textAndButton.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
|
54
|
|
55
|
//uri text
|
56
|
initText(formFactory, null, textHeight, null, false, style, textAndButton);
|
57
|
setParsedText(initialObject);
|
58
|
//button
|
59
|
btnOpenBrowser = formFactory.createButton(textAndButton, "", SWT.NONE); //$NON-NLS-1$
|
60
|
btnOpenBrowser.setImage(ImageResources.getImage(ImageResources.WEB));
|
61
|
btnOpenBrowser.setToolTipText(Messages.UriWithLabelElement_OPEN_EXTERNAL_BROWSER);
|
62
|
btnOpenBrowser.addSelectionListener(new SelectionAdapter() {
|
63
|
@Override
|
64
|
public void widgetSelected(SelectionEvent e) {
|
65
|
String errorTitle = Messages.UriWithLabelElement_INVALID_URL;
|
66
|
String errorText = Messages.UriWithLabelElement_COULD_NOT_OPEN_BROWSER;
|
67
|
|
68
|
URI uri = parseText();
|
69
|
if(uri!=null){
|
70
|
try {
|
71
|
WorkbenchUtility.openWebpage(uri.toURL());
|
72
|
} catch (IOException pie) {
|
73
|
MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, pie));
|
74
|
} catch (IllegalArgumentException iae) {
|
75
|
MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, iae));
|
76
|
}
|
77
|
}
|
78
|
else{
|
79
|
MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, null));
|
80
|
}
|
81
|
}
|
82
|
});
|
83
|
btnOpenBrowser.setLayoutData(LayoutConstants.RIGHT());
|
84
|
if (StringUtils.isBlank(getText())){
|
85
|
btnOpenBrowser.setEnabled(false);
|
86
|
}
|
87
|
|
88
|
initExceptionLabel(getLayoutComposite(), formFactory);
|
89
|
}
|
90
|
|
91
|
@Override
|
92
|
public void setParsedText(URI object) {
|
93
|
if(object != null){
|
94
|
super.setText(object.toString());
|
95
|
}
|
96
|
}
|
97
|
|
98
|
@Override
|
99
|
protected URI getParsedText() throws Exception {
|
100
|
String uriText = super.getText();
|
101
|
if(uriText!=null){
|
102
|
btnOpenBrowser.setEnabled(true);
|
103
|
return new URI(super.getText());
|
104
|
}else{
|
105
|
btnOpenBrowser.setEnabled(false);
|
106
|
return null;
|
107
|
}
|
108
|
}
|
109
|
}
|