Path: blob/master/src/demo/share/jfc/SwingSet2/ExampleFileView.java
41149 views
/*1*2* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132import javax.swing.*;33import javax.swing.filechooser.*;3435import java.io.File;36import java.util.Hashtable;3738/**39* A convenience implementation of the FileView interface that40* manages name, icon, traversable, and file type information.41*42* This this implemention will work well with file systems that use43* "dot" extensions to indicate file type. For example: "picture.gif"44* as a gif image.45*46* If the java.io.File ever contains some of this information, such as47* file type, icon, and hidden file inforation, this implementation may48* become obsolete. At minimum, it should be rewritten at that time to49* use any new type information provided by java.io.File50*51* Example:52* JFileChooser chooser = new JFileChooser();53* fileView = new ExampleFileView();54* fileView.putIcon("jpg", new ImageIcon("images/jpgIcon.jpg"));55* fileView.putIcon("gif", new ImageIcon("images/gifIcon.gif"));56* chooser.setFileView(fileView);57*58* @author Jeff Dinkins59*/60public class ExampleFileView extends FileView {61private Hashtable<String, Icon> icons = new Hashtable<>(5);62private Hashtable<File, String> fileDescriptions = new Hashtable<>(5);63private Hashtable<String, String> typeDescriptions = new Hashtable<>(5);6465/**66* The name of the file. Do nothing special here. Let67* the system file view handle this.68* @see FileView#getName69*/70public String getName(File f) {71return null;72}7374/**75* Adds a human readable description of the file.76*/77public void putDescription(File f, String fileDescription) {78fileDescriptions.put(f, fileDescription);79}8081/**82* A human readable description of the file.83*84* @see FileView#getDescription85*/86public String getDescription(File f) {87return fileDescriptions.get(f);88};8990/**91* Adds a human readable type description for files. Based on "dot"92* extension strings, e.g: ".gif". Case is ignored.93*/94public void putTypeDescription(String extension, String typeDescription) {95typeDescriptions.put(extension, typeDescription);96}9798/**99* Adds a human readable type description for files of the type of100* the passed in file. Based on "dot" extension strings, e.g: ".gif".101* Case is ignored.102*/103public void putTypeDescription(File f, String typeDescription) {104putTypeDescription(getExtension(f), typeDescription);105}106107/**108* A human readable description of the type of the file.109*110* @see FileView#getTypeDescription111*/112public String getTypeDescription(File f) {113return typeDescriptions.get(getExtension(f));114}115116/**117* Convenience method that returns the "dot" extension for the118* given file.119*/120public String getExtension(File f) {121String name = f.getName();122if(name != null) {123int extensionIndex = name.lastIndexOf('.');124if(extensionIndex < 0) {125return null;126}127return name.substring(extensionIndex+1).toLowerCase();128}129return null;130}131132/**133* Adds an icon based on the file type "dot" extension134* string, e.g: ".gif". Case is ignored.135*/136public void putIcon(String extension, Icon icon) {137icons.put(extension, icon);138}139140/**141* Icon that reperesents this file. Default implementation returns142* null. You might want to override this to return something more143* interesting.144*145* @see FileView#getIcon146*/147public Icon getIcon(File f) {148Icon icon = null;149String extension = getExtension(f);150if(extension != null) {151icon = icons.get(extension);152}153return icon;154}155156/**157* Whether the directory is traversable or not. Generic implementation158* returns true for all directories and special folders.159*160* You might want to subtype ExampleFileView to do somethimg more interesting,161* such as recognize compound documents directories; in such a case you might162* return a special icon for the directory that makes it look like a regular163* document, and return false for isTraversable to not allow users to164* descend into the directory.165*166* @see FileView#isTraversable167*/168public Boolean isTraversable(File f) {169// if (some_reason) {170// return Boolean.FALSE;171// }172return null; // Use default from FileSystemView173};174175}176177178