Project

General

Profile

Download (5.59 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

    
84

    
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
      //uri text
91
        initText(formFactory, null, textHeight, null, false, style, textAndButton);
92

    
93
      //button
94
        btnOpenBrowser = formFactory.createButton(textAndButton, "", SWT.NONE); //$NON-NLS-1$
95
        btnOpenBrowser.setImage(ImageResources.getImage(ImageResources.WEB));
96
        btnOpenBrowser.setToolTipText(Messages.UriWithLabelElement_OPEN_EXTERNAL_BROWSER);
97
        btnOpenBrowser.addSelectionListener(new SelectionAdapter() {
98
            @Override
99
            public void widgetSelected(SelectionEvent e) {
100
                String errorTitle = Messages.UriWithLabelElement_INVALID_URL;
101
                String errorText = Messages.UriWithLabelElement_COULD_NOT_OPEN_BROWSER;
102

    
103
                DOI doi= parseText();
104
                if(doi!=null){
105
                    try {
106
                        WorkbenchUtility.openWebpage(new URL(doi.asURI()));
107
                    } catch (IOException pie) {
108
                        MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, pie));
109
                    } catch (IllegalArgumentException iae) {
110
                        MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, iae));
111
                    }
112
                }
113
                else{
114
                    MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, null));
115
                }
116
            }
117
        });
118
        btnOpenBrowser.setLayoutData(LayoutConstants.RIGHT());
119
        initExceptionLabel(getLayoutComposite(), formFactory, initialObject);
120
    }
121

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

    
149
}
(13-13/53)