Project

General

Profile

Download (4.95 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.section.supplemental;
11

    
12
import java.util.Comparator;
13

    
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.events.SelectionListener;
16

    
17
import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
18
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
19
import eu.etaxonomy.taxeditor.ui.combo.EnumComboElement;
20
import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
21
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
22
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
23
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
24

    
25
/**
26
 * @author n.hoffmann
27
 * @created Mar 16, 2010
28
 * @version 1.0
29
 */
30
public abstract class AbstractOriginalSourceElement<T extends OriginalSourceBase> extends AbstractReferencedEntityElement<T> {
31
	protected EnumComboElement<OriginalSourceType> combo_origsourcetype;
32
	protected TextWithLabelElement text_idInSource;
33
	protected TextWithLabelElement text_idNamespace;
34
    protected TextWithLabelElement text_originaleNameString;
35

    
36
	public AbstractOriginalSourceElement(CdmFormFactory formFactory,
37
			AbstractFormSection section,
38
			T element, SelectionListener removeListener,
39
			int style) {
40
		super(formFactory, section, element, removeListener, style);
41
	}
42

    
43
	/**
44
	 * {@inheritDoc}
45
	 */
46
	@Override
47
	public void setEntity(T entity) {
48
	    super.setEntity(entity);
49
        text_originaleNameString.setText(entity.getOriginalNameString());
50
	}
51

    
52
	/** {@inheritDoc} */
53
	@Override
54
	public void createControls(ICdmFormElement formElement, int style) {
55
		combo_origsourcetype = formFactory
56
				.createEnumComboElement(OriginalSourceType.class,
57
						formElement, new OriginalSourceTypeComparator(getEntity()), style);
58
		super.createControls(formElement, style);
59
		text_idInSource = formFactory.createTextWithLabelElement(formElement, "ID in Source", null, style);
60
		text_idNamespace = formFactory.createTextWithLabelElement(formElement, "ID Namespace", null, style);
61
		text_originaleNameString = formFactory.createTextWithLabelElement(
62
		        formElement, "Original Name", null, SWT.NULL);
63
	}
64

    
65
	/**
66
	 * Sorts source type combo alphabetically <b>except</b> for PrimaryTaxonomicSource resp.
67
	 * PrimaryMediaSource which are always on top depending on the IdentifiableSource;
68
	 * <br>
69
	 * The last two are always "Other" followed by "Unknown"
70
	 * @author pplitzner
71
	 * @date Jan 6, 2017
72
	 *
73
	 */
74
	private class OriginalSourceTypeComparator implements Comparator<OriginalSourceType>{
75

    
76
	    private OriginalSourceBase entity;
77

    
78
        public OriginalSourceTypeComparator(OriginalSourceBase entity) {
79
            this.entity = entity;
80
        }
81

    
82
        /**
83
         * {@inheritDoc}
84
         */
85
        @Override
86
        public int compare(OriginalSourceType o1, OriginalSourceType o2) {
87
            if(o1==null){
88
                if(o2==null){
89
                    return 0;
90
                }
91
                else{
92
                    return -1;
93
                }
94
            }
95
            if(o2==null){
96
                return 1;
97
            }
98
            //both are either taxonomic or media
99
            else if(o1.equals(OriginalSourceType.PrimaryTaxonomicSource)
100
                    && o2.equals(OriginalSourceType.PrimaryMediaSource)){
101
                if(entity.getType().equals(OriginalSourceType.PrimaryMediaSource)){
102
                    return 1;
103
                }
104
                else{
105
                    return -1;
106
                }
107
            }
108
            else if(o2.equals(OriginalSourceType.PrimaryTaxonomicSource)
109
                    && o1.equals(OriginalSourceType.PrimaryMediaSource)){
110
                if(entity.getType().equals(OriginalSourceType.PrimaryMediaSource)){
111
                    return -1;
112
                }
113
                else{
114
                    return 1;
115
                }
116
            }
117
            //one is not taxonomic or media
118
            else if(o1.equals(OriginalSourceType.PrimaryTaxonomicSource)
119
                    ||o1.equals(OriginalSourceType.PrimaryMediaSource)){
120
                return -1;
121

    
122
            }
123
            else if(o2.equals(OriginalSourceType.PrimaryTaxonomicSource)
124
                    || o2.equals(OriginalSourceType.PrimaryMediaSource)){
125
                return 1;
126
            }
127
            //The last two are always "Other" followed by "Unknown"
128
            else if(o1.equals(OriginalSourceType.Other)){
129
                if(o2.equals(OriginalSourceType.Unknown)){
130
                    return -11;
131
                }
132
                else{
133
                    return 1;
134
                }
135
            }
136
            if(o2.equals(OriginalSourceType.Other)){
137
                return 1;
138
            }
139
            else{
140
                String message1 = o1.getKey();
141
                String message2 = o2.getKey();
142
                return message1.compareTo(message2);
143
            }
144
        }
145

    
146
	}
147
}
(1-1/19)