Project

General

Profile

Download (4.26 KB) Statistics
| Branch: | Tag: | Revision:
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

    
13
import org.eclipse.core.runtime.IStatus;
14
import org.eclipse.core.runtime.Status;
15
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.events.SelectionAdapter;
17
import org.eclipse.swt.events.SelectionEvent;
18
import org.eclipse.swt.widgets.Button;
19
import org.eclipse.swt.widgets.Composite;
20

    
21
import eu.etaxonomy.cdm.common.URI;
22
import eu.etaxonomy.taxeditor.l10n.Messages;
23
import eu.etaxonomy.taxeditor.model.ImageResources;
24
import eu.etaxonomy.taxeditor.model.MessagingUtils;
25
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
26
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
27

    
28
/**
29
 * @author n.hoffmann
30
 * @created Dec 20, 2010
31
 */
32
public class UriWithLabelElement extends AbstractUriWithExceptionLabelElement<URI> {
33

    
34
    private Button btnOpenBrowser;
35
    private boolean isValidUri = false;
36

    
37
    protected UriWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
38
            URI initialObject, Integer textHeight, int style) {
39
        super(formFactory, parentElement, labelString, initialObject, textHeight, style);
40
        exceptionString = Messages.UriWithLabelElement_URL_NOT_SAVED;
41
	}
42

    
43
    @Override
44
    protected void init(CdmFormFactory formFactory, String labelString, URI initialObject, Integer textHeight, int style) {
45

    
46
        //label
47
        initLabel(formFactory, labelString, false, getLayoutComposite());
48

    
49
        //composite(uri + button)
50
        Composite textAndButton = formFactory.createComposite(getLayoutComposite(), style);
51
        addControl(textAndButton);
52
        textAndButton.setLayout(LayoutConstants.LAYOUT(2, false));
53
        textAndButton.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
54

    
55
        //uri text
56
        initText(formFactory, null, textHeight, null, false, style, textAndButton);
57
        setParsedText(initialObject);
58
        //button
59
        btnOpenBrowser = formFactory.createButton(textAndButton, "", SWT.NONE); //$NON-NLS-1$
60
        btnOpenBrowser.setImage(ImageResources.getImage(ImageResources.WEB));
61
        btnOpenBrowser.setToolTipText(Messages.UriWithLabelElement_OPEN_EXTERNAL_BROWSER);
62
        btnOpenBrowser.addSelectionListener(new SelectionAdapter() {
63
            @Override
64
            public void widgetSelected(SelectionEvent e) {
65
                String errorTitle = Messages.UriWithLabelElement_INVALID_URL;
66
                String errorText = Messages.UriWithLabelElement_COULD_NOT_OPEN_BROWSER;
67

    
68
                URI uri = parseText();
69
                if(uri!=null){
70
                    try {
71
                        WorkbenchUtility.openWebpage(uri.toURL());
72
                    } catch (IOException pie) {
73
                        MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, pie));
74
                    } catch (IllegalArgumentException iae) {
75
                        MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, iae));
76
                    }
77
                }
78
                else{
79
                    MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, null));
80
                }
81
            }
82
        });
83
        btnOpenBrowser.setLayoutData(LayoutConstants.RIGHT());
84
        initExceptionLabel(getLayoutComposite(), formFactory);
85
    }
86

    
87
    @Override
88
    public void setParsedText(URI object) {
89
        if(object != null){
90
            super.setText(object.toString());
91
        }
92

    
93

    
94
    }
95

    
96
    @Override
97
    protected URI getParsedText() throws Exception {
98
        String uriText = super.getText();
99
        if(uriText!=null){
100
            btnOpenBrowser.setEnabled(true);
101
            try{
102
                URI uri = new URI(super.getText());
103
                return uri;
104
            }catch (Exception e){
105
                btnOpenBrowser.setEnabled(false);
106
                throw e;
107
            }
108
        }else{
109
            btnOpenBrowser.setEnabled(false);
110
        	return null;
111
        }
112
    }
113
}
(57-57/58)