Merge branch 'release/5.18.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / editor / definedterm / DefinedTermSorter.java
1 // $Id$
2 /**
3 * Copyright (C) 2018 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 package eu.etaxonomy.taxeditor.editor.definedterm;
11
12 import org.eclipse.jface.viewers.Viewer;
13 import org.eclipse.jface.viewers.ViewerComparator;
14
15 import eu.etaxonomy.cdm.api.service.l10n.TermRepresentation_L10n;
16 import eu.etaxonomy.cdm.model.term.OrderedTermBase;
17 import eu.etaxonomy.cdm.persistence.dto.TermDto;
18 import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
19
20 /**
21 * @author pplitzner
22 * @date 13.02.2018
23 */
24 public class DefinedTermSorter extends ViewerComparator {
25
26 @Override
27 public int compare(Viewer viewer, Object e1, Object e2) {
28 // the comparison value in this method determines the
29 // location <-> add term method used in the MoveDefinedTermOperation
30 // execute call
31 //FIXME: remove this when all viewer are changed to use DTOs
32 if(e1 instanceof OrderedTermBase && e2 instanceof OrderedTermBase) {
33 OrderedTermBase otbe1 = (OrderedTermBase)e1;
34 OrderedTermBase otbe2 = (OrderedTermBase)e2;
35 if(otbe1.getOrderIndex() == otbe2.getOrderIndex()) {
36 return 0;
37 } else if (otbe1.getOrderIndex() < otbe2.getOrderIndex()){
38 return -1;
39 } else{
40 return 1;
41 }
42 }
43 else if(e1 instanceof TermDto && e2 instanceof TermDto) {
44 TermDto termDto1 = (TermDto)e1;
45 TermDto termDto2 = (TermDto)e2;
46 int orderIndexCompare = 0;
47 if(termDto1.getOrderIndex()!=null){
48 if(termDto2.getOrderIndex()!=null){
49 orderIndexCompare = termDto1.getOrderIndex() - termDto2.getOrderIndex();
50 }
51 else{
52 orderIndexCompare = 1;
53 }
54 }
55 else if(termDto2.getOrderIndex()!=null){
56 orderIndexCompare = -1;
57 }
58 if(orderIndexCompare != 0){
59 return orderIndexCompare;
60 }
61 else {
62 // order indexes are equal or null -> compare by label
63 termDto1.localize(new TermRepresentation_L10n());
64 termDto2.localize(new TermRepresentation_L10n());
65 if(termDto1.getRepresentation_L10n()!=null){
66 if(termDto2.getRepresentation_L10n()!=null) {
67 return termDto1.getRepresentation_L10n().toLowerCase().compareTo(termDto2.getRepresentation_L10n().toLowerCase());
68 }
69 else{
70 return 1;
71 }
72 }
73 else if(termDto2.getRepresentation_L10n()!=null){
74 return -1;
75 }
76 }
77 }
78 else if(e1 instanceof TermVocabularyDto && e2 instanceof TermVocabularyDto) {
79 TermVocabularyDto termVoc1 = (TermVocabularyDto)e1;
80 TermVocabularyDto termVoc2 = (TermVocabularyDto)e2;
81 termVoc1.localize(new TermRepresentation_L10n());
82 termVoc2.localize(new TermRepresentation_L10n());
83 if(termVoc1.getRepresentation_L10n()!=null){
84 if(termVoc2.getRepresentation_L10n()!=null) {
85 return termVoc1.getRepresentation_L10n().toLowerCase().compareTo(termVoc2.getRepresentation_L10n().toLowerCase());
86 }
87 else{
88 return 1;
89 }
90 }
91 else if(termVoc2.getRepresentation_L10n()!=null){
92 return -1;
93 }
94 }
95 return super.compare(viewer, e1, e2);
96 }
97 }