1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.common.comparators;
\r
14 import org.simantics.browsing.ui.BuiltinKeys;
\r
15 import org.simantics.browsing.ui.NodeContext;
\r
16 import org.simantics.browsing.ui.NodeQueryManager;
\r
17 import org.simantics.browsing.ui.content.ComparableContext;
\r
18 import org.simantics.browsing.ui.content.ComparableContextFactory;
\r
19 import org.simantics.browsing.ui.content.Labeler;
\r
20 import org.simantics.utils.strings.AlphanumComparator;
\r
23 * @author Tuukka Lehtonen
\r
25 public class AlphanumericComparatorFactory implements ComparableContextFactory {
\r
27 private final String column;
\r
28 private final String label;
\r
29 private final boolean reverse;
\r
31 public AlphanumericComparatorFactory(String column) {
\r
32 this(column, null, false);
\r
35 public AlphanumericComparatorFactory(String column, boolean reverse) {
\r
36 this(column, null, reverse);
\r
39 public AlphanumericComparatorFactory(String column, String label, boolean reverse) {
\r
40 this.column = column;
\r
42 this.reverse = reverse;
\r
45 protected String adjustLabel(String label) {
\r
50 public ComparableContext[] create(NodeQueryManager manager, NodeContext parent, NodeContext[] children) {
\r
51 ComparableContext[] result = new ComparableContext[children.length];
\r
52 for (int i = 0; i < children.length; i++) {
\r
53 NodeContext child = children[i];
\r
54 Labeler labeler = manager.query(child, BuiltinKeys.SELECTED_LABELER);
\r
56 int category = (labeler != null) ? labeler.getCategory() : 0;
\r
57 String label = (labeler != null) ? labeler.getLabels().get(column) : "";
\r
61 result[i] = new ImmutableLexicalComparable(category, adjustLabel(label), child) {
\r
63 public int compareTo(ComparableContext arg0) {
\r
64 ImmutableLexicalComparable other = (ImmutableLexicalComparable) arg0;
\r
66 int catDelta = getCategory() - other.getCategory();
\r
68 catDelta = -catDelta;
\r
72 String label1 = getLabel();
\r
73 String label2 = other.getLabel();
\r
75 return reverse ? AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(label2, label1)
\r
76 : AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(label1, label2);
\r
85 public String toString() {
\r
86 return label != null ? label :
\r
87 reverse ? "Reversed Natural" : "Natural";
\r