KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > jwi > jgallery > ImageComparator


1 package de.jwi.jgallery;
2
3 /*
4  * jGallery - Java web application to display image galleries
5  *
6  * Copyright (C) 2004 Juergen Weber
7  *
8  * This file is part of jGallery.
9  *
10  * jGallery is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * jGallery is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with jGallery; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston
23  */

24
25
26 import java.util.Comparator JavaDoc;
27
28
29 /**
30  * @author Jürgen Weber
31  * Source file created on 08.03.2004
32  */

33 public class ImageComparator implements Comparator JavaDoc
34 {
35     public static final int SORTNONE = 0;
36     public static final int SORTBYNAME = 1;
37     public static final int SORTBYFILEDATE = 2;
38     public static final int SORTBYEXIFDATE = 3;
39     
40     private int mode;
41     
42     ImageComparator(int mode) throws GalleryException
43     {
44         this.mode = mode;
45         
46         if ((mode != SORTBYFILEDATE) && (mode != SORTBYNAME) && (mode != SORTBYEXIFDATE))
47         {
48             throw new GalleryException("illegal sorting mode");
49         }
50     }
51     
52     public int compare(Object JavaDoc o1,
53             Object JavaDoc o2)
54     {
55         int rc = 0;
56         
57         Image i1 = (Image)o1;
58         Image i2 = (Image)o2;
59         if (SORTBYFILEDATE == mode)
60         {
61             rc = (int) (i1.getLastModified() - i2.getLastModified()) ;
62         }
63         else if (SORTBYEXIFDATE == mode)
64         {
65             EXIFInfo e1 = i1.getExif();
66             EXIFInfo e2 = i2.getExif();
67             if ((null==e1) || (null==e2))
68             {
69                 rc = 0;
70             }
71             else
72             {
73                 String JavaDoc s1 = e1.getOriginalDate();
74                 String JavaDoc s2 = e2.getOriginalDate();
75                 
76                 if ((null==s1) || (null==s2))
77                 {
78                     rc = 0;
79                 }
80                 else
81                 {
82                     rc = s1.compareTo(s2);
83                 }
84             }
85         }
86         else if (SORTBYNAME == mode)
87         {
88             rc= i1.getName().compareToIgnoreCase(i2.getName());
89         }
90         return rc;
91     }
92 }
93
Popular Tags