KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JFileChooser.java
3  *
4  * Created on 26. Juli 2005, 00:14
5  */

6 /*
7  * Copyright 2005-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.awt.*;
27 import javax.swing.*;
28
29 /**
30  * This class extends {@link javax.swing.JFileChooser} in order to allow
31  * browsing of ZIP compatible files in a JFileChooser.
32  *
33  * @author Christian Schlichtherle
34  * @version @version@
35  */

36 public class JFileChooser extends javax.swing.JFileChooser JavaDoc {
37
38     private PaintingLock paintingLock = getPaintingLock();
39     private volatile short paintingDisabled;
40
41     public JFileChooser() {
42         super(FileSystemView.getFileSystemView());
43         super.setFileView(new FileView(super.getFileView()));
44         super.setDoubleBuffered(false);
45     }
46
47     public JFileChooser(ArchiveDetector archiveDetector) {
48         super(FileSystemView.getFileSystemView(archiveDetector));
49         super.setFileView(new FileView(super.getFileView()));
50         super.setDoubleBuffered(false);
51     }
52
53     /**
54      * Returns a {@link de.schlichtherle.io.File de.schlichtherle.io.File}
55      * instead of {@link java.io.File java.io.File}.
56      *
57      * @see javax.swing.JFileChooser#getSelectedFile()
58      */

59     public java.io.File JavaDoc getSelectedFile() {
60         java.io.File JavaDoc file = super.getSelectedFile();
61         return ((FileSystemView) getFileSystemView()).wrap(file);
62     }
63
64     /**
65      * Returns an array of
66      * {@link de.schlichtherle.io.File de.schlichtherle.io.File}
67      * objects instead of {@link java.io.File java.io.File} objects.
68      *
69      * @see javax.swing.JFileChooser#getSelectedFiles()
70      */

71     public java.io.File JavaDoc[] getSelectedFiles() {
72         java.io.File JavaDoc files[] = super.getSelectedFiles();
73         if (files != null) {
74             FileSystemView fsv = (FileSystemView) getFileSystemView();
75             for (int i = files.length; --i >= 0; ) {
76                 files[i] = fsv.wrap(files[i]);
77                 //files[i] = files[i] != null ? new File(files[i]) : null;
78
}
79         }
80         return files;
81     }
82
83     public Icon getIcon(final java.io.File JavaDoc file) {
84         beginPaintingDisabled();
85         try {
86             return super.getIcon(file);
87         } finally {
88             endPaintingDisabled();
89         }
90     }
91
92     public String JavaDoc getTypeDescription(final java.io.File JavaDoc file) {
93         beginPaintingDisabled();
94         try {
95             return super.getTypeDescription(file);
96         } finally {
97             endPaintingDisabled();
98         }
99     }
100
101     public boolean isTraversable(final java.io.File JavaDoc file) {
102         beginPaintingDisabled();
103         try {
104             return super.isTraversable(file);
105         } finally {
106             endPaintingDisabled();
107         }
108     }
109
110     public void paint(final Graphics g) {
111         if (paintingDisabled > 0) {
112             /*EventQueue.invokeLater(new Runnable() {
113                 public void run() {
114                     JFileChooser.super.paint(g);
115                 }
116             });*/

117         } else {
118             beginPaintingDisabled();
119             try {
120                 super.paintChildren(g);
121             } finally {
122                 endPaintingDisabled();
123             }
124         }
125     }
126
127     private void beginPaintingDisabled() {
128         synchronized (getPaintingLock()) {
129             paintingDisabled++;
130         }
131     }
132
133     private void endPaintingDisabled() {
134         synchronized (getPaintingLock()) {
135             paintingDisabled--;
136             if (paintingDisabled <= 0)
137                 repaint();
138         }
139     }
140
141     /**
142      * Returns the painting lock.
143      * This method is indirectly called from the super class constructor!
144      */

145     private final PaintingLock getPaintingLock() {
146         if (paintingLock == null)
147             paintingLock = new PaintingLock();
148         return paintingLock;
149     }
150
151     private static class PaintingLock {
152     }
153 }
154
Popular Tags