KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > util > FileNameComparator


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.util;
24
25 import java.io.*;
26 import java.util.*;
27
28 /**
29  * Sorts files by name (directories before files).
30  *
31  * @author Luciano Vernaschi
32  */

33 public final class FileNameComparator implements Comparator, Serializable {
34   private boolean caseSensitive = true;
35
36   public int compare(Object JavaDoc o1, Object JavaDoc o2) {
37     try {
38       File f1 = (File) o1;
39       File f2 = (File) o2;
40
41       if (f1.isDirectory() && !f2.isDirectory()) {
42         return -1;
43       } else if (!f1.isDirectory() && f2.isDirectory()) {
44         return 1;
45       } else if (caseSensitive) {
46         return f1.getName().compareTo(f2.getName());
47       } else {
48         return f1.getName().toLowerCase().compareTo(f2.getName().toLowerCase());
49       }
50     } catch (ClassCastException JavaDoc ex) {}
51
52     return 0;
53   }
54
55   public boolean isCaseSensitive() {
56     return caseSensitive;
57   }
58
59   public void setCaseSensitive(boolean caseSensitive) {
60     this.caseSensitive = caseSensitive;
61   }
62 }
63
Popular Tags