Coverage Report - org.argouml.ui.explorer.TypeThenNameOrder
 
Classes in this File Line Coverage Branch Coverage Complexity
TypeThenNameOrder
65%
19/29
60%
17/28
8.333
 
 1  
 /* $Id: TypeThenNameOrder.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  
  *    tfmorris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2006 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 javax.swing.tree.DefaultMutableTreeNode;
 42  
 import org.argouml.i18n.Translator;
 43  
 
 44  
 /**
 45  
  * Sorts by user object type,
 46  
  * diagrams first,
 47  
  * then packages,
 48  
  * then other types.
 49  
  *
 50  
  * sorts by name within type groups.
 51  
  *
 52  
  * @since 0.15.2, Created on 28 September 2003, 10:02
 53  
  *
 54  
  * @author  alexb
 55  
  */
 56  
 public class TypeThenNameOrder extends NameOrder {
 57  
 
 58  
     /** Creates a new instance of TypeThenNameOrder */
 59  1800
     public TypeThenNameOrder() {
 60  1800
     }
 61  
 
 62  
     /*
 63  
      * Compares obj1 and obj2 sorting by user object type, then name. Diagrams
 64  
      * are sorted first, then packages, then other types. sorts by name within
 65  
      * type groups. Nulls are sorted first for names.
 66  
      * 
 67  
      * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
 68  
      */
 69  
     @Override
 70  
     public int compare(Object obj1, Object obj2) {
 71  20693
         if (obj1 instanceof DefaultMutableTreeNode) {
 72  0
             DefaultMutableTreeNode node = (DefaultMutableTreeNode) obj1;
 73  0
             obj1 = node.getUserObject();
 74  
         }
 75  
 
 76  20693
         if (obj2 instanceof DefaultMutableTreeNode) {
 77  0
             DefaultMutableTreeNode node = (DefaultMutableTreeNode) obj2;
 78  0
             obj2 = node.getUserObject();
 79  
         }
 80  
 
 81  20693
         if (obj1 == null) {
 82  0
             if (obj2 == null)
 83  0
                 return 0;
 84  0
             return -1;
 85  20693
         } else if (obj2 == null) {
 86  0
             return 1;
 87  
         }
 88  
 
 89  20693
         String typeName = obj1.getClass().getName();
 90  20693
         String typeName1 = obj2.getClass().getName();
 91  
 
 92  
         // all diagram types treated equally, see issue 2260
 93  
         // if (typeName.indexOf("Diagram") != -1
 94  
         //     && typeName1.indexOf("Diagram") != -1)
 95  
         //     return compareUserObjects(obj1, obj2);
 96  
 
 97  20693
         int typeNameOrder = typeName.compareTo(typeName1);
 98  20693
         if (typeNameOrder == 0)
 99  186
             return compareUserObjects(obj1, obj2);
 100  
 
 101  20507
         if (typeName.indexOf("Diagram") == -1
 102  
             && typeName1.indexOf("Diagram") != -1)
 103  341
             return 1;
 104  
 
 105  20166
         if (typeName.indexOf("Diagram") != -1
 106  
             && typeName1.indexOf("Diagram") == -1)
 107  50
             return -1;
 108  
 
 109  20116
         if (typeName.indexOf("Package") == -1
 110  
             && typeName1.indexOf("Package") != -1)
 111  0
             return 1;
 112  
 
 113  20116
         if (typeName.indexOf("Package") != -1
 114  
             && typeName1.indexOf("Package") == -1)
 115  0
             return -1;
 116  
 
 117  20116
         return typeNameOrder;
 118  
     }
 119  
 
 120  
     /*
 121  
      * @see java.lang.Object#toString()
 122  
      */
 123  
     @Override
 124  
     public String toString() {
 125  2920
         return Translator.localize("combobox.order-by-type-name");
 126  
     }
 127  
 }