1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.common.comparators;
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;
23 * @author Tuukka Lehtonen
25 public class AlphanumericComparatorFactory implements ComparableContextFactory {
27 private final String column;
28 private final String label;
29 private final boolean reverse;
31 public AlphanumericComparatorFactory(String column) {
32 this(column, null, false);
35 public AlphanumericComparatorFactory(String column, boolean reverse) {
36 this(column, null, reverse);
39 public AlphanumericComparatorFactory(String column, String label, boolean reverse) {
42 this.reverse = reverse;
45 protected String adjustLabel(String label) {
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);
56 int category = (labeler != null) ? labeler.getCategory() : 0;
57 String label = (labeler != null) ? labeler.getLabels().get(column) : "";
61 result[i] = new ImmutableLexicalComparable(category, adjustLabel(label), child) {
63 public int compareTo(ComparableContext arg0) {
64 ImmutableLexicalComparable other = (ImmutableLexicalComparable) arg0;
66 int catDelta = getCategory() - other.getCategory();
72 String label1 = getLabel();
73 String label2 = other.getLabel();
75 return reverse ? AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(label2, label1)
76 : AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(label1, label2);
85 public String toString() {
86 return label != null ? label :
87 reverse ? "Reversed Natural" : "Natural";