]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/comparators/LexicalComparatorFactory.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / comparators / LexicalComparatorFactory.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
21 public class LexicalComparatorFactory implements ComparableContextFactory {
22
23     private final String column;
24     private final String label;
25
26     public LexicalComparatorFactory(String column) {
27         this.column = column;
28         this.label = null;
29     }
30
31     public LexicalComparatorFactory(String column, String label) {
32         this.column = column;
33         this.label = label;
34     }
35
36     @Override
37     public ComparableContext[] create(NodeQueryManager manager, NodeContext parent, NodeContext[] children) {
38         ComparableContext[] result = new ComparableContext[children.length];
39         for (int i = 0; i < children.length; i++) {
40             NodeContext child = children[i];
41             Labeler labeler = manager.query(child, BuiltinKeys.SELECTED_LABELER);
42
43             int category = (labeler != null) ? labeler.getCategory() : 0;
44             String label = (labeler != null) ? labeler.getLabels().get(column) : "";
45             if (label == null)
46                 label = "";
47
48             result[i] = new ImmutableLexicalComparable(category, label, child) {
49                 @Override
50                 public int compareTo(ComparableContext arg0) {
51                     ImmutableLexicalComparable other = (ImmutableLexicalComparable) arg0;
52
53                     int catDelta = getCategory() - other.getCategory();
54                     if (catDelta != 0)
55                         return catDelta;
56
57                     String label1 = getLabel();
58                     String label2 = other.getLabel();
59
60                     return label1.compareToIgnoreCase(label2);
61                 }
62             };
63         }
64
65         return result;
66     }
67
68     @Override
69     public String toString() {
70         return label != null ? label : "Alphabetical";
71     }
72
73 }