KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > applet > upload > ImageFileView


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-components/org/opencms/applet/upload/ImageFileView.java,v $
3  * Date : $Date: 2006/10/17 13:33:11 $
4  * Version: $Revision: 1.10 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.applet.upload;
33
34 import java.io.File JavaDoc;
35 import java.net.URL JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.StringTokenizer JavaDoc;
38
39 import javax.swing.Icon JavaDoc;
40 import javax.swing.ImageIcon JavaDoc;
41 import javax.swing.filechooser.FileView JavaDoc;
42
43 /**
44  * Image File View class, plugs image preview into the file selector box.<p>
45  *
46  * Based on the Java 1.4 example.<p>
47  *
48  * @author Michael Emmerich
49  *
50  * @version $Revision: 1.10 $
51  *
52  * @since 6.0.0
53  */

54 public class ImageFileView extends FileView JavaDoc {
55
56     /** Extension storage. */
57     private HashMap JavaDoc m_extensions;
58
59     /** The path to OpenCms, required to read the images. */
60     private String JavaDoc m_opencms;
61
62     /**
63      * Creates a new ImageFile Vew object.<p>
64      */

65     public ImageFileView() {
66
67         super();
68     }
69
70     /**
71      * Creates a new ImageFile Vew object.<p>
72      *
73      * @param opencms the complete path to opencms
74      * @param fileExtensions list of file extensions to select the correct icons
75      */

76     public ImageFileView(String JavaDoc opencms, String JavaDoc fileExtensions) {
77
78         super();
79
80         m_opencms = opencms;
81         m_extensions = extractExtensions(fileExtensions);
82
83     }
84
85     /**
86      * @see javax.swing.filechooser.FileView#getDescription(java.io.File)
87      */

88     public String JavaDoc getDescription(File JavaDoc f) {
89
90         return null;
91     }
92
93     /**
94      * @see javax.swing.filechooser.FileView#getIcon(java.io.File)
95      */

96     public Icon JavaDoc getIcon(File JavaDoc f) {
97
98         String JavaDoc extension = FileUploadUtils.getExtension(f);
99         // set the default icon
100
Icon JavaDoc icon = null;
101
102         if (f.isDirectory()) {
103
104             icon = (Icon JavaDoc)m_extensions.get("FOLDER");
105         } else {
106
107             icon = (Icon JavaDoc)m_extensions.get(extension);
108         }
109         // if no icon was found, set it to the default
110
if (icon == null) {
111
112             icon = (Icon JavaDoc)m_extensions.get("txt");
113         }
114
115         return icon;
116     }
117
118     /**
119      * @see javax.swing.filechooser.FileView#getName(java.io.File)
120      */

121     public String JavaDoc getName(File JavaDoc f) {
122
123         return null;
124     }
125
126     /**
127      * @see javax.swing.filechooser.FileView#isTraversable(java.io.File)
128      */

129     public Boolean JavaDoc isTraversable(File JavaDoc f) {
130
131         return null;
132     }
133
134     /**
135      * Returns a resource image icon for a given path. <p>
136      *
137      * The resource icon is chossen by the resource path, i.g. the resource type.
138      *
139      * @param path the path of the resource
140      * @return ImageIcon, or null if the path was invalid
141      */

142     private ImageIcon JavaDoc createImageIcon(String JavaDoc path) {
143
144         try {
145             URL JavaDoc imgURL = new URL JavaDoc(path);
146             ImageIcon JavaDoc img = new ImageIcon JavaDoc(imgURL);
147             return img;
148         } catch (Exception JavaDoc e) {
149             System.err.println(e);
150             return null;
151         }
152     }
153
154     /**
155      * Extracts the file extensions from the parameter String.<p>
156      *
157      * @param fileExtensions list of file extensions and their file type
158      * @return HashMap with file extension
159      */

160     private HashMap JavaDoc extractExtensions(String JavaDoc fileExtensions) {
161
162         HashMap JavaDoc extensions = new HashMap JavaDoc();
163         // add a dummy extention for the folder
164
fileExtensions += ",FOLDER=folder,";
165         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(fileExtensions, ",");
166         // loop through the tokens
167
// all tokens have the format "extension=type"
168
while (tok.hasMoreElements()) {
169             String JavaDoc token = tok.nextToken();
170             // now extract the file extension and the type
171
String JavaDoc extension = token.substring(0, token.indexOf("="));
172             String JavaDoc type = token.substring(token.indexOf("=") + 1);
173             // try to load the image
174
ImageIcon JavaDoc icon = createImageIcon(m_opencms + type + ".gif");
175             extensions.put(extension, icon);
176
177         }
178         return extensions;
179     }
180 }
Popular Tags