Merge branch 'release/5.18.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / DoiWithLabelElement.java
1 /**
2 * Copyright (C) 2018 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.URL;
13
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.DOI;
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 k.luther
31 * @since 27.11.2018
32 *
33 */
34 public class DoiWithLabelElement extends AbstractUriWithExceptionLabelElement<DOI> {
35
36 private Button btnOpenBrowser;
37
38 /**
39 * @param formFactory
40 * @param parentElement
41 * @param labelString
42 * @param initialObject
43 * @param textHeight
44 * @param style
45 */
46 protected DoiWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
47 DOI initialObject, Integer textHeight, int style) {
48 super(formFactory, parentElement, labelString, initialObject, textHeight, style);
49 exceptionString = Messages.DoiWithLabelElement_DOI_NOT_SAVED;
50 }
51
52 /**
53 * {@inheritDoc}
54 */
55 @Override
56 public void setParsedText(DOI object) {
57 if(object != null){
58 super.setText(object.toString());
59 }
60
61 }
62
63 /**
64 * {@inheritDoc}
65 */
66 @Override
67 protected DOI getParsedText() throws Exception {
68 String doiText = super.getText();
69 if(doiText!=null){
70 return DOI.fromString(super.getText());
71 }
72 return null;
73 }
74
75 @Override
76 protected void init(CdmFormFactory formFactory, String labelString, DOI initialObject, Integer textHeight, int style) {
77
78 //label
79 initLabel(formFactory, labelString, false, getLayoutComposite());
80
81
82
83 Composite textAndButton = formFactory.createComposite(getLayoutComposite(), style);
84 addControl(textAndButton);
85 textAndButton.setLayout(LayoutConstants.LAYOUT(2, false));
86 textAndButton.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
87 //uri text
88 initText(formFactory, null, textHeight, null, false, style, textAndButton);
89 setParsedText(initialObject);
90 //button
91 btnOpenBrowser = formFactory.createButton(textAndButton, "", SWT.NONE); //$NON-NLS-1$
92 btnOpenBrowser.setImage(ImageResources.getImage(ImageResources.WEB));
93 btnOpenBrowser.setToolTipText(Messages.UriWithLabelElement_OPEN_EXTERNAL_BROWSER);
94 btnOpenBrowser.addSelectionListener(new SelectionAdapter() {
95 @Override
96 public void widgetSelected(SelectionEvent e) {
97 String errorTitle = Messages.UriWithLabelElement_INVALID_URL;
98 String errorText = Messages.UriWithLabelElement_COULD_NOT_OPEN_BROWSER;
99
100 DOI doi= parseText();
101 if(doi!=null){
102 try {
103 WorkbenchUtility.openWebpage(new URL(doi.asURI()));
104 } catch (IOException pie) {
105 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, pie));
106 } catch (IllegalArgumentException iae) {
107 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, iae));
108 }
109 }
110 else{
111 MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, null));
112 }
113 }
114 });
115 btnOpenBrowser.setLayoutData(LayoutConstants.RIGHT());
116 initExceptionLabel(getLayoutComposite(), formFactory);
117 }
118
119
120
121 }