KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > FolderComparator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.loaders;
21
22 import java.util.Date JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import org.openide.filesystems.FileObject;
25 import org.openide.nodes.Node;
26
27 /**
28  * Compares objects in a folder.
29  * @author Jaroslav Tulach, Jesse Glick
30  */

31 class FolderComparator extends DataFolder.SortMode {
32     /** modes */
33     public static final int NONE = 0;
34     public static final int NAMES = 1;
35     public static final int CLASS = 2;
36     /** first of all DataFolders, then other object, everything sorted
37     * by names
38     */

39     public static final int FOLDER_NAMES = 3;
40     /** by folders, then modification time */
41     public static final int LAST_MODIFIED = 4;
42     /** by folders, then size */
43     public static final int SIZE = 5;
44
45
46     /** mode to use */
47     private int mode;
48
49     /** New comparator. Sorts folders first and everything by names.
50     */

51     public FolderComparator () {
52         this (FOLDER_NAMES);
53     }
54
55     /** New comparator.
56     * @param mode one of sorting type constants
57     */

58     public FolderComparator (int mode) {
59         this.mode = mode;
60     }
61
62     /** Comparing method. Can compare two DataObjects
63     * or two Nodes (if they have data object cookie)
64     */

65     public int compare (Object JavaDoc o1, Object JavaDoc o2) {
66         DataObject obj1;
67         DataObject obj2;
68
69         if (o1 instanceof Node) {
70             obj1 = (DataObject)((Node)o1).getCookie (DataObject.class);
71             obj2 = (DataObject)((Node)o2).getCookie (DataObject.class);
72         } else {
73             obj1 = (DataObject)o1;
74             obj2 = (DataObject)o2;
75         }
76
77         switch (mode) {
78         case NONE:
79             return 0;
80         case NAMES:
81             return compareNames (obj1, obj2);
82         case CLASS:
83             return compareClass (obj1, obj2);
84         case FOLDER_NAMES:
85             return compareFoldersFirst (obj1, obj2);
86         case LAST_MODIFIED:
87             return compareLastModified(obj1, obj2);
88         case SIZE:
89             return compareSize(obj1, obj2);
90         default:
91             assert false : mode;
92             return 0;
93         }
94     }
95
96
97     /** for sorting data objects by names */
98     private int compareNames (DataObject obj1, DataObject obj2) {
99         // #35069 - use extension for sorting if displayname is same.
100
// Otherwise the order of files is random.
101
int part = obj1.getName().compareTo(obj2.getName());
102         return part != 0 ? part :
103             obj1.getPrimaryFile().getExt().compareTo(obj2.getPrimaryFile().getExt());
104     }
105
106     /** for sorting folders first and then by names */
107     private int compareFoldersFirst (DataObject obj1, DataObject obj2) {
108         if (obj1.getClass () != obj2.getClass ()) {
109             // if classes are different than the folder goes first
110
if (obj1 instanceof DataFolder) {
111                 return -1;
112             }
113             if (obj2 instanceof DataFolder) {
114                 return 1;
115             }
116         }
117
118         // otherwise compare by names
119
return compareNames(obj1, obj2);
120     }
121
122     /** for sorting data objects by their classes */
123     private int compareClass (DataObject obj1, DataObject obj2) {
124         Class JavaDoc<?> c1 = obj1.getClass ();
125         Class JavaDoc<?> c2 = obj2.getClass ();
126
127         if (c1 == c2) {
128             return compareNames(obj1, obj2);
129         }
130
131         // sort by classes
132
DataLoaderPool dlp = DataLoaderPool.getDefault();
133         final Enumeration JavaDoc loaders = dlp.allLoaders ();
134
135         // PENDING, very very slow
136
while (loaders.hasMoreElements ()) {
137             Class JavaDoc<? extends DataObject> clazz = ((DataLoader) (loaders.nextElement ())).getRepresentationClass ();
138
139             // Sometimes people give generic DataObject as representation class.
140
// It is not always avoidable: see e.g. org.netbeans.core.windows.layers.WSLoader.
141
// In this case the overly flexible loader would "poison" sort-by-type, so we
142
// make sure to ignore this.
143
if (clazz == DataObject.class) continue;
144             
145             boolean r1 = clazz.isAssignableFrom (c1);
146             boolean r2 = clazz.isAssignableFrom (c2);
147
148             if (r1 && r2) return compareNames(obj1, obj2);
149             if (r1) return -1;
150             if (r2) return 1;
151         }
152         return compareNames(obj1, obj2);
153     }
154
155     /**
156      * Sort folders alphabetically first. Then files, newest to oldest.
157      */

158     private static int compareLastModified(DataObject obj1, DataObject obj2) {
159         if (obj1 instanceof DataFolder) {
160             if (obj2 instanceof DataFolder) {
161                 return obj1.getName().compareTo(obj2.getName());
162             } else {
163                 return -1;
164             }
165         } else {
166             if (obj2 instanceof DataFolder) {
167                 return 1;
168             } else {
169                 FileObject fo1 = obj1.getPrimaryFile();
170                 FileObject fo2 = obj2.getPrimaryFile();
171                 Date JavaDoc d1 = fo1.lastModified();
172                 Date JavaDoc d2 = fo2.lastModified();
173                 if (d1.after(d2)) {
174                     return -1;
175                 } else if (d2.after(d1)) {
176                     return 1;
177                 } else {
178                     return fo1.getNameExt().compareTo(fo2.getNameExt());
179                 }
180             }
181         }
182     }
183
184     /**
185      * Sort folders alphabetically first. Then files, biggest to smallest.
186      */

187     private static int compareSize(DataObject obj1, DataObject obj2) {
188         if (obj1 instanceof DataFolder) {
189             if (obj2 instanceof DataFolder) {
190                 return obj1.getName().compareTo(obj2.getName());
191             } else {
192                 return -1;
193             }
194         } else {
195             if (obj2 instanceof DataFolder) {
196                 return 1;
197             } else {
198                 FileObject fo1 = obj1.getPrimaryFile();
199                 FileObject fo2 = obj2.getPrimaryFile();
200                 long s1 = fo1.getSize();
201                 long s2 = fo2.getSize();
202                 if (s1 > s2) {
203                     return -1;
204                 } else if (s2 > s1) {
205                     return 1;
206                 } else {
207                     return fo1.getNameExt().compareTo(fo2.getNameExt());
208                 }
209             }
210         }
211     }
212
213 }
214
Popular Tags