Revision 0c7c9656
Added by Patrick Plitzner over 7 years ago
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/combo/EnumComboElement.java | ||
---|---|---|
12 | 12 |
import java.util.ArrayList; |
13 | 13 |
import java.util.Arrays; |
14 | 14 |
import java.util.Collection; |
15 |
import java.util.Collections; |
|
16 |
import java.util.Comparator; |
|
15 | 17 |
import java.util.LinkedHashMap; |
16 | 18 |
import java.util.List; |
17 | 19 |
import java.util.Map.Entry; |
... | ... | |
123 | 125 |
private final Class<T> enumType; |
124 | 126 |
|
125 | 127 |
public EnumComboElement(CdmFormFactory formFactory, |
126 |
ICdmFormElement parentElement, Class<T> enumType, int style) { |
|
128 |
ICdmFormElement parentElement, Class<T> enumType, Comparator<T> comparator, int style) {
|
|
127 | 129 |
super(formFactory, parentElement); |
128 | 130 |
this.enumType = enumType; |
129 | 131 |
label.setText(getDefaultLabelString()); |
130 | 132 |
|
131 |
populateTypes(); |
|
133 |
populateTypes(comparator);
|
|
132 | 134 |
|
133 | 135 |
combo.addSelectionListener(this); |
134 | 136 |
} |
... | ... | |
146 | 148 |
return "No Label"; |
147 | 149 |
} |
148 | 150 |
|
149 |
private void populateTypes(){ |
|
150 |
Collection<T> elementsForClass = getElementsForClass(enumType); |
|
151 |
private void populateTypes(Comparator<T> comparator){ |
|
152 |
List<T> elementsForClass = new ArrayList<T>(getElementsForClass(enumType)); |
|
153 |
if(comparator!=null){ |
|
154 |
Collections.sort(elementsForClass, comparator); |
|
155 |
} |
|
151 | 156 |
// special hierarchical order for specimens |
152 | 157 |
if(enumType.equals(SpecimenOrObservationType.class)){ |
153 | 158 |
LinkedHashMap<T, String> orderedTerms = AbstractUtility.orderTerms(elementsForClass); |
... | ... | |
158 | 163 |
} |
159 | 164 |
// default order |
160 | 165 |
else{ |
161 |
for(T enumElement : getElementsForClass(enumType)){
|
|
166 |
for(T enumElement : elementsForClass){
|
|
162 | 167 |
elementTypeList.add(enumElement); |
163 | 168 |
combo.add(((Enum) enumElement).name()); |
164 | 169 |
} |
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/element/CdmFormFactory.java | ||
---|---|---|
5 | 5 |
|
6 | 6 |
import java.net.URI; |
7 | 7 |
import java.util.ArrayList; |
8 |
import java.util.Comparator; |
|
8 | 9 |
import java.util.HashSet; |
9 | 10 |
import java.util.List; |
10 | 11 |
import java.util.Map; |
... | ... | |
971 | 972 |
return element; |
972 | 973 |
} |
973 | 974 |
|
974 |
/** |
|
975 |
* <p> |
|
976 |
* createEnumComboElement |
|
977 |
* </p> |
|
978 |
* |
|
979 |
* @param enumComboType |
|
980 |
* a |
|
981 |
* {@link eu.eu.etaxonomy.taxeditor.ui.element.CdmFormFactory.EnumComboType} |
|
982 |
* object. |
|
983 |
* @param parentElement |
|
984 |
* a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} |
|
985 |
* object. |
|
986 |
* @param style |
|
987 |
* a int. |
|
988 |
* @return a {@link eu.etaxonomy.taxeditor.ui.term.AbstractEnumComboElement} |
|
989 |
* object. |
|
990 |
*/ |
|
991 | 975 |
public <T extends IEnumTerm<T>> EnumComboElement<T> createEnumComboElement( |
992 |
Class<T> enumComboType, ICdmFormElement parentElement, |
|
976 |
Class<T> enumComboType, ICdmFormElement parentElement, |
|
977 |
int style) { |
|
978 |
return createEnumComboElement(enumComboType, parentElement, null, style); |
|
979 |
} |
|
980 |
|
|
981 |
public <T extends IEnumTerm<T>> EnumComboElement<T> createEnumComboElement( |
|
982 |
Class<T> enumComboType, ICdmFormElement parentElement, Comparator<T> comparator, |
|
993 | 983 |
int style) { |
994 |
EnumComboElement<T> element = new EnumComboElement<T>(this, parentElement, enumComboType, style); |
|
984 |
EnumComboElement<T> element = new EnumComboElement<T>(this, parentElement, enumComboType, comparator, style);
|
|
995 | 985 |
adapt(element); |
996 | 986 |
parentElement.addElement(element); |
997 | 987 |
return element; |
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/AbstractEntityCollectionElement.java | ||
---|---|---|
68 | 68 |
AbstractFormSection section, ENTITY entity, SelectionListener removeListener, |
69 | 69 |
boolean isChoosableEntity, Color backgroundColor, int style) { |
70 | 70 |
super(formFactory, (ICdmFormElement) section); |
71 |
this.entity = entity; |
|
71 | 72 |
|
72 | 73 |
init(); |
73 | 74 |
|
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/supplemental/AbstractOriginalSourceElement.java | ||
---|---|---|
9 | 9 |
|
10 | 10 |
package eu.etaxonomy.taxeditor.ui.section.supplemental; |
11 | 11 |
|
12 |
import java.util.Comparator; |
|
13 |
|
|
12 | 14 |
import org.eclipse.swt.SWT; |
13 | 15 |
import org.eclipse.swt.events.SelectionListener; |
14 | 16 |
|
... | ... | |
52 | 54 |
public void createControls(ICdmFormElement formElement, int style) { |
53 | 55 |
combo_origsourcetype = formFactory |
54 | 56 |
.createEnumComboElement(OriginalSourceType.class, |
55 |
formElement, style); |
|
57 |
formElement, new OriginalSourceTypeComparator(getEntity()), style);
|
|
56 | 58 |
super.createControls(formElement, style); |
57 | 59 |
text_idInSource = formFactory.createTextWithLabelElement(formElement, "ID in Source", null, style); |
58 | 60 |
text_idNamespace = formFactory.createTextWithLabelElement(formElement, "ID Namespace", null, style); |
59 | 61 |
text_originaleNameString = formFactory.createTextWithLabelElement( |
60 | 62 |
formElement, "Original Name", null, SWT.NULL); |
61 | 63 |
} |
64 |
|
|
65 |
/** |
|
66 |
* Sorts source type combo alphabetically <b>except</b> for PrimaryTaxonomicSource resp. |
|
67 |
* PrimaryMediaSource which are alway on top depending on the IdentifiableSource; |
|
68 |
* @author pplitzner |
|
69 |
* @date Jan 6, 2017 |
|
70 |
* |
|
71 |
*/ |
|
72 |
private class OriginalSourceTypeComparator implements Comparator<OriginalSourceType>{ |
|
73 |
|
|
74 |
private OriginalSourceBase entity; |
|
75 |
|
|
76 |
public OriginalSourceTypeComparator(OriginalSourceBase entity) { |
|
77 |
this.entity = entity; |
|
78 |
} |
|
79 |
|
|
80 |
/** |
|
81 |
* {@inheritDoc} |
|
82 |
*/ |
|
83 |
@Override |
|
84 |
public int compare(OriginalSourceType o1, OriginalSourceType o2) { |
|
85 |
if(o1!=null && o2==null){ |
|
86 |
return 1; |
|
87 |
} |
|
88 |
else if(o1==null && o2!=null){ |
|
89 |
return -1; |
|
90 |
} |
|
91 |
else if(o1!=null && o2 !=null){ |
|
92 |
if(entity.getType().equals(OriginalSourceType.PrimaryMediaSource)){ |
|
93 |
if(o1.equals(OriginalSourceType.PrimaryMediaSource)){ |
|
94 |
return -1; |
|
95 |
} |
|
96 |
else if(o2.equals(OriginalSourceType.PrimaryMediaSource)){ |
|
97 |
return 1; |
|
98 |
} |
|
99 |
} |
|
100 |
else{ |
|
101 |
if(o1.equals(OriginalSourceType.PrimaryTaxonomicSource)){ |
|
102 |
return -1; |
|
103 |
} |
|
104 |
else if(o2.equals(OriginalSourceType.PrimaryTaxonomicSource)){ |
|
105 |
return 1; |
|
106 |
} |
|
107 |
} |
|
108 |
String message1 = o1.getMessage(); |
|
109 |
String message2 = o2.getMessage(); |
|
110 |
return message1.compareTo(message2); |
|
111 |
} |
|
112 |
return 0; |
|
113 |
} |
|
114 |
|
|
115 |
} |
|
62 | 116 |
} |
Also available in: Unified diff
fix #5714 Add possibility for adding a comparator to EnumComboElement