KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > slideshow > FileUtils


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.slideshow;
20
21 import java.io.File JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.imageio.ImageIO JavaDoc;
28
29 import org.lucane.applications.slideshow.gui.PreloadDialog;
30 import org.lucane.client.widgets.DialogBox;
31
32 public class FileUtils
33 {
34     public static Object JavaDoc[] preloadImages(SlideShow plugin, List JavaDoc files, PreloadDialog dialog)
35     {
36         Object JavaDoc[] objects = new Object JavaDoc[files.size()];
37         for(int i=0;i<objects.length;i++)
38         {
39             File JavaDoc file = (File JavaDoc)files.get(i);
40             dialog.setValue(i, file.getName());
41             
42             try {
43                 objects[i] = ImageIO.read(file);
44             } catch(Exception JavaDoc e) {
45                 String JavaDoc msg = plugin.tr("err.unableToLoadImage").replaceAll("%1", file.getName());
46                 DialogBox.error(msg + " " + e);
47             }
48         }
49         
50         return objects;
51     }
52     
53     public static List JavaDoc sortFiles(File JavaDoc[] files)
54     {
55         ArrayList JavaDoc list = new ArrayList JavaDoc(files.length);
56         for(int i=0;i<files.length;i++)
57             list.add(files[i]);
58         
59         Collections.sort(list, new Comparator JavaDoc() {
60             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
61                 String JavaDoc s1 = ((File JavaDoc)o1).getName();
62                 String JavaDoc s2 = ((File JavaDoc)o2).getName();
63                 
64                 if(s1.length() < s2.length())
65                     return -1;
66                 else if(s1.length() > s2.length())
67                     return 1;
68                 else
69                     return s1.compareTo(s2);
70             }
71         });
72         
73         return list;
74     }
75 }
Popular Tags