Path: blob/master/src/demo/share/jfc/FileChooserDemo/ExampleFileView.java
41149 views
/*1* Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/38394041import javax.swing.*;42import javax.swing.filechooser.*;43import java.io.File;44import java.util.HashMap;45import java.util.Map;464748/**49* A convenience implementation of the FileView interface that50* manages name, icon, traversable, and file type information.51*52* This implementation will work well with file systems that use53* "dot" extensions to indicate file type. For example: "picture.gif"54* as a gif image.55*56* If the java.io.File ever contains some of this information, such as57* file type, icon, and hidden file inforation, this implementation may58* become obsolete. At minimum, it should be rewritten at that time to59* use any new type information provided by java.io.File60*61* Example:62* JFileChooser chooser = new JFileChooser();63* fileView = new ExampleFileView();64* fileView.putIcon("jpg", new ImageIcon("images/jpgIcon.jpg"));65* fileView.putIcon("gif", new ImageIcon("images/gifIcon.gif"));66* chooser.setFileView(fileView);67*68* @author Jeff Dinkins69*/70public class ExampleFileView extends FileView {7172private final Map<String, Icon> icons = new HashMap<String, Icon>();73private final Map<File, String> fileDescriptions =74new HashMap<File, String>();75private final Map<String, String> typeDescriptions =76new HashMap<String, String>();7778/**79* The name of the file. Do nothing special here. Let80* the system file view handle this.81* @see FileView#getName82*/83@Override84public String getName(File f) {85return null;86}8788/**89* Adds a human readable description of the file.90*/91public void putDescription(File f, String fileDescription) {92fileDescriptions.put(f, fileDescription);93}9495/**96* A human readable description of the file.97*98* @see FileView#getDescription99*/100@Override101public String getDescription(File f) {102return fileDescriptions.get(f);103}104105/**106* Adds a human readable type description for files. Based on "dot"107* extension strings, e.g: ".gif". Case is ignored.108*/109public void putTypeDescription(String extension, String typeDescription) {110typeDescriptions.put(extension, typeDescription);111}112113/**114* Adds a human readable type description for files of the type of115* the passed in file. Based on "dot" extension strings, e.g: ".gif".116* Case is ignored.117*/118public void putTypeDescription(File f, String typeDescription) {119putTypeDescription(getExtension(f), typeDescription);120}121122/**123* A human readable description of the type of the file.124*125* @see FileView#getTypeDescription126*/127@Override128public String getTypeDescription(File f) {129return typeDescriptions.get(getExtension(f));130}131132/**133* Convenience method that returns the "dot" extension for the134* given file.135*/136private String getExtension(File f) {137String name = f.getName();138if (name != null) {139int extensionIndex = name.lastIndexOf('.');140if (extensionIndex < 0) {141return null;142}143return name.substring(extensionIndex + 1).toLowerCase();144}145return null;146}147148/**149* Adds an icon based on the file type "dot" extension150* string, e.g: ".gif". Case is ignored.151*/152public void putIcon(String extension, Icon icon) {153icons.put(extension, icon);154}155156/**157* Icon that reperesents this file. Default implementation returns158* null. You might want to override this to return something more159* interesting.160*161* @see FileView#getIcon162*/163@Override164public Icon getIcon(File f) {165Icon icon = null;166String extension = getExtension(f);167if (extension != null) {168icon = icons.get(extension);169}170return icon;171}172173/**174* Whether the directory is traversable or not. Generic implementation175* returns true for all directories and special folders.176*177* You might want to subtype ExampleFileView to do somethimg more interesting,178* such as recognize compound documents directories; in such a case you might179* return a special icon for the directory that makes it look like a regular180* document, and return false for isTraversable to not allow users to181* descend into the directory.182*183* @see FileView#isTraversable184*/185@Override186public Boolean isTraversable(File f) {187// if (some_reason) {188// return Boolean.FALSE;189// }190return null; // Use default from FileSystemView191}192}193194195