001: /*
002: * Copyright (C) 2004 Giuseppe MANNA
003: *
004: * This file is part of FreeReportBuilder
005: *
006: * FreeReportBuilder is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package it.frb.tree;
023:
024: import java.io.File;
025: import java.util.Hashtable;
026: import javax.swing.Icon;
027:
028: public class CustomFileView extends javax.swing.filechooser.FileView {
029: private Hashtable icons = new Hashtable(10);
030: private Hashtable fileDescriptions = new Hashtable(10);
031: private Hashtable typeDescriptions = new Hashtable(10);
032: private java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit();
033:
034: public String getName(File f) {
035: return null;
036: }
037:
038: public void putDescription(File f, String fileDescription) {
039: fileDescriptions.put(fileDescription, f);
040: }
041:
042: public String getDescription(File f) {
043: return (String) fileDescriptions.get(f);
044: };
045:
046: public void putTypeDescription(String extension,
047: String typeDescription) {
048: typeDescriptions.put(typeDescription, extension);
049: }
050:
051: public void putTypeDescription(File f, String typeDescription) {
052: putTypeDescription(getExtension(f), typeDescription);
053: }
054:
055: public String getTypeDescription(File f) {
056: return (String) typeDescriptions.get(getExtension(f));
057: }
058:
059: public String getExtension(File f) {
060: String name = f.getName();
061: if (name != null) {
062: int extensionIndex = name.lastIndexOf('.');
063: if (extensionIndex < 0) {
064: return null;
065: }
066: return name.substring(extensionIndex + 1).toLowerCase();
067: }
068: return null;
069: }
070:
071: public void putIcon(String extension, Icon icon) {
072: icons.put(extension, icon);
073: }
074:
075: public Icon getIcon(File f) {
076: Icon icon = null;
077: String extension = getExtension(f);
078: if (extension != null) {
079: icon = (Icon) icons.get(extension);
080: if (icon == null) {
081: if (extension.equals("png") || extension.equals("gif")) {
082: icon = (Icon) new javax.swing.ImageIcon(f.getPath());
083: } else if (extension.equals("jpg")
084: || extension.equals("jpeg")
085: || extension.equals("tif")) {
086: java.awt.Image img = tk.getImage(f.getPath());
087: //img = img.getScaledInstance(50,40,img.SCALE_FAST);
088: img = img.getScaledInstance(30, 20, img.SCALE_FAST);
089: icon = (Icon) new javax.swing.ImageIcon(img);
090: }
091: }
092: }
093: return icon;
094: }
095:
096: public Boolean isHidden(File f) {
097: String name = f.getName();
098: if (name != null && !name.equals("") && name.charAt(0) == '.') {
099: return Boolean.TRUE;
100: } else {
101: return Boolean.FALSE;
102: }
103: };
104:
105: public Boolean isTraversable(File f) {
106: if (f.isDirectory()) {
107: return Boolean.TRUE;
108: } else {
109: return Boolean.FALSE;
110: }
111: };
112:
113: }
|