KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > tree > comparator > FolderComparator


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.mail.gui.tree.comparator;
17
18 import java.util.Comparator JavaDoc;
19
20 import org.columba.mail.folder.IMailFolder;
21 import org.columba.mail.folder.IMailbox;
22 import org.columba.mail.folder.virtual.VirtualFolder;
23
24 /**
25  * A comparator that can be used to sort Folders. Other folder comparators
26  * should extend this class and only implement the sorting in the
27  * <code>compareFolders()</code> method. This comparator will always put the
28  * Inbox folders at the top of the tree.
29  * <p>
30  * The folders are by default sorted by their name. Note that the Inbox folder
31  * will always be put at the top.
32  *
33  * @author redsolo
34  */

35 public class FolderComparator implements Comparator JavaDoc {
36
37     private boolean isAscending = true;
38
39     /**
40      * @param ascending
41      * if the sorting is ascending or not.
42      */

43     public FolderComparator(boolean ascending) {
44         isAscending = ascending;
45     }
46
47     /** {@inheritDoc} */
48     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
49         int compValue;
50
51         if ((o1 instanceof IMailFolder) && (o2 instanceof IMailFolder)) {
52             // If it isnt a message folder, then it must be a root, and those
53
// should not be sorted.
54
if (!(o1 instanceof IMailbox)) {
55                 compValue = 0;
56             } else if (o1 instanceof VirtualFolder) {
57                 compValue = 1;
58             } else {
59                 IMailbox folder1 = (IMailbox) o1;
60                 IMailbox folder2 = (IMailbox) o2;
61
62                 boolean folder1IsInbox = folder1.isInboxFolder();
63                 boolean folder2IsInbox = folder2.isInboxFolder();
64
65                 if (folder1IsInbox) {
66                     compValue = -1;
67                 } else if (folder2IsInbox) {
68                     compValue = 1;
69                 } else if (folder2IsInbox && folder1IsInbox) {
70                     compValue = 0;
71                 } else {
72                     compValue = compareFolders(folder1, folder2);
73                     if (!isAscending) {
74                         compValue *= -1;
75                     }
76                 }
77             }
78         } else {
79             compValue = o1.toString().toLowerCase().compareTo(
80                     o2.toString().toLowerCase());
81             if (!isAscending) {
82                 compValue *= -1;
83             }
84         }
85         return compValue;
86     }
87
88     /**
89      * Compares the folders. Returns a negative integer, zero, or a positive
90      * integer as the first argument is less than, equal to, or greater than the
91      * second.
92      *
93      * @param folder1
94      * the first folder to be compared.
95      * @param folder2
96      * the second folder to be compared.
97      * @return a negative integer, zero, or a positive integer as the first
98      * argument is less than, equal to, or greater than the second.
99      */

100     protected int compareFolders(IMailbox folder1, IMailbox folder2) {
101         return folder1.getName().toLowerCase().compareTo(
102                 folder2.getName().toLowerCase());
103     }
104
105     /**
106      * @return Returns if the comparator should sort ascending or not.
107      */

108     public boolean isAscending() {
109         return isAscending;
110     }
111
112     /**
113      * @param ascending
114      * if the comparator should sorted ascending or not.
115      */

116     public void setAscending(boolean ascending) {
117         isAscending = ascending;
118     }
119 }
Popular Tags