Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
NameOrder |
|
| 2.8;2.8 |
1 | /* $Id: NameOrder.java 17843 2010-01-12 19:23:29Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
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 | * euluis | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2007 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.ui.explorer; | |
40 | ||
41 | import java.text.Collator; | |
42 | import java.util.Comparator; | |
43 | ||
44 | import javax.swing.tree.DefaultMutableTreeNode; | |
45 | ||
46 | import org.argouml.i18n.Translator; | |
47 | import org.argouml.model.InvalidElementException; | |
48 | import org.argouml.model.Model; | |
49 | import org.argouml.profile.Profile; | |
50 | import org.argouml.kernel.ProfileConfiguration; | |
51 | import org.tigris.gef.base.Diagram; | |
52 | ||
53 | /** | |
54 | * Sorts explorer nodes by their user object name. | |
55 | * | |
56 | * @author alexb | |
57 | * @since 0.15.2, Created on 28 September 2003, 10:02 | |
58 | */ | |
59 | public class NameOrder | |
60 | implements Comparator { | |
61 | ||
62 | 2700 | private Collator collator = Collator.getInstance(); |
63 | ||
64 | /** | |
65 | * Creates a new instance of NameOrder. | |
66 | */ | |
67 | 2700 | public NameOrder() { |
68 | 2700 | collator.setStrength(Collator.PRIMARY); |
69 | 2700 | } |
70 | ||
71 | /* | |
72 | * Do string compare of names of UML objects. Comparison is | |
73 | * case insensitive using a primary strength collator in the user's | |
74 | * locale. | |
75 | * | |
76 | * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) | |
77 | */ | |
78 | public int compare(Object obj1, Object obj2) { | |
79 | ||
80 | 0 | if (obj1 instanceof DefaultMutableTreeNode) { |
81 | 0 | DefaultMutableTreeNode node = (DefaultMutableTreeNode) obj1; |
82 | 0 | obj1 = node.getUserObject(); |
83 | } | |
84 | ||
85 | 0 | if (obj2 instanceof DefaultMutableTreeNode) { |
86 | 0 | DefaultMutableTreeNode node = (DefaultMutableTreeNode) obj2; |
87 | 0 | obj2 = node.getUserObject(); |
88 | } | |
89 | ||
90 | 0 | return compareUserObjects(obj1, obj2); |
91 | } | |
92 | ||
93 | /** | |
94 | * Alphabetic ordering of user object names instead of type names. | |
95 | * | |
96 | * @param obj Diagram or Base | |
97 | * @param obj1 Diagram or Base | |
98 | * @return 0 if invalid params. 0 if the objects are equally named. | |
99 | * A positive or negative int if the names differ. | |
100 | */ | |
101 | protected int compareUserObjects(Object obj, Object obj1) { | |
102 | // this is safe because getName always returns a string of some type | |
103 | 186 | return collator.compare(getName(obj), getName(obj1)); |
104 | } | |
105 | ||
106 | /** | |
107 | * Get the name of the diagram or model element. | |
108 | * | |
109 | * @param obj the item to fetch name from | |
110 | * @return the name | |
111 | */ | |
112 | private String getName(Object obj) { | |
113 | String name; | |
114 | 372 | if (obj instanceof Diagram) { |
115 | 368 | name = ((Diagram) obj).getName(); |
116 | 4 | } else if (obj instanceof ProfileConfiguration) { |
117 | 0 | name = "Profile Configuration"; |
118 | 4 | } else if (obj instanceof Profile) { |
119 | 0 | name = ((Profile) obj).getDisplayName(); |
120 | 4 | } else if (Model.getFacade().isAModelElement(obj)) { |
121 | try { | |
122 | 4 | name = Model.getFacade().getName(obj); |
123 | 0 | } catch (InvalidElementException e) { |
124 | 0 | name = Translator.localize("misc.name.deleted"); |
125 | 4 | } |
126 | } else { | |
127 | 0 | name = "??"; |
128 | } | |
129 | ||
130 | 372 | if (name == null) { |
131 | 4 | return ""; |
132 | } | |
133 | 368 | return name; |
134 | } | |
135 | ||
136 | /* | |
137 | * @see java.lang.Object#toString() | |
138 | */ | |
139 | @Override | |
140 | public String toString() { | |
141 | 900 | return Translator.localize("combobox.order-by-name"); |
142 | } | |
143 | } |