Merge branch 'release/5.18.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / UriWithLabelElement.java
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 import java.net.URI;
13 import java.net.URL;
14
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22
23 import eu.etaxonomy.cdm.common.UrlUtf8Coder;
24 import eu.etaxonomy.taxeditor.l10n.Messages;
25 import eu.etaxonomy.taxeditor.model.ImageResources;
26 import eu.etaxonomy.taxeditor.model.MessagingUtils;
27 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
28 import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
29
30 /**
31 * @author n.hoffmann
32 * @created Dec 20, 2010
33 */
34 public class UriWithLabelElement extends AbstractUriWithExceptionLabelElement<URI> {
35
36 private Button btnOpenBrowser;
37
38 protected UriWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
39 URI initialObject, Integer textHeight, int style) {
40 super(formFactory, parentElement, labelString, initialObject, textHeight, style);
41 exceptionString = Messages.UriWithLabelElement_URL_NOT_SAVED;
42 }
43
44 @Override
45 protected void init(CdmFormFactory formFactory, String labelString, URI initialObject, Integer textHeight, int style) {
46
47 //label
48 initLabel(formFactory, labelString, false, getLayoutComposite());
49
50 //composite(uri + button)
51 Composite textAndButton = formFactory.createComposite(getLayoutComposite(), style);
52 addControl(textAndButton);
53 textAndButton.setLayout(LayoutConstants.LAYOUT(2, false));
54 textAndButton.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
55
56 //uri text
57 initText(formFactory, null, textHeight, null, false, style, textAndButton);
58 setParsedText(initialObject);
59 //button
60 btnOpenBrowser = formFactory.createButton(textAndButton, "", SWT.NONE); //$NON-NLS-1$
61 btnOpenBrowser.setImage(ImageResources.getImage(ImageResources.WEB));
62 btnOpenBrowser.setToolTipText(Messages.UriWithLabelElement_OPEN_EXTERNAL_BROWSER);
63 btnOpenBrowser.addSelectionListener(new SelectionAdapter() {
64 @Override
65 public void widgetSelected(SelectionEvent e) {
66 String errorTitle = Messages.UriWithLabelElement_INVALID_URL;
67 String errorText = Messages.UriWithLabelElement_COULD_NOT_OPEN_BROWSER;
68
69 URI uri = parseText();
70 if(uri!=null){
71 try {
72 WorkbenchUtility.openWebpage(uri.toURL());
73 } catch (IOException pie) {
74 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, pie));
75 } catch (IllegalArgumentException iae) {
76 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, iae));
77 }
78 }
79 else{
80 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, null));
81 }
82 }
83 });
84 btnOpenBrowser.setLayoutData(LayoutConstants.RIGHT());
85
86 initExceptionLabel(getLayoutComposite(), formFactory);
87 }
88
89 @Override
90 public void setParsedText(URI object) {
91 if(object != null){
92 super.setText(object.toString());
93 }
94 }
95
96 @Override
97 protected URI getParsedText() throws Exception {
98 String uriText = super.getText();
99 if(uriText!=null){
100 try{
101 return new URI(super.getText());
102 }catch(Exception e){
103 URL url = new URL(uriText);
104 String[] pathElements = url.getPath().split("/");
105
106 for (String element: pathElements){
107 String replacement = UrlUtf8Coder.encode(element);
108 uriText = uriText.replace(element, replacement);
109 }
110 if (url.getQuery() != null){
111 uriText = uriText.replace(url.getQuery(), UrlUtf8Coder.encode(url.getQuery()));
112 }
113 url = new URL(uriText);
114 return url.toURI();
115 }
116 }
117 return null;
118 }
119 }