Project

General

Profile

« Previous | Next » 

Revision a45e3c4a

Added by Patrick Plitzner over 8 years ago

Generalized UriWithLabelElement to use super class

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/element/AbstractUriWithExceptionLabelElement.java
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

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

  
13
import org.eclipse.jface.resource.JFaceResources;
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.widgets.Composite;
16
import org.eclipse.swt.widgets.Display;
17
import org.eclipse.swt.widgets.Label;
18
import org.eclipse.ui.forms.widgets.TableWrapLayout;
19

  
20
import eu.etaxonomy.taxeditor.Messages;
21

  
22
/**
23
 *
24
 * @author pplitzner
25
 * @date Sep 21, 2015
26
 *
27
 */
28
public abstract class AbstractUriWithExceptionLabelElement <T> extends TextWithLabelElement {
29

  
30
    protected Label labelException;
31

  
32
	protected AbstractUriWithExceptionLabelElement(CdmFormFactory formFactory,
33
			ICdmFormElement parentElement, String labelString,
34
			T initialObject, Integer textHeight, int style) {
35
		super(formFactory, parentElement, false);
36

  
37
        init(formFactory, labelString, initialObject, textHeight, style);
38
    }
39

  
40
    protected void init(CdmFormFactory formFactory, String labelString, T initialObject, Integer textHeight, int style) {
41
        //label
42
        initLabel(formFactory, labelString, false, getLayoutComposite());
43

  
44
        //uri text
45
        initText(formFactory, null, textHeight, null, false, style, getLayoutComposite());
46

  
47
        //exceptionLabel
48
        initExceptionLabel(getLayoutComposite(), formFactory, initialObject);
49
    }
50

  
51
    protected void initExceptionLabel(Composite parent, CdmFormFactory formFactory, T initialObject) {
52
        labelException = formFactory.createLabel(parent, "", SWT.WRAP); //$NON-NLS-1$
53
		int numColumns = AbstractFormSection.DEFAULT_NUM_COLUMNS;
54
		if(parent.getLayout() instanceof TableWrapLayout){
55
		    numColumns = ((TableWrapLayout)parent.getLayout()).numColumns;
56
		}
57
        labelException.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(numColumns, 1));
58
		addControl(labelException);
59
		setParsedText(initialObject);
60
    }
61

  
62

  
63
    public abstract void setParsedText(T object);
64

  
65
    protected abstract T getParsedText() throws Exception;
66

  
67
    public T parseText(){
68
        try {
69
            labelException.setFont(JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT));
70
            labelException.setForeground(getPersistentBackground());
71
            labelException.setText(""); //$NON-NLS-1$
72
            return getParsedText();
73
        } catch (Exception e) {
74
            labelException.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
75
            labelException.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
76
            labelException.setText(Messages.UriWithLabelElement_URL_NOT_SAVED+e.getLocalizedMessage());
77
            return null;
78
        }
79
    }
80

  
81
}
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/element/LsidWithExceptionLabelElement.java
18 18
 * @date Sep 21, 2015
19 19
 *
20 20
 */
21
public class LsidWithExceptionLabelElement extends UriWithExceptionLabelElement<LSID> {
21
public class LsidWithExceptionLabelElement extends AbstractUriWithExceptionLabelElement<LSID> {
22 22

  
23 23
    protected LsidWithExceptionLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement,
24 24
            String labelString, LSID initialObject, Integer textHeight, int style) {
......
39 39
	 * {@inheritDoc}
40 40
	 */
41 41
	@Override
42
	public LSID parseText() {
43
        try {
44
            updateExceptionLabel(null);
42
	public LSID getParsedText() throws Exception {
45 43
            return new LSID(super.getText());
46
        } catch (Exception e) {
47
            updateExceptionLabel(e);
48
            return null;
49
        }
50 44
	}
51 45

  
52 46
}
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/element/UriWithExceptionLabelElement.java
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

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

  
13
import org.eclipse.jface.resource.JFaceResources;
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.widgets.Display;
16
import org.eclipse.swt.widgets.Label;
17
import org.eclipse.ui.forms.widgets.TableWrapLayout;
18

  
19
import eu.etaxonomy.taxeditor.Messages;
20

  
21
/**
22
 *
23
 * @author pplitzner
24
 * @date Sep 21, 2015
25
 *
26
 */
27
public abstract class UriWithExceptionLabelElement <T> extends TextWithLabelElement {
28

  
29
    protected Label labelException;
30

  
31
	protected UriWithExceptionLabelElement(CdmFormFactory formFactory,
32
			ICdmFormElement parentElement, String labelString,
33
			T initialObject, Integer textHeight, int style) {
34
		super(formFactory, parentElement, false);
35

  
36
		//label
37
        initLabel(formFactory, labelString, false, getLayoutComposite());
38

  
39
        //uri text
40
        initText(formFactory, null, textHeight, null, false, style, getLayoutComposite());
41

  
42
        //exceptionLabel
43
		initExceptionLabel(formFactory, initialObject);
44
	}
45

  
46
    private void initExceptionLabel(CdmFormFactory formFactory, T initialObject) {
47
        labelException = formFactory.createLabel(getLayoutComposite(), "", SWT.WRAP); //$NON-NLS-1$
48
		int numColumns = AbstractFormSection.DEFAULT_NUM_COLUMNS;
49
		if(getLayoutComposite().getLayout() instanceof TableWrapLayout){
50
		    numColumns = ((TableWrapLayout)getLayoutComposite().getLayout()).numColumns;
51
		}
52
        labelException.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(numColumns, 1));
53
		addControl(labelException);
54
		setParsedText(initialObject);
55
    }
56

  
57

  
58
    public abstract void setParsedText(T object);
59

  
60
    public abstract T parseText();
61

  
62
    protected void updateExceptionLabel(Exception e){
63
        if(e!=null){
64
            labelException.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
65
            labelException.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
66
            labelException.setText(Messages.UriWithLabelElement_URL_NOT_SAVED+e.getLocalizedMessage());
67
        }
68
        else{
69
            labelException.setFont(JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT));
70
            labelException.setForeground(getPersistentBackground());
71
            labelException.setText(""); //$NON-NLS-1$
72
        }
73
    }
74

  
75
}
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/element/UriWithLabelElement.java
15 15

  
16 16
import org.eclipse.core.runtime.IStatus;
17 17
import org.eclipse.core.runtime.Status;
18
import org.eclipse.jface.resource.JFaceResources;
19 18
import org.eclipse.swt.SWT;
20 19
import org.eclipse.swt.events.SelectionAdapter;
21 20
import org.eclipse.swt.events.SelectionEvent;
22 21
import org.eclipse.swt.widgets.Button;
23 22
import org.eclipse.swt.widgets.Composite;
24
import org.eclipse.swt.widgets.Display;
25
import org.eclipse.swt.widgets.Label;
26 23
import org.eclipse.ui.PartInitException;
27 24
import org.eclipse.ui.PlatformUI;
28
import org.eclipse.ui.forms.widgets.TableWrapLayout;
29 25

  
30 26
import eu.etaxonomy.taxeditor.Messages;
31 27
import eu.etaxonomy.taxeditor.model.ImageResources;
......
37 33
 * @created Dec 20, 2010
38 34
 * @version 1.0
39 35
 */
40
public class UriWithLabelElement extends TextWithLabelElement {
36
public class UriWithLabelElement extends AbstractUriWithExceptionLabelElement<URI> {
41 37

  
42
    private final Label labelException;
43
    private final Button btnOpenBrowser;
38
    private Button btnOpenBrowser;
44 39

  
45
	protected UriWithLabelElement(CdmFormFactory formFactory,
46
			ICdmFormElement parentElement, String labelString,
47
			URI initialUri, Integer textHeight, int style) {
48
		super(formFactory, parentElement, false);
40
    protected UriWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
41
            URI initialObject, Integer textHeight, int style) {
42
        super(formFactory, parentElement, labelString, initialObject, textHeight, style);
49 43

  
50
		//label
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
51 53
        initLabel(formFactory, labelString, false, getLayoutComposite());
52 54

  
53 55
        //composite(uri + button)
......
60 62
        initText(formFactory, null, textHeight, null, false, style, textAndButton);
61 63

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

  
72
		        URI uri = getUri();
73
		        if(uri!=null){
74
		            try {
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 {
75 77
                        PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(uri.toURL());
76 78
                    } catch (PartInitException pie) {
77 79
                        MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, pie));
......
80 82
                    } catch (IllegalArgumentException iae) {
81 83
                        MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, iae));
82 84
                    }
83
		        }
84
		        else{
85
		            MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, null));
86
		        }
87
		    }
85
                }
86
                else{
87
                    MessagingUtils.informationDialog(errorTitle, new Status(IStatus.WARNING, TaxeditorStorePlugin.PLUGIN_ID, errorText, null));
88
                }
89
            }
88 90
        });
89
		btnOpenBrowser.setLayoutData(LayoutConstants.RIGHT());
90

  
91
		labelException = formFactory.createLabel(getLayoutComposite(), "", SWT.WRAP); //$NON-NLS-1$
92
		int numColumns = AbstractFormSection.DEFAULT_NUM_COLUMNS;
93
		if(getLayoutComposite().getLayout() instanceof TableWrapLayout){
94
		    numColumns = ((TableWrapLayout)getLayoutComposite().getLayout()).numColumns;
95
		}
96
        labelException.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(numColumns, 1));
97
		addControl(labelException);
98
		setUri(initialUri);
99
	}
91
        btnOpenBrowser.setLayoutData(LayoutConstants.RIGHT());
92

  
93
        initExceptionLabel(getLayoutComposite(), formFactory, initialObject);
94
    }
100 95

  
101
	public void setUri(URI uri) {
102
		if(uri != null){
103
			super.setText(uri.toString());
104
		}
105
	}
106 96

  
107
	public URI getUri(){
108
        try {
109
            labelException.setFont(JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT));
110
            labelException.setForeground(getPersistentBackground());
111
            labelException.setText(""); //$NON-NLS-1$
112
            return new URI(super.getText());
113
        } catch (Exception e) {
114
            labelException.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
115
            labelException.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
116
            labelException.setText(Messages.UriWithLabelElement_URL_NOT_SAVED+e.getLocalizedMessage());
117
            return null;
97

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

  
108
    /**
109
     * {@inheritDoc}
110
     */
111
    @Override
112
    protected URI getParsedText() throws Exception {
113
        return new URI(super.getText());
114
    }
120 115

  
121 116
}
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/media/ImageFileElement.java
117 117
	public void handleEvent(Object eventSource) {
118 118
		if(eventSource == text_uri){
119 119
			try {
120
			    URI uri = text_uri.getUri();
120
			    URI uri = text_uri.parseText();
121 121
				getEntity().setUri(uri);
122 122
				if(uri==null){
123 123
				    //buffer URI if parsing error occurred
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/media/MediaDetailElement.java
70 70
    public void handleEvent(Object eventSource){
71 71
        if(eventSource==textUri){
72 72
            textUri.setBackground(getPersistentBackground());
73
            URI uri = textUri.getUri();
73
            URI uri = textUri.parseText();
74 74
            singleMediaRepresentationPart.setUri(uri);
75 75
            if(uri==null){
76 76
                uriBuffer=textUri.getText();
......
113 113
            }
114 114
            textUri = formFactory.createUriWithLabelElement(parentFormElement, "Media URI", null, style);
115 115
            URI uri = singleMediaRepresentationPart.getUri();
116
            textUri.setUri(uri);
116
            textUri.setParsedText(uri);
117 117
            //set buffered uri as text if uri had parsing problems in advanced view
118 118
            if(uri==null && uriBuffer!=null){
119 119
                textUri.setText(uriBuffer);
120
                textUri.getUri();
120
                textUri.parseText();
121 121
            }
122 122
            textUri.getLayoutComposite().layout();
123 123
        }
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/media/MediaRepresentationPartElement.java
58 58
			text_size.setText(FileUtils.byteCountToDisplaySize(entity.getSize()));
59 59
		}
60 60
		if(entity.getUri() != null){
61
			text_uri.setUri(entity.getUri());
61
			text_uri.setParsedText(entity.getUri());
62 62
		} else {
63 63
            String uriBuffer = getParentMediaDetailElement().getUriBuffer();
64 64
            if(uriBuffer!=null){
65 65
                text_uri.setText(uriBuffer);
66
                text_uri.getUri();//just to update the error label
66
                text_uri.parseText();//just to update the error label
67 67
            }
68 68
        }
69 69
	}
......
71 71
	@Override
72 72
	public void handleEvent(Object eventSource) {
73 73
		if(eventSource == text_uri){
74
		    URI uri = text_uri.getUri();
74
		    URI uri = text_uri.parseText();
75 75
		    getEntity().setUri(uri);
76 76
		    if(uri==null){
77 77
                //buffer URI if parsing error occurred
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/name/ProtologueElement.java
110 110
	@Override
111 111
	public void handleEvent(Object eventSource) {
112 112
		if(eventSource == protologueUriText && protologueUriText.getText()!=null){
113
		    mediaRepresentationPart.setUri(protologueUriText.getUri());
113
		    mediaRepresentationPart.setUri(protologueUriText.parseText());
114 114
		}
115 115
	}
116 116

  
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/occurrence/dna/SequenceGeneralDetailElement.java
113 113
            getEntity().setGeneticAccessionNumber(textGeneticAccessNo.getText());
114 114
            if(textGeneticAccessNo.getText()!=null && !textGeneticAccessNo.getText().isEmpty()){
115 115
                try {
116
                    textNCBIUri.setUri(getEntity().getGenBankUri());
117
                    textENAUri.setUri(getEntity().getEmblUri());
118
                    textDDBJUri.setUri(getEntity().getDdbjUri());
116
                    textNCBIUri.setParsedText(getEntity().getGenBankUri());
117
                    textENAUri.setParsedText(getEntity().getEmblUri());
118
                    textDDBJUri.setParsedText(getEntity().getDdbjUri());
119 119
                } catch (URISyntaxException e) {
120 120
                    textGeneticAccessNo.setBackground(getColor(Resources.COLOR_PARSE_ERROR));
121 121
                }
......
131 131
            getEntity().setBoldProcessId(textBoldProcessID.getText());
132 132
            if(textBoldProcessID.getText()!=null && !textBoldProcessID.getText().isEmpty()){
133 133
                try {
134
                    textBoldUri.setUri(getEntity().getBoldUri());
134
                    textBoldUri.setParsedText(getEntity().getBoldUri());
135 135
                } catch (URISyntaxException e) {
136 136
                    textBoldProcessID.setBackground(getColor(Resources.COLOR_PARSE_ERROR));
137 137
                }
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/reference/ReferenceDetailElement.java
441 441
		    getEntity().setAbbrevTitle(text_abbrevTitle.getText());
442 442
		    toggleableAbbrevCache.setText(text_abbrevTitle.getText());
443 443
		} else if (eventSource == text_uri) {
444
			getEntity().setUri(text_uri.getUri());
444
			getEntity().setUri(text_uri.parseText());
445 445
		} else if (eventSource == text_volume) {
446 446
			getEntity().setVolume(text_volume.getText());
447 447
		}
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/vocabulary/NamedAreaDetailElement.java
66 66
		} else if (eventSource == text_description) {
67 67
			getEntity().getRepresentation(Language.getDefaultLanguage()).setText(text_description.getText());
68 68
		} else if (eventSource == uri_uri) {
69
			getEntity().setUri(uri_uri.getUri());
69
			getEntity().setUri(uri_uri.parseText());
70 70
		} else if (eventSource == text_abbreviatedLabel) {
71 71
			getEntity().getRepresentation(Language.getDefaultLanguage()).setAbbreviatedLabel(text_abbreviatedLabel.getText());
72 72
		} else if (eventSource == timePeriod_validPeriod) {
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/vocabulary/TermVocabularyDetailElement.java
51 51
		handleRepresentation(eventSource);
52 52

  
53 53
		if (eventSource == uri_uri) {
54
			getEntity().setUri(uri_uri.getUri());
54
			getEntity().setUri(uri_uri.parseText());
55 55
		} else if (eventSource == uri_uriTermSource) {
56
			getEntity().setTermSourceUri(uri_uriTermSource.getUri());
56
			getEntity().setTermSourceUri(uri_uriTermSource.parseText());
57 57
		}
58 58
	}
59 59

  

Also available in: Unified diff