9f1a896794ef7da0554f7e5f5342bc515afe676e
[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.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 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 }
116 }