Project

General

Profile

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

    
10
package eu.etaxonomy.taxeditor.ui.element;
11

    
12
import java.io.IOException;
13
import java.net.URI;
14
import java.net.URL;
15

    
16
import org.eclipse.core.runtime.IStatus;
17
import org.eclipse.core.runtime.Status;
18
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.events.SelectionAdapter;
20
import org.eclipse.swt.events.SelectionEvent;
21
import org.eclipse.swt.widgets.Button;
22
import org.eclipse.swt.widgets.Composite;
23

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

    
31
/**
32
 * @author n.hoffmann
33
 * @created Dec 20, 2010
34
 * @version 1.0
35
 */
36
public class UriWithLabelElement extends AbstractUriWithExceptionLabelElement<URI> {
37

    
38
    private Button btnOpenBrowser;
39

    
40
    protected UriWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
41
            URI initialObject, Integer textHeight, int style) {
42
        super(formFactory, parentElement, labelString, initialObject, textHeight, style);
43

    
44
	}
45

    
46
    /**
47
     * {@inheritDoc}
48
     */
49
    @Override
50
    protected void init(CdmFormFactory formFactory, String labelString, URI initialObject, Integer textHeight, int style) {
51

    
52
        //label
53
        initLabel(formFactory, labelString, false, getLayoutComposite());
54

    
55
        //composite(uri + button)
56
        Composite textAndButton = formFactory.createComposite(getLayoutComposite(), style);
57
        addControl(textAndButton);
58
        textAndButton.setLayout(LayoutConstants.LAYOUT(2, false));
59
        textAndButton.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
60

    
61
        //uri text
62
        initText(formFactory, null, textHeight, null, false, style, textAndButton);
63

    
64
        //button
65
        btnOpenBrowser = formFactory.createButton(textAndButton, "", SWT.NONE); //$NON-NLS-1$
66
        btnOpenBrowser.setImage(ImageResources.getImage(ImageResources.WEB));
67
        btnOpenBrowser.setToolTipText(Messages.UriWithLabelElement_OPEN_EXTERNAL_BROWSER);
68
        btnOpenBrowser.addSelectionListener(new SelectionAdapter() {
69
            @Override
70
            public void widgetSelected(SelectionEvent e) {
71
                String errorTitle = Messages.UriWithLabelElement_INVALID_URL;
72
                String errorText = Messages.UriWithLabelElement_COULD_NOT_OPEN_BROWSER;
73

    
74
                URI uri = parseText();
75
                if(uri!=null){
76
                    try {
77
                        WorkbenchUtility.openWebpage(uri.toURL());
78
                    } catch (IOException pie) {
79
                        MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, pie));
80
                    } catch (IllegalArgumentException iae) {
81
                        MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, iae));
82
                    }
83
                }
84
                else{
85
                    MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, null));
86
                }
87
            }
88
        });
89
        btnOpenBrowser.setLayoutData(LayoutConstants.RIGHT());
90

    
91
        initExceptionLabel(getLayoutComposite(), formFactory, initialObject);
92
    }
93

    
94

    
95

    
96

    
97
    /**
98
     * {@inheritDoc}
99
     */
100
    @Override
101
    public void setParsedText(URI object) {
102
        if(object != null){
103
            super.setText(object.toString());
104
        }
105
    }
106

    
107
    /**
108
     * {@inheritDoc}
109
     */
110
    @Override
111
    protected URI getParsedText() throws Exception {
112
        String uriText = super.getText();
113
        if(uriText!=null){
114
            try{
115
                return new URI(super.getText());
116
            }catch(Exception e){
117
                URL url = new URL(uriText);
118
                String[] pathElements =  url.getPath().split("/");
119

    
120
                for (String element: pathElements){
121
                    String replacement = UrlUtf8Coder.encode(element);
122
                    uriText = uriText.replace(element, replacement);
123
                }
124
                if (url.getQuery() != null){
125
                    uriText = uriText.replace(url.getQuery(), UrlUtf8Coder.encode(url.getQuery()));
126
                }
127
                url = new URL(uriText);
128
                return url.toURI();
129

    
130
            }
131

    
132
        }
133
        return null;
134
    }
135

    
136
}
(52-52/53)