001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.mail.gui.tree.comparator;
017:
018: import java.util.Comparator;
019:
020: import org.columba.mail.folder.IMailFolder;
021: import org.columba.mail.folder.IMailbox;
022: import org.columba.mail.folder.virtual.VirtualFolder;
023:
024: /**
025: * A comparator that can be used to sort Folders. Other folder comparators
026: * should extend this class and only implement the sorting in the
027: * <code>compareFolders()</code> method. This comparator will always put the
028: * Inbox folders at the top of the tree.
029: * <p>
030: * The folders are by default sorted by their name. Note that the Inbox folder
031: * will always be put at the top.
032: *
033: * @author redsolo
034: */
035: public class FolderComparator implements Comparator {
036:
037: private boolean isAscending = true;
038:
039: /**
040: * @param ascending
041: * if the sorting is ascending or not.
042: */
043: public FolderComparator(boolean ascending) {
044: isAscending = ascending;
045: }
046:
047: /** {@inheritDoc} */
048: public int compare(Object o1, Object o2) {
049: int compValue;
050:
051: if ((o1 instanceof IMailFolder) && (o2 instanceof IMailFolder)) {
052: // If it isnt a message folder, then it must be a root, and those
053: // should not be sorted.
054: if (!(o1 instanceof IMailbox)) {
055: compValue = 0;
056: } else if (o1 instanceof VirtualFolder) {
057: compValue = 1;
058: } else {
059: IMailbox folder1 = (IMailbox) o1;
060: IMailbox folder2 = (IMailbox) o2;
061:
062: boolean folder1IsInbox = folder1.isInboxFolder();
063: boolean folder2IsInbox = folder2.isInboxFolder();
064:
065: if (folder1IsInbox) {
066: compValue = -1;
067: } else if (folder2IsInbox) {
068: compValue = 1;
069: } else if (folder2IsInbox && folder1IsInbox) {
070: compValue = 0;
071: } else {
072: compValue = compareFolders(folder1, folder2);
073: if (!isAscending) {
074: compValue *= -1;
075: }
076: }
077: }
078: } else {
079: compValue = o1.toString().toLowerCase().compareTo(
080: o2.toString().toLowerCase());
081: if (!isAscending) {
082: compValue *= -1;
083: }
084: }
085: return compValue;
086: }
087:
088: /**
089: * Compares the folders. Returns a negative integer, zero, or a positive
090: * integer as the first argument is less than, equal to, or greater than the
091: * second.
092: *
093: * @param folder1
094: * the first folder to be compared.
095: * @param folder2
096: * the second folder to be compared.
097: * @return a negative integer, zero, or a positive integer as the first
098: * argument is less than, equal to, or greater than the second.
099: */
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: }
|