Author: tfmorris
Date: 2007-12-07 19:05:03-0800
New Revision: 13875
Added:
trunk/src_new/org/argouml/uml/util/PathComparator.java (contents, props changed)
Modified:
trunk/src_new/org/argouml/uml/ui/foundation/core/UMLStructuralFeatureTypeComboBoxModel.java
Log:
Refactor path comparator into its own class so that it can be reused
Modified: trunk/src_new/org/argouml/uml/ui/foundation/core/UMLStructuralFeatureTypeComboBoxModel.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src_new/org/argouml/uml/ui/foundation/core/UMLStructuralFeatureTypeComboBoxModel.java?view=diff&rev=13875&p1=trunk/src_new/org/argouml/uml/ui/foundation/core/UMLStructuralFeatureTypeComboBoxModel.java&p2=trunk/src_new/org/argouml/uml/ui/foundation/core/UMLStructuralFeatureTypeComboBoxModel.java&r1=13874&r2=13875
==============================================================================
--- trunk/src_new/org/argouml/uml/ui/foundation/core/UMLStructuralFeatureTypeComboBoxModel.java (original)
+++ trunk/src_new/org/argouml/uml/ui/foundation/core/UMLStructuralFeatureTypeComboBoxModel.java 2007-12-07 19:05:03-0800
@@ -25,11 +25,6 @@
package org.argouml.uml.ui.foundation.core;
import java.beans.PropertyChangeEvent;
-import java.text.Collator;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.Iterator;
-import java.util.List;
import java.util.Set;
import java.util.TreeSet;
@@ -37,6 +32,7 @@
import org.argouml.kernel.ProjectManager;
import org.argouml.model.Model;
import org.argouml.uml.ui.UMLComboBoxModel2;
+import org.argouml.uml.util.PathComparator;
/**
* The combobox model for the type belonging to some attribute.
@@ -73,22 +69,7 @@
* @see org.argouml.uml.ui.UMLComboBoxModel2#buildModelList()
*/
protected void buildModelList() {
- Set<Object> elements = new TreeSet<Object>(new Comparator<Object>() {
- public int compare(Object o1, Object o2) {
- if (o1.equals(o2)) {
- return 0;
- }
- // Elements are collated first by name and then by
- // their enclosing path to distinguish them
- List<String> path1 = Model.getModelManagementHelper()
- .getPathList(o1);
- Collections.reverse(path1);
- List<String> path2 = Model.getModelManagementHelper()
- .getPathList(o2);
- Collections.reverse(path2);
- return compareStringLists(path1, path2);
- }
- });
+ Set<Object> elements = new TreeSet<Object>(new PathComparator());
Project p = ProjectManager.getManager().getCurrentProject();
if (p == null) {
@@ -117,52 +98,6 @@
addAll(elements);
}
- /**
- * Compare two lists of strings using a primary strength text collator.
- * This will collate e, E, é, É together, but not eliminate non-identical
- * strings which collate in the same place.
- *
- * @return equivalent of list1.compareTo(list2)
- */
- private static int compareStringLists(List<String> list1,
- List<String> list2) {
- Collator collator = Collator.getInstance();
- collator.setStrength(Collator.PRIMARY);
- Iterator<String> i2 = list2.iterator();
- Iterator<String> i1 = list1.iterator();
- boolean caseDiffers = false;
- while (i2.hasNext()) {
- String name2 = i2.next();
- if (!i1.hasNext()) {
- return -1;
- }
- String name1 = i1.next();
- if (name1 == null) {
- return -1;
- }
- int comparison = collator.compare(name1, name2);
- if (comparison != 0) {
- return comparison;
- }
- caseDiffers = caseDiffers | !(name1.equals(name2));
- }
- if (i2.hasNext()) {
- return 1;
- }
- // If the strings differed only in non-primary characteristics at
- // some point (case, accent, etc) pick an arbitrary collating order.
- // We don't call them equal to keep them from being merged in the list.
- if (caseDiffers) {
- return 1;
- }
- // It's illegal in UML to have multiple elements in a namespace with
- // the same name, but if it happens, keep them distinct so the user
- // has a chance of catching the error. Pick an arbitrary collating
- // order.
- // Note: this may make the collating order unstable.
- return 1;
- }
-
/*
* @see org.argouml.uml.ui.UMLComboBoxModel2#getSelectedModelElement()
*/
@@ -191,3 +126,4 @@
}
}
+
Added: trunk/src_new/org/argouml/uml/util/PathComparator.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src_new/org/argouml/uml/util/PathComparator.java?view=auto&rev=13875
==============================================================================
--- (empty file)
+++ trunk/src_new/org/argouml/uml/util/PathComparator.java 2007-12-07 19:05:03-0800
@@ -0,0 +1,113 @@
+// $Id$
+// Copyright (c) 2007 The Regents of the University of California. All
+// Rights Reserved. Permission to use, copy, modify, and distribute this
+// software and its documentation without fee, and without a written
+// agreement is hereby granted, provided that the above copyright notice
+// and this paragraph appear in all copies. This software program and
+// documentation are copyrighted by The Regents of the University of
+// California. The software program and documentation are supplied "AS
+// IS", without any accompanying services from The Regents. The Regents
+// does not warrant that the operation of the program will be
+// uninterrupted or error-free. The end-user understands that the program
+// was developed for research purposes and is advised not to rely
+// exclusively on the program for any reason. IN NO EVENT SHALL THE
+// UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
+// SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
+// ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
+// THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
+// SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
+// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
+// PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
+// CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
+// UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
+
+package org.argouml.uml.util;
+
+import java.text.Collator;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+
+import org.argouml.model.Model;
+
+
+/**
+ * Comparator which orders model elements alphabetically by name, ignoring case.
+ * Ties are broken using names from the path in reverse order.
+ *
+ * @author Tom Morris <[email protected]>
+ */
+public class PathComparator implements Comparator {
+
+ /**
+ * Compare two UML elements names, ignoring case, using names from the path
+ * as tie breakers.
+ *
+ * @param o1 first model element
+ * @param o2 second model element
+ * @return -1, 0, 1
+ * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
+ */
+ public int compare(Object o1, Object o2) {
+ if (o1.equals(o2)) {
+ return 0;
+ }
+ // Elements are collated first by name and then by
+ // their enclosing path to distinguish them
+ List<String> path1 = Model.getModelManagementHelper().getPathList(o1);
+ Collections.reverse(path1);
+ List<String> path2 = Model.getModelManagementHelper().getPathList(o2);
+ Collections.reverse(path2);
+ return compareStringLists(path1, path2);
+ }
+
+ /*
+ * Compare two lists of strings using a primary strength text collator.
+ * This will collate e, E, é, É together, but not eliminate non-identical
+ * strings which collate in the same place.
+ *
+ * @return equivalent of list1.compareTo(list2)
+ */
+ private int compareStringLists(List<String> list1, List<String> list2) {
+ Collator collator = Collator.getInstance();
+ collator.setStrength(Collator.PRIMARY);
+ Iterator<String> i2 = list2.iterator();
+ Iterator<String> i1 = list1.iterator();
+ boolean caseDiffers = false;
+ while (i2.hasNext()) {
+ String name2 = i2.next();
+ if (!i1.hasNext()) {
+ return -1;
+ }
+ String name1 = i1.next();
+ if (name1 == null) {
+ return -1;
+ }
+ int comparison = collator.compare(name1, name2);
+ if (comparison != 0) {
+ return comparison;
+ }
+ caseDiffers = caseDiffers | !(name1.equals(name2));
+ }
+ if (i2.hasNext()) {
+ return 1;
+ }
+ // If the strings differed only in non-primary characteristics at
+ // some point (case, accent, etc) pick an arbitrary collating order.
+ // We don't call them equal to keep them from being merged in the list.
+ if (caseDiffers) {
+ return 1;
+ }
+ // It's illegal in UML to have multiple elements in a namespace with
+ // the same name, but if it happens, keep them distinct so the user
+ // has a chance of catching the error. Pick an arbitrary collating
+ // order.
+ // Note: this may make the collating order unstable.
+ return 1;
+ }
+}
+
+
+
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.