KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > io > swing > FileView


1 /*
2  * FileView.java
3  *
4  * Created on 7. Dezember 2006, 15:08
5  */

6 /*
7  * Copyright 2006 Schlichtherle IT Services
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package de.schlichtherle.io.swing;
23
24 import de.schlichtherle.io.*;
25
26 import java.util.*;
27
28 import javax.swing.*;
29 import javax.swing.filechooser.*;
30
31 /**
32  * An archive enabled file view.
33  * This class recognizes instances of the {@link File} class and returns
34  * custom icons and type descriptions if it's an archive file or an archive
35  * entry.
36  * Otherwise, the super class behaviour is used.
37  * <p>
38  * Note that this class accesses archive files lazily, i.e. it does not
39  * eagerly check the true status with {@link File#isDirectory} or similar
40  * unless really necessary. This is to prevent dead locks between the Event
41  * Dispatch Thread and the Basic L&F File Loading Threads which are forked
42  * by JFileChooser.
43  *
44  * @author Christian Schlichtherle
45  */

46 class FileView extends AbstractFileView {
47
48     private static final String JavaDoc CLASS_NAME
49             = "de/schlichtherle/io/swing/FileView".replace('/', '.'); // support code obfuscation!
50
private static final ResourceBundle resources
51             = ResourceBundle.getBundle(CLASS_NAME);
52
53     /**
54      * Creates a new archive enabled file view.
55      *
56      * @param delegate The file view to be decorated - may be <code>null</code>.
57      */

58     public FileView(javax.swing.filechooser.FileView JavaDoc delegate) {
59         super(delegate);
60     }
61
62     public Icon getIcon(java.io.File JavaDoc file) {
63         Icon icon = closedIcon(file);
64         return icon != null
65                 ? icon
66                 : super.getIcon(file);
67     }
68
69     public String JavaDoc getTypeDescription(java.io.File JavaDoc file) {
70         String JavaDoc typeDescription = typeDescription(file);
71         return typeDescription != null
72                 ? typeDescription
73                 : super.getTypeDescription(file);
74     }
75
76     public Boolean JavaDoc isTraversable(java.io.File JavaDoc file) {
77         Boolean JavaDoc traversable = traversable(file);
78         return traversable != null
79                 ? traversable
80                 : super.isTraversable(file);
81     }
82
83     static Icon openIcon(java.io.File JavaDoc file) {
84         if (!(file instanceof File))
85             return null;
86
87         return icon((File) file);
88     }
89
90     static Icon closedIcon(java.io.File JavaDoc file) {
91         if (!(file instanceof File))
92             return null;
93
94         return icon((File) file);
95     }
96
97     private static Icon icon(File file) {
98         if (isValidArchive(file)) {
99                 return UIManager.getIcon("FileView.directoryIcon");
100         } else if (isEntryInValidArchive(file)) {
101             if (file.isDirectory())
102                 return UIManager.getIcon("FileView.directoryIcon");
103             else
104                 return UIManager.getIcon("FileView.fileIcon");
105         }
106         return null;
107     }
108
109     static String JavaDoc typeDescription(java.io.File JavaDoc file) {
110         if (!(file instanceof File))
111             return null;
112
113         File smartFile = (File) file;
114         if (isValidArchive(smartFile)) {
115             return resources.getString("archiveFile");
116         } else if (isEntryInValidArchive(smartFile)) {
117             if (smartFile.isDirectory())
118                 return resources.getString("archiveDirectoryEntry");
119             else
120                 return resources.getString("archiveFileEntry");
121         }
122         return null;
123     }
124
125     private static boolean isValidArchive(File file) {
126         return file.isArchive() && file.isDirectory()
127                 && !createNonArchiveFile(file).isDirectory();
128     }
129
130     private static File createNonArchiveFile(File file) {
131         return ArchiveDetector.NULL.createFile(
132                 file .getParentFile(), file.getName());
133     }
134
135     private static boolean isEntryInValidArchive(File file) {
136         // An archive entry always names a parent. This parent must not be
137
// a regular directory.
138
if (!file.isEntry())
139             return false;
140         java.io.File JavaDoc parent = file.getParentFile();
141         assert parent != null : "An archive entry must always name a parent!";
142         return parent.isDirectory()
143                 && !ArchiveDetector.NULL.createFile(parent.getPath())
144                     .isDirectory();
145     }
146
147     static Boolean JavaDoc traversable(java.io.File JavaDoc file) {
148         if (!(file instanceof File))
149             return null;
150
151         File smartFile = (File) file;
152         return smartFile.isDirectory() ? Boolean.TRUE : Boolean.FALSE;
153     }
154 }
155
Popular Tags