001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.tasklist.impl;
043:
044: import java.util.Comparator;
045: import org.netbeans.spi.tasklist.Task;
046: import org.openide.filesystems.FileObject;
047:
048: /**
049: * @author S. Aubrecht
050: */
051: public class TaskComparator {
052:
053: private static Comparator<Task> DEFAULT_COMPARATOR;
054:
055: /** Creates a new instance of DefaultTaskComparator */
056: private TaskComparator() {
057: }
058:
059: public static Comparator<Task> getDefault() {
060: if (null == DEFAULT_COMPARATOR) {
061: DEFAULT_COMPARATOR = new Comparator<Task>() {
062: public int compare(Task t1, Task t2) {
063: int result = 0;
064: //compare groups
065: result = Accessor.getGroup(t1).compareTo(
066: Accessor.getGroup(t2));
067:
068: //compare file
069: if (0 == result) {
070: FileObject f1 = Accessor.getResource(t1);
071: FileObject f2 = Accessor.getResource(t2);
072: if (null == f1 && null != f2)
073: result = -1;
074: else if (null != f1 && null == f2)
075: result = 1;
076: else if (null != f1 && null != f2)
077: result = f1.getPath().compareTo(
078: f2.getPath());
079: }
080:
081: //compare line number
082: if (0 == result) {
083: if (Accessor.getLine(t1) <= 0
084: && Accessor.getLine(t2) > 0)
085: result = -1;
086: else if (Accessor.getLine(t1) > 0
087: && Accessor.getLine(t2) <= 0)
088: result = 1;
089: else if (Accessor.getLine(t1) > 0
090: && Accessor.getLine(t2) > 0)
091: result = Accessor.getLine(t1)
092: - Accessor.getLine(t2);
093: }
094:
095: //compare description
096: if (0 == result) {
097: result = Accessor.getDescription(t1).compareTo(
098: Accessor.getDescription(t2));
099: }
100: return result;
101: }
102: };
103: }
104: return DEFAULT_COMPARATOR;
105: }
106:
107: public static Comparator<Task> getDescriptionComparator(boolean asc) {
108: return new DescriptionComparator(asc);
109: }
110:
111: public static Comparator<Task> getLocationComparator(boolean asc) {
112: return new LocationComparator(asc);
113: }
114:
115: public static Comparator<Task> getLineComparator(boolean asc) {
116: return new LineComparator(asc);
117: }
118:
119: public static Comparator<Task> getFileComparator(boolean asc) {
120: return new FileComparator(asc);
121: }
122:
123: private static class DescriptionComparator implements
124: Comparator<Task> {
125: private boolean asc;
126:
127: public DescriptionComparator(boolean asc) {
128: this .asc = asc;
129: }
130:
131: public int compare(Task t1, Task t2) {
132: int result = Accessor.getDescription(t1).compareTo(
133: Accessor.getDescription(t2));
134: if (0 == result)
135: result = getDefault().compare(t1, t2);
136: else if (!asc)
137: result *= -1;
138: return result;
139: }
140:
141: public boolean equals(Object o) {
142: if (o == null)
143: return false;
144: if (getClass() != o.getClass())
145: return false;
146: final DescriptionComparator test = (DescriptionComparator) o;
147:
148: if (this .asc != test.asc)
149: return false;
150: return true;
151: }
152:
153: public int hashCode() {
154: int hash = 7;
155: hash = 69 * hash + (this .asc ? 1 : 0);
156: return hash;
157: }
158: }
159:
160: private static class LineComparator implements Comparator<Task> {
161: private boolean asc;
162:
163: public LineComparator(boolean asc) {
164: this .asc = asc;
165: }
166:
167: public int compare(Task t1, Task t2) {
168: int result = 0;
169: if (Accessor.getLine(t1) <= 0 && Accessor.getLine(t2) > 0)
170: result = -1;
171: else if (Accessor.getLine(t1) > 0
172: && Accessor.getLine(t2) <= 0)
173: result = 1;
174: else if (Accessor.getLine(t1) > 0
175: && Accessor.getLine(t2) > 0)
176: result = Accessor.getLine(t1) - Accessor.getLine(t2);
177:
178: if (0 == result)
179: result = getDefault().compare(t1, t2);
180: else if (!asc)
181: result *= -1;
182:
183: return result;
184: }
185:
186: public boolean equals(Object o) {
187: if (o == null)
188: return false;
189: if (getClass() != o.getClass())
190: return false;
191: final LineComparator test = (LineComparator) o;
192:
193: if (this .asc != test.asc)
194: return false;
195: return true;
196: }
197:
198: public int hashCode() {
199: int hash = 7;
200: hash = 79 * hash + (this .asc ? 1 : 0);
201: return hash;
202: }
203: }
204:
205: private static class FileComparator implements Comparator<Task> {
206: private boolean asc;
207:
208: public FileComparator(boolean asc) {
209: this .asc = asc;
210: }
211:
212: public int compare(Task t1, Task t2) {
213: int result = 0;
214:
215: FileObject f1 = Accessor.getResource(t1);
216: FileObject f2 = Accessor.getResource(t2);
217: if (null == f1 && null != f2)
218: result = -1;
219: else if (null != f1 && null == f2)
220: result = 1;
221: else if (null != f1 && null != f2) {
222: result = f1.getNameExt().compareTo(f2.getNameExt());
223: }
224:
225: if (0 == result)
226: result = getDefault().compare(t1, t2);
227: else if (!asc)
228: result *= -1;
229:
230: return result;
231: }
232:
233: public boolean equals(Object o) {
234: if (o == null)
235: return false;
236: if (getClass() != o.getClass())
237: return false;
238: final FileComparator test = (FileComparator) o;
239:
240: if (this .asc != test.asc)
241: return false;
242: return true;
243: }
244:
245: public int hashCode() {
246: int hash = 7;
247: hash = 89 * hash + (this .asc ? 1 : 0);
248: return hash;
249: }
250: }
251:
252: private static class LocationComparator implements Comparator<Task> {
253: private boolean asc;
254:
255: public LocationComparator(boolean asc) {
256: this .asc = asc;
257: }
258:
259: public int compare(Task t1, Task t2) {
260: int result = 0;
261:
262: FileObject f1 = Accessor.getResource(t1);
263: FileObject f2 = Accessor.getResource(t2);
264: if (null == f1 && null != f2)
265: result = -1;
266: else if (null != f1 && null == f2)
267: result = 1;
268: else if (null != f1 && null != f2) {
269: result = f1.getPath().compareTo(f2.getPath());
270: }
271:
272: if (0 == result)
273: result = getDefault().compare(t1, t2);
274: else if (!asc)
275: result *= -1;
276:
277: return result;
278: }
279:
280: public boolean equals(Object o) {
281: if (o == null)
282: return false;
283: if (getClass() != o.getClass())
284: return false;
285: final LocationComparator test = (LocationComparator) o;
286:
287: if (this .asc != test.asc)
288: return false;
289: return true;
290: }
291:
292: public int hashCode() {
293: int hash = 7;
294: hash = 99 * hash + (this .asc ? 1 : 0);
295: return hash;
296: }
297: }
298: }
|