ref #8162 move OriginalSourceXXX to reference package
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / supplemental / AbstractOriginalSourceElement.java
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.reference.OriginalSourceBase;
18 import eu.etaxonomy.cdm.model.reference.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 import eu.etaxonomy.taxeditor.ui.section.common.ExternalLinksSection;
25
26 /**
27 * @author n.hoffmann
28 * @created Mar 16, 2010
29 * @version 1.0
30 */
31 public abstract class AbstractOriginalSourceElement<T extends OriginalSourceBase> extends AbstractReferencedEntityElement<T> {
32 protected EnumComboElement<OriginalSourceType> combo_origsourcetype;
33 protected TextWithLabelElement text_idInSource;
34 protected TextWithLabelElement text_idNamespace;
35 protected TextWithLabelElement text_originaleNameString;
36
37 protected ExternalLinksSection externalLinks;
38
39 public AbstractOriginalSourceElement(CdmFormFactory formFactory,
40 AbstractFormSection section,
41 T element, SelectionListener removeListener,
42 int style, boolean isCommonNameReference) {
43 super(formFactory, section, element, removeListener, style, isCommonNameReference);
44 }
45
46 public AbstractOriginalSourceElement(CdmFormFactory formFactory,
47 AbstractFormSection section,
48 T element, SelectionListener removeListener,
49 int style) {
50 super(formFactory, section, element, removeListener, style);
51 }
52
53 /**
54 * {@inheritDoc}
55 */
56 @Override
57 public void setEntity(T entity) {
58 super.setEntity(entity);
59 text_originaleNameString.setText(entity.getOriginalNameString());
60 externalLinks.setEntity(entity);
61 }
62
63 /** {@inheritDoc}
64 * @wbp.parser.entryPoint*/
65 @Override
66 public void createControls(ICdmFormElement formElement, int style) {
67 combo_origsourcetype = formFactory
68 .createEnumComboElement(OriginalSourceType.class,
69 formElement, new OriginalSourceTypeComparator(getEntity()), style);
70 super.createControls(formElement, style);
71 text_idInSource = formFactory.createTextWithLabelElement(formElement, "ID in Source", null, style);
72 text_idNamespace = formFactory.createTextWithLabelElement(formElement, "ID Namespace", null, style);
73 text_originaleNameString = formFactory.createTextWithLabelElement(
74 formElement, "Original Information", null, SWT.NULL);
75
76
77
78 }
79
80 /**
81 * Sorts source type combo alphabetically <b>except</b> for PrimaryTaxonomicSource resp.
82 * PrimaryMediaSource which are always on top depending on the IdentifiableSource;
83 * <br>
84 * The last two are always "Other" followed by "Unknown"
85 * @author pplitzner
86 * @date Jan 6, 2017
87 *
88 */
89 private class OriginalSourceTypeComparator implements Comparator<OriginalSourceType>{
90
91 private OriginalSourceBase entity;
92
93 public OriginalSourceTypeComparator(OriginalSourceBase entity) {
94 this.entity = entity;
95 }
96
97 /**
98 * {@inheritDoc}
99 */
100 @Override
101 public int compare(OriginalSourceType o1, OriginalSourceType o2) {
102 if(o1==null){
103 if(o2==null){
104 return 0;
105 }
106 else{
107 return -1;
108 }
109 }
110 if(o2==null){
111 return 1;
112 }
113 //both are either taxonomic or media
114 else if(o1.equals(OriginalSourceType.PrimaryTaxonomicSource)
115 && o2.equals(OriginalSourceType.PrimaryMediaSource)){
116 if(entity.getType().equals(OriginalSourceType.PrimaryMediaSource)){
117 return 1;
118 }
119 else{
120 return -1;
121 }
122 }
123 else if(o2.equals(OriginalSourceType.PrimaryTaxonomicSource)
124 && o1.equals(OriginalSourceType.PrimaryMediaSource)){
125 if(entity.getType().equals(OriginalSourceType.PrimaryMediaSource)){
126 return -1;
127 }
128 else{
129 return 1;
130 }
131 }
132 //one is not taxonomic or media
133 else if(o1.equals(OriginalSourceType.PrimaryTaxonomicSource)
134 ||o1.equals(OriginalSourceType.PrimaryMediaSource)){
135 return -1;
136
137 }
138 else if(o2.equals(OriginalSourceType.PrimaryTaxonomicSource)
139 || o2.equals(OriginalSourceType.PrimaryMediaSource)){
140 return 1;
141 }
142 //The last two are always "Other" followed by "Unknown"
143 else if(o1.equals(OriginalSourceType.Other)){
144 if(o2.equals(OriginalSourceType.Unknown)){
145 return -11;
146 }
147 else{
148 return 1;
149 }
150 }
151 if(o2.equals(OriginalSourceType.Other)){
152 return 1;
153 }
154 else{
155 String message1 = o1.getKey();
156 String message2 = o2.getKey();
157 return message1.compareTo(message2);
158 }
159 }
160
161 }
162 }