author | alexp |
Wed, 02 Sep 2009 17:47:19 +0400 | |
changeset 3749 | d0ecbd7075b1 |
parent 3346 | 1c65be97eaa2 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
3346
1c65be97eaa2
6460525: javax/swing/JFileChooser/6396844/TwentyThousandTest.java times out
rupashka
parents:
2658
diff
changeset
|
2 |
* Copyright 2003-2009 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
package sun.swing; |
|
26 |
||
27 |
import java.awt.*; |
|
28 |
import java.awt.event.*; |
|
29 |
import java.beans.PropertyChangeEvent; |
|
30 |
import java.beans.PropertyChangeListener; |
|
31 |
import java.io.*; |
|
32 |
import java.text.DateFormat; |
|
33 |
import java.text.MessageFormat; |
|
34 |
import java.util.*; |
|
35 |
import java.util.List; |
|
2489
5052722686e2
6491795: COM should be initialized for Shell API calls in ShellFolder2.cpp
rupashka
parents:
1842
diff
changeset
|
36 |
import java.util.concurrent.Callable; |
2 | 37 |
|
38 |
import javax.swing.*; |
|
39 |
import javax.swing.border.*; |
|
40 |
import javax.swing.event.*; |
|
41 |
import javax.swing.filechooser.*; |
|
42 |
import javax.swing.plaf.basic.*; |
|
43 |
import javax.swing.table.*; |
|
44 |
import javax.swing.text.*; |
|
45 |
||
46 |
import sun.awt.shell.*; |
|
47 |
||
48 |
/** |
|
49 |
* <b>WARNING:</b> This class is an implementation detail and is only |
|
50 |
* public so that it can be used by two packages. You should NOT consider |
|
51 |
* this public API. |
|
52 |
* <p> |
|
53 |
* This component is intended to be used in a subclass of |
|
54 |
* javax.swing.plaf.basic.BasicFileChooserUI. It realies heavily on the |
|
55 |
* implementation of BasicFileChooserUI, and is intended to be API compatible |
|
56 |
* with earlier implementations of MetalFileChooserUI and WindowsFileChooserUI. |
|
57 |
* |
|
58 |
* @author Leif Samuelsson |
|
59 |
*/ |
|
60 |
public class FilePane extends JPanel implements PropertyChangeListener { |
|
61 |
// Constants for actions. These are used for the actions' ACTION_COMMAND_KEY |
|
62 |
// and as keys in the action maps for FilePane and the corresponding UI classes |
|
63 |
||
64 |
public final static String ACTION_APPROVE_SELECTION = "approveSelection"; |
|
65 |
public final static String ACTION_CANCEL = "cancelSelection"; |
|
66 |
public final static String ACTION_EDIT_FILE_NAME = "editFileName"; |
|
67 |
public final static String ACTION_REFRESH = "refresh"; |
|
68 |
public final static String ACTION_CHANGE_TO_PARENT_DIRECTORY = "Go Up"; |
|
69 |
public final static String ACTION_NEW_FOLDER = "New Folder"; |
|
70 |
public final static String ACTION_VIEW_LIST = "viewTypeList"; |
|
71 |
public final static String ACTION_VIEW_DETAILS = "viewTypeDetails"; |
|
72 |
||
73 |
private Action[] actions; |
|
74 |
||
75 |
// "enums" for setViewType() |
|
76 |
public static final int VIEWTYPE_LIST = 0; |
|
77 |
public static final int VIEWTYPE_DETAILS = 1; |
|
78 |
private static final int VIEWTYPE_COUNT = 2; |
|
79 |
||
80 |
private int viewType = -1; |
|
81 |
private JPanel[] viewPanels = new JPanel[VIEWTYPE_COUNT]; |
|
82 |
private JPanel currentViewPanel; |
|
83 |
private String[] viewTypeActionNames; |
|
84 |
||
85 |
private JPopupMenu contextMenu; |
|
86 |
private JMenu viewMenu; |
|
87 |
||
88 |
private String viewMenuLabelText; |
|
89 |
private String refreshActionLabelText; |
|
90 |
private String newFolderActionLabelText; |
|
91 |
||
92 |
private String kiloByteString; |
|
93 |
private String megaByteString; |
|
94 |
private String gigaByteString; |
|
95 |
||
96 |
private String renameErrorTitleText; |
|
97 |
private String renameErrorText; |
|
98 |
private String renameErrorFileExistsText; |
|
99 |
||
100 |
private static final Cursor waitCursor = |
|
101 |
Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR); |
|
102 |
||
103 |
private final KeyListener detailsKeyListener = new KeyAdapter() { |
|
104 |
private final long timeFactor; |
|
105 |
||
106 |
private final StringBuilder typedString = new StringBuilder(); |
|
107 |
||
108 |
private long lastTime = 1000L; |
|
109 |
||
110 |
{ |
|
111 |
Long l = (Long) UIManager.get("Table.timeFactor"); |
|
112 |
timeFactor = (l != null) ? l : 1000L; |
|
113 |
} |
|
114 |
||
115 |
/** |
|
116 |
* Moves the keyboard focus to the first element whose prefix matches |
|
117 |
* the sequence of alphanumeric keys pressed by the user with delay |
|
118 |
* less than value of <code>timeFactor</code>. Subsequent same key |
|
119 |
* presses move the keyboard focus to the next object that starts with |
|
120 |
* the same letter until another key is pressed, then it is treated |
|
121 |
* as the prefix with appropriate number of the same letters followed |
|
122 |
* by first typed another letter. |
|
123 |
*/ |
|
124 |
public void keyTyped(KeyEvent e) { |
|
125 |
BasicDirectoryModel model = getModel(); |
|
126 |
int rowCount = model.getSize(); |
|
127 |
||
128 |
if (detailsTable == null || rowCount == 0 || |
|
129 |
e.isAltDown() || e.isControlDown() || e.isMetaDown()) { |
|
130 |
return; |
|
131 |
} |
|
132 |
||
133 |
InputMap inputMap = detailsTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); |
|
134 |
KeyStroke key = KeyStroke.getKeyStrokeForEvent(e); |
|
135 |
||
136 |
if (inputMap != null && inputMap.get(key) != null) { |
|
137 |
return; |
|
138 |
} |
|
139 |
||
140 |
int startIndex = detailsTable.getSelectionModel().getLeadSelectionIndex(); |
|
141 |
||
142 |
if (startIndex < 0) { |
|
143 |
startIndex = 0; |
|
144 |
} |
|
145 |
||
146 |
if (startIndex >= rowCount) { |
|
147 |
startIndex = rowCount - 1; |
|
148 |
} |
|
149 |
||
150 |
char c = e.getKeyChar(); |
|
151 |
||
152 |
long time = e.getWhen(); |
|
153 |
||
154 |
if (time - lastTime < timeFactor) { |
|
155 |
if (typedString.length() == 1 && typedString.charAt(0) == c) { |
|
156 |
// Subsequent same key presses move the keyboard focus to the next |
|
157 |
// object that starts with the same letter. |
|
158 |
startIndex++; |
|
159 |
} else { |
|
160 |
typedString.append(c); |
|
161 |
} |
|
162 |
} else { |
|
163 |
startIndex++; |
|
164 |
||
165 |
typedString.setLength(0); |
|
166 |
typedString.append(c); |
|
167 |
} |
|
168 |
||
169 |
lastTime = time; |
|
170 |
||
171 |
if (startIndex >= rowCount) { |
|
172 |
startIndex = 0; |
|
173 |
} |
|
174 |
||
175 |
// Find next file |
|
176 |
int index = getNextMatch(startIndex, rowCount - 1); |
|
177 |
||
178 |
if (index < 0 && startIndex > 0) { // wrap |
|
179 |
index = getNextMatch(0, startIndex - 1); |
|
180 |
} |
|
181 |
||
182 |
if (index >= 0) { |
|
183 |
detailsTable.getSelectionModel().setSelectionInterval(index, index); |
|
184 |
||
185 |
Rectangle cellRect = detailsTable.getCellRect(index, |
|
186 |
detailsTable.convertColumnIndexToView(COLUMN_FILENAME), false); |
|
187 |
detailsTable.scrollRectToVisible(cellRect); |
|
188 |
} |
|
189 |
} |
|
190 |
||
191 |
private int getNextMatch(int startIndex, int finishIndex) { |
|
192 |
BasicDirectoryModel model = getModel(); |
|
193 |
JFileChooser fileChooser = getFileChooser(); |
|
194 |
DetailsTableRowSorter rowSorter = getRowSorter(); |
|
195 |
||
196 |
String prefix = typedString.toString().toLowerCase(); |
|
197 |
||
198 |
// Search element |
|
199 |
for (int index = startIndex; index <= finishIndex; index++) { |
|
200 |
File file = (File) model.getElementAt(rowSorter.convertRowIndexToModel(index)); |
|
201 |
||
202 |
String fileName = fileChooser.getName(file).toLowerCase(); |
|
203 |
||
204 |
if (fileName.startsWith(prefix)) { |
|
205 |
return index; |
|
206 |
} |
|
207 |
} |
|
208 |
||
209 |
return -1; |
|
210 |
} |
|
211 |
}; |
|
212 |
||
213 |
private FocusListener editorFocusListener = new FocusAdapter() { |
|
214 |
public void focusLost(FocusEvent e) { |
|
215 |
if (! e.isTemporary()) { |
|
216 |
applyEdit(); |
|
217 |
} |
|
218 |
} |
|
219 |
}; |
|
220 |
||
221 |
private static FocusListener repaintListener = new FocusListener() { |
|
222 |
public void focusGained(FocusEvent fe) { |
|
223 |
repaintSelection(fe.getSource()); |
|
224 |
} |
|
225 |
||
226 |
public void focusLost(FocusEvent fe) { |
|
227 |
repaintSelection(fe.getSource()); |
|
228 |
} |
|
229 |
||
230 |
private void repaintSelection(Object source) { |
|
231 |
if (source instanceof JList) { |
|
232 |
repaintListSelection((JList)source); |
|
233 |
} else if (source instanceof JTable) { |
|
234 |
repaintTableSelection((JTable)source); |
|
235 |
} |
|
236 |
} |
|
237 |
||
238 |
private void repaintListSelection(JList list) { |
|
239 |
int[] indices = list.getSelectedIndices(); |
|
240 |
for (int i : indices) { |
|
241 |
Rectangle bounds = list.getCellBounds(i, i); |
|
242 |
list.repaint(bounds); |
|
243 |
} |
|
244 |
} |
|
245 |
||
246 |
private void repaintTableSelection(JTable table) { |
|
247 |
int minRow = table.getSelectionModel().getMinSelectionIndex(); |
|
248 |
int maxRow = table.getSelectionModel().getMaxSelectionIndex(); |
|
249 |
if (minRow == -1 || maxRow == -1) { |
|
250 |
return; |
|
251 |
} |
|
252 |
||
253 |
int col0 = table.convertColumnIndexToView(COLUMN_FILENAME); |
|
254 |
||
255 |
Rectangle first = table.getCellRect(minRow, col0, false); |
|
256 |
Rectangle last = table.getCellRect(maxRow, col0, false); |
|
257 |
Rectangle dirty = first.union(last); |
|
258 |
table.repaint(dirty); |
|
259 |
} |
|
260 |
}; |
|
261 |
||
262 |
private boolean smallIconsView = false; |
|
263 |
private Border listViewBorder; |
|
264 |
private Color listViewBackground; |
|
265 |
private boolean listViewWindowsStyle; |
|
266 |
private boolean readOnly; |
|
2658 | 267 |
private boolean fullRowSelection = false; |
2 | 268 |
|
269 |
private ListSelectionModel listSelectionModel; |
|
270 |
private JList list; |
|
271 |
private JTable detailsTable; |
|
272 |
||
273 |
private static final int COLUMN_FILENAME = 0; |
|
274 |
||
275 |
// Provides a way to recognize a newly created folder, so it can |
|
276 |
// be selected when it appears in the model. |
|
277 |
private File newFolderFile; |
|
278 |
||
279 |
// Used for accessing methods in the corresponding UI class |
|
280 |
private FileChooserUIAccessor fileChooserUIAccessor; |
|
281 |
private DetailsTableModel detailsTableModel; |
|
282 |
private DetailsTableRowSorter rowSorter; |
|
283 |
||
284 |
public FilePane(FileChooserUIAccessor fileChooserUIAccessor) { |
|
285 |
super(new BorderLayout()); |
|
286 |
||
287 |
this.fileChooserUIAccessor = fileChooserUIAccessor; |
|
288 |
||
289 |
installDefaults(); |
|
290 |
createActionMap(); |
|
291 |
} |
|
292 |
||
293 |
public void uninstallUI() { |
|
294 |
if (getModel() != null) { |
|
295 |
getModel().removePropertyChangeListener(this); |
|
296 |
} |
|
297 |
} |
|
298 |
||
299 |
protected JFileChooser getFileChooser() { |
|
300 |
return fileChooserUIAccessor.getFileChooser(); |
|
301 |
} |
|
302 |
||
303 |
protected BasicDirectoryModel getModel() { |
|
304 |
return fileChooserUIAccessor.getModel(); |
|
305 |
} |
|
306 |
||
307 |
public int getViewType() { |
|
308 |
return viewType; |
|
309 |
} |
|
310 |
||
311 |
public void setViewType(int viewType) { |
|
1305
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
312 |
if (viewType == this.viewType) { |
2 | 313 |
return; |
314 |
} |
|
1305
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
315 |
|
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
316 |
int oldValue = this.viewType; |
2 | 317 |
this.viewType = viewType; |
318 |
||
1305
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
319 |
JPanel createdViewPanel = null; |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
320 |
Component newFocusOwner = null; |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
321 |
|
2 | 322 |
switch (viewType) { |
323 |
case VIEWTYPE_LIST: |
|
324 |
if (viewPanels[viewType] == null) { |
|
1305
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
325 |
createdViewPanel = fileChooserUIAccessor.createList(); |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
326 |
if (createdViewPanel == null) { |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
327 |
createdViewPanel = createList(); |
2 | 328 |
} |
1305
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
329 |
|
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
330 |
list = (JList) findChildComponent(createdViewPanel, JList.class); |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
331 |
if (listSelectionModel == null) { |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
332 |
listSelectionModel = list.getSelectionModel(); |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
333 |
if (detailsTable != null) { |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
334 |
detailsTable.setSelectionModel(listSelectionModel); |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
335 |
} |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
336 |
} else { |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
337 |
list.setSelectionModel(listSelectionModel); |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
338 |
} |
2 | 339 |
} |
340 |
list.setLayoutOrientation(JList.VERTICAL_WRAP); |
|
1305
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
341 |
newFocusOwner = list; |
2 | 342 |
break; |
343 |
||
344 |
case VIEWTYPE_DETAILS: |
|
345 |
if (viewPanels[viewType] == null) { |
|
1305
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
346 |
createdViewPanel = fileChooserUIAccessor.createDetailsView(); |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
347 |
if (createdViewPanel == null) { |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
348 |
createdViewPanel = createDetailsView(); |
2 | 349 |
} |
1305
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
350 |
|
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
351 |
detailsTable = (JTable) findChildComponent(createdViewPanel, JTable.class); |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
352 |
detailsTable.setRowHeight(Math.max(detailsTable.getFont().getSize() + 4, 16 + 1)); |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
353 |
if (listSelectionModel != null) { |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
354 |
detailsTable.setSelectionModel(listSelectionModel); |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
355 |
} |
2 | 356 |
} |
1305
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
357 |
newFocusOwner = detailsTable; |
2 | 358 |
break; |
359 |
} |
|
1305
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
360 |
|
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
361 |
if (createdViewPanel != null) { |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
362 |
viewPanels[viewType] = createdViewPanel; |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
363 |
recursivelySetInheritsPopupMenu(createdViewPanel, true); |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
364 |
} |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
365 |
|
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
366 |
boolean isFocusOwner = false; |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
367 |
|
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
368 |
if (currentViewPanel != null) { |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
369 |
Component owner = DefaultKeyboardFocusManager. |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
370 |
getCurrentKeyboardFocusManager().getPermanentFocusOwner(); |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
371 |
|
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
372 |
isFocusOwner = owner == detailsTable || owner == list; |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
373 |
|
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
374 |
remove(currentViewPanel); |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
375 |
} |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
376 |
|
2 | 377 |
currentViewPanel = viewPanels[viewType]; |
1305
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
378 |
add(currentViewPanel, BorderLayout.CENTER); |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
379 |
|
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
380 |
if (isFocusOwner && newFocusOwner != null) { |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
381 |
newFocusOwner.requestFocusInWindow(); |
2 | 382 |
} |
1305
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
383 |
|
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
384 |
revalidate(); |
e6f0b298de50
6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents:
1301
diff
changeset
|
385 |
repaint(); |
2 | 386 |
updateViewMenu(); |
387 |
firePropertyChange("viewType", oldValue, viewType); |
|
388 |
} |
|
389 |
||
390 |
class ViewTypeAction extends AbstractAction { |
|
391 |
private int viewType; |
|
392 |
||
393 |
ViewTypeAction(int viewType) { |
|
394 |
super(viewTypeActionNames[viewType]); |
|
395 |
this.viewType = viewType; |
|
396 |
||
397 |
String cmd; |
|
398 |
switch (viewType) { |
|
399 |
case VIEWTYPE_LIST: cmd = ACTION_VIEW_LIST; break; |
|
400 |
case VIEWTYPE_DETAILS: cmd = ACTION_VIEW_DETAILS; break; |
|
401 |
default: cmd = (String)getValue(Action.NAME); |
|
402 |
} |
|
403 |
putValue(Action.ACTION_COMMAND_KEY, cmd); |
|
404 |
} |
|
405 |
||
406 |
public void actionPerformed(ActionEvent e) { |
|
407 |
setViewType(viewType); |
|
408 |
} |
|
409 |
} |
|
410 |
||
411 |
public Action getViewTypeAction(int viewType) { |
|
412 |
return new ViewTypeAction(viewType); |
|
413 |
} |
|
414 |
||
415 |
private static void recursivelySetInheritsPopupMenu(Container container, boolean b) { |
|
416 |
if (container instanceof JComponent) { |
|
417 |
((JComponent)container).setInheritsPopupMenu(b); |
|
418 |
} |
|
419 |
int n = container.getComponentCount(); |
|
420 |
for (int i = 0; i < n; i++) { |
|
421 |
recursivelySetInheritsPopupMenu((Container)container.getComponent(i), b); |
|
422 |
} |
|
423 |
} |
|
424 |
||
425 |
protected void installDefaults() { |
|
426 |
Locale l = getFileChooser().getLocale(); |
|
427 |
||
428 |
listViewBorder = UIManager.getBorder("FileChooser.listViewBorder"); |
|
429 |
listViewBackground = UIManager.getColor("FileChooser.listViewBackground"); |
|
430 |
listViewWindowsStyle = UIManager.getBoolean("FileChooser.listViewWindowsStyle"); |
|
431 |
readOnly = UIManager.getBoolean("FileChooser.readOnly"); |
|
432 |
||
433 |
// TODO: On windows, get the following localized strings from the OS |
|
434 |
||
435 |
viewMenuLabelText = |
|
436 |
UIManager.getString("FileChooser.viewMenuLabelText", l); |
|
437 |
refreshActionLabelText = |
|
438 |
UIManager.getString("FileChooser.refreshActionLabelText", l); |
|
439 |
newFolderActionLabelText = |
|
440 |
UIManager.getString("FileChooser.newFolderActionLabelText", l); |
|
441 |
||
442 |
viewTypeActionNames = new String[VIEWTYPE_COUNT]; |
|
443 |
viewTypeActionNames[VIEWTYPE_LIST] = |
|
444 |
UIManager.getString("FileChooser.listViewActionLabelText", l); |
|
445 |
viewTypeActionNames[VIEWTYPE_DETAILS] = |
|
446 |
UIManager.getString("FileChooser.detailsViewActionLabelText", l); |
|
447 |
||
448 |
kiloByteString = UIManager.getString("FileChooser.fileSizeKiloBytes", l); |
|
449 |
megaByteString = UIManager.getString("FileChooser.fileSizeMegaBytes", l); |
|
450 |
gigaByteString = UIManager.getString("FileChooser.fileSizeGigaBytes", l); |
|
2658 | 451 |
fullRowSelection = UIManager.getBoolean("FileView.fullRowSelection"); |
2 | 452 |
|
453 |
renameErrorTitleText = UIManager.getString("FileChooser.renameErrorTitleText", l); |
|
454 |
renameErrorText = UIManager.getString("FileChooser.renameErrorText", l); |
|
455 |
renameErrorFileExistsText = UIManager.getString("FileChooser.renameErrorFileExistsText", l); |
|
456 |
} |
|
457 |
||
458 |
/** |
|
459 |
* Fetches the command list for the FilePane. These commands |
|
460 |
* are useful for binding to events, such as in a keymap. |
|
461 |
* |
|
462 |
* @return the command list |
|
463 |
*/ |
|
464 |
public Action[] getActions() { |
|
465 |
if (actions == null) { |
|
466 |
class FilePaneAction extends AbstractAction { |
|
467 |
FilePaneAction(String name) { |
|
468 |
this(name, name); |
|
469 |
} |
|
470 |
||
471 |
FilePaneAction(String name, String cmd) { |
|
472 |
super(name); |
|
473 |
putValue(Action.ACTION_COMMAND_KEY, cmd); |
|
474 |
} |
|
475 |
||
476 |
public void actionPerformed(ActionEvent e) { |
|
477 |
String cmd = (String)getValue(Action.ACTION_COMMAND_KEY); |
|
478 |
||
479 |
if (cmd == ACTION_CANCEL) { |
|
480 |
if (editFile != null) { |
|
481 |
cancelEdit(); |
|
482 |
} else { |
|
483 |
getFileChooser().cancelSelection(); |
|
484 |
} |
|
485 |
} else if (cmd == ACTION_EDIT_FILE_NAME) { |
|
486 |
JFileChooser fc = getFileChooser(); |
|
487 |
int index = listSelectionModel.getMinSelectionIndex(); |
|
488 |
if (index >= 0 && editFile == null && |
|
489 |
(!fc.isMultiSelectionEnabled() || |
|
490 |
fc.getSelectedFiles().length <= 1)) { |
|
491 |
||
492 |
editFileName(index); |
|
493 |
} |
|
494 |
} else if (cmd == ACTION_REFRESH) { |
|
495 |
getFileChooser().rescanCurrentDirectory(); |
|
496 |
} |
|
497 |
} |
|
498 |
||
499 |
public boolean isEnabled() { |
|
500 |
String cmd = (String)getValue(Action.ACTION_COMMAND_KEY); |
|
501 |
if (cmd == ACTION_CANCEL) { |
|
502 |
return getFileChooser().isEnabled(); |
|
503 |
} else if (cmd == ACTION_EDIT_FILE_NAME) { |
|
504 |
return !readOnly && getFileChooser().isEnabled(); |
|
505 |
} else { |
|
506 |
return true; |
|
507 |
} |
|
508 |
} |
|
509 |
} |
|
510 |
||
511 |
ArrayList<Action> actionList = new ArrayList<Action>(8); |
|
512 |
Action action; |
|
513 |
||
514 |
actionList.add(new FilePaneAction(ACTION_CANCEL)); |
|
515 |
actionList.add(new FilePaneAction(ACTION_EDIT_FILE_NAME)); |
|
516 |
actionList.add(new FilePaneAction(refreshActionLabelText, ACTION_REFRESH)); |
|
517 |
||
518 |
action = fileChooserUIAccessor.getApproveSelectionAction(); |
|
519 |
if (action != null) { |
|
520 |
actionList.add(action); |
|
521 |
} |
|
522 |
action = fileChooserUIAccessor.getChangeToParentDirectoryAction(); |
|
523 |
if (action != null) { |
|
524 |
actionList.add(action); |
|
525 |
} |
|
526 |
action = getNewFolderAction(); |
|
527 |
if (action != null) { |
|
528 |
actionList.add(action); |
|
529 |
} |
|
530 |
action = getViewTypeAction(VIEWTYPE_LIST); |
|
531 |
if (action != null) { |
|
532 |
actionList.add(action); |
|
533 |
} |
|
534 |
action = getViewTypeAction(VIEWTYPE_DETAILS); |
|
535 |
if (action != null) { |
|
536 |
actionList.add(action); |
|
537 |
} |
|
538 |
actions = actionList.toArray(new Action[actionList.size()]); |
|
539 |
} |
|
540 |
||
541 |
return actions; |
|
542 |
} |
|
543 |
||
544 |
protected void createActionMap() { |
|
545 |
addActionsToMap(super.getActionMap(), getActions()); |
|
546 |
} |
|
547 |
||
548 |
||
549 |
public static void addActionsToMap(ActionMap map, Action[] actions) { |
|
550 |
if (map != null && actions != null) { |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
551 |
for (Action a : actions) { |
2 | 552 |
String cmd = (String)a.getValue(Action.ACTION_COMMAND_KEY); |
553 |
if (cmd == null) { |
|
554 |
cmd = (String)a.getValue(Action.NAME); |
|
555 |
} |
|
556 |
map.put(cmd, a); |
|
557 |
} |
|
558 |
} |
|
559 |
} |
|
560 |
||
561 |
||
562 |
private void updateListRowCount(JList list) { |
|
563 |
if (smallIconsView) { |
|
564 |
list.setVisibleRowCount(getModel().getSize() / 3); |
|
565 |
} else { |
|
566 |
list.setVisibleRowCount(-1); |
|
567 |
} |
|
568 |
} |
|
569 |
||
570 |
public JPanel createList() { |
|
571 |
JPanel p = new JPanel(new BorderLayout()); |
|
572 |
final JFileChooser fileChooser = getFileChooser(); |
|
573 |
final JList list = new JList() { |
|
574 |
public int getNextMatch(String prefix, int startIndex, Position.Bias bias) { |
|
575 |
ListModel model = getModel(); |
|
576 |
int max = model.getSize(); |
|
577 |
if (prefix == null || startIndex < 0 || startIndex >= max) { |
|
578 |
throw new IllegalArgumentException(); |
|
579 |
} |
|
580 |
// start search from the next element before/after the selected element |
|
581 |
boolean backwards = (bias == Position.Bias.Backward); |
|
582 |
for (int i = startIndex; backwards ? i >= 0 : i < max; i += (backwards ? -1 : 1)) { |
|
583 |
String filename = fileChooser.getName((File)model.getElementAt(i)); |
|
584 |
if (filename.regionMatches(true, 0, prefix, 0, prefix.length())) { |
|
585 |
return i; |
|
586 |
} |
|
587 |
} |
|
588 |
return -1; |
|
589 |
} |
|
590 |
}; |
|
591 |
list.setCellRenderer(new FileRenderer()); |
|
592 |
list.setLayoutOrientation(JList.VERTICAL_WRAP); |
|
593 |
||
594 |
// 4835633 : tell BasicListUI that this is a file list |
|
595 |
list.putClientProperty("List.isFileList", Boolean.TRUE); |
|
596 |
||
597 |
if (listViewWindowsStyle) { |
|
598 |
list.addFocusListener(repaintListener); |
|
599 |
} |
|
600 |
||
601 |
updateListRowCount(list); |
|
602 |
||
603 |
getModel().addListDataListener(new ListDataListener() { |
|
604 |
public void intervalAdded(ListDataEvent e) { |
|
605 |
updateListRowCount(list); |
|
606 |
} |
|
607 |
public void intervalRemoved(ListDataEvent e) { |
|
608 |
updateListRowCount(list); |
|
609 |
} |
|
610 |
public void contentsChanged(ListDataEvent e) { |
|
611 |
if (isShowing()) { |
|
612 |
clearSelection(); |
|
613 |
} |
|
614 |
updateListRowCount(list); |
|
615 |
} |
|
616 |
}); |
|
617 |
||
618 |
getModel().addPropertyChangeListener(this); |
|
619 |
||
620 |
if (fileChooser.isMultiSelectionEnabled()) { |
|
621 |
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); |
|
622 |
} else { |
|
623 |
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
|
624 |
} |
|
625 |
list.setModel(new SortableListModel()); |
|
626 |
||
627 |
list.addListSelectionListener(createListSelectionListener()); |
|
628 |
list.addMouseListener(getMouseHandler()); |
|
629 |
||
630 |
JScrollPane scrollpane = new JScrollPane(list); |
|
631 |
if (listViewBackground != null) { |
|
632 |
list.setBackground(listViewBackground); |
|
633 |
} |
|
634 |
if (listViewBorder != null) { |
|
635 |
scrollpane.setBorder(listViewBorder); |
|
636 |
} |
|
637 |
p.add(scrollpane, BorderLayout.CENTER); |
|
638 |
return p; |
|
639 |
} |
|
640 |
||
641 |
/** |
|
642 |
* This model allows for sorting JList |
|
643 |
*/ |
|
644 |
private class SortableListModel extends AbstractListModel |
|
645 |
implements TableModelListener, RowSorterListener { |
|
646 |
||
647 |
public SortableListModel() { |
|
648 |
getDetailsTableModel().addTableModelListener(this); |
|
649 |
getRowSorter().addRowSorterListener(this); |
|
650 |
} |
|
651 |
||
652 |
public int getSize() { |
|
653 |
return getModel().getSize(); |
|
654 |
} |
|
655 |
||
656 |
public Object getElementAt(int index) { |
|
657 |
// JList doesn't support RowSorter so far, so we put it into the list model |
|
658 |
return getModel().getElementAt(getRowSorter().convertRowIndexToModel(index)); |
|
659 |
} |
|
660 |
||
661 |
public void tableChanged(TableModelEvent e) { |
|
662 |
fireContentsChanged(this, 0, getSize()); |
|
663 |
} |
|
664 |
||
665 |
public void sorterChanged(RowSorterEvent e) { |
|
666 |
fireContentsChanged(this, 0, getSize()); |
|
667 |
} |
|
668 |
} |
|
669 |
||
670 |
private DetailsTableModel getDetailsTableModel() { |
|
671 |
if(detailsTableModel == null) { |
|
672 |
detailsTableModel = new DetailsTableModel(getFileChooser()); |
|
673 |
} |
|
674 |
return detailsTableModel; |
|
675 |
} |
|
676 |
||
677 |
class DetailsTableModel extends AbstractTableModel implements ListDataListener { |
|
678 |
JFileChooser chooser; |
|
679 |
BasicDirectoryModel directoryModel; |
|
680 |
||
681 |
ShellFolderColumnInfo[] columns; |
|
682 |
int[] columnMap; |
|
683 |
||
684 |
DetailsTableModel(JFileChooser fc) { |
|
685 |
this.chooser = fc; |
|
686 |
directoryModel = getModel(); |
|
687 |
directoryModel.addListDataListener(this); |
|
688 |
||
689 |
updateColumnInfo(); |
|
690 |
} |
|
691 |
||
692 |
void updateColumnInfo() { |
|
693 |
File dir = chooser.getCurrentDirectory(); |
|
1842
824c7f885a64
6776856: Code with useShellFolder field shuold be simplify
rupashka
parents:
1840
diff
changeset
|
694 |
if (dir != null && usesShellFolder(chooser)) { |
2 | 695 |
try { |
696 |
dir = ShellFolder.getShellFolder(dir); |
|
697 |
} catch (FileNotFoundException e) { |
|
698 |
// Leave dir without changing |
|
699 |
} |
|
700 |
} |
|
701 |
||
702 |
ShellFolderColumnInfo[] allColumns = ShellFolder.getFolderColumns(dir); |
|
703 |
||
704 |
ArrayList<ShellFolderColumnInfo> visibleColumns = |
|
705 |
new ArrayList<ShellFolderColumnInfo>(); |
|
706 |
columnMap = new int[allColumns.length]; |
|
707 |
for (int i = 0; i < allColumns.length; i++) { |
|
708 |
ShellFolderColumnInfo column = allColumns[i]; |
|
709 |
if (column.isVisible()) { |
|
710 |
columnMap[visibleColumns.size()] = i; |
|
711 |
visibleColumns.add(column); |
|
712 |
} |
|
713 |
} |
|
714 |
||
715 |
columns = new ShellFolderColumnInfo[visibleColumns.size()]; |
|
716 |
visibleColumns.toArray(columns); |
|
717 |
columnMap = Arrays.copyOf(columnMap, columns.length); |
|
718 |
||
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
719 |
List<? extends RowSorter.SortKey> sortKeys = |
2 | 720 |
(rowSorter == null) ? null : rowSorter.getSortKeys(); |
721 |
fireTableStructureChanged(); |
|
722 |
restoreSortKeys(sortKeys); |
|
723 |
} |
|
724 |
||
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
725 |
private void restoreSortKeys(List<? extends RowSorter.SortKey> sortKeys) { |
2 | 726 |
if (sortKeys != null) { |
727 |
// check if preserved sortKeys are valid for this folder |
|
728 |
for (int i = 0; i < sortKeys.size(); i++) { |
|
729 |
RowSorter.SortKey sortKey = sortKeys.get(i); |
|
730 |
if (sortKey.getColumn() >= columns.length) { |
|
731 |
sortKeys = null; |
|
732 |
break; |
|
733 |
} |
|
734 |
} |
|
735 |
if (sortKeys != null) { |
|
736 |
rowSorter.setSortKeys(sortKeys); |
|
737 |
} |
|
738 |
} |
|
739 |
} |
|
740 |
||
741 |
public int getRowCount() { |
|
742 |
return directoryModel.getSize(); |
|
743 |
} |
|
744 |
||
745 |
public int getColumnCount() { |
|
746 |
return columns.length; |
|
747 |
} |
|
748 |
||
749 |
public Object getValueAt(int row, int col) { |
|
750 |
// Note: It is very important to avoid getting info on drives, as |
|
751 |
// this will trigger "No disk in A:" and similar dialogs. |
|
752 |
// |
|
753 |
// Use (f.exists() && !chooser.getFileSystemView().isFileSystemRoot(f)) to |
|
754 |
// determine if it is safe to call methods directly on f. |
|
755 |
return getFileColumnValue((File)directoryModel.getElementAt(row), col); |
|
756 |
} |
|
757 |
||
758 |
private Object getFileColumnValue(File f, int col) { |
|
759 |
return (col == COLUMN_FILENAME) |
|
760 |
? f // always return the file itself for the 1st column |
|
761 |
: ShellFolder.getFolderColumnValue(f, columnMap[col]); |
|
762 |
} |
|
763 |
||
764 |
public void setValueAt(Object value, int row, int col) { |
|
765 |
if (col == COLUMN_FILENAME) { |
|
766 |
JFileChooser chooser = getFileChooser(); |
|
767 |
File f = (File)getValueAt(row, col); |
|
768 |
if (f != null) { |
|
769 |
String oldDisplayName = chooser.getName(f); |
|
770 |
String oldFileName = f.getName(); |
|
771 |
String newDisplayName = ((String)value).trim(); |
|
772 |
String newFileName; |
|
773 |
||
774 |
if (!newDisplayName.equals(oldDisplayName)) { |
|
775 |
newFileName = newDisplayName; |
|
776 |
//Check if extension is hidden from user |
|
777 |
int i1 = oldFileName.length(); |
|
778 |
int i2 = oldDisplayName.length(); |
|
779 |
if (i1 > i2 && oldFileName.charAt(i2) == '.') { |
|
780 |
newFileName = newDisplayName + oldFileName.substring(i2); |
|
781 |
} |
|
782 |
||
783 |
// rename |
|
784 |
FileSystemView fsv = chooser.getFileSystemView(); |
|
785 |
File f2 = fsv.createFileObject(f.getParentFile(), newFileName); |
|
786 |
if (f2.exists()) { |
|
787 |
JOptionPane.showMessageDialog(chooser, MessageFormat.format(renameErrorFileExistsText, |
|
788 |
oldFileName), renameErrorTitleText, JOptionPane.ERROR_MESSAGE); |
|
789 |
} else { |
|
790 |
if (FilePane.this.getModel().renameFile(f, f2)) { |
|
791 |
if (fsv.isParent(chooser.getCurrentDirectory(), f2)) { |
|
792 |
if (chooser.isMultiSelectionEnabled()) { |
|
793 |
chooser.setSelectedFiles(new File[]{f2}); |
|
794 |
} else { |
|
795 |
chooser.setSelectedFile(f2); |
|
796 |
} |
|
797 |
} else { |
|
798 |
// Could be because of delay in updating Desktop folder |
|
799 |
// chooser.setSelectedFile(null); |
|
800 |
} |
|
801 |
} else { |
|
802 |
JOptionPane.showMessageDialog(chooser, MessageFormat.format(renameErrorText, oldFileName), |
|
803 |
renameErrorTitleText, JOptionPane.ERROR_MESSAGE); |
|
804 |
} |
|
805 |
} |
|
806 |
} |
|
807 |
} |
|
808 |
} |
|
809 |
} |
|
810 |
||
811 |
public boolean isCellEditable(int row, int column) { |
|
812 |
File currentDirectory = getFileChooser().getCurrentDirectory(); |
|
813 |
return (!readOnly && column == COLUMN_FILENAME && canWrite(currentDirectory)); |
|
814 |
} |
|
815 |
||
816 |
public void contentsChanged(ListDataEvent e) { |
|
817 |
// Update the selection after the model has been updated |
|
818 |
new DelayedSelectionUpdater(); |
|
819 |
fireTableDataChanged(); |
|
820 |
} |
|
821 |
||
822 |
public void intervalAdded(ListDataEvent e) { |
|
823 |
int i0 = e.getIndex0(); |
|
824 |
int i1 = e.getIndex1(); |
|
825 |
if (i0 == i1) { |
|
826 |
File file = (File)getModel().getElementAt(i0); |
|
827 |
if (file.equals(newFolderFile)) { |
|
828 |
new DelayedSelectionUpdater(file); |
|
829 |
newFolderFile = null; |
|
830 |
} |
|
831 |
} |
|
832 |
||
833 |
fireTableRowsInserted(e.getIndex0(), e.getIndex1()); |
|
834 |
} |
|
835 |
public void intervalRemoved(ListDataEvent e) { |
|
836 |
fireTableRowsDeleted(e.getIndex0(), e.getIndex1()); |
|
837 |
} |
|
838 |
||
839 |
public ShellFolderColumnInfo[] getColumns() { |
|
840 |
return columns; |
|
841 |
} |
|
842 |
} |
|
843 |
||
844 |
||
845 |
private void updateDetailsColumnModel(JTable table) { |
|
846 |
if (table != null) { |
|
847 |
ShellFolderColumnInfo[] columns = detailsTableModel.getColumns(); |
|
848 |
||
849 |
TableColumnModel columnModel = new DefaultTableColumnModel(); |
|
850 |
for (int i = 0; i < columns.length; i++) { |
|
851 |
ShellFolderColumnInfo dataItem = columns[i]; |
|
852 |
TableColumn column = new TableColumn(i); |
|
853 |
||
854 |
String title = dataItem.getTitle(); |
|
855 |
if (title != null && title.startsWith("FileChooser.") && title.endsWith("HeaderText")) { |
|
856 |
// the column must have a string resource that we try to get |
|
857 |
String uiTitle = UIManager.getString(title, table.getLocale()); |
|
858 |
if (uiTitle != null) { |
|
859 |
title = uiTitle; |
|
860 |
} |
|
861 |
} |
|
862 |
column.setHeaderValue(title); |
|
863 |
||
864 |
Integer width = dataItem.getWidth(); |
|
865 |
if (width != null) { |
|
866 |
column.setPreferredWidth(width); |
|
867 |
// otherwise we let JTable to decide the actual width |
|
868 |
} |
|
869 |
||
870 |
columnModel.addColumn(column); |
|
871 |
} |
|
872 |
||
873 |
// Install cell editor for editing file name |
|
874 |
if (!readOnly && columnModel.getColumnCount() > COLUMN_FILENAME) { |
|
875 |
columnModel.getColumn(COLUMN_FILENAME). |
|
876 |
setCellEditor(getDetailsTableCellEditor()); |
|
877 |
} |
|
878 |
||
879 |
table.setColumnModel(columnModel); |
|
880 |
} |
|
881 |
} |
|
882 |
||
883 |
private DetailsTableRowSorter getRowSorter() { |
|
884 |
if (rowSorter == null) { |
|
885 |
rowSorter = new DetailsTableRowSorter(); |
|
886 |
} |
|
887 |
return rowSorter; |
|
888 |
} |
|
889 |
||
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
890 |
private class DetailsTableRowSorter extends TableRowSorter<TableModel> { |
2 | 891 |
public DetailsTableRowSorter() { |
892 |
setModelWrapper(new SorterModelWrapper()); |
|
893 |
} |
|
894 |
||
895 |
public void updateComparators(ShellFolderColumnInfo [] columns) { |
|
896 |
for (int i = 0; i < columns.length; i++) { |
|
897 |
Comparator c = columns[i].getComparator(); |
|
898 |
if (c != null) { |
|
899 |
c = new DirectoriesFirstComparatorWrapper(i, c); |
|
900 |
} |
|
901 |
setComparator(i, c); |
|
902 |
} |
|
903 |
} |
|
904 |
||
2489
5052722686e2
6491795: COM should be initialized for Shell API calls in ShellFolder2.cpp
rupashka
parents:
1842
diff
changeset
|
905 |
@Override |
5052722686e2
6491795: COM should be initialized for Shell API calls in ShellFolder2.cpp
rupashka
parents:
1842
diff
changeset
|
906 |
public void sort() { |
3346
1c65be97eaa2
6460525: javax/swing/JFileChooser/6396844/TwentyThousandTest.java times out
rupashka
parents:
2658
diff
changeset
|
907 |
ShellFolder.invoke(new Callable<Void>() { |
1c65be97eaa2
6460525: javax/swing/JFileChooser/6396844/TwentyThousandTest.java times out
rupashka
parents:
2658
diff
changeset
|
908 |
public Void call() { |
2489
5052722686e2
6491795: COM should be initialized for Shell API calls in ShellFolder2.cpp
rupashka
parents:
1842
diff
changeset
|
909 |
DetailsTableRowSorter.super.sort(); |
5052722686e2
6491795: COM should be initialized for Shell API calls in ShellFolder2.cpp
rupashka
parents:
1842
diff
changeset
|
910 |
return null; |
5052722686e2
6491795: COM should be initialized for Shell API calls in ShellFolder2.cpp
rupashka
parents:
1842
diff
changeset
|
911 |
} |
5052722686e2
6491795: COM should be initialized for Shell API calls in ShellFolder2.cpp
rupashka
parents:
1842
diff
changeset
|
912 |
}); |
5052722686e2
6491795: COM should be initialized for Shell API calls in ShellFolder2.cpp
rupashka
parents:
1842
diff
changeset
|
913 |
} |
5052722686e2
6491795: COM should be initialized for Shell API calls in ShellFolder2.cpp
rupashka
parents:
1842
diff
changeset
|
914 |
|
2 | 915 |
public void modelStructureChanged() { |
916 |
super.modelStructureChanged(); |
|
917 |
updateComparators(detailsTableModel.getColumns()); |
|
918 |
} |
|
919 |
||
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
920 |
private class SorterModelWrapper extends ModelWrapper<TableModel, Integer> { |
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
921 |
public TableModel getModel() { |
2 | 922 |
return getDetailsTableModel(); |
923 |
} |
|
924 |
||
925 |
public int getColumnCount() { |
|
926 |
return getDetailsTableModel().getColumnCount(); |
|
927 |
} |
|
928 |
||
929 |
public int getRowCount() { |
|
930 |
return getDetailsTableModel().getRowCount(); |
|
931 |
} |
|
932 |
||
933 |
public Object getValueAt(int row, int column) { |
|
934 |
return FilePane.this.getModel().getElementAt(row); |
|
935 |
} |
|
936 |
||
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
937 |
public Integer getIdentifier(int row) { |
2 | 938 |
return row; |
939 |
} |
|
940 |
} |
|
941 |
} |
|
942 |
||
943 |
/** |
|
944 |
* This class sorts directories before files, comparing directory to |
|
945 |
* directory and file to file using the wrapped comparator. |
|
946 |
*/ |
|
947 |
private class DirectoriesFirstComparatorWrapper implements Comparator<File> { |
|
948 |
private Comparator comparator; |
|
949 |
private int column; |
|
950 |
||
951 |
public DirectoriesFirstComparatorWrapper(int column, Comparator comparator) { |
|
952 |
this.column = column; |
|
953 |
this.comparator = comparator; |
|
954 |
} |
|
955 |
||
956 |
public int compare(File f1, File f2) { |
|
957 |
if (f1 != null && f2 != null) { |
|
958 |
boolean traversable1 = getFileChooser().isTraversable(f1); |
|
959 |
boolean traversable2 = getFileChooser().isTraversable(f2); |
|
960 |
// directories go first |
|
961 |
if (traversable1 && !traversable2) { |
|
962 |
return -1; |
|
963 |
} |
|
964 |
if (!traversable1 && traversable2) { |
|
965 |
return 1; |
|
966 |
} |
|
967 |
} |
|
968 |
if (detailsTableModel.getColumns()[column].isCompareByColumn()) { |
|
969 |
return comparator.compare( |
|
970 |
getDetailsTableModel().getFileColumnValue(f1, column), |
|
971 |
getDetailsTableModel().getFileColumnValue(f2, column) |
|
972 |
); |
|
973 |
} |
|
974 |
// For this column we need to pass the file itself (not a |
|
975 |
// column value) to the comparator |
|
976 |
return comparator.compare(f1, f2); |
|
977 |
} |
|
978 |
} |
|
979 |
||
980 |
private DetailsTableCellEditor tableCellEditor; |
|
981 |
||
982 |
private DetailsTableCellEditor getDetailsTableCellEditor() { |
|
983 |
if (tableCellEditor == null) { |
|
984 |
tableCellEditor = new DetailsTableCellEditor(new JTextField()); |
|
985 |
} |
|
986 |
return tableCellEditor; |
|
987 |
} |
|
988 |
||
989 |
private class DetailsTableCellEditor extends DefaultCellEditor { |
|
990 |
private final JTextField tf; |
|
991 |
||
992 |
public DetailsTableCellEditor(JTextField tf) { |
|
993 |
super(tf); |
|
994 |
this.tf = tf; |
|
2658 | 995 |
tf.setName("Table.editor"); |
2 | 996 |
tf.addFocusListener(editorFocusListener); |
997 |
} |
|
998 |
||
999 |
public Component getTableCellEditorComponent(JTable table, Object value, |
|
1000 |
boolean isSelected, int row, int column) { |
|
1001 |
Component comp = super.getTableCellEditorComponent(table, value, |
|
1002 |
isSelected, row, column); |
|
1003 |
if (value instanceof File) { |
|
1004 |
tf.setText(getFileChooser().getName((File) value)); |
|
1005 |
tf.selectAll(); |
|
1006 |
} |
|
1007 |
return comp; |
|
1008 |
} |
|
1009 |
} |
|
1010 |
||
1011 |
||
1012 |
class DetailsTableCellRenderer extends DefaultTableCellRenderer { |
|
1013 |
JFileChooser chooser; |
|
1014 |
DateFormat df; |
|
1015 |
||
1016 |
DetailsTableCellRenderer(JFileChooser chooser) { |
|
1017 |
this.chooser = chooser; |
|
1018 |
df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, |
|
1019 |
chooser.getLocale()); |
|
1020 |
} |
|
1021 |
||
1022 |
public void setBounds(int x, int y, int width, int height) { |
|
2658 | 1023 |
if (getHorizontalAlignment() == SwingConstants.LEADING && |
1024 |
!fullRowSelection) { |
|
2 | 1025 |
// Restrict width to actual text |
1026 |
width = Math.min(width, this.getPreferredSize().width+4); |
|
1027 |
} else { |
|
1028 |
x -= 4; |
|
1029 |
} |
|
1030 |
super.setBounds(x, y, width, height); |
|
1031 |
} |
|
1032 |
||
1033 |
||
1034 |
public Insets getInsets(Insets i) { |
|
1035 |
// Provide some space between columns |
|
1036 |
i = super.getInsets(i); |
|
1037 |
i.left += 4; |
|
1038 |
i.right += 4; |
|
1039 |
return i; |
|
1040 |
} |
|
1041 |
||
1042 |
public Component getTableCellRendererComponent(JTable table, Object value, |
|
1043 |
boolean isSelected, boolean hasFocus, int row, int column) { |
|
1044 |
||
2658 | 1045 |
if ((table.convertColumnIndexToModel(column) != COLUMN_FILENAME || |
1046 |
(listViewWindowsStyle && !table.isFocusOwner())) && |
|
1047 |
!fullRowSelection) { |
|
2 | 1048 |
isSelected = false; |
1049 |
} |
|
1050 |
||
1051 |
super.getTableCellRendererComponent(table, value, isSelected, |
|
1052 |
hasFocus, row, column); |
|
1053 |
||
1054 |
setIcon(null); |
|
1055 |
||
1056 |
int modelColumn = table.convertColumnIndexToModel(column); |
|
1057 |
ShellFolderColumnInfo columnInfo = detailsTableModel.getColumns()[modelColumn]; |
|
1058 |
||
1059 |
Integer alignment = columnInfo.getAlignment(); |
|
1060 |
if (alignment == null) { |
|
1061 |
alignment = (value instanceof Number) |
|
1062 |
? SwingConstants.RIGHT |
|
1063 |
: SwingConstants.LEADING; |
|
1064 |
} |
|
1065 |
||
1066 |
setHorizontalAlignment(alignment); |
|
1067 |
||
1068 |
// formatting cell text |
|
1069 |
// TODO: it's rather a temporary trick, to be revised |
|
1070 |
String text; |
|
1071 |
||
1072 |
if (value == null) { |
|
1073 |
text = ""; |
|
1074 |
||
1075 |
} else if (value instanceof File) { |
|
1076 |
File file = (File)value; |
|
1077 |
text = chooser.getName(file); |
|
1078 |
Icon icon = chooser.getIcon(file); |
|
1079 |
setIcon(icon); |
|
1080 |
||
1081 |
} else if (value instanceof Long) { |
|
1082 |
long len = ((Long) value) / 1024L; |
|
1083 |
if (listViewWindowsStyle) { |
|
1084 |
text = MessageFormat.format(kiloByteString, len + 1); |
|
1085 |
} else if (len < 1024L) { |
|
1086 |
text = MessageFormat.format(kiloByteString, (len == 0L) ? 1L : len); |
|
1087 |
} else { |
|
1088 |
len /= 1024L; |
|
1089 |
if (len < 1024L) { |
|
1090 |
text = MessageFormat.format(megaByteString, len); |
|
1091 |
} else { |
|
1092 |
len /= 1024L; |
|
1093 |
text = MessageFormat.format(gigaByteString, len); |
|
1094 |
} |
|
1095 |
} |
|
1096 |
||
1097 |
} else if (value instanceof Date) { |
|
1098 |
text = df.format((Date)value); |
|
1099 |
||
1100 |
} else { |
|
1101 |
text = value.toString(); |
|
1102 |
} |
|
1103 |
||
1104 |
setText(text); |
|
1105 |
||
1106 |
return this; |
|
1107 |
} |
|
1108 |
} |
|
1109 |
||
1110 |
public JPanel createDetailsView() { |
|
1111 |
final JFileChooser chooser = getFileChooser(); |
|
1112 |
||
1113 |
JPanel p = new JPanel(new BorderLayout()); |
|
1114 |
||
1115 |
final JTable detailsTable = new JTable(getDetailsTableModel()) { |
|
1116 |
// Handle Escape key events here |
|
1117 |
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) { |
|
1118 |
if (e.getKeyCode() == KeyEvent.VK_ESCAPE && getCellEditor() == null) { |
|
1119 |
// We are not editing, forward to filechooser. |
|
1120 |
chooser.dispatchEvent(e); |
|
1121 |
return true; |
|
1122 |
} |
|
1123 |
return super.processKeyBinding(ks, e, condition, pressed); |
|
1124 |
} |
|
1125 |
||
1126 |
public void tableChanged(TableModelEvent e) { |
|
1127 |
super.tableChanged(e); |
|
1128 |
||
1129 |
if (e.getFirstRow() == TableModelEvent.HEADER_ROW) { |
|
1130 |
// update header with possibly changed column set |
|
1131 |
updateDetailsColumnModel(this); |
|
1132 |
} |
|
1133 |
} |
|
1134 |
}; |
|
1135 |
||
1136 |
detailsTable.setRowSorter(getRowSorter()); |
|
1137 |
detailsTable.setAutoCreateColumnsFromModel(false); |
|
1138 |
detailsTable.setComponentOrientation(chooser.getComponentOrientation()); |
|
1139 |
detailsTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); |
|
1140 |
detailsTable.setShowGrid(false); |
|
1141 |
detailsTable.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE); |
|
1142 |
detailsTable.addKeyListener(detailsKeyListener); |
|
1143 |
||
1144 |
Font font = list.getFont(); |
|
1145 |
detailsTable.setFont(font); |
|
1146 |
detailsTable.setIntercellSpacing(new Dimension(0, 0)); |
|
1147 |
||
1148 |
TableCellRenderer headerRenderer = |
|
1149 |
new AlignableTableHeaderRenderer(detailsTable.getTableHeader().getDefaultRenderer()); |
|
1150 |
detailsTable.getTableHeader().setDefaultRenderer(headerRenderer); |
|
1151 |
TableCellRenderer cellRenderer = new DetailsTableCellRenderer(chooser); |
|
1152 |
detailsTable.setDefaultRenderer(Object.class, cellRenderer); |
|
1153 |
||
1154 |
// So that drag can be started on a mouse press |
|
1155 |
detailsTable.getColumnModel().getSelectionModel(). |
|
1156 |
setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
|
1157 |
||
1158 |
detailsTable.addMouseListener(getMouseHandler()); |
|
1159 |
// No need to addListSelectionListener because selections are forwarded |
|
1160 |
// to our JList. |
|
1161 |
||
1162 |
// 4835633 : tell BasicTableUI that this is a file list |
|
1163 |
detailsTable.putClientProperty("Table.isFileList", Boolean.TRUE); |
|
1164 |
||
1165 |
if (listViewWindowsStyle) { |
|
1166 |
detailsTable.addFocusListener(repaintListener); |
|
1167 |
} |
|
1168 |
||
1169 |
// TAB/SHIFT-TAB should transfer focus and ENTER should select an item. |
|
1170 |
// We don't want them to navigate within the table |
|
1171 |
ActionMap am = SwingUtilities.getUIActionMap(detailsTable); |
|
1172 |
am.remove("selectNextRowCell"); |
|
1173 |
am.remove("selectPreviousRowCell"); |
|
1174 |
am.remove("selectNextColumnCell"); |
|
1175 |
am.remove("selectPreviousColumnCell"); |
|
1176 |
detailsTable.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, |
|
1177 |
null); |
|
1178 |
detailsTable.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, |
|
1179 |
null); |
|
1180 |
||
1181 |
JScrollPane scrollpane = new JScrollPane(detailsTable); |
|
1182 |
scrollpane.setComponentOrientation(chooser.getComponentOrientation()); |
|
1183 |
LookAndFeel.installColors(scrollpane.getViewport(), "Table.background", "Table.foreground"); |
|
1184 |
||
1185 |
// Adjust width of first column so the table fills the viewport when |
|
1186 |
// first displayed (temporary listener). |
|
1187 |
scrollpane.addComponentListener(new ComponentAdapter() { |
|
1188 |
public void componentResized(ComponentEvent e) { |
|
1189 |
JScrollPane sp = (JScrollPane)e.getComponent(); |
|
1190 |
fixNameColumnWidth(sp.getViewport().getSize().width); |
|
1191 |
sp.removeComponentListener(this); |
|
1192 |
} |
|
1193 |
}); |
|
1194 |
||
1195 |
// 4835633. |
|
1196 |
// If the mouse is pressed in the area below the Details view table, the |
|
1197 |
// event is not dispatched to the Table MouseListener but to the |
|
1198 |
// scrollpane. Listen for that here so we can clear the selection. |
|
1199 |
scrollpane.addMouseListener(new MouseAdapter() { |
|
1200 |
public void mousePressed(MouseEvent e) { |
|
1201 |
JScrollPane jsp = ((JScrollPane)e.getComponent()); |
|
1202 |
JTable table = (JTable)jsp.getViewport().getView(); |
|
1203 |
||
1204 |
if (!e.isShiftDown() || table.getSelectionModel().getSelectionMode() == ListSelectionModel.SINGLE_SELECTION) { |
|
1205 |
clearSelection(); |
|
1206 |
TableCellEditor tce = table.getCellEditor(); |
|
1207 |
if (tce != null) { |
|
1208 |
tce.stopCellEditing(); |
|
1209 |
} |
|
1210 |
} |
|
1211 |
} |
|
1212 |
}); |
|
1213 |
||
1214 |
detailsTable.setForeground(list.getForeground()); |
|
1215 |
detailsTable.setBackground(list.getBackground()); |
|
1216 |
||
1217 |
if (listViewBorder != null) { |
|
1218 |
scrollpane.setBorder(listViewBorder); |
|
1219 |
} |
|
1220 |
p.add(scrollpane, BorderLayout.CENTER); |
|
1221 |
||
1222 |
detailsTableModel.fireTableStructureChanged(); |
|
1223 |
||
1224 |
return p; |
|
1225 |
} // createDetailsView |
|
1226 |
||
1227 |
private class AlignableTableHeaderRenderer implements TableCellRenderer { |
|
1228 |
TableCellRenderer wrappedRenderer; |
|
1229 |
||
1230 |
public AlignableTableHeaderRenderer(TableCellRenderer wrappedRenderer) { |
|
1231 |
this.wrappedRenderer = wrappedRenderer; |
|
1232 |
} |
|
1233 |
||
1234 |
public Component getTableCellRendererComponent( |
|
1235 |
JTable table, Object value, boolean isSelected, |
|
1236 |
boolean hasFocus, int row, int column) { |
|
1237 |
||
1238 |
Component c = wrappedRenderer.getTableCellRendererComponent( |
|
1239 |
table, value, isSelected, hasFocus, row, column); |
|
1240 |
||
1241 |
int modelColumn = table.convertColumnIndexToModel(column); |
|
1242 |
ShellFolderColumnInfo columnInfo = detailsTableModel.getColumns()[modelColumn]; |
|
1243 |
||
1244 |
Integer alignment = columnInfo.getAlignment(); |
|
1245 |
if (alignment == null) { |
|
1246 |
alignment = SwingConstants.CENTER; |
|
1247 |
} |
|
1248 |
if (c instanceof JLabel) { |
|
1249 |
((JLabel) c).setHorizontalAlignment(alignment); |
|
1250 |
} |
|
1251 |
||
1252 |
return c; |
|
1253 |
} |
|
1254 |
} |
|
1255 |
||
1256 |
private void fixNameColumnWidth(int viewWidth) { |
|
1257 |
TableColumn nameCol = detailsTable.getColumnModel().getColumn(COLUMN_FILENAME); |
|
1258 |
int tableWidth = detailsTable.getPreferredSize().width; |
|
1259 |
||
1260 |
if (tableWidth < viewWidth) { |
|
1261 |
nameCol.setPreferredWidth(nameCol.getPreferredWidth() + viewWidth - tableWidth); |
|
1262 |
} |
|
1263 |
} |
|
1264 |
||
1265 |
private class DelayedSelectionUpdater implements Runnable { |
|
1266 |
File editFile; |
|
1267 |
||
1268 |
DelayedSelectionUpdater() { |
|
1269 |
this(null); |
|
1270 |
} |
|
1271 |
||
1272 |
DelayedSelectionUpdater(File editFile) { |
|
1273 |
this.editFile = editFile; |
|
1274 |
if (isShowing()) { |
|
1275 |
SwingUtilities.invokeLater(this); |
|
1276 |
} |
|
1277 |
} |
|
1278 |
||
1279 |
public void run() { |
|
1280 |
setFileSelected(); |
|
1281 |
if (editFile != null) { |
|
1282 |
editFileName(getRowSorter().convertRowIndexToView( |
|
1283 |
getModel().indexOf(editFile))); |
|
1284 |
editFile = null; |
|
1285 |
} |
|
1286 |
} |
|
1287 |
} |
|
1288 |
||
1289 |
||
1290 |
/** |
|
1291 |
* Creates a selection listener for the list of files and directories. |
|
1292 |
* |
|
1293 |
* @return a <code>ListSelectionListener</code> |
|
1294 |
*/ |
|
1295 |
public ListSelectionListener createListSelectionListener() { |
|
1296 |
return fileChooserUIAccessor.createListSelectionListener(); |
|
1297 |
} |
|
1298 |
||
1299 |
int lastIndex = -1; |
|
1300 |
File editFile = null; |
|
1301 |
||
1302 |
private int getEditIndex() { |
|
1303 |
return lastIndex; |
|
1304 |
} |
|
1305 |
||
1306 |
private void setEditIndex(int i) { |
|
1307 |
lastIndex = i; |
|
1308 |
} |
|
1309 |
||
1310 |
private void resetEditIndex() { |
|
1311 |
lastIndex = -1; |
|
1312 |
} |
|
1313 |
||
1314 |
private void cancelEdit() { |
|
1315 |
if (editFile != null) { |
|
1316 |
editFile = null; |
|
1317 |
list.remove(editCell); |
|
1318 |
repaint(); |
|
1319 |
} else if (detailsTable != null && detailsTable.isEditing()) { |
|
1320 |
detailsTable.getCellEditor().cancelCellEditing(); |
|
1321 |
} |
|
1322 |
} |
|
1323 |
||
1324 |
JTextField editCell = null; |
|
1325 |
||
1326 |
/** |
|
1327 |
* @param index visual index of the file to be edited |
|
1328 |
*/ |
|
1329 |
private void editFileName(int index) { |
|
464
9c414b00e007
6635277: Incorrect text seen when creating a new folder, when selection is on the image file in JFileChooser
rupashka
parents:
2
diff
changeset
|
1330 |
JFileChooser chooser = getFileChooser(); |
9c414b00e007
6635277: Incorrect text seen when creating a new folder, when selection is on the image file in JFileChooser
rupashka
parents:
2
diff
changeset
|
1331 |
File currentDirectory = chooser.getCurrentDirectory(); |
9c414b00e007
6635277: Incorrect text seen when creating a new folder, when selection is on the image file in JFileChooser
rupashka
parents:
2
diff
changeset
|
1332 |
|
2 | 1333 |
if (readOnly || !canWrite(currentDirectory)) { |
1334 |
return; |
|
1335 |
} |
|
1336 |
||
1337 |
ensureIndexIsVisible(index); |
|
1338 |
switch (viewType) { |
|
1339 |
case VIEWTYPE_LIST: |
|
1340 |
editFile = (File)getModel().getElementAt(getRowSorter().convertRowIndexToModel(index)); |
|
1341 |
Rectangle r = list.getCellBounds(index, index); |
|
1342 |
if (editCell == null) { |
|
1343 |
editCell = new JTextField(); |
|
2658 | 1344 |
editCell.setName("Tree.cellEditor"); |
2 | 1345 |
editCell.addActionListener(new EditActionListener()); |
1346 |
editCell.addFocusListener(editorFocusListener); |
|
1347 |
editCell.setNextFocusableComponent(list); |
|
1348 |
} |
|
1349 |
list.add(editCell); |
|
464
9c414b00e007
6635277: Incorrect text seen when creating a new folder, when selection is on the image file in JFileChooser
rupashka
parents:
2
diff
changeset
|
1350 |
editCell.setText(chooser.getName(editFile)); |
2 | 1351 |
ComponentOrientation orientation = list.getComponentOrientation(); |
1352 |
editCell.setComponentOrientation(orientation); |
|
464
9c414b00e007
6635277: Incorrect text seen when creating a new folder, when selection is on the image file in JFileChooser
rupashka
parents:
2
diff
changeset
|
1353 |
|
9c414b00e007
6635277: Incorrect text seen when creating a new folder, when selection is on the image file in JFileChooser
rupashka
parents:
2
diff
changeset
|
1354 |
Icon icon = chooser.getIcon(editFile); |
9c414b00e007
6635277: Incorrect text seen when creating a new folder, when selection is on the image file in JFileChooser
rupashka
parents:
2
diff
changeset
|
1355 |
|
9c414b00e007
6635277: Incorrect text seen when creating a new folder, when selection is on the image file in JFileChooser
rupashka
parents:
2
diff
changeset
|
1356 |
// PENDING - grab padding (4) below from defaults table. |
9c414b00e007
6635277: Incorrect text seen when creating a new folder, when selection is on the image file in JFileChooser
rupashka
parents:
2
diff
changeset
|
1357 |
int editX = icon == null ? 20 : icon.getIconWidth() + 4; |
9c414b00e007
6635277: Incorrect text seen when creating a new folder, when selection is on the image file in JFileChooser
rupashka
parents:
2
diff
changeset
|
1358 |
|
2 | 1359 |
if (orientation.isLeftToRight()) { |
1360 |
editCell.setBounds(editX + r.x, r.y, r.width - editX, r.height); |
|
1361 |
} else { |
|
1362 |
editCell.setBounds(r.x, r.y, r.width - editX, r.height); |
|
1363 |
} |
|
1364 |
editCell.requestFocus(); |
|
1365 |
editCell.selectAll(); |
|
1366 |
break; |
|
1367 |
||
1368 |
case VIEWTYPE_DETAILS: |
|
1369 |
detailsTable.editCellAt(index, COLUMN_FILENAME); |
|
1370 |
break; |
|
1371 |
} |
|
1372 |
} |
|
1373 |
||
1374 |
||
1375 |
class EditActionListener implements ActionListener { |
|
1376 |
public void actionPerformed(ActionEvent e) { |
|
1377 |
applyEdit(); |
|
1378 |
} |
|
1379 |
} |
|
1380 |
||
1381 |
private void applyEdit() { |
|
1382 |
if (editFile != null && editFile.exists()) { |
|
1383 |
JFileChooser chooser = getFileChooser(); |
|
1384 |
String oldDisplayName = chooser.getName(editFile); |
|
1385 |
String oldFileName = editFile.getName(); |
|
1386 |
String newDisplayName = editCell.getText().trim(); |
|
1387 |
String newFileName; |
|
1388 |
||
1389 |
if (!newDisplayName.equals(oldDisplayName)) { |
|
1390 |
newFileName = newDisplayName; |
|
1391 |
//Check if extension is hidden from user |
|
1392 |
int i1 = oldFileName.length(); |
|
1393 |
int i2 = oldDisplayName.length(); |
|
1394 |
if (i1 > i2 && oldFileName.charAt(i2) == '.') { |
|
1395 |
newFileName = newDisplayName + oldFileName.substring(i2); |
|
1396 |
} |
|
1397 |
||
1398 |
// rename |
|
1399 |
FileSystemView fsv = chooser.getFileSystemView(); |
|
1400 |
File f2 = fsv.createFileObject(editFile.getParentFile(), newFileName); |
|
1401 |
if (f2.exists()) { |
|
1402 |
JOptionPane.showMessageDialog(chooser, MessageFormat.format(renameErrorFileExistsText, oldFileName), |
|
1403 |
renameErrorTitleText, JOptionPane.ERROR_MESSAGE); |
|
1404 |
} else { |
|
1405 |
if (getModel().renameFile(editFile, f2)) { |
|
1406 |
if (fsv.isParent(chooser.getCurrentDirectory(), f2)) { |
|
1407 |
if (chooser.isMultiSelectionEnabled()) { |
|
1408 |
chooser.setSelectedFiles(new File[]{f2}); |
|
1409 |
} else { |
|
1410 |
chooser.setSelectedFile(f2); |
|
1411 |
} |
|
1412 |
} else { |
|
1413 |
//Could be because of delay in updating Desktop folder |
|
1414 |
//chooser.setSelectedFile(null); |
|
1415 |
} |
|
1416 |
} else { |
|
1417 |
JOptionPane.showMessageDialog(chooser, MessageFormat.format(renameErrorText, oldFileName), |
|
1418 |
renameErrorTitleText, JOptionPane.ERROR_MESSAGE); |
|
1419 |
} |
|
1420 |
} |
|
1421 |
} |
|
1422 |
} |
|
1423 |
if (detailsTable != null && detailsTable.isEditing()) { |
|
1424 |
detailsTable.getCellEditor().stopCellEditing(); |
|
1425 |
} |
|
1426 |
cancelEdit(); |
|
1427 |
} |
|
1428 |
||
1429 |
protected Action newFolderAction; |
|
1430 |
||
1431 |
public Action getNewFolderAction() { |
|
1432 |
if (!readOnly && newFolderAction == null) { |
|
1433 |
newFolderAction = new AbstractAction(newFolderActionLabelText) { |
|
1434 |
private Action basicNewFolderAction; |
|
1435 |
||
1436 |
// Initializer |
|
1437 |
{ |
|
1438 |
putValue(Action.ACTION_COMMAND_KEY, FilePane.ACTION_NEW_FOLDER); |
|
1439 |
||
1440 |
File currentDirectory = getFileChooser().getCurrentDirectory(); |
|
1441 |
if (currentDirectory != null) { |
|
1442 |
setEnabled(canWrite(currentDirectory)); |
|
1443 |
} |
|
1444 |
} |
|
1445 |
||
1446 |
public void actionPerformed(ActionEvent ev) { |
|
1447 |
if (basicNewFolderAction == null) { |
|
1448 |
basicNewFolderAction = fileChooserUIAccessor.getNewFolderAction(); |
|
1449 |
} |
|
1450 |
JFileChooser fc = getFileChooser(); |
|
1451 |
File oldFile = fc.getSelectedFile(); |
|
1452 |
basicNewFolderAction.actionPerformed(ev); |
|
1453 |
File newFile = fc.getSelectedFile(); |
|
1454 |
if (newFile != null && !newFile.equals(oldFile) && newFile.isDirectory()) { |
|
1455 |
newFolderFile = newFile; |
|
1456 |
} |
|
1457 |
} |
|
1458 |
}; |
|
1459 |
} |
|
1460 |
return newFolderAction; |
|
1461 |
} |
|
1462 |
||
1463 |
protected class FileRenderer extends DefaultListCellRenderer { |
|
1464 |
||
1465 |
public Component getListCellRendererComponent(JList list, Object value, |
|
1466 |
int index, boolean isSelected, |
|
1467 |
boolean cellHasFocus) { |
|
1468 |
||
1469 |
if (listViewWindowsStyle && !list.isFocusOwner()) { |
|
1470 |
isSelected = false; |
|
1471 |
} |
|
1472 |
||
1473 |
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
|
1474 |
File file = (File) value; |
|
1475 |
String fileName = getFileChooser().getName(file); |
|
1476 |
setText(fileName); |
|
1477 |
setFont(list.getFont()); |
|
1478 |
||
1479 |
Icon icon = getFileChooser().getIcon(file); |
|
1480 |
if (icon != null) { |
|
1481 |
setIcon(icon); |
|
1482 |
} else { |
|
1483 |
if (getFileChooser().getFileSystemView().isTraversable(file)) { |
|
1484 |
setText(fileName+File.separator); |
|
1485 |
} |
|
1486 |
} |
|
1487 |
||
1488 |
return this; |
|
1489 |
} |
|
1490 |
} |
|
1491 |
||
1492 |
||
1493 |
void setFileSelected() { |
|
1494 |
if (getFileChooser().isMultiSelectionEnabled() && !isDirectorySelected()) { |
|
1495 |
File[] files = getFileChooser().getSelectedFiles(); // Should be selected |
|
1496 |
Object[] selectedObjects = list.getSelectedValues(); // Are actually selected |
|
1497 |
||
1498 |
listSelectionModel.setValueIsAdjusting(true); |
|
1499 |
try { |
|
1500 |
int lead = listSelectionModel.getLeadSelectionIndex(); |
|
1501 |
int anchor = listSelectionModel.getAnchorSelectionIndex(); |
|
1502 |
||
1503 |
Arrays.sort(files); |
|
1504 |
Arrays.sort(selectedObjects); |
|
1505 |
||
1506 |
int shouldIndex = 0; |
|
1507 |
int actuallyIndex = 0; |
|
1508 |
||
1509 |
// Remove files that shouldn't be selected and add files which should be selected |
|
1510 |
// Note: Assume files are already sorted in compareTo order. |
|
1511 |
while (shouldIndex < files.length && |
|
1512 |
actuallyIndex < selectedObjects.length) { |
|
1513 |
int comparison = files[shouldIndex].compareTo((File)selectedObjects[actuallyIndex]); |
|
1514 |
if (comparison < 0) { |
|
1515 |
doSelectFile(files[shouldIndex++]); |
|
1516 |
} else if (comparison > 0) { |
|
1517 |
doDeselectFile(selectedObjects[actuallyIndex++]); |
|
1518 |
} else { |
|
1519 |
// Do nothing |
|
1520 |
shouldIndex++; |
|
1521 |
actuallyIndex++; |
|
1522 |
} |
|
1523 |
||
1524 |
} |
|
1525 |
||
1526 |
while (shouldIndex < files.length) { |
|
1527 |
doSelectFile(files[shouldIndex++]); |
|
1528 |
} |
|
1529 |
||
1530 |
while (actuallyIndex < selectedObjects.length) { |
|
1531 |
doDeselectFile(selectedObjects[actuallyIndex++]); |
|
1532 |
} |
|
1533 |
||
1534 |
// restore the anchor and lead |
|
1535 |
if (listSelectionModel instanceof DefaultListSelectionModel) { |
|
1536 |
((DefaultListSelectionModel)listSelectionModel). |
|
1537 |
moveLeadSelectionIndex(lead); |
|
1538 |
listSelectionModel.setAnchorSelectionIndex(anchor); |
|
1539 |
} |
|
1540 |
} finally { |
|
1541 |
listSelectionModel.setValueIsAdjusting(false); |
|
1542 |
} |
|
1543 |
} else { |
|
1544 |
JFileChooser chooser = getFileChooser(); |
|
1545 |
File f; |
|
1546 |
if (isDirectorySelected()) { |
|
1547 |
f = getDirectory(); |
|
1548 |
} else { |
|
1549 |
f = chooser.getSelectedFile(); |
|
1550 |
} |
|
1551 |
int i; |
|
1552 |
if (f != null && (i = getModel().indexOf(f)) >= 0) { |
|
1553 |
int viewIndex = getRowSorter().convertRowIndexToView(i); |
|
1554 |
listSelectionModel.setSelectionInterval(viewIndex, viewIndex); |
|
1555 |
ensureIndexIsVisible(viewIndex); |
|
1556 |
} else { |
|
1557 |
clearSelection(); |
|
1558 |
} |
|
1559 |
} |
|
1560 |
} |
|
1561 |
||
1562 |
private void doSelectFile(File fileToSelect) { |
|
1563 |
int index = getModel().indexOf(fileToSelect); |
|
1564 |
// could be missed in the current directory if it changed |
|
1565 |
if (index >= 0) { |
|
1566 |
index = getRowSorter().convertRowIndexToView(index); |
|
1567 |
listSelectionModel.addSelectionInterval(index, index); |
|
1568 |
} |
|
1569 |
} |
|
1570 |
||
1571 |
private void doDeselectFile(Object fileToDeselect) { |
|
1572 |
int index = getRowSorter().convertRowIndexToView( |
|
1573 |
getModel().indexOf(fileToDeselect)); |
|
1574 |
listSelectionModel.removeSelectionInterval(index, index); |
|
1575 |
} |
|
1576 |
||
1577 |
/* The following methods are used by the PropertyChange Listener */ |
|
1578 |
||
1579 |
private void doSelectedFileChanged(PropertyChangeEvent e) { |
|
1580 |
applyEdit(); |
|
1581 |
File f = (File) e.getNewValue(); |
|
1582 |
JFileChooser fc = getFileChooser(); |
|
1583 |
if (f != null |
|
1584 |
&& ((fc.isFileSelectionEnabled() && !f.isDirectory()) |
|
1585 |
|| (f.isDirectory() && fc.isDirectorySelectionEnabled()))) { |
|
1586 |
||
1587 |
setFileSelected(); |
|
1588 |
} |
|
1589 |
} |
|
1590 |
||
1591 |
private void doSelectedFilesChanged(PropertyChangeEvent e) { |
|
1592 |
applyEdit(); |
|
1593 |
File[] files = (File[]) e.getNewValue(); |
|
1594 |
JFileChooser fc = getFileChooser(); |
|
1595 |
if (files != null |
|
1596 |
&& files.length > 0 |
|
1597 |
&& (files.length > 1 || fc.isDirectorySelectionEnabled() || !files[0].isDirectory())) { |
|
1598 |
setFileSelected(); |
|
1599 |
} |
|
1600 |
} |
|
1601 |
||
1602 |
private void doDirectoryChanged(PropertyChangeEvent e) { |
|
1603 |
getDetailsTableModel().updateColumnInfo(); |
|
1604 |
||
1605 |
JFileChooser fc = getFileChooser(); |
|
1606 |
FileSystemView fsv = fc.getFileSystemView(); |
|
1607 |
||
1608 |
applyEdit(); |
|
1609 |
resetEditIndex(); |
|
1610 |
ensureIndexIsVisible(0); |
|
1611 |
File currentDirectory = fc.getCurrentDirectory(); |
|
1612 |
if (currentDirectory != null) { |
|
1613 |
if (!readOnly) { |
|
1614 |
getNewFolderAction().setEnabled(canWrite(currentDirectory)); |
|
1615 |
} |
|
1616 |
fileChooserUIAccessor.getChangeToParentDirectoryAction().setEnabled(!fsv.isRoot(currentDirectory)); |
|
1617 |
} |
|
1618 |
if (list != null) { |
|
1619 |
list.clearSelection(); |
|
1620 |
} |
|
1621 |
} |
|
1622 |
||
1623 |
private void doFilterChanged(PropertyChangeEvent e) { |
|
1624 |
applyEdit(); |
|
1625 |
resetEditIndex(); |
|
1626 |
clearSelection(); |
|
1627 |
} |
|
1628 |
||
1629 |
private void doFileSelectionModeChanged(PropertyChangeEvent e) { |
|
1630 |
applyEdit(); |
|
1631 |
resetEditIndex(); |
|
1632 |
clearSelection(); |
|
1633 |
} |
|
1634 |
||
1635 |
private void doMultiSelectionChanged(PropertyChangeEvent e) { |
|
1636 |
if (getFileChooser().isMultiSelectionEnabled()) { |
|
1637 |
listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); |
|
1638 |
} else { |
|
1639 |
listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
|
1640 |
clearSelection(); |
|
1641 |
getFileChooser().setSelectedFiles(null); |
|
1642 |
} |
|
1643 |
} |
|
1644 |
||
1645 |
/* |
|
1646 |
* Listen for filechooser property changes, such as |
|
1647 |
* the selected file changing, or the type of the dialog changing. |
|
1648 |
*/ |
|
1649 |
public void propertyChange(PropertyChangeEvent e) { |
|
1650 |
if (viewType == -1) { |
|
1651 |
setViewType(VIEWTYPE_LIST); |
|
1652 |
} |
|
1653 |
||
1654 |
String s = e.getPropertyName(); |
|
1655 |
if (s.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) { |
|
1656 |
doSelectedFileChanged(e); |
|
1657 |
} else if (s.equals(JFileChooser.SELECTED_FILES_CHANGED_PROPERTY)) { |
|
1658 |
doSelectedFilesChanged(e); |
|
1659 |
} else if (s.equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)) { |
|
1660 |
doDirectoryChanged(e); |
|
1661 |
} else if (s.equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY)) { |
|
1662 |
doFilterChanged(e); |
|
1663 |
} else if (s.equals(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY)) { |
|
1664 |
doFileSelectionModeChanged(e); |
|
1665 |
} else if (s.equals(JFileChooser.MULTI_SELECTION_ENABLED_CHANGED_PROPERTY)) { |
|
1666 |
doMultiSelectionChanged(e); |
|
1667 |
} else if (s.equals(JFileChooser.CANCEL_SELECTION)) { |
|
1668 |
applyEdit(); |
|
1669 |
} else if (s.equals("busy")) { |
|
1670 |
setCursor((Boolean)e.getNewValue() ? waitCursor : null); |
|
1671 |
} else if (s.equals("componentOrientation")) { |
|
1672 |
ComponentOrientation o = (ComponentOrientation)e.getNewValue(); |
|
1673 |
JFileChooser cc = (JFileChooser)e.getSource(); |
|
1674 |
if (o != e.getOldValue()) { |
|
1675 |
cc.applyComponentOrientation(o); |
|
1676 |
} |
|
1677 |
if (detailsTable != null) { |
|
1678 |
detailsTable.setComponentOrientation(o); |
|
1679 |
detailsTable.getParent().getParent().setComponentOrientation(o); |
|
1680 |
} |
|
1681 |
} |
|
1682 |
} |
|
1683 |
||
1684 |
private void ensureIndexIsVisible(int i) { |
|
1685 |
if (i >= 0) { |
|
1686 |
if (list != null) { |
|
1687 |
list.ensureIndexIsVisible(i); |
|
1688 |
} |
|
1689 |
if (detailsTable != null) { |
|
1690 |
detailsTable.scrollRectToVisible(detailsTable.getCellRect(i, COLUMN_FILENAME, true)); |
|
1691 |
} |
|
1692 |
} |
|
1693 |
} |
|
1694 |
||
1695 |
public void ensureFileIsVisible(JFileChooser fc, File f) { |
|
1696 |
int modelIndex = getModel().indexOf(f); |
|
1697 |
if (modelIndex >= 0) { |
|
1698 |
ensureIndexIsVisible(getRowSorter().convertRowIndexToView(modelIndex)); |
|
1699 |
} |
|
1700 |
} |
|
1701 |
||
1702 |
public void rescanCurrentDirectory() { |
|
1703 |
getModel().validateFileCache(); |
|
1704 |
} |
|
1705 |
||
1706 |
public void clearSelection() { |
|
1707 |
if (listSelectionModel != null) { |
|
1708 |
listSelectionModel.clearSelection(); |
|
1709 |
if (listSelectionModel instanceof DefaultListSelectionModel) { |
|
1710 |
((DefaultListSelectionModel)listSelectionModel).moveLeadSelectionIndex(0); |
|
1711 |
listSelectionModel.setAnchorSelectionIndex(0); |
|
1712 |
} |
|
1713 |
} |
|
1714 |
} |
|
1715 |
||
1716 |
public JMenu getViewMenu() { |
|
1717 |
if (viewMenu == null) { |
|
1718 |
viewMenu = new JMenu(viewMenuLabelText); |
|
1719 |
ButtonGroup viewButtonGroup = new ButtonGroup(); |
|
1720 |
||
1721 |
for (int i = 0; i < VIEWTYPE_COUNT; i++) { |
|
1722 |
JRadioButtonMenuItem mi = |
|
1723 |
new JRadioButtonMenuItem(new ViewTypeAction(i)); |
|
1724 |
viewButtonGroup.add(mi); |
|
1725 |
viewMenu.add(mi); |
|
1726 |
} |
|
1727 |
updateViewMenu(); |
|
1728 |
} |
|
1729 |
return viewMenu; |
|
1730 |
} |
|
1731 |
||
1732 |
private void updateViewMenu() { |
|
1733 |
if (viewMenu != null) { |
|
1734 |
Component[] comps = viewMenu.getMenuComponents(); |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
1735 |
for (Component comp : comps) { |
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
1736 |
if (comp instanceof JRadioButtonMenuItem) { |
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
715
diff
changeset
|
1737 |
JRadioButtonMenuItem mi = (JRadioButtonMenuItem) comp; |
2 | 1738 |
if (((ViewTypeAction)mi.getAction()).viewType == viewType) { |
1739 |
mi.setSelected(true); |
|
1740 |
} |
|
1741 |
} |
|
1742 |
} |
|
1743 |
} |
|
1744 |
} |
|
1745 |
||
1746 |
public JPopupMenu getComponentPopupMenu() { |
|
1747 |
JPopupMenu popupMenu = getFileChooser().getComponentPopupMenu(); |
|
1748 |
if (popupMenu != null) { |
|
1749 |
return popupMenu; |
|
1750 |
} |
|
1751 |
||
1752 |
JMenu viewMenu = getViewMenu(); |
|
1753 |
if (contextMenu == null) { |
|
1754 |
contextMenu = new JPopupMenu(); |
|
1755 |
if (viewMenu != null) { |
|
1756 |
contextMenu.add(viewMenu); |
|
1757 |
if (listViewWindowsStyle) { |
|
1758 |
contextMenu.addSeparator(); |
|
1759 |
} |
|
1760 |
} |
|
1761 |
ActionMap actionMap = getActionMap(); |
|
1762 |
Action refreshAction = actionMap.get(ACTION_REFRESH); |
|
1763 |
Action newFolderAction = actionMap.get(ACTION_NEW_FOLDER); |
|
1764 |
if (refreshAction != null) { |
|
1765 |
contextMenu.add(refreshAction); |
|
1766 |
if (listViewWindowsStyle && newFolderAction != null) { |
|
1767 |
contextMenu.addSeparator(); |
|
1768 |
} |
|
1769 |
} |
|
1770 |
if (newFolderAction != null) { |
|
1771 |
contextMenu.add(newFolderAction); |
|
1772 |
} |
|
1773 |
} |
|
1774 |
if (viewMenu != null) { |
|
1775 |
viewMenu.getPopupMenu().setInvoker(viewMenu); |
|
1776 |
} |
|
1777 |
return contextMenu; |
|
1778 |
} |
|
1779 |
||
1780 |
||
1781 |
private Handler handler; |
|
1782 |
||
1783 |
protected Handler getMouseHandler() { |
|
1784 |
if (handler == null) { |
|
1785 |
handler = new Handler(); |
|
1786 |
} |
|
1787 |
return handler; |
|
1788 |
} |
|
1789 |
||
1790 |
private class Handler implements MouseListener { |
|
1791 |
private MouseListener doubleClickListener; |
|
1792 |
||
1793 |
public void mouseClicked(MouseEvent evt) { |
|
1794 |
JComponent source = (JComponent)evt.getSource(); |
|
1795 |
||
1796 |
int index; |
|
1797 |
if (source instanceof JList) { |
|
1798 |
index = SwingUtilities2.loc2IndexFileList(list, evt.getPoint()); |
|
1799 |
} else if (source instanceof JTable) { |
|
1800 |
JTable table = (JTable)source; |
|
1801 |
Point p = evt.getPoint(); |
|
1802 |
index = table.rowAtPoint(p); |
|
1803 |
||
2658 | 1804 |
boolean pointOutsidePrefSize = |
1805 |
SwingUtilities2.pointOutsidePrefSize( |
|
1806 |
table, index, table.columnAtPoint(p), p); |
|
2 | 1807 |
|
2658 | 1808 |
if (pointOutsidePrefSize && !fullRowSelection) { |
2 | 1809 |
return; |
1810 |
} |
|
1811 |
||
1812 |
// Translate point from table to list |
|
1813 |
if (index >= 0 && list != null && |
|
1814 |
listSelectionModel.isSelectedIndex(index)) { |
|
1815 |
||
1816 |
// Make a new event with the list as source, placing the |
|
1817 |
// click in the corresponding list cell. |
|
1818 |
Rectangle r = list.getCellBounds(index, index); |
|
1819 |
evt = new MouseEvent(list, evt.getID(), |
|
1820 |
evt.getWhen(), evt.getModifiers(), |
|
1821 |
r.x + 1, r.y + r.height/2, |
|
1822 |
evt.getXOnScreen(), |
|
1823 |
evt.getYOnScreen(), |
|
1824 |
evt.getClickCount(), evt.isPopupTrigger(), |
|
1825 |
evt.getButton()); |
|
1826 |
} |
|
1827 |
} else { |
|
1828 |
return; |
|
1829 |
} |
|
1830 |
||
1831 |
if (index >= 0 && SwingUtilities.isLeftMouseButton(evt)) { |
|
1832 |
JFileChooser fc = getFileChooser(); |
|
1833 |
||
1834 |
// For single click, we handle editing file name |
|
1835 |
if (evt.getClickCount() == 1 && source instanceof JList) { |
|
1836 |
if ((!fc.isMultiSelectionEnabled() || fc.getSelectedFiles().length <= 1) |
|
1837 |
&& index >= 0 && listSelectionModel.isSelectedIndex(index) |
|
1838 |
&& getEditIndex() == index && editFile == null) { |
|
1839 |
||
1840 |
editFileName(index); |
|
1841 |
} else { |
|
1842 |
if (index >= 0) { |
|
1843 |
setEditIndex(index); |
|
1844 |
} else { |
|
1845 |
resetEditIndex(); |
|
1846 |
} |
|
1847 |
} |
|
1848 |
} else if (evt.getClickCount() == 2) { |
|
1849 |
// on double click (open or drill down one directory) be |
|
1850 |
// sure to clear the edit index |
|
1851 |
resetEditIndex(); |
|
1852 |
} |
|
1853 |
} |
|
1854 |
||
1855 |
// Forward event to Basic |
|
1856 |
if (getDoubleClickListener() != null) { |
|
1857 |
getDoubleClickListener().mouseClicked(evt); |
|
1858 |
} |
|
1859 |
} |
|
1860 |
||
1861 |
public void mouseEntered(MouseEvent evt) { |
|
1862 |
JComponent source = (JComponent)evt.getSource(); |
|
1863 |
if (source instanceof JTable) { |
|
1864 |
JTable table = (JTable)evt.getSource(); |
|
1865 |
||
1866 |
TransferHandler th1 = getFileChooser().getTransferHandler(); |
|
1867 |
TransferHandler th2 = table.getTransferHandler(); |
|
1868 |
if (th1 != th2) { |
|
1869 |
table.setTransferHandler(th1); |
|
1870 |
} |
|
1871 |
||
1872 |
boolean dragEnabled = getFileChooser().getDragEnabled(); |
|
1873 |
if (dragEnabled != table.getDragEnabled()) { |
|
1874 |
table.setDragEnabled(dragEnabled); |
|
1875 |
} |
|
1876 |
} else if (source instanceof JList) { |
|
1877 |
// Forward event to Basic |
|
1878 |
if (getDoubleClickListener() != null) { |
|
1879 |
getDoubleClickListener().mouseEntered(evt); |
|
1880 |
} |
|
1881 |
} |
|
1882 |
} |
|
1883 |
||
1884 |
public void mouseExited(MouseEvent evt) { |
|
1885 |
if (evt.getSource() instanceof JList) { |
|
1886 |
// Forward event to Basic |
|
1887 |
if (getDoubleClickListener() != null) { |
|
1888 |
getDoubleClickListener().mouseExited(evt); |
|
1889 |
} |
|
1890 |
} |
|
1891 |
} |
|
1892 |
||
1893 |
public void mousePressed(MouseEvent evt) { |
|
1894 |
if (evt.getSource() instanceof JList) { |
|
1895 |
// Forward event to Basic |
|
1896 |
if (getDoubleClickListener() != null) { |
|
1897 |
getDoubleClickListener().mousePressed(evt); |
|
1898 |
} |
|
1899 |
} |
|
1900 |
} |
|
1901 |
||
1902 |
public void mouseReleased(MouseEvent evt) { |
|
1903 |
if (evt.getSource() instanceof JList) { |
|
1904 |
// Forward event to Basic |
|
1905 |
if (getDoubleClickListener() != null) { |
|
1906 |
getDoubleClickListener().mouseReleased(evt); |
|
1907 |
} |
|
1908 |
} |
|
1909 |
} |
|
1910 |
||
1911 |
private MouseListener getDoubleClickListener() { |
|
1912 |
// Lazy creation of Basic's listener |
|
1913 |
if (doubleClickListener == null && list != null) { |
|
1914 |
doubleClickListener = |
|
1915 |
fileChooserUIAccessor.createDoubleClickListener(list); |
|
1916 |
} |
|
1917 |
return doubleClickListener; |
|
1918 |
} |
|
1919 |
} |
|
1920 |
||
1921 |
/** |
|
1922 |
* Property to remember whether a directory is currently selected in the UI. |
|
1923 |
* |
|
1924 |
* @return <code>true</code> iff a directory is currently selected. |
|
1925 |
*/ |
|
1926 |
protected boolean isDirectorySelected() { |
|
1927 |
return fileChooserUIAccessor.isDirectorySelected(); |
|
1928 |
} |
|
1929 |
||
1930 |
||
1931 |
/** |
|
1932 |
* Property to remember the directory that is currently selected in the UI. |
|
1933 |
* |
|
1934 |
* @return the value of the <code>directory</code> property |
|
1935 |
* @see javax.swing.plaf.basic.BasicFileChooserUI#setDirectory |
|
1936 |
*/ |
|
1937 |
protected File getDirectory() { |
|
1938 |
return fileChooserUIAccessor.getDirectory(); |
|
1939 |
} |
|
1940 |
||
1941 |
private Component findChildComponent(Container container, Class cls) { |
|
1942 |
int n = container.getComponentCount(); |
|
1943 |
for (int i = 0; i < n; i++) { |
|
1944 |
Component comp = container.getComponent(i); |
|
1945 |
if (cls.isInstance(comp)) { |
|
1946 |
return comp; |
|
1947 |
} else if (comp instanceof Container) { |
|
1948 |
Component c = findChildComponent((Container)comp, cls); |
|
1949 |
if (c != null) { |
|
1950 |
return c; |
|
1951 |
} |
|
1952 |
} |
|
1953 |
} |
|
1954 |
return null; |
|
1955 |
} |
|
1956 |
||
1957 |
public boolean canWrite(File f) { |
|
1958 |
// Return false for non FileSystem files or if file doesn't exist. |
|
1959 |
if (!f.exists()) { |
|
1960 |
return false; |
|
1961 |
} |
|
1962 |
||
1963 |
if (f instanceof ShellFolder) { |
|
1964 |
return ((ShellFolder) f).isFileSystem(); |
|
1965 |
} else { |
|
1842
824c7f885a64
6776856: Code with useShellFolder field shuold be simplify
rupashka
parents:
1840
diff
changeset
|
1966 |
if (usesShellFolder(getFileChooser())) { |
2 | 1967 |
try { |
1968 |
return ShellFolder.getShellFolder(f).isFileSystem(); |
|
1969 |
} catch (FileNotFoundException ex) { |
|
1970 |
// File doesn't exist |
|
1971 |
return false; |
|
1972 |
} |
|
1973 |
} else { |
|
1974 |
// Ordinary file |
|
1975 |
return true; |
|
1976 |
} |
|
1977 |
} |
|
1978 |
} |
|
1979 |
||
1840
984ba55ac827
6698013: JFileChooser can no longer navigate non-local file systems.
rupashka
parents:
1305
diff
changeset
|
1980 |
/** |
984ba55ac827
6698013: JFileChooser can no longer navigate non-local file systems.
rupashka
parents:
1305
diff
changeset
|
1981 |
* Returns true if specified FileChooser should use ShellFolder |
984ba55ac827
6698013: JFileChooser can no longer navigate non-local file systems.
rupashka
parents:
1305
diff
changeset
|
1982 |
*/ |
984ba55ac827
6698013: JFileChooser can no longer navigate non-local file systems.
rupashka
parents:
1305
diff
changeset
|
1983 |
public static boolean usesShellFolder(JFileChooser chooser) { |
984ba55ac827
6698013: JFileChooser can no longer navigate non-local file systems.
rupashka
parents:
1305
diff
changeset
|
1984 |
Boolean prop = (Boolean) chooser.getClientProperty("FileChooser.useShellFolder"); |
984ba55ac827
6698013: JFileChooser can no longer navigate non-local file systems.
rupashka
parents:
1305
diff
changeset
|
1985 |
|
984ba55ac827
6698013: JFileChooser can no longer navigate non-local file systems.
rupashka
parents:
1305
diff
changeset
|
1986 |
return prop == null ? chooser.getFileSystemView().equals(FileSystemView.getFileSystemView()) |
984ba55ac827
6698013: JFileChooser can no longer navigate non-local file systems.
rupashka
parents:
1305
diff
changeset
|
1987 |
: prop.booleanValue(); |
984ba55ac827
6698013: JFileChooser can no longer navigate non-local file systems.
rupashka
parents:
1305
diff
changeset
|
1988 |
} |
984ba55ac827
6698013: JFileChooser can no longer navigate non-local file systems.
rupashka
parents:
1305
diff
changeset
|
1989 |
|
2 | 1990 |
// This interface is used to access methods in the FileChooserUI |
1991 |
// that are not public. |
|
1992 |
public interface FileChooserUIAccessor { |
|
1993 |
public JFileChooser getFileChooser(); |
|
1994 |
public BasicDirectoryModel getModel(); |
|
1995 |
public JPanel createList(); |
|
1996 |
public JPanel createDetailsView(); |
|
1997 |
public boolean isDirectorySelected(); |
|
1998 |
public File getDirectory(); |
|
1999 |
public Action getApproveSelectionAction(); |
|
2000 |
public Action getChangeToParentDirectoryAction(); |
|
2001 |
public Action getNewFolderAction(); |
|
2002 |
public MouseListener createDoubleClickListener(JList list); |
|
2003 |
public ListSelectionListener createListSelectionListener(); |
|
2004 |
} |
|
2005 |
} |