2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.ui
.element
;
12 import java
.io
.IOException
;
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
;
24 import eu
.etaxonomy
.cdm
.common
.UrlUtf8Coder
;
25 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
26 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
27 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
28 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
29 import eu
.etaxonomy
.taxeditor
.workbench
.WorkbenchUtility
;
33 * @created Dec 20, 2010
36 public class UriWithLabelElement
extends AbstractUriWithExceptionLabelElement
<URI
> {
38 private Button btnOpenBrowser
;
40 protected UriWithLabelElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
, String labelString
,
41 URI initialObject
, Integer textHeight
, int style
) {
42 super(formFactory
, parentElement
, labelString
, initialObject
, textHeight
, style
);
50 protected void init(CdmFormFactory formFactory
, String labelString
, URI initialObject
, Integer textHeight
, int style
) {
53 initLabel(formFactory
, labelString
, false, getLayoutComposite());
55 //composite(uri + button)
56 Composite textAndButton
= formFactory
.createComposite(getLayoutComposite(), style
);
57 addControl(textAndButton
);
58 textAndButton
.setLayout(LayoutConstants
.LAYOUT(2, false));
59 textAndButton
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
62 initText(formFactory
, null, textHeight
, null, false, style
, textAndButton
);
65 btnOpenBrowser
= formFactory
.createButton(textAndButton
, "", SWT
.NONE
); //$NON-NLS-1$
66 btnOpenBrowser
.setImage(ImageResources
.getImage(ImageResources
.WEB
));
67 btnOpenBrowser
.setToolTipText(Messages
.UriWithLabelElement_OPEN_EXTERNAL_BROWSER
);
68 btnOpenBrowser
.addSelectionListener(new SelectionAdapter() {
70 public void widgetSelected(SelectionEvent e
) {
71 String errorTitle
= Messages
.UriWithLabelElement_INVALID_URL
;
72 String errorText
= Messages
.UriWithLabelElement_COULD_NOT_OPEN_BROWSER
;
74 URI uri
= parseText();
77 WorkbenchUtility
.openWebpage(uri
.toURL());
78 } catch (IOException pie
) {
79 MessagingUtils
.informationDialog(errorTitle
, new Status(IStatus
.WARNING
, TaxeditorStorePlugin
.PLUGIN_ID
, errorText
, pie
));
80 } catch (IllegalArgumentException iae
) {
81 MessagingUtils
.informationDialog(errorTitle
, new Status(IStatus
.WARNING
, TaxeditorStorePlugin
.PLUGIN_ID
, errorText
, iae
));
85 MessagingUtils
.informationDialog(errorTitle
, new Status(IStatus
.WARNING
, TaxeditorStorePlugin
.PLUGIN_ID
, errorText
, null));
89 btnOpenBrowser
.setLayoutData(LayoutConstants
.RIGHT());
91 initExceptionLabel(getLayoutComposite(), formFactory
, initialObject
);
101 public void setParsedText(URI object
) {
103 super.setText(object
.toString());
111 protected URI
getParsedText() throws Exception
{
112 String uriText
= super.getText();
115 return new URI(super.getText());
117 URL url
= new URL(uriText
);
118 String
[] pathElements
= url
.getPath().split("/");
120 for (String element
: pathElements
){
121 String replacement
= UrlUtf8Coder
.encode(element
);
122 uriText
= uriText
.replace(element
, replacement
);
124 if (url
.getQuery() != null){
125 uriText
= uriText
.replace(url
.getQuery(), UrlUtf8Coder
.encode(url
.getQuery()));
127 url
= new URL(uriText
);