author | lana |
Thu, 29 Jan 2009 09:25:47 -0800 | |
changeset 1853 | 63c1129add13 |
parent 1639 | a97859015238 |
parent 1842 | 824c7f885a64 |
child 2491 | 123954ad0034 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1639 | 2 |
* Copyright 2003-2008 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.plaf.synth; |
|
26 |
||
27 |
import java.awt.*; |
|
28 |
import java.awt.event.*; |
|
29 |
import java.beans.*; |
|
30 |
import java.io.*; |
|
31 |
import java.util.*; |
|
32 |
||
33 |
import javax.swing.*; |
|
34 |
import javax.swing.event.*; |
|
35 |
import javax.swing.filechooser.*; |
|
36 |
import javax.swing.filechooser.FileFilter; |
|
37 |
import javax.swing.plaf.basic.*; |
|
38 |
import javax.swing.plaf.synth.*; |
|
39 |
||
40 |
import sun.swing.SwingUtilities2; |
|
41 |
||
42 |
import sun.awt.shell.ShellFolder; |
|
43 |
import sun.swing.*; |
|
44 |
||
45 |
/** |
|
46 |
* Synth FileChooserUI implementation. |
|
47 |
* <p> |
|
48 |
* Note that the classes in the com.sun.java.swing.plaf.synth |
|
49 |
* package are not |
|
50 |
* part of the core Java APIs. They are a part of Sun's JDK and JRE |
|
51 |
* distributions. Although other licensees may choose to distribute |
|
52 |
* these classes, developers cannot depend on their availability in |
|
53 |
* non-Sun implementations. Additionally this API may change in |
|
54 |
* incompatible ways between releases. While this class is public, it |
|
55 |
* shoud be considered an implementation detail, and subject to change. |
|
56 |
* |
|
57 |
* @author Leif Samuelsson |
|
58 |
* @author Jeff Dinkins |
|
59 |
*/ |
|
60 |
public class SynthFileChooserUIImpl extends SynthFileChooserUI { |
|
61 |
private JLabel lookInLabel; |
|
62 |
private JComboBox directoryComboBox; |
|
63 |
private DirectoryComboBoxModel directoryComboBoxModel; |
|
64 |
private Action directoryComboBoxAction = new DirectoryComboBoxAction(); |
|
65 |
||
66 |
private FilterComboBoxModel filterComboBoxModel; |
|
67 |
||
68 |
private JTextField fileNameTextField; |
|
69 |
||
70 |
private FilePane filePane; |
|
71 |
private JToggleButton listViewButton; |
|
72 |
private JToggleButton detailsViewButton; |
|
73 |
||
74 |
private boolean readOnly; |
|
75 |
||
76 |
private JPanel buttonPanel; |
|
77 |
private JPanel bottomPanel; |
|
78 |
||
79 |
private JComboBox filterComboBox; |
|
80 |
||
81 |
private static final Dimension hstrut5 = new Dimension(5, 1); |
|
82 |
private static final Dimension vstrut5 = new Dimension(1, 5); |
|
83 |
||
84 |
private static final Insets shrinkwrap = new Insets(0,0,0,0); |
|
85 |
||
86 |
// Preferred and Minimum sizes for the dialog box |
|
87 |
private static Dimension LIST_PREF_SIZE = new Dimension(405, 135); |
|
88 |
||
89 |
// Labels, mnemonics, and tooltips (oh my!) |
|
90 |
private int lookInLabelMnemonic = 0; |
|
91 |
private String lookInLabelText = null; |
|
92 |
private String saveInLabelText = null; |
|
93 |
||
94 |
private int fileNameLabelMnemonic = 0; |
|
95 |
private String fileNameLabelText = null; |
|
96 |
private int folderNameLabelMnemonic = 0; |
|
97 |
private String folderNameLabelText = null; |
|
98 |
||
99 |
private int filesOfTypeLabelMnemonic = 0; |
|
100 |
private String filesOfTypeLabelText = null; |
|
101 |
||
102 |
private String upFolderToolTipText = null; |
|
103 |
private String upFolderAccessibleName = null; |
|
104 |
||
105 |
private String homeFolderToolTipText = null; |
|
106 |
private String homeFolderAccessibleName = null; |
|
107 |
||
108 |
private String newFolderToolTipText = null; |
|
109 |
private String newFolderAccessibleName = null; |
|
110 |
||
111 |
private String listViewButtonToolTipText = null; |
|
112 |
private String listViewButtonAccessibleName = null; |
|
113 |
||
114 |
private String detailsViewButtonToolTipText = null; |
|
115 |
private String detailsViewButtonAccessibleName = null; |
|
116 |
||
117 |
private AlignedLabel fileNameLabel; |
|
118 |
private final PropertyChangeListener modeListener = new PropertyChangeListener() { |
|
119 |
public void propertyChange(PropertyChangeEvent event) { |
|
120 |
if (fileNameLabel != null) { |
|
121 |
populateFileNameLabel(); |
|
122 |
} |
|
123 |
} |
|
124 |
}; |
|
125 |
||
126 |
private void populateFileNameLabel() { |
|
127 |
if (getFileChooser().getFileSelectionMode() == JFileChooser.DIRECTORIES_ONLY) { |
|
128 |
fileNameLabel.setText(folderNameLabelText); |
|
129 |
fileNameLabel.setDisplayedMnemonic(folderNameLabelMnemonic); |
|
130 |
} else { |
|
131 |
fileNameLabel.setText(fileNameLabelText); |
|
132 |
fileNameLabel.setDisplayedMnemonic(fileNameLabelMnemonic); |
|
133 |
} |
|
134 |
} |
|
135 |
||
136 |
public SynthFileChooserUIImpl(JFileChooser b) { |
|
137 |
super(b); |
|
138 |
} |
|
139 |
||
140 |
||
141 |
private class SynthFileChooserUIAccessor implements FilePane.FileChooserUIAccessor { |
|
142 |
public JFileChooser getFileChooser() { |
|
143 |
return SynthFileChooserUIImpl.this.getFileChooser(); |
|
144 |
} |
|
145 |
||
146 |
public BasicDirectoryModel getModel() { |
|
147 |
return SynthFileChooserUIImpl.this.getModel(); |
|
148 |
} |
|
149 |
||
150 |
public JPanel createList() { |
|
151 |
return null; |
|
152 |
} |
|
153 |
||
154 |
public JPanel createDetailsView() { |
|
155 |
return null; |
|
156 |
} |
|
157 |
||
158 |
public boolean isDirectorySelected() { |
|
159 |
return SynthFileChooserUIImpl.this.isDirectorySelected(); |
|
160 |
} |
|
161 |
||
162 |
public File getDirectory() { |
|
163 |
return SynthFileChooserUIImpl.this.getDirectory(); |
|
164 |
} |
|
165 |
||
166 |
public Action getChangeToParentDirectoryAction() { |
|
167 |
return SynthFileChooserUIImpl.this.getChangeToParentDirectoryAction(); |
|
168 |
} |
|
169 |
||
170 |
public Action getApproveSelectionAction() { |
|
171 |
return SynthFileChooserUIImpl.this.getApproveSelectionAction(); |
|
172 |
} |
|
173 |
||
174 |
public Action getNewFolderAction() { |
|
175 |
return SynthFileChooserUIImpl.this.getNewFolderAction(); |
|
176 |
} |
|
177 |
||
178 |
public MouseListener createDoubleClickListener(JList list) { |
|
179 |
return SynthFileChooserUIImpl.this.createDoubleClickListener(getFileChooser(), |
|
180 |
list); |
|
181 |
} |
|
182 |
||
183 |
public ListSelectionListener createListSelectionListener() { |
|
184 |
return SynthFileChooserUIImpl.this.createListSelectionListener(getFileChooser()); |
|
185 |
} |
|
186 |
} |
|
187 |
||
188 |
protected void installDefaults(JFileChooser fc) { |
|
189 |
super.installDefaults(fc); |
|
190 |
readOnly = UIManager.getBoolean("FileChooser.readOnly"); |
|
191 |
} |
|
192 |
||
193 |
public void installComponents(JFileChooser fc) { |
|
194 |
super.installComponents(fc); |
|
195 |
||
196 |
SynthContext context = getContext(fc, ENABLED); |
|
197 |
||
198 |
fc.setLayout(new BorderLayout(0, 11)); |
|
199 |
||
200 |
// ********************************* // |
|
201 |
// **** Construct the top panel **** // |
|
202 |
// ********************************* // |
|
203 |
||
204 |
// Directory manipulation buttons |
|
205 |
JPanel topPanel = new JPanel(new BorderLayout(11, 0)); |
|
206 |
JPanel topButtonPanel = new JPanel(); |
|
207 |
topButtonPanel.setLayout(new BoxLayout(topButtonPanel, BoxLayout.LINE_AXIS)); |
|
208 |
topPanel.add(topButtonPanel, BorderLayout.AFTER_LINE_ENDS); |
|
209 |
||
210 |
// Add the top panel to the fileChooser |
|
211 |
fc.add(topPanel, BorderLayout.NORTH); |
|
212 |
||
213 |
// ComboBox Label |
|
214 |
lookInLabel = new JLabel(lookInLabelText); |
|
215 |
lookInLabel.setDisplayedMnemonic(lookInLabelMnemonic); |
|
216 |
topPanel.add(lookInLabel, BorderLayout.BEFORE_LINE_BEGINS); |
|
217 |
||
218 |
// CurrentDir ComboBox |
|
219 |
directoryComboBox = new JComboBox(); |
|
220 |
directoryComboBox.getAccessibleContext().setAccessibleDescription(lookInLabelText); |
|
221 |
directoryComboBox.putClientProperty( "JComboBox.isTableCellEditor", Boolean.TRUE ); |
|
222 |
lookInLabel.setLabelFor(directoryComboBox); |
|
223 |
directoryComboBoxModel = createDirectoryComboBoxModel(fc); |
|
224 |
directoryComboBox.setModel(directoryComboBoxModel); |
|
225 |
directoryComboBox.addActionListener(directoryComboBoxAction); |
|
226 |
directoryComboBox.setRenderer(createDirectoryComboBoxRenderer(fc)); |
|
227 |
directoryComboBox.setAlignmentX(JComponent.LEFT_ALIGNMENT); |
|
228 |
directoryComboBox.setAlignmentY(JComponent.TOP_ALIGNMENT); |
|
229 |
directoryComboBox.setMaximumRowCount(8); |
|
230 |
topPanel.add(directoryComboBox, BorderLayout.CENTER); |
|
231 |
||
232 |
filePane = new FilePane(new SynthFileChooserUIAccessor()); |
|
233 |
fc.addPropertyChangeListener(filePane); |
|
234 |
||
235 |
// Add 'Go Up' to context menu, plus 'Go Home' if on Unix |
|
236 |
JPopupMenu contextMenu = filePane.getComponentPopupMenu(); |
|
237 |
if (contextMenu != null) { |
|
238 |
contextMenu.insert(getChangeToParentDirectoryAction(), 0); |
|
239 |
if (File.separatorChar == '/') { |
|
240 |
contextMenu.insert(getGoHomeAction(), 1); |
|
241 |
} |
|
242 |
} |
|
243 |
||
244 |
FileSystemView fsv = fc.getFileSystemView(); |
|
245 |
||
246 |
// Up Button |
|
247 |
JButton upFolderButton = new JButton(getChangeToParentDirectoryAction()); |
|
248 |
upFolderButton.setText(null); |
|
249 |
upFolderButton.setIcon(upFolderIcon); |
|
250 |
upFolderButton.setToolTipText(upFolderToolTipText); |
|
251 |
upFolderButton.getAccessibleContext().setAccessibleName(upFolderAccessibleName); |
|
252 |
upFolderButton.setAlignmentX(JComponent.LEFT_ALIGNMENT); |
|
253 |
upFolderButton.setAlignmentY(JComponent.CENTER_ALIGNMENT); |
|
254 |
upFolderButton.setMargin(shrinkwrap); |
|
255 |
||
256 |
topButtonPanel.add(upFolderButton); |
|
257 |
topButtonPanel.add(Box.createRigidArea(hstrut5)); |
|
258 |
||
259 |
// Home Button |
|
260 |
File homeDir = fsv.getHomeDirectory(); |
|
261 |
String toolTipText = homeFolderToolTipText; |
|
262 |
if (fsv.isRoot(homeDir)) { |
|
263 |
toolTipText = getFileView(fc).getName(homeDir); // Probably "Desktop". |
|
264 |
} |
|
265 |
||
266 |
JButton b = new JButton(homeFolderIcon); |
|
267 |
b.setToolTipText(toolTipText); |
|
268 |
b.getAccessibleContext().setAccessibleName(homeFolderAccessibleName); |
|
269 |
b.setAlignmentX(JComponent.LEFT_ALIGNMENT); |
|
270 |
b.setAlignmentY(JComponent.CENTER_ALIGNMENT); |
|
271 |
b.setMargin(shrinkwrap); |
|
272 |
||
273 |
b.addActionListener(getGoHomeAction()); |
|
274 |
topButtonPanel.add(b); |
|
275 |
topButtonPanel.add(Box.createRigidArea(hstrut5)); |
|
276 |
||
277 |
// New Directory Button |
|
278 |
if (!readOnly) { |
|
279 |
b = new JButton(filePane.getNewFolderAction()); |
|
280 |
b.setText(null); |
|
281 |
b.setIcon(newFolderIcon); |
|
282 |
b.setToolTipText(newFolderToolTipText); |
|
283 |
b.getAccessibleContext().setAccessibleName(newFolderAccessibleName); |
|
284 |
b.setAlignmentX(JComponent.LEFT_ALIGNMENT); |
|
285 |
b.setAlignmentY(JComponent.CENTER_ALIGNMENT); |
|
286 |
b.setMargin(shrinkwrap); |
|
287 |
} |
|
288 |
topButtonPanel.add(b); |
|
289 |
topButtonPanel.add(Box.createRigidArea(hstrut5)); |
|
290 |
||
291 |
// View button group |
|
292 |
ButtonGroup viewButtonGroup = new ButtonGroup(); |
|
293 |
||
294 |
// List Button |
|
295 |
listViewButton = new JToggleButton(listViewIcon); |
|
296 |
listViewButton.setToolTipText(listViewButtonToolTipText); |
|
297 |
listViewButton.getAccessibleContext().setAccessibleName(listViewButtonAccessibleName); |
|
298 |
listViewButton.setSelected(true); |
|
299 |
listViewButton.setAlignmentX(JComponent.LEFT_ALIGNMENT); |
|
300 |
listViewButton.setAlignmentY(JComponent.CENTER_ALIGNMENT); |
|
301 |
listViewButton.setMargin(shrinkwrap); |
|
302 |
listViewButton.addActionListener(filePane.getViewTypeAction(FilePane.VIEWTYPE_LIST)); |
|
303 |
topButtonPanel.add(listViewButton); |
|
304 |
viewButtonGroup.add(listViewButton); |
|
305 |
||
306 |
// Details Button |
|
307 |
detailsViewButton = new JToggleButton(detailsViewIcon); |
|
308 |
detailsViewButton.setToolTipText(detailsViewButtonToolTipText); |
|
309 |
detailsViewButton.getAccessibleContext().setAccessibleName(detailsViewButtonAccessibleName); |
|
310 |
detailsViewButton.setAlignmentX(JComponent.LEFT_ALIGNMENT); |
|
311 |
detailsViewButton.setAlignmentY(JComponent.CENTER_ALIGNMENT); |
|
312 |
detailsViewButton.setMargin(shrinkwrap); |
|
313 |
detailsViewButton.addActionListener(filePane.getViewTypeAction(FilePane.VIEWTYPE_DETAILS)); |
|
314 |
topButtonPanel.add(detailsViewButton); |
|
315 |
viewButtonGroup.add(detailsViewButton); |
|
316 |
||
317 |
filePane.addPropertyChangeListener(new PropertyChangeListener() { |
|
318 |
public void propertyChange(PropertyChangeEvent e) { |
|
319 |
if ("viewType".equals(e.getPropertyName())) { |
|
320 |
int viewType = filePane.getViewType(); |
|
321 |
switch (viewType) { |
|
322 |
case FilePane.VIEWTYPE_LIST: |
|
323 |
listViewButton.setSelected(true); |
|
324 |
break; |
|
325 |
case FilePane.VIEWTYPE_DETAILS: |
|
326 |
detailsViewButton.setSelected(true); |
|
327 |
break; |
|
328 |
} |
|
329 |
} |
|
330 |
} |
|
331 |
}); |
|
332 |
||
333 |
// ************************************** // |
|
334 |
// ******* Add the directory pane ******* // |
|
335 |
// ************************************** // |
|
336 |
fc.add(getAccessoryPanel(), BorderLayout.AFTER_LINE_ENDS); |
|
337 |
JComponent accessory = fc.getAccessory(); |
|
338 |
if (accessory != null) { |
|
339 |
getAccessoryPanel().add(accessory); |
|
340 |
} |
|
341 |
filePane.setPreferredSize(LIST_PREF_SIZE); |
|
342 |
fc.add(filePane, BorderLayout.CENTER); |
|
343 |
||
344 |
||
345 |
// ********************************** // |
|
346 |
// **** Construct the bottom panel ** // |
|
347 |
// ********************************** // |
|
348 |
bottomPanel = new JPanel(); |
|
349 |
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS)); |
|
350 |
fc.add(bottomPanel, BorderLayout.SOUTH); |
|
351 |
||
352 |
// FileName label and textfield |
|
353 |
JPanel fileNamePanel = new JPanel(); |
|
354 |
fileNamePanel.setLayout(new BoxLayout(fileNamePanel, BoxLayout.LINE_AXIS)); |
|
355 |
bottomPanel.add(fileNamePanel); |
|
356 |
bottomPanel.add(Box.createRigidArea(new Dimension(1, 5))); |
|
357 |
||
358 |
fileNameLabel = new AlignedLabel(); |
|
359 |
populateFileNameLabel(); |
|
360 |
fileNamePanel.add(fileNameLabel); |
|
361 |
||
362 |
fileNameTextField = new JTextField(35) { |
|
363 |
public Dimension getMaximumSize() { |
|
364 |
return new Dimension(Short.MAX_VALUE, super.getPreferredSize().height); |
|
365 |
} |
|
366 |
}; |
|
367 |
fileNamePanel.add(fileNameTextField); |
|
368 |
fileNameLabel.setLabelFor(fileNameTextField); |
|
369 |
fileNameTextField.addFocusListener( |
|
370 |
new FocusAdapter() { |
|
371 |
public void focusGained(FocusEvent e) { |
|
372 |
if (!getFileChooser().isMultiSelectionEnabled()) { |
|
373 |
filePane.clearSelection(); |
|
374 |
} |
|
375 |
} |
|
376 |
} |
|
377 |
); |
|
378 |
if (fc.isMultiSelectionEnabled()) { |
|
379 |
setFileName(fileNameString(fc.getSelectedFiles())); |
|
380 |
} else { |
|
381 |
setFileName(fileNameString(fc.getSelectedFile())); |
|
382 |
} |
|
383 |
||
384 |
||
385 |
// Filetype label and combobox |
|
386 |
JPanel filesOfTypePanel = new JPanel(); |
|
387 |
filesOfTypePanel.setLayout(new BoxLayout(filesOfTypePanel, BoxLayout.LINE_AXIS)); |
|
388 |
bottomPanel.add(filesOfTypePanel); |
|
389 |
||
390 |
AlignedLabel filesOfTypeLabel = new AlignedLabel(filesOfTypeLabelText); |
|
391 |
filesOfTypeLabel.setDisplayedMnemonic(filesOfTypeLabelMnemonic); |
|
392 |
filesOfTypePanel.add(filesOfTypeLabel); |
|
393 |
||
394 |
filterComboBoxModel = createFilterComboBoxModel(); |
|
395 |
fc.addPropertyChangeListener(filterComboBoxModel); |
|
396 |
filterComboBox = new JComboBox(filterComboBoxModel); |
|
397 |
filterComboBox.getAccessibleContext().setAccessibleDescription(filesOfTypeLabelText); |
|
398 |
filesOfTypeLabel.setLabelFor(filterComboBox); |
|
399 |
filterComboBox.setRenderer(createFilterComboBoxRenderer()); |
|
400 |
filesOfTypePanel.add(filterComboBox); |
|
401 |
||
402 |
||
403 |
// buttons |
|
404 |
buttonPanel = new JPanel(); |
|
405 |
buttonPanel.setLayout(new ButtonAreaLayout()); |
|
406 |
||
407 |
buttonPanel.add(getApproveButton(fc)); |
|
408 |
buttonPanel.add(getCancelButton(fc)); |
|
409 |
||
410 |
if (fc.getControlButtonsAreShown()) { |
|
411 |
addControlButtons(); |
|
412 |
} |
|
413 |
||
414 |
groupLabels(new AlignedLabel[] { fileNameLabel, filesOfTypeLabel }); |
|
415 |
} |
|
416 |
||
417 |
protected void installListeners(JFileChooser fc) { |
|
418 |
super.installListeners(fc); |
|
419 |
fc.addPropertyChangeListener(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY, modeListener); |
|
420 |
} |
|
421 |
||
422 |
protected void uninstallListeners(JFileChooser fc) { |
|
423 |
fc.removePropertyChangeListener(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY, modeListener); |
|
424 |
super.uninstallListeners(fc); |
|
425 |
} |
|
426 |
||
427 |
private String fileNameString(File file) { |
|
428 |
if (file == null) { |
|
429 |
return null; |
|
430 |
} else { |
|
431 |
JFileChooser fc = getFileChooser(); |
|
432 |
if (fc.isDirectorySelectionEnabled() && !fc.isFileSelectionEnabled()) { |
|
433 |
return file.getPath(); |
|
434 |
} else { |
|
435 |
return file.getName(); |
|
436 |
} |
|
437 |
} |
|
438 |
} |
|
439 |
||
440 |
private String fileNameString(File[] files) { |
|
441 |
StringBuffer buf = new StringBuffer(); |
|
442 |
for (int i = 0; files != null && i < files.length; i++) { |
|
443 |
if (i > 0) { |
|
444 |
buf.append(" "); |
|
445 |
} |
|
446 |
if (files.length > 1) { |
|
447 |
buf.append("\""); |
|
448 |
} |
|
449 |
buf.append(fileNameString(files[i])); |
|
450 |
if (files.length > 1) { |
|
451 |
buf.append("\""); |
|
452 |
} |
|
453 |
} |
|
454 |
return buf.toString(); |
|
455 |
} |
|
456 |
||
457 |
public void uninstallUI(JComponent c) { |
|
458 |
// Remove listeners |
|
459 |
c.removePropertyChangeListener(filterComboBoxModel); |
|
460 |
c.removePropertyChangeListener(filePane); |
|
461 |
||
462 |
if (filePane != null) { |
|
463 |
filePane.uninstallUI(); |
|
464 |
filePane = null; |
|
465 |
} |
|
466 |
||
467 |
super.uninstallUI(c); |
|
468 |
} |
|
469 |
||
470 |
protected void installStrings(JFileChooser fc) { |
|
471 |
super.installStrings(fc); |
|
472 |
||
473 |
Locale l = fc.getLocale(); |
|
474 |
||
475 |
lookInLabelMnemonic = getMnemonic("FileChooser.lookInLabelMnemonic", l); |
|
476 |
lookInLabelText = UIManager.getString("FileChooser.lookInLabelText", l); |
|
477 |
saveInLabelText = UIManager.getString("FileChooser.saveInLabelText", l); |
|
478 |
||
479 |
fileNameLabelMnemonic = getMnemonic("FileChooser.fileNameLabelMnemonic", l); |
|
480 |
fileNameLabelText = UIManager.getString("FileChooser.fileNameLabelText", l); |
|
481 |
folderNameLabelMnemonic = getMnemonic("FileChooser.folderNameLabelMnemonic", l); |
|
482 |
folderNameLabelText = UIManager.getString("FileChooser.folderNameLabelText", l); |
|
483 |
||
484 |
filesOfTypeLabelMnemonic = getMnemonic("FileChooser.filesOfTypeLabelMnemonic", l); |
|
485 |
filesOfTypeLabelText = UIManager.getString("FileChooser.filesOfTypeLabelText", l); |
|
486 |
||
487 |
upFolderToolTipText = UIManager.getString("FileChooser.upFolderToolTipText",l); |
|
488 |
upFolderAccessibleName = UIManager.getString("FileChooser.upFolderAccessibleName",l); |
|
489 |
||
490 |
homeFolderToolTipText = UIManager.getString("FileChooser.homeFolderToolTipText",l); |
|
491 |
homeFolderAccessibleName = UIManager.getString("FileChooser.homeFolderAccessibleName",l); |
|
492 |
||
493 |
newFolderToolTipText = UIManager.getString("FileChooser.newFolderToolTipText",l); |
|
494 |
newFolderAccessibleName = UIManager.getString("FileChooser.newFolderAccessibleName",l); |
|
495 |
||
496 |
listViewButtonToolTipText = UIManager.getString("FileChooser.listViewButtonToolTipText",l); |
|
497 |
listViewButtonAccessibleName = UIManager.getString("FileChooser.listViewButtonAccessibleName",l); |
|
498 |
||
499 |
detailsViewButtonToolTipText = UIManager.getString("FileChooser.detailsViewButtonToolTipText",l); |
|
500 |
detailsViewButtonAccessibleName = UIManager.getString("FileChooser.detailsViewButtonAccessibleName",l); |
|
501 |
} |
|
502 |
||
503 |
private int getMnemonic(String key, Locale l) { |
|
504 |
return SwingUtilities2.getUIDefaultsInt(key, l); |
|
505 |
} |
|
506 |
||
507 |
||
508 |
public String getFileName() { |
|
509 |
if (fileNameTextField != null) { |
|
510 |
return fileNameTextField.getText(); |
|
511 |
} else { |
|
512 |
return null; |
|
513 |
} |
|
514 |
} |
|
515 |
||
516 |
public void setFileName(String fileName) { |
|
517 |
if (fileNameTextField != null) { |
|
518 |
fileNameTextField.setText(fileName); |
|
519 |
} |
|
520 |
} |
|
521 |
||
522 |
||
523 |
protected void doSelectedFileChanged(PropertyChangeEvent e) { |
|
524 |
super.doSelectedFileChanged(e); |
|
525 |
||
526 |
File f = (File) e.getNewValue(); |
|
527 |
JFileChooser fc = getFileChooser(); |
|
528 |
if (f != null |
|
529 |
&& ((fc.isFileSelectionEnabled() && !f.isDirectory()) |
|
530 |
|| (f.isDirectory() && fc.isDirectorySelectionEnabled()))) { |
|
531 |
||
532 |
setFileName(fileNameString(f)); |
|
533 |
} |
|
534 |
} |
|
535 |
||
536 |
protected void doSelectedFilesChanged(PropertyChangeEvent e) { |
|
537 |
super.doSelectedFilesChanged(e); |
|
538 |
||
539 |
File[] files = (File[]) e.getNewValue(); |
|
540 |
JFileChooser fc = getFileChooser(); |
|
541 |
if (files != null |
|
542 |
&& files.length > 0 |
|
543 |
&& (files.length > 1 || fc.isDirectorySelectionEnabled() || !files[0].isDirectory())) { |
|
544 |
setFileName(fileNameString(files)); |
|
545 |
} |
|
546 |
} |
|
547 |
||
548 |
protected void doDirectoryChanged(PropertyChangeEvent e) { |
|
549 |
super.doDirectoryChanged(e); |
|
550 |
||
551 |
JFileChooser fc = getFileChooser(); |
|
552 |
FileSystemView fsv = fc.getFileSystemView(); |
|
553 |
File currentDirectory = fc.getCurrentDirectory(); |
|
554 |
||
555 |
if (!readOnly && currentDirectory != null) { |
|
556 |
getNewFolderAction().setEnabled(filePane.canWrite(currentDirectory)); |
|
557 |
} |
|
558 |
||
559 |
if (currentDirectory != null) { |
|
560 |
JComponent cb = getDirectoryComboBox(); |
|
561 |
if (cb instanceof JComboBox) { |
|
562 |
ComboBoxModel model = ((JComboBox)cb).getModel(); |
|
563 |
if (model instanceof DirectoryComboBoxModel) { |
|
564 |
((DirectoryComboBoxModel)model).addItem(currentDirectory); |
|
565 |
} |
|
566 |
} |
|
567 |
||
568 |
if (fc.isDirectorySelectionEnabled() && !fc.isFileSelectionEnabled()) { |
|
569 |
if (fsv.isFileSystem(currentDirectory)) { |
|
570 |
setFileName(currentDirectory.getPath()); |
|
571 |
} else { |
|
572 |
setFileName(null); |
|
573 |
} |
|
574 |
} |
|
575 |
} |
|
576 |
} |
|
577 |
||
578 |
||
579 |
protected void doFileSelectionModeChanged(PropertyChangeEvent e) { |
|
580 |
super.doFileSelectionModeChanged(e); |
|
581 |
||
582 |
JFileChooser fc = getFileChooser(); |
|
583 |
File currentDirectory = fc.getCurrentDirectory(); |
|
584 |
if (currentDirectory != null |
|
585 |
&& fc.isDirectorySelectionEnabled() |
|
586 |
&& !fc.isFileSelectionEnabled() |
|
587 |
&& fc.getFileSystemView().isFileSystem(currentDirectory)) { |
|
588 |
||
589 |
setFileName(currentDirectory.getPath()); |
|
590 |
} else { |
|
591 |
setFileName(null); |
|
592 |
} |
|
593 |
} |
|
594 |
||
595 |
protected void doAccessoryChanged(PropertyChangeEvent e) { |
|
596 |
if (getAccessoryPanel() != null) { |
|
597 |
if (e.getOldValue() != null) { |
|
598 |
getAccessoryPanel().remove((JComponent)e.getOldValue()); |
|
599 |
} |
|
600 |
JComponent accessory = (JComponent)e.getNewValue(); |
|
601 |
if (accessory != null) { |
|
602 |
getAccessoryPanel().add(accessory, BorderLayout.CENTER); |
|
603 |
} |
|
604 |
} |
|
605 |
} |
|
606 |
||
607 |
protected void doControlButtonsChanged(PropertyChangeEvent e) { |
|
608 |
super.doControlButtonsChanged(e); |
|
609 |
||
610 |
if (getFileChooser().getControlButtonsAreShown()) { |
|
611 |
addControlButtons(); |
|
612 |
} else { |
|
613 |
removeControlButtons(); |
|
614 |
} |
|
615 |
} |
|
616 |
||
617 |
protected void addControlButtons() { |
|
618 |
if (bottomPanel != null) { |
|
619 |
bottomPanel.add(buttonPanel); |
|
620 |
} |
|
621 |
} |
|
622 |
||
623 |
protected void removeControlButtons() { |
|
624 |
if (bottomPanel != null) { |
|
625 |
bottomPanel.remove(buttonPanel); |
|
626 |
} |
|
627 |
} |
|
628 |
||
629 |
||
630 |
||
631 |
||
632 |
// ******************************************************* |
|
633 |
// ************ FileChooser UI PLAF methods ************** |
|
634 |
// ******************************************************* |
|
635 |
||
636 |
||
637 |
// ***************************** |
|
638 |
// ***** Directory Actions ***** |
|
639 |
// ***************************** |
|
640 |
||
641 |
protected JComponent getDirectoryComboBox() { |
|
642 |
return directoryComboBox; |
|
643 |
} |
|
644 |
||
645 |
protected Action getDirectoryComboBoxAction() { |
|
646 |
return directoryComboBoxAction; |
|
647 |
} |
|
648 |
||
649 |
protected DirectoryComboBoxRenderer createDirectoryComboBoxRenderer(JFileChooser fc) { |
|
650 |
return new DirectoryComboBoxRenderer(); |
|
651 |
} |
|
652 |
||
653 |
// |
|
654 |
// Renderer for DirectoryComboBox |
|
655 |
// |
|
656 |
class DirectoryComboBoxRenderer extends DefaultListCellRenderer { |
|
657 |
IndentIcon ii = new IndentIcon(); |
|
658 |
public Component getListCellRendererComponent(JList list, Object value, |
|
659 |
int index, boolean isSelected, |
|
660 |
boolean cellHasFocus) { |
|
661 |
||
662 |
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
|
663 |
||
664 |
if (value == null) { |
|
665 |
setText(""); |
|
666 |
return this; |
|
667 |
} |
|
668 |
File directory = (File)value; |
|
669 |
setText(getFileChooser().getName(directory)); |
|
670 |
Icon icon = getFileChooser().getIcon(directory); |
|
671 |
ii.icon = icon; |
|
672 |
ii.depth = directoryComboBoxModel.getDepth(index); |
|
673 |
setIcon(ii); |
|
674 |
||
675 |
return this; |
|
676 |
} |
|
677 |
} |
|
678 |
||
679 |
final static int space = 10; |
|
680 |
class IndentIcon implements Icon { |
|
681 |
||
682 |
Icon icon = null; |
|
683 |
int depth = 0; |
|
684 |
||
685 |
public void paintIcon(Component c, Graphics g, int x, int y) { |
|
686 |
if (icon != null) { |
|
687 |
if (c.getComponentOrientation().isLeftToRight()) { |
|
688 |
icon.paintIcon(c, g, x+depth*space, y); |
|
689 |
} else { |
|
690 |
icon.paintIcon(c, g, x, y); |
|
691 |
} |
|
692 |
} |
|
693 |
} |
|
694 |
||
695 |
public int getIconWidth() { |
|
696 |
return ((icon != null) ? icon.getIconWidth() : 0) + depth*space; |
|
697 |
} |
|
698 |
||
699 |
public int getIconHeight() { |
|
700 |
return (icon != null) ? icon.getIconHeight() : 0; |
|
701 |
} |
|
702 |
||
703 |
} |
|
704 |
||
705 |
// |
|
706 |
// DataModel for DirectoryComboxbox |
|
707 |
// |
|
708 |
protected DirectoryComboBoxModel createDirectoryComboBoxModel(JFileChooser fc) { |
|
709 |
return new DirectoryComboBoxModel(); |
|
710 |
} |
|
711 |
||
712 |
/** |
|
713 |
* Data model for a type-face selection combo-box. |
|
714 |
*/ |
|
715 |
protected class DirectoryComboBoxModel extends AbstractListModel implements ComboBoxModel { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
716 |
Vector<File> directories = new Vector<File>(); |
2 | 717 |
int[] depths = null; |
718 |
File selectedDirectory = null; |
|
719 |
JFileChooser chooser = getFileChooser(); |
|
720 |
FileSystemView fsv = chooser.getFileSystemView(); |
|
721 |
||
722 |
public DirectoryComboBoxModel() { |
|
723 |
// Add the current directory to the model, and make it the |
|
724 |
// selectedDirectory |
|
725 |
File dir = getFileChooser().getCurrentDirectory(); |
|
726 |
if (dir != null) { |
|
727 |
addItem(dir); |
|
728 |
} |
|
729 |
} |
|
730 |
||
731 |
/** |
|
732 |
* Adds the directory to the model and sets it to be selected, |
|
733 |
* additionally clears out the previous selected directory and |
|
734 |
* the paths leading up to it, if any. |
|
735 |
*/ |
|
736 |
public void addItem(File directory) { |
|
737 |
||
738 |
if (directory == null) { |
|
739 |
return; |
|
740 |
} |
|
741 |
||
1842
824c7f885a64
6776856: Code with useShellFolder field shuold be simplify
rupashka
parents:
1290
diff
changeset
|
742 |
boolean useShellFolder = FilePane.usesShellFolder(chooser); |
824c7f885a64
6776856: Code with useShellFolder field shuold be simplify
rupashka
parents:
1290
diff
changeset
|
743 |
|
2 | 744 |
int oldSize = directories.size(); |
745 |
directories.clear(); |
|
746 |
if (oldSize > 0) { |
|
747 |
fireIntervalRemoved(this, 0, oldSize); |
|
748 |
} |
|
749 |
||
750 |
File[] baseFolders; |
|
751 |
if (useShellFolder) { |
|
752 |
baseFolders = (File[])ShellFolder.get("fileChooserComboBoxFolders"); |
|
753 |
} else { |
|
754 |
baseFolders = fsv.getRoots(); |
|
755 |
} |
|
756 |
directories.addAll(Arrays.asList(baseFolders)); |
|
757 |
||
758 |
// Get the canonical (full) path. This has the side |
|
759 |
// benefit of removing extraneous chars from the path, |
|
760 |
// for example /foo/bar/ becomes /foo/bar |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
761 |
File canonical; |
2 | 762 |
try { |
763 |
canonical = directory.getCanonicalFile(); |
|
764 |
} catch (IOException e) { |
|
765 |
// Maybe drive is not ready. Can't abort here. |
|
766 |
canonical = directory; |
|
767 |
} |
|
768 |
||
769 |
// create File instances of each directory leading up to the top |
|
770 |
try { |
|
771 |
File sf = useShellFolder ? ShellFolder.getShellFolder(canonical) |
|
772 |
: canonical; |
|
773 |
File f = sf; |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
774 |
Vector<File> path = new Vector<File>(10); |
2 | 775 |
do { |
776 |
path.addElement(f); |
|
777 |
} while ((f = f.getParentFile()) != null); |
|
778 |
||
779 |
int pathCount = path.size(); |
|
780 |
// Insert chain at appropriate place in vector |
|
781 |
for (int i = 0; i < pathCount; i++) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
782 |
f = path.get(i); |
2 | 783 |
if (directories.contains(f)) { |
784 |
int topIndex = directories.indexOf(f); |
|
785 |
for (int j = i-1; j >= 0; j--) { |
|
786 |
directories.insertElementAt(path.get(j), topIndex+i-j); |
|
787 |
} |
|
788 |
break; |
|
789 |
} |
|
790 |
} |
|
791 |
calculateDepths(); |
|
792 |
setSelectedItem(sf); |
|
793 |
} catch (FileNotFoundException ex) { |
|
794 |
calculateDepths(); |
|
795 |
} |
|
796 |
} |
|
797 |
||
798 |
private void calculateDepths() { |
|
799 |
depths = new int[directories.size()]; |
|
800 |
for (int i = 0; i < depths.length; i++) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
801 |
File dir = directories.get(i); |
2 | 802 |
File parent = dir.getParentFile(); |
803 |
depths[i] = 0; |
|
804 |
if (parent != null) { |
|
805 |
for (int j = i-1; j >= 0; j--) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
806 |
if (parent.equals(directories.get(j))) { |
2 | 807 |
depths[i] = depths[j] + 1; |
808 |
break; |
|
809 |
} |
|
810 |
} |
|
811 |
} |
|
812 |
} |
|
813 |
} |
|
814 |
||
815 |
public int getDepth(int i) { |
|
816 |
return (depths != null && i >= 0 && i < depths.length) ? depths[i] : 0; |
|
817 |
} |
|
818 |
||
819 |
public void setSelectedItem(Object selectedDirectory) { |
|
820 |
this.selectedDirectory = (File)selectedDirectory; |
|
821 |
fireContentsChanged(this, -1, -1); |
|
822 |
} |
|
823 |
||
824 |
public Object getSelectedItem() { |
|
825 |
return selectedDirectory; |
|
826 |
} |
|
827 |
||
828 |
public int getSize() { |
|
829 |
return directories.size(); |
|
830 |
} |
|
831 |
||
832 |
public Object getElementAt(int index) { |
|
833 |
return directories.elementAt(index); |
|
834 |
} |
|
835 |
} |
|
836 |
||
837 |
/** |
|
838 |
* Acts when DirectoryComboBox has changed the selected item. |
|
839 |
*/ |
|
840 |
protected class DirectoryComboBoxAction extends AbstractAction { |
|
841 |
protected DirectoryComboBoxAction() { |
|
842 |
super("DirectoryComboBoxAction"); |
|
843 |
} |
|
844 |
||
845 |
public void actionPerformed(ActionEvent e) { |
|
846 |
directoryComboBox.hidePopup(); |
|
847 |
JComponent cb = getDirectoryComboBox(); |
|
848 |
if (cb instanceof JComboBox) { |
|
849 |
File f = (File)((JComboBox)cb).getSelectedItem(); |
|
850 |
getFileChooser().setCurrentDirectory(f); |
|
851 |
} |
|
852 |
} |
|
853 |
} |
|
854 |
||
855 |
// |
|
856 |
// Renderer for Types ComboBox |
|
857 |
// |
|
858 |
protected FilterComboBoxRenderer createFilterComboBoxRenderer() { |
|
859 |
return new FilterComboBoxRenderer(); |
|
860 |
} |
|
861 |
||
862 |
/** |
|
863 |
* Render different type sizes and styles. |
|
864 |
*/ |
|
865 |
public class FilterComboBoxRenderer extends DefaultListCellRenderer { |
|
866 |
public Component getListCellRendererComponent(JList list, |
|
867 |
Object value, int index, boolean isSelected, |
|
868 |
boolean cellHasFocus) { |
|
869 |
||
870 |
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
|
871 |
||
872 |
if (value != null && value instanceof FileFilter) { |
|
873 |
setText(((FileFilter)value).getDescription()); |
|
874 |
} |
|
875 |
||
876 |
return this; |
|
877 |
} |
|
878 |
} |
|
879 |
||
880 |
// |
|
881 |
// DataModel for Types Comboxbox |
|
882 |
// |
|
883 |
protected FilterComboBoxModel createFilterComboBoxModel() { |
|
884 |
return new FilterComboBoxModel(); |
|
885 |
} |
|
886 |
||
887 |
/** |
|
888 |
* Data model for a type-face selection combo-box. |
|
889 |
*/ |
|
890 |
protected class FilterComboBoxModel extends AbstractListModel implements ComboBoxModel, PropertyChangeListener { |
|
891 |
protected FileFilter[] filters; |
|
892 |
protected FilterComboBoxModel() { |
|
893 |
super(); |
|
894 |
filters = getFileChooser().getChoosableFileFilters(); |
|
895 |
} |
|
896 |
||
897 |
public void propertyChange(PropertyChangeEvent e) { |
|
898 |
String prop = e.getPropertyName(); |
|
899 |
if(prop == JFileChooser.CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY) { |
|
900 |
filters = (FileFilter[]) e.getNewValue(); |
|
901 |
fireContentsChanged(this, -1, -1); |
|
902 |
} else if (prop == JFileChooser.FILE_FILTER_CHANGED_PROPERTY) { |
|
903 |
fireContentsChanged(this, -1, -1); |
|
904 |
} |
|
905 |
} |
|
906 |
||
907 |
public void setSelectedItem(Object filter) { |
|
908 |
if(filter != null) { |
|
909 |
getFileChooser().setFileFilter((FileFilter) filter); |
|
910 |
fireContentsChanged(this, -1, -1); |
|
911 |
} |
|
912 |
} |
|
913 |
||
914 |
public Object getSelectedItem() { |
|
915 |
// Ensure that the current filter is in the list. |
|
916 |
// NOTE: we shouldnt' have to do this, since JFileChooser adds |
|
917 |
// the filter to the choosable filters list when the filter |
|
918 |
// is set. Lets be paranoid just in case someone overrides |
|
919 |
// setFileFilter in JFileChooser. |
|
920 |
FileFilter currentFilter = getFileChooser().getFileFilter(); |
|
921 |
boolean found = false; |
|
922 |
if(currentFilter != null) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
923 |
for (FileFilter filter : filters) { |
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
924 |
if (filter == currentFilter) { |
2 | 925 |
found = true; |
926 |
} |
|
927 |
} |
|
928 |
if(found == false) { |
|
929 |
getFileChooser().addChoosableFileFilter(currentFilter); |
|
930 |
} |
|
931 |
} |
|
932 |
return getFileChooser().getFileFilter(); |
|
933 |
} |
|
934 |
||
935 |
public int getSize() { |
|
936 |
if(filters != null) { |
|
937 |
return filters.length; |
|
938 |
} else { |
|
939 |
return 0; |
|
940 |
} |
|
941 |
} |
|
942 |
||
943 |
public Object getElementAt(int index) { |
|
944 |
if(index > getSize() - 1) { |
|
945 |
// This shouldn't happen. Try to recover gracefully. |
|
946 |
return getFileChooser().getFileFilter(); |
|
947 |
} |
|
948 |
if(filters != null) { |
|
949 |
return filters[index]; |
|
950 |
} else { |
|
951 |
return null; |
|
952 |
} |
|
953 |
} |
|
954 |
} |
|
955 |
||
956 |
||
957 |
||
958 |
/** |
|
959 |
* <code>ButtonAreaLayout</code> behaves in a similar manner to |
|
960 |
* <code>FlowLayout</code>. It lays out all components from left to |
|
961 |
* right, flushed right. The widths of all components will be set |
|
962 |
* to the largest preferred size width. |
|
963 |
*/ |
|
964 |
private static class ButtonAreaLayout implements LayoutManager { |
|
965 |
private int hGap = 5; |
|
966 |
private int topMargin = 17; |
|
967 |
||
968 |
public void addLayoutComponent(String string, Component comp) { |
|
969 |
} |
|
970 |
||
971 |
public void layoutContainer(Container container) { |
|
972 |
Component[] children = container.getComponents(); |
|
973 |
||
974 |
if (children != null && children.length > 0) { |
|
975 |
int numChildren = children.length; |
|
976 |
Dimension[] sizes = new Dimension[numChildren]; |
|
977 |
Insets insets = container.getInsets(); |
|
978 |
int yLocation = insets.top + topMargin; |
|
979 |
int maxWidth = 0; |
|
980 |
||
981 |
for (int counter = 0; counter < numChildren; counter++) { |
|
982 |
sizes[counter] = children[counter].getPreferredSize(); |
|
983 |
maxWidth = Math.max(maxWidth, sizes[counter].width); |
|
984 |
} |
|
985 |
int xLocation, xOffset; |
|
986 |
if (container.getComponentOrientation().isLeftToRight()) { |
|
987 |
xLocation = container.getSize().width - insets.left - maxWidth; |
|
988 |
xOffset = hGap + maxWidth; |
|
989 |
} else { |
|
990 |
xLocation = insets.left; |
|
991 |
xOffset = -(hGap + maxWidth); |
|
992 |
} |
|
993 |
for (int counter = numChildren - 1; counter >= 0; counter--) { |
|
994 |
children[counter].setBounds(xLocation, yLocation, |
|
995 |
maxWidth, sizes[counter].height); |
|
996 |
xLocation -= xOffset; |
|
997 |
} |
|
998 |
} |
|
999 |
} |
|
1000 |
||
1001 |
public Dimension minimumLayoutSize(Container c) { |
|
1002 |
if (c != null) { |
|
1003 |
Component[] children = c.getComponents(); |
|
1004 |
||
1005 |
if (children != null && children.length > 0) { |
|
1006 |
int numChildren = children.length; |
|
1007 |
int height = 0; |
|
1008 |
Insets cInsets = c.getInsets(); |
|
1009 |
int extraHeight = topMargin + cInsets.top + cInsets.bottom; |
|
1010 |
int extraWidth = cInsets.left + cInsets.right; |
|
1011 |
int maxWidth = 0; |
|
1012 |
||
1013 |
for (int counter = 0; counter < numChildren; counter++) { |
|
1014 |
Dimension aSize = children[counter].getPreferredSize(); |
|
1015 |
height = Math.max(height, aSize.height); |
|
1016 |
maxWidth = Math.max(maxWidth, aSize.width); |
|
1017 |
} |
|
1018 |
return new Dimension(extraWidth + numChildren * maxWidth + |
|
1019 |
(numChildren - 1) * hGap, |
|
1020 |
extraHeight + height); |
|
1021 |
} |
|
1022 |
} |
|
1023 |
return new Dimension(0, 0); |
|
1024 |
} |
|
1025 |
||
1026 |
public Dimension preferredLayoutSize(Container c) { |
|
1027 |
return minimumLayoutSize(c); |
|
1028 |
} |
|
1029 |
||
1030 |
public void removeLayoutComponent(Component c) { } |
|
1031 |
} |
|
1032 |
||
1033 |
private static void groupLabels(AlignedLabel[] group) { |
|
1034 |
for (int i = 0; i < group.length; i++) { |
|
1035 |
group[i].group = group; |
|
1036 |
} |
|
1037 |
} |
|
1038 |
||
1039 |
private class AlignedLabel extends JLabel { |
|
1040 |
private AlignedLabel[] group; |
|
1041 |
private int maxWidth = 0; |
|
1042 |
||
1043 |
AlignedLabel() { |
|
1044 |
super(); |
|
1045 |
setAlignmentX(JComponent.LEFT_ALIGNMENT); |
|
1046 |
} |
|
1047 |
||
1048 |
AlignedLabel(String text) { |
|
1049 |
super(text); |
|
1050 |
setAlignmentX(JComponent.LEFT_ALIGNMENT); |
|
1051 |
} |
|
1052 |
||
1053 |
public Dimension getPreferredSize() { |
|
1054 |
Dimension d = super.getPreferredSize(); |
|
1055 |
// Align the width with all other labels in group. |
|
1056 |
return new Dimension(getMaxWidth() + 11, d.height); |
|
1057 |
} |
|
1058 |
||
1059 |
private int getMaxWidth() { |
|
1060 |
if (maxWidth == 0 && group != null) { |
|
1061 |
int max = 0; |
|
1062 |
for (int i = 0; i < group.length; i++) { |
|
1063 |
max = Math.max(group[i].getSuperPreferredWidth(), max); |
|
1064 |
} |
|
1065 |
for (int i = 0; i < group.length; i++) { |
|
1066 |
group[i].maxWidth = max; |
|
1067 |
} |
|
1068 |
} |
|
1069 |
return maxWidth; |
|
1070 |
} |
|
1071 |
||
1072 |
private int getSuperPreferredWidth() { |
|
1073 |
return super.getPreferredSize().width; |
|
1074 |
} |
|
1075 |
} |
|
1076 |
} |