Project

General

Profile

Download (5.6 KB) Statistics
| Branch: | Tag: | Revision:
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.jface.resource.JFaceResources;
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
import org.eclipse.swt.widgets.Display;
23
import org.eclipse.ui.forms.widgets.TableWrapLayout;
24

    
25
import eu.etaxonomy.cdm.common.DOI;
26
import eu.etaxonomy.taxeditor.l10n.Messages;
27
import eu.etaxonomy.taxeditor.model.ImageResources;
28
import eu.etaxonomy.taxeditor.model.MessagingUtils;
29
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
30
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
31

    
32
/**
33
 * @author k.luther
34
 * @since 27.11.2018
35
 *
36
 */
37
public class DoiWithLabelElement extends AbstractUriWithExceptionLabelElement<DOI> {
38

    
39
    private Button btnOpenBrowser;
40

    
41
    /**
42
     * @param formFactory
43
     * @param parentElement
44
     * @param labelString
45
     * @param initialObject
46
     * @param textHeight
47
     * @param style
48
     */
49
    protected DoiWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
50
            DOI initialObject, Integer textHeight, int style) {
51
        super(formFactory, parentElement, labelString, initialObject, textHeight, style);
52
        // TODO Auto-generated constructor stub
53
    }
54

    
55
    /**
56
     * {@inheritDoc}
57
     */
58
    @Override
59
    public void setParsedText(DOI object) {
60
        if(object != null){
61
            super.setText(object.toString());
62
        }
63

    
64
    }
65

    
66
    /**
67
     * {@inheritDoc}
68
     */
69
    @Override
70
    protected DOI getParsedText() throws Exception {
71
        String doiText = super.getText();
72
        if(doiText!=null){
73
            return DOI.fromString(super.getText());
74
        }
75
        return null;
76
    }
77

    
78
    @Override
79
    protected void init(CdmFormFactory formFactory, String labelString, DOI initialObject, Integer textHeight, int style) {
80

    
81
        //label
82
        initLabel(formFactory, labelString, false, getLayoutComposite());
83
        //uri text
84
        initText(formFactory, null, textHeight, null, false, style, getLayoutComposite());
85

    
86
        Composite textAndButton = formFactory.createComposite(getLayoutComposite(), style);
87
        addControl(textAndButton);
88
        textAndButton.setLayout(LayoutConstants.LAYOUT(2, false));
89
        textAndButton.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
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, initialObject);
117
    }
118

    
119
    @Override
120
    protected void initExceptionLabel(Composite parent, CdmFormFactory formFactory, DOI initialObject) {
121
        labelException = formFactory.createLabel(parent, "", SWT.WRAP); //$NON-NLS-1$
122
        int numColumns = AbstractFormSection.DEFAULT_NUM_COLUMNS;
123
        if(parent.getLayout() instanceof TableWrapLayout){
124
            numColumns = ((TableWrapLayout)parent.getLayout()).numColumns;
125
        }
126
        labelException.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(numColumns, 1));
127
        labelException.setBackground(getPersistentBackground());
128
        addControl(labelException);
129
        setParsedText(initialObject);
130
    }
131
    @Override
132
    public DOI parseText(){
133
        try {
134
            labelException.setFont(JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT));
135
            labelException.setForeground(getPersistentBackground());
136
            labelException.setText(""); //$NON-NLS-1$
137
            return getParsedText();
138
        } catch (Exception e) {
139
            labelException.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
140
            labelException.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
141
            labelException.setText(Messages.DoiWithLabelElement_DOI_NOT_SAVED+e.getLocalizedMessage());
142
            return null;
143
        }
144
    }
145

    
146
}
(12-12/49)