]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/comparators/AlphanumericComparatorFactory.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / comparators / AlphanumericComparatorFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.common.comparators;
13
14 import org.simantics.browsing.ui.BuiltinKeys;
15 import org.simantics.browsing.ui.NodeContext;
16 import org.simantics.browsing.ui.NodeQueryManager;
17 import org.simantics.browsing.ui.content.ComparableContext;
18 import org.simantics.browsing.ui.content.ComparableContextFactory;
19 import org.simantics.browsing.ui.content.Labeler;
20 import org.simantics.utils.strings.AlphanumComparator;
21
22 /**
23  * @author Tuukka Lehtonen
24  */
25 public class AlphanumericComparatorFactory implements ComparableContextFactory {
26
27     private final String  column;
28     private final String  label;
29     private final boolean reverse;
30
31     public AlphanumericComparatorFactory(String column) {
32         this(column, null, false);
33     }
34
35     public AlphanumericComparatorFactory(String column, boolean reverse) {
36         this(column, null, reverse);
37     }
38
39     public AlphanumericComparatorFactory(String column, String label, boolean reverse) {
40         this.column = column;
41         this.label = label;
42         this.reverse = reverse;
43     }
44     
45     protected String adjustLabel(String label) {
46         return label;
47     }
48
49     @Override
50     public ComparableContext[] create(NodeQueryManager manager, NodeContext parent, NodeContext[] children) {
51         ComparableContext[] result = new ComparableContext[children.length];
52         for (int i = 0; i < children.length; i++) {
53             NodeContext child = children[i];
54             Labeler labeler = manager.query(child, BuiltinKeys.SELECTED_LABELER);
55
56             int category = (labeler != null) ? labeler.getCategory() : 0;
57             String label = (labeler != null) ? labeler.getLabels().get(column) : "";
58             if (label == null)
59                 label = "";
60
61             result[i] = new ImmutableLexicalComparable(category, adjustLabel(label), child) {
62                 @Override
63                 public int compareTo(ComparableContext arg0) {
64                     ImmutableLexicalComparable other = (ImmutableLexicalComparable) arg0;
65
66                     int catDelta = getCategory() - other.getCategory();
67                     if (reverse)
68                         catDelta = -catDelta;
69                     if (catDelta != 0)
70                         return catDelta;
71
72                     String label1 = getLabel();
73                     String label2 = other.getLabel();
74
75                     return reverse ? AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(label2, label1)
76                             : AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(label1, label2);
77                 }
78             };
79         }
80
81         return result;
82     }
83
84     @Override
85     public String toString() {
86         return label != null ? label :
87             reverse ? "Reversed Natural" : "Natural";
88     }
89
90 }