001: /*
002: * Copyright (c) 2000 Silvere Martin-Michiellot All Rights Reserved.
003: *
004: * Silvere Martin-Michiellot grants you ("Licensee") a non-exclusive,
005: * royalty free, license to use, modify and redistribute this
006: * software in source and binary code form,
007: * provided that i) this copyright notice and license appear on all copies of
008: * the software; and ii) Licensee does not utilize the software in a manner
009: * which is disparaging to Silvere Martin-Michiellot.
010: *
011: * This software is provided "AS IS," without a warranty of any kind. ALL
012: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
013: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
014: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Silvere Martin-Michiellot
015: * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
016: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
017: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
018: * Silvere Martin-Michiellot OR ITS LICENSORS BE LIABLE
019: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
020: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
021: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
022: * OR INABILITY TO USE SOFTWARE, EVEN IF Silvere Martin-Michiellot HAS BEEN
023: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
024: *
025: * This software is not designed or intended for use in on-line control of
026: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
027: * the design, construction, operation or maintenance of any nuclear
028: * facility. Licensee represents and warrants that it will not use or
029: * redistribute the Software for such purposes.
030: *
031: */
032:
033: // This code is repackaged after the demo from Sun Microsystems (in the Java3D demo packages) /demo/jfc/FileChooserDemo/ExampleFileView.java
034: // Site http://java.sun.com/
035: // Email
036: package com.db.utils;
037:
038: /*
039: * @(#)BasicFileView.java 1.11 01/12/03
040: *
041: * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
042: * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
043: */
044:
045: import javax.swing.*;
046: import javax.swing.filechooser.*;
047:
048: import java.io.File;
049: import java.util.Hashtable;
050:
051: /**
052: * A convenience implementation of the FileView interface that
053: * manages name, icon, traversable, and file type information.
054: *
055: * This this implemention will work well with file systems that use
056: * "dot" extensions to indicate file type. For example: "picture.gif"
057: * as a gif image.
058: *
059: * If the java.io.File ever contains some of this information, such as
060: * file type, icon, and hidden file inforation, this implementation may
061: * become obsolete. At minimum, it should be rewritten at that time to
062: * use any new type information provided by java.io.File
063: *
064: * Example:
065: * JFileChooser chooser = new JFileChooser();
066: * fileView = new BasicFileView();
067: * fileView.putIcon("jpg", new ImageIcon("images/jpgIcon.jpg"));
068: * fileView.putIcon("gif", new ImageIcon("images/gifIcon.gif"));
069: * chooser.setFileView(fileView);
070: *
071: * @version 1.11 12/03/01
072: * @author Jeff Dinkins
073: */
074: public class BasicFileView extends FileView {
075: private Hashtable icons = new Hashtable(5);
076: private Hashtable fileDescriptions = new Hashtable(5);
077: private Hashtable typeDescriptions = new Hashtable(5);
078:
079: /**
080: * The name of the file. Do nothing special here. Let
081: * the system file view handle this.
082: * @see #setName
083: * @see FileView#getName
084: */
085: public String getName(File f) {
086: return null;
087: }
088:
089: /**
090: * Adds a human readable description of the file.
091: */
092: public void putDescription(File f, String fileDescription) {
093: fileDescriptions.put(fileDescription, f);
094: }
095:
096: /**
097: * A human readable description of the file.
098: *
099: * @see FileView#getDescription
100: */
101: public String getDescription(File f) {
102: return (String) fileDescriptions.get(f);
103: };
104:
105: /**
106: * Adds a human readable type description for files. Based on "dot"
107: * extension strings, e.g: ".gif". Case is ignored.
108: */
109: public void putTypeDescription(String extension,
110: String typeDescription) {
111: typeDescriptions.put(typeDescription, extension);
112: }
113:
114: /**
115: * Adds a human readable type description for files of the type of
116: * the passed in file. Based on "dot" extension strings, e.g: ".gif".
117: * Case is ignored.
118: */
119: public void putTypeDescription(File f, String typeDescription) {
120: putTypeDescription(getExtension(f), typeDescription);
121: }
122:
123: /**
124: * A human readable description of the type of the file.
125: *
126: * @see FileView#getTypeDescription
127: */
128: public String getTypeDescription(File f) {
129: return (String) typeDescriptions.get(getExtension(f));
130: }
131:
132: /**
133: * Conveinience method that returnsa the "dot" extension for the
134: * given file.
135: */
136: public String getExtension(File f) {
137: String name = f.getName();
138: if (name != null) {
139: int extensionIndex = name.lastIndexOf('.');
140: if (extensionIndex < 0) {
141: return null;
142: }
143: return name.substring(extensionIndex + 1).toLowerCase();
144: }
145: return null;
146: }
147:
148: /**
149: * Adds an icon based on the file type "dot" extension
150: * string, e.g: ".gif". Case is ignored.
151: */
152: public void putIcon(String extension, Icon icon) {
153: icons.put(extension, icon);
154: }
155:
156: /**
157: * Icon that reperesents this file. Default implementation returns
158: * null. You might want to override this to return something more
159: * interesting.
160: *
161: * @see FileView#getIcon
162: */
163: public Icon getIcon(File f) {
164: Icon icon = null;
165: String extension = getExtension(f);
166: if (extension != null) {
167: icon = (Icon) icons.get(extension);
168: }
169: return icon;
170: }
171:
172: /**
173: * Whether the file is hidden or not. This implementation returns
174: * true if the filename starts with a "."
175: *
176: * @see FileView#isHidden
177: */
178: public Boolean isHidden(File f) {
179: String name = f.getName();
180: if (name != null && !name.equals("") && name.charAt(0) == '.') {
181: return Boolean.TRUE;
182: } else {
183: return Boolean.FALSE;
184: }
185: };
186:
187: /**
188: * Whether the directory is traversable or not. Generic implementation
189: * returns true for all directories and special folders.
190: *
191: * You might want to subtype BasicFileView to do somethimg more interesting,
192: * such as recognize compound documents directories; in such a case you might
193: * return a special icon for the diretory that makes it look like a regular
194: * document, and return false for isTraversable to not allow users to
195: * descend into the directory.
196: *
197: * @see FileView#isTraversable
198: */
199: public Boolean isTraversable(File f) {
200: // if (some_reason) {
201: // return Boolean.FALSE;
202: // }
203: return null; // Use default from FileSystemView
204: };
205:
206: }
|