1 /* BasicFileChooserUI.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.swing.plaf.basic;
40 import java.awt.BorderLayout;
41 import java.awt.Color;
42 import java.awt.Component;
43 import java.awt.Dimension;
44 import java.awt.Graphics;
45 import java.awt.GridBagConstraints;
46 import java.awt.GridBagLayout;
47 import java.awt.Point;
48 import java.awt.Polygon;
49 import java.awt.Window;
50 import java.awt.event.ActionEvent;
51 import java.awt.event.ActionListener;
52 import java.awt.event.ItemEvent;
53 import java.awt.event.ItemListener;
54 import java.awt.event.MouseAdapter;
55 import java.awt.event.MouseEvent;
56 import java.awt.event.MouseListener;
57 import java.beans.PropertyChangeEvent;
58 import java.beans.PropertyChangeListener;
60 import java.io.IOException;
61 import java.util.ArrayList;
62 import java.util.Hashtable;
63 import javax.swing.AbstractAction;
64 import javax.swing.Action;
65 import javax.swing.BoxLayout;
66 import javax.swing.ButtonGroup;
67 import javax.swing.Icon;
68 import javax.swing.JButton;
69 import javax.swing.JComboBox;
70 import javax.swing.JComponent;
71 import javax.swing.JDialog;
72 import javax.swing.JFileChooser;
73 import javax.swing.JLabel;
74 import javax.swing.JList;
75 import javax.swing.JPanel;
76 import javax.swing.JScrollPane;
77 import javax.swing.JTextField;
78 import javax.swing.JToggleButton;
79 import javax.swing.ListCellRenderer;
80 import javax.swing.SwingConstants;
81 import javax.swing.SwingUtilities;
82 import javax.swing.Timer;
83 import javax.swing.UIDefaults;
84 import javax.swing.UIManager;
85 import javax.swing.event.ListSelectionEvent;
86 import javax.swing.event.ListSelectionListener;
87 import javax.swing.filechooser.FileFilter;
88 import javax.swing.filechooser.FileSystemView;
89 import javax.swing.filechooser.FileView;
90 import javax.swing.plaf.ComponentUI;
91 import javax.swing.plaf.FileChooserUI;
97 public class BasicFileChooserUI extends FileChooserUI
102 protected class AcceptAllFileFilter extends FileFilter
104 public AcceptAllFileFilter()
111 * @param f DOCUMENT ME!
113 * @return DOCUMENT ME!
115 public boolean accept(File f)
123 * @return DOCUMENT ME!
125 public String getDescription()
127 return acceptAllFileFilterText;
134 protected class ApproveSelectionAction extends AbstractAction
137 * Creates a new ApproveSelectionAction object.
139 protected ApproveSelectionAction()
146 * @param e DOCUMENT ME!
148 public void actionPerformed(ActionEvent e)
150 Object obj = filelist.getSelectedValue();
153 File f = filechooser.getFileSystemView().createFileObject(obj
155 if (filechooser.isTraversable(f) &&
156 filechooser.getFileSelectionMode() == JFileChooser.FILES_ONLY)
157 filechooser.setCurrentDirectory(f);
160 filechooser.setSelectedFile(f);
161 filechooser.approveSelection();
171 protected class BasicFileView extends FileView
174 protected Hashtable iconCache = new Hashtable();
176 public BasicFileView()
183 * @param f DOCUMENT ME!
184 * @param i DOCUMENT ME!
186 public void cacheIcon(File f, Icon i)
194 public void clearIconCache()
202 * @param f DOCUMENT ME!
204 * @return DOCUMENT ME!
206 public Icon getCachedIcon(File f)
208 return (Icon) iconCache.get(f);
214 * @param f DOCUMENT ME!
216 * @return DOCUMENT ME!
218 public String getDescription(File f)
226 * @param f DOCUMENT ME!
228 * @return DOCUMENT ME!
230 public Icon getIcon(File f)
232 Icon val = getCachedIcon(f);
235 if (filechooser.isTraversable(f))
246 * @param f DOCUMENT ME!
248 * @return DOCUMENT ME!
250 public String getName(File f)
258 * @param f DOCUMENT ME!
260 * @return DOCUMENT ME!
262 public String getTypeDescription(File f)
264 if (filechooser.isTraversable(f))
273 * @param f DOCUMENT ME!
275 * @return DOCUMENT ME!
277 public Boolean isHidden(File f)
279 return new Boolean(filechooser.getFileSystemView().isHiddenFile(f));
286 protected class CancelSelectionAction extends AbstractAction
289 * Creates a new CancelSelectionAction object.
291 protected CancelSelectionAction()
298 * @param e DOCUMENT ME!
300 public void actionPerformed(ActionEvent e)
302 filechooser.cancelSelection();
310 protected class ChangeToParentDirectoryAction extends AbstractAction
313 * Creates a new ChangeToParentDirectoryAction object.
315 protected ChangeToParentDirectoryAction()
322 * @param e DOCUMENT ME!
324 public void actionPerformed(ActionEvent e)
326 filechooser.changeToParentDirectory();
327 filechooser.revalidate();
328 filechooser.repaint();
335 protected class DoubleClickListener extends MouseAdapter
338 private Timer timer = null;
341 private Object lastSelected = null;
344 private JList list = null;
347 * Creates a new DoubleClickListener object.
349 * @param list DOCUMENT ME!
351 public DoubleClickListener(JList list)
354 timer = new Timer(1000, null);
355 timer.setRepeats(false);
356 lastSelected = list.getSelectedValue();
357 setDirectorySelected(false);
363 * @param e DOCUMENT ME!
365 public void mouseClicked(MouseEvent e)
367 if (list.getSelectedValue() == null)
369 FileSystemView fsv = filechooser.getFileSystemView();
370 if (timer.isRunning()
371 && list.getSelectedValue().toString().equals(lastSelected.toString()))
373 File f = fsv.createFileObject(lastSelected.toString());
375 if (filechooser.isTraversable(f))
377 filechooser.setCurrentDirectory(f);
378 filechooser.rescanCurrentDirectory();
382 filechooser.setSelectedFile(f);
383 filechooser.approveSelection();
389 File f = fsv.createFileObject(list.getSelectedValue().toString());
390 if (filechooser.isTraversable(f))
392 setDirectorySelected(true);
397 setDirectorySelected(false);
400 lastSelected = list.getSelectedValue().toString();
408 * @param e DOCUMENT ME!
410 public void mouseEntered(MouseEvent e)
419 protected class GoHomeAction extends AbstractAction
422 * Creates a new GoHomeAction object.
424 protected GoHomeAction()
431 * @param e DOCUMENT ME!
433 public void actionPerformed(ActionEvent e)
435 filechooser.setCurrentDirectory(filechooser.getFileSystemView()
436 .getHomeDirectory());
437 filechooser.revalidate();
438 filechooser.repaint();
445 protected class NewFolderAction extends AbstractAction
448 * Creates a new NewFolderAction object.
450 protected NewFolderAction()
457 * @param e DOCUMENT ME!
459 public void actionPerformed(ActionEvent e)
463 filechooser.getFileSystemView().createNewFolder(filechooser
464 .getCurrentDirectory());
466 catch (IOException ioe)
470 filechooser.rescanCurrentDirectory();
471 filechooser.repaint();
478 protected class SelectionListener implements ListSelectionListener
481 * Creates a new SelectionListener object.
483 protected SelectionListener()
490 * @param e DOCUMENT ME!
492 public void valueChanged(ListSelectionEvent e)
494 Object f = filelist.getSelectedValue();
497 File file = filechooser.getFileSystemView().createFileObject(f.toString());
498 if (! filechooser.isTraversable(file))
499 filechooser.setSelectedFile(file);
501 filechooser.setSelectedFile(null);
508 protected class UpdateAction extends AbstractAction
511 * Creates a new UpdateAction object.
513 protected UpdateAction()
520 * @param e DOCUMENT ME!
522 public void actionPerformed(ActionEvent e)
528 protected int cancelButtonMnemonic;
531 protected String cancelButtonText;
534 protected String cancelButtonToolTipText;
537 protected Icon computerIcon = new Icon()
539 public int getIconHeight()
544 public int getIconWidth()
549 public void paintIcon(Component c, Graphics g, int x, int y)
555 protected Icon detailsViewIcon = new Icon()
557 public int getIconHeight()
562 public int getIconWidth()
567 public void paintIcon(Component c, Graphics g, int x, int y)
569 Color saved = g.getColor();
572 g.setColor(Color.GRAY);
573 g.drawRect(1, 1, 15, 20);
574 g.drawLine(17, 6, 23, 6);
575 g.drawLine(17, 12, 23, 12);
576 g.drawLine(17, 18, 23, 18);
584 protected Icon directoryIcon = new Icon()
586 public int getIconHeight()
591 public int getIconWidth()
596 public void paintIcon(Component c, Graphics g, int x, int y)
598 Color saved = g.getColor();
601 Point ap = new Point(3, 7);
602 Point bp = new Point(3, 21);
603 Point cp = new Point(21, 21);
604 Point dp = new Point(21, 12);
605 Point ep = new Point(16, 12);
606 Point fp = new Point(13, 7);
608 Polygon dir = new Polygon(new int[] { ap.x, bp.x, cp.x, dp.x, ep.x, fp.x },
609 new int[] { ap.y, bp.y, cp.y, dp.y, ep.y, fp.y },
612 g.setColor(new Color(153, 204, 255));
614 g.setColor(Color.BLACK);
623 protected int directoryOpenButtonMnemonic;
626 protected String directoryOpenButtonText;
629 protected String directoryOpenButtonToolTipText;
632 protected Icon fileIcon = new Icon()
634 public int getIconHeight()
639 public int getIconWidth()
644 public void paintIcon(Component c, Graphics g, int x, int y)
646 Color saved = g.getColor();
649 Point a = new Point(5, 4);
650 Point b = new Point(5, 20);
651 Point d = new Point(19, 20);
652 Point e = new Point(19, 7);
653 Point f = new Point(16, 4);
655 Polygon p = new Polygon(new int[] { a.x, b.x, d.x, e.x, f.x, },
656 new int[] { a.y, b.y, d.y, e.y, f.y }, 5);
658 g.setColor(Color.WHITE);
660 g.setColor(Color.BLACK);
663 g.drawLine(16, 4, 14, 6);
664 g.drawLine(14, 6, 19, 7);
672 protected Icon floppyDriveIcon = new Icon()
674 public int getIconHeight()
679 public int getIconWidth()
684 public void paintIcon(Component c, Graphics g, int x, int y)
690 protected Icon hardDriveIcon = new Icon()
692 public int getIconHeight()
697 public int getIconWidth()
702 public void paintIcon(Component c, Graphics g, int x, int y)
708 protected int helpButtonMnemonic;
711 protected String helpButtonText;
714 protected String helpButtonToolTipText;
717 protected Icon homeFolderIcon = new Icon()
719 public int getIconHeight()
724 public int getIconWidth()
729 public void paintIcon(Component c, Graphics g, int x, int y)
731 Color saved = g.getColor();
734 Point a = new Point(12, 3);
735 Point b = new Point(4, 10);
736 Point d = new Point(20, 10);
738 Polygon p = new Polygon(new int[] { a.x, b.x, d.x },
739 new int[] { a.y, b.y, d.y }, 3);
741 g.setColor(new Color(104, 51, 0));
743 g.setColor(Color.BLACK);
746 g.setColor(Color.WHITE);
747 g.fillRect(8, 10, 8, 10);
748 g.setColor(Color.BLACK);
749 g.drawRect(8, 10, 8, 10);
757 protected Icon listViewIcon = new Icon()
759 public int getIconHeight()
764 public int getIconWidth()
769 // Not needed. Only simplifies things until we get real icons.
770 private void paintPartial(Graphics g, int x, int y)
772 Color saved = g.getColor();
775 g.setColor(Color.GRAY);
776 g.drawRect(1, 1, 7, 10);
777 g.drawLine(8, 6, 11, 6);
783 public void paintIcon(Component c, Graphics g, int x, int y)
785 Color saved = g.getColor();
788 paintPartial(g, 0, 0);
789 paintPartial(g, 12, 0);
790 paintPartial(g, 0, 12);
791 paintPartial(g, 12, 12);
799 protected Icon newFolderIcon = directoryIcon;
802 protected int openButtonMnemonic;
805 protected String openButtonText;
808 protected String openButtonToolTipText;
811 protected int saveButtonMnemonic;
814 protected String saveButtonText;
817 protected String saveButtonToolTipText;
820 protected int updateButtonMnemonic;
823 protected String updateButtonText;
826 protected String updateButtonToolTipText;
829 protected Icon upFolderIcon = new Icon()
831 public int getIconHeight()
836 public int getIconWidth()
841 public void paintIcon(Component comp, Graphics g, int x, int y)
843 Color saved = g.getColor();
846 Point a = new Point(3, 7);
847 Point b = new Point(3, 21);
848 Point c = new Point(21, 21);
849 Point d = new Point(21, 12);
850 Point e = new Point(16, 12);
851 Point f = new Point(13, 7);
853 Polygon dir = new Polygon(new int[] { a.x, b.x, c.x, d.x, e.x, f.x },
854 new int[] { a.y, b.y, c.y, d.y, e.y, f.y }, 6);
856 g.setColor(new Color(153, 204, 255));
858 g.setColor(Color.BLACK);
861 a = new Point(12, 15);
862 b = new Point(9, 18);
863 c = new Point(15, 18);
865 Polygon arrow = new Polygon(new int[] { a.x, b.x, c.x },
866 new int[] { a.y, b.y, c.y }, 3);
868 g.fillPolygon(arrow);
870 g.drawLine(12, 15, 12, 22);
877 // -- begin private, but package local since used in inner classes --
879 JFileChooser filechooser;
888 BasicDirectoryModel model;
891 FileFilter acceptAll = new AcceptAllFileFilter();
894 FileView fv = new BasicFileView();
897 static final int ICON_SIZE = 24;
912 JButton upFolderButton;
915 JButton newFolderButton;
918 JButton homeFolderButton;
921 JPanel accessoryPanel;
924 PropertyChangeListener propertyChangeListener;
927 String acceptAllFileFilterText;
936 boolean dirSelected = false;
947 private class ListLabelRenderer
949 implements ListCellRenderer
952 final Color selected = new Color(153, 204, 255);
955 * Creates a new ListLabelRenderer object.
957 public ListLabelRenderer()
966 * @param list DOCUMENT ME!
967 * @param value DOCUMENT ME!
968 * @param index DOCUMENT ME!
969 * @param isSelected DOCUMENT ME!
970 * @param cellHasFocus DOCUMENT ME!
972 * @return DOCUMENT ME!
974 public Component getListCellRendererComponent(JList list, Object value,
977 boolean cellHasFocus)
979 setHorizontalAlignment(SwingConstants.LEFT);
980 File file = (File) value;
981 setText(filechooser.getName(file));
982 setIcon(filechooser.getIcon(file));
983 setBackground(isSelected ? selected : Color.WHITE);
984 setForeground(Color.BLACK);
993 public class CBLabelRenderer extends JLabel implements ListCellRenderer
996 * Creates a new CBLabelRenderer object.
998 public CBLabelRenderer()
1007 * @param list DOCUMENT ME!
1008 * @param value DOCUMENT ME!
1009 * @param index DOCUMENT ME!
1010 * @param isSelected DOCUMENT ME!
1011 * @param cellHasFocus DOCUMENT ME!
1013 * @return DOCUMENT ME!
1015 public Component getListCellRendererComponent(JList list, Object value,
1018 boolean cellHasFocus)
1020 setHorizontalAlignment(SwingConstants.LEFT);
1021 setIcon(directoryIcon);
1022 setText(value.toString());
1023 setForeground(Color.BLACK);
1024 setBackground(Color.WHITE);
1032 Window owner = SwingUtilities.windowForComponent(filechooser);
1033 if (owner instanceof JDialog)
1034 ((JDialog) owner).dispose();
1038 * Creates a new BasicFileChooserUI object.
1040 * @param b DOCUMENT ME!
1042 public BasicFileChooserUI(JFileChooser b)
1044 this.filechooser = b;
1050 * @param c DOCUMENT ME!
1052 * @return DOCUMENT ME!
1054 public static ComponentUI createUI(JComponent c)
1056 return new BasicFileChooserUI((JFileChooser) c);
1062 * @param c DOCUMENT ME!
1064 public void installUI(JComponent c)
1066 if (c instanceof JFileChooser)
1068 JFileChooser fc = (JFileChooser) c;
1069 fc.resetChoosableFileFilters();
1072 installDefaults(fc);
1073 installComponents(fc);
1074 installListeners(fc);
1081 * @param c DOCUMENT ME!
1083 public void uninstallUI(JComponent c)
1086 uninstallListeners(filechooser);
1087 uninstallComponents(filechooser);
1088 uninstallDefaults(filechooser);
1092 // FIXME: Indent the entries in the combobox
1093 // Made this method package private to access it from within inner classes
1094 // with better performance
1097 ArrayList parentFiles = new ArrayList();
1098 File parent = filechooser.getCurrentDirectory();
1100 parent = filechooser.getFileSystemView().getDefaultDirectory();
1101 while (parent != null)
1103 String name = parent.getName();
1104 if (name.equals(""))
1105 name = parent.getAbsolutePath();
1107 parentFiles.add(parentFiles.size(), name);
1108 parent = parent.getParentFile();
1111 if (parentFiles.size() == 0)
1114 if (parents.getItemCount() > 0)
1115 parents.removeAllItems();
1116 for (int i = parentFiles.size() - 1; i >= 0; i--)
1117 parents.addItem(parentFiles.get(i));
1118 parents.setSelectedIndex(parentFiles.size() - 1);
1119 parents.revalidate();
1126 * @return DOCUMENT ME!
1128 private ItemListener createBoxListener()
1130 return new ItemListener()
1132 public void itemStateChanged(ItemEvent e)
1134 if (parents.getItemCount() - 1 == parents.getSelectedIndex())
1136 StringBuffer dir = new StringBuffer();
1137 for (int i = 0; i <= parents.getSelectedIndex(); i++)
1139 dir.append(parents.getItemAt(i));
1140 dir.append(File.separatorChar);
1142 filechooser.setCurrentDirectory(filechooser.getFileSystemView()
1143 .createFileObject(dir
1152 * @return DOCUMENT ME!
1154 private ItemListener createFilterListener()
1156 return new ItemListener()
1158 public void itemStateChanged(ItemEvent e)
1160 int index = filters.getSelectedIndex();
1163 filechooser.setFileFilter(filechooser.getChoosableFileFilters()[index]);
1168 void filterEntries()
1170 FileFilter[] list = filechooser.getChoosableFileFilters();
1171 if (filters.getItemCount() > 0)
1172 filters.removeAllItems();
1175 String selected = filechooser.getFileFilter().getDescription();
1176 for (int i = 0; i < list.length; i++)
1178 if (selected.equals(list[i].getDescription()))
1180 filters.addItem(list[i].getDescription());
1182 filters.setSelectedIndex(index);
1183 filters.revalidate();
1190 * @param fc DOCUMENT ME!
1192 public void installComponents(JFileChooser fc)
1194 JLabel look = new JLabel("Look In:");
1196 parents = new JComboBox();
1197 parents.setRenderer(new CBLabelRenderer());
1199 look.setLabelFor(parents);
1200 JPanel parentsPanel = new JPanel();
1201 parentsPanel.add(look);
1202 parentsPanel.add(parents);
1203 JPanel buttonPanel = new JPanel();
1205 upFolderButton = new JButton();
1206 upFolderButton.setIcon(upFolderIcon);
1207 buttonPanel.add(upFolderButton);
1209 homeFolderButton = new JButton();
1210 homeFolderButton = new JButton(homeFolderIcon);
1211 buttonPanel.add(homeFolderButton);
1213 newFolderButton = new JButton();
1214 newFolderButton.setIcon(newFolderIcon);
1215 buttonPanel.add(newFolderButton);
1217 ButtonGroup toggles = new ButtonGroup();
1218 JToggleButton listViewButton = new JToggleButton();
1219 listViewButton.setIcon(listViewIcon);
1220 toggles.add(listViewButton);
1221 buttonPanel.add(listViewButton);
1223 JToggleButton detailsViewButton = new JToggleButton();
1224 detailsViewButton.setIcon(detailsViewIcon);
1225 toggles.add(detailsViewButton);
1226 buttonPanel.add(detailsViewButton);
1228 JPanel topPanel = new JPanel();
1229 topPanel.setLayout(new java.awt.FlowLayout());
1230 topPanel.add(parentsPanel);
1231 topPanel.add(buttonPanel);
1233 accessoryPanel = new JPanel();
1234 if (filechooser.getAccessory() != null)
1235 accessoryPanel.add(filechooser.getAccessory(), BorderLayout.CENTER);
1237 filelist = new JList(model);
1238 filelist.setVisibleRowCount(6);
1239 JScrollPane scrollp = new JScrollPane(filelist);
1240 scrollp.setPreferredSize(new Dimension(400, 175));
1241 filelist.setBackground(Color.WHITE);
1243 filelist.setLayoutOrientation(JList.VERTICAL_WRAP);
1244 filelist.setCellRenderer(new ListLabelRenderer());
1246 GridBagConstraints c = new GridBagConstraints();
1249 c.fill = GridBagConstraints.BOTH;
1253 JPanel centrePanel = new JPanel();
1254 centrePanel.setLayout(new GridBagLayout());
1255 centrePanel.add(scrollp, c);
1258 centrePanel.add(accessoryPanel, c);
1260 JLabel fileNameLabel = new JLabel("File Name:");
1261 JLabel fileTypesLabel = new JLabel("Files of Type:");
1263 JTextField entry = new JTextField();
1264 filters = new JComboBox();
1267 fileNameLabel.setLabelFor(entry);
1268 fileNameLabel.setHorizontalTextPosition(SwingConstants.LEFT);
1269 fileTypesLabel.setLabelFor(filters);
1270 fileTypesLabel.setHorizontalTextPosition(SwingConstants.LEFT);
1272 closePanel = new JPanel();
1273 accept = getApproveButton(filechooser);
1274 cancel = new JButton(cancelButtonText);
1275 cancel.setMnemonic(cancelButtonMnemonic);
1276 cancel.setToolTipText(cancelButtonToolTipText);
1277 closePanel.add(accept);
1278 closePanel.add(cancel);
1280 c.anchor = GridBagConstraints.WEST;
1285 bottomPanel = new JPanel();
1286 bottomPanel.setLayout(new GridBagLayout());
1287 bottomPanel.add(fileNameLabel, c);
1290 bottomPanel.add(fileTypesLabel, c);
1295 bottomPanel.add(entry, c);
1298 bottomPanel.add(filters, c);
1300 c.fill = GridBagConstraints.NONE;
1302 c.anchor = GridBagConstraints.EAST;
1303 bottomPanel.add(closePanel, c);
1305 filechooser.setLayout(new BorderLayout());
1306 filechooser.add(topPanel, BorderLayout.NORTH);
1307 filechooser.add(centrePanel, BorderLayout.CENTER);
1308 filechooser.add(bottomPanel, BorderLayout.SOUTH);
1314 * @param fc DOCUMENT ME!
1316 public void uninstallComponents(JFileChooser fc)
1322 upFolderButton = null;
1323 homeFolderButton = null;
1324 newFolderButton = null;
1332 * @param fc DOCUMENT ME!
1334 protected void installListeners(JFileChooser fc)
1336 propertyChangeListener = createPropertyChangeListener(filechooser);
1337 filechooser.addPropertyChangeListener(propertyChangeListener);
1339 //parents.addItemListener(createBoxListener());
1340 accept.addActionListener(getApproveSelectionAction());
1341 cancel.addActionListener(getCancelSelectionAction());
1342 upFolderButton.addActionListener(getChangeToParentDirectoryAction());
1343 homeFolderButton.addActionListener(getGoHomeAction());
1344 newFolderButton.addActionListener(getNewFolderAction());
1345 filters.addItemListener(createFilterListener());
1347 filelist.addMouseListener(createDoubleClickListener(filechooser, filelist));
1348 filelist.addListSelectionListener(createListSelectionListener(filechooser));
1354 * @param fc DOCUMENT ME!
1356 protected void uninstallListeners(JFileChooser fc)
1358 filechooser.removePropertyChangeListener(propertyChangeListener);
1359 propertyChangeListener = null;
1365 * @param fc DOCUMENT ME!
1367 protected void installDefaults(JFileChooser fc)
1376 * @param fc DOCUMENT ME!
1378 protected void uninstallDefaults(JFileChooser fc)
1380 uninstallStrings(fc);
1387 * @param fc DOCUMENT ME!
1389 protected void installIcons(JFileChooser fc)
1391 // FIXME: Implement.
1397 * @param fc DOCUMENT ME!
1399 protected void uninstallIcons(JFileChooser fc)
1401 // FIXME: Implement.
1407 * @param fc DOCUMENT ME!
1409 protected void installStrings(JFileChooser fc)
1411 UIDefaults defaults = UIManager.getLookAndFeelDefaults();
1413 acceptAllFileFilterText = defaults.getString("FileChooser.acceptAllFileFilterText");
1414 cancelButtonMnemonic = defaults.getInt("FileChooser.cancelButtonMnemonic");
1415 cancelButtonText = defaults.getString("FileChooser.cancelButtonText");
1416 cancelButtonToolTipText = defaults.getString("FileChooser.cancelButtonToolTipText");
1418 dirDescText = defaults.getString("FileChooser.directoryDescriptionText");
1419 fileDescText = defaults.getString("FileChooser.fileDescriptionText");
1421 helpButtonMnemonic = defaults.getInt("FileChooser.helpButtonMnemonic");
1422 helpButtonText = defaults.getString("FileChooser.helpButtonText");
1423 helpButtonToolTipText = defaults.getString("FileChooser.helpButtonToolTipText");
1425 openButtonMnemonic = defaults.getInt("FileChooser.openButtonMnemonic");
1426 openButtonText = defaults.getString("FileChooser.openButtonText");
1427 openButtonToolTipText = defaults.getString("FileChooser.openButtonToolTipText");
1429 saveButtonMnemonic = defaults.getInt("FileChooser.saveButtonMnemonic");
1430 saveButtonText = defaults.getString("FileChooser.saveButtonText");
1431 saveButtonToolTipText = defaults.getString("FileChooser.saveButtonToolTipText");
1437 * @param fc DOCUMENT ME!
1439 protected void uninstallStrings(JFileChooser fc)
1441 acceptAllFileFilterText = null;
1442 cancelButtonMnemonic = 0;
1443 cancelButtonText = null;
1444 cancelButtonToolTipText = null;
1447 fileDescText = null;
1449 helpButtonMnemonic = 0;
1450 helpButtonText = null;
1451 helpButtonToolTipText = null;
1453 openButtonMnemonic = 0;
1454 openButtonText = null;
1455 openButtonToolTipText = null;
1457 saveButtonMnemonic = 0;
1458 saveButtonText = null;
1459 saveButtonToolTipText = null;
1465 protected void createModel()
1467 model = new BasicDirectoryModel(filechooser);
1473 * @return DOCUMENT ME!
1475 public BasicDirectoryModel getModel()
1483 * @param fc DOCUMENT ME!
1485 * @return DOCUMENT ME!
1487 public PropertyChangeListener createPropertyChangeListener(JFileChooser fc)
1489 return new PropertyChangeListener()
1491 public void propertyChange(PropertyChangeEvent e)
1493 // FIXME: Multiple file selection waiting on JList multiple selection bug.
1494 if (e.getPropertyName().equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY))
1496 if (filechooser.getSelectedFile() == null)
1499 setFileName(filechooser.getSelectedFile().toString());
1501 File file = filechooser.getSelectedFile();
1502 for (index = 0; index < model.getSize(); index++)
1503 if (((File) model.getElementAt(index)).equals(file))
1507 filelist.setSelectedIndex(index);
1508 filelist.ensureIndexIsVisible(index);
1509 filelist.revalidate();
1512 else if (e.getPropertyName().equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY))
1514 filelist.clearSelection();
1515 filelist.revalidate();
1517 setDirectorySelected(false);
1518 setDirectory(filechooser.getCurrentDirectory());
1521 else if (e.getPropertyName().equals(JFileChooser.CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY)
1522 || e.getPropertyName().equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY))
1524 else if (e.getPropertyName().equals(JFileChooser.DIALOG_TYPE_CHANGED_PROPERTY)
1525 || e.getPropertyName().equals(JFileChooser.DIALOG_TITLE_CHANGED_PROPERTY))
1527 Window owner = SwingUtilities.windowForComponent(filechooser);
1528 if (owner instanceof JDialog)
1529 ((JDialog) owner).setTitle(getDialogTitle(filechooser));
1530 accept.setText(getApproveButtonText(filechooser));
1531 accept.setToolTipText(getApproveButtonToolTipText(filechooser));
1532 accept.setMnemonic(getApproveButtonMnemonic(filechooser));
1534 else if (e.getPropertyName().equals(JFileChooser.APPROVE_BUTTON_TEXT_CHANGED_PROPERTY))
1535 accept.setText(getApproveButtonText(filechooser));
1536 else if (e.getPropertyName().equals(JFileChooser.APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY))
1537 accept.setToolTipText(getApproveButtonToolTipText(filechooser));
1538 else if (e.getPropertyName().equals(JFileChooser.APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY))
1539 accept.setMnemonic(getApproveButtonMnemonic(filechooser));
1540 else if (e.getPropertyName().equals(JFileChooser.CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY))
1542 if (filechooser.getControlButtonsAreShown())
1544 GridBagConstraints c = new GridBagConstraints();
1546 bottomPanel.add(filters, c);
1548 c.fill = GridBagConstraints.BOTH;
1550 c.anchor = GridBagConstraints.EAST;
1551 bottomPanel.add(closePanel, c);
1552 bottomPanel.revalidate();
1553 bottomPanel.repaint();
1554 bottomPanel.doLayout();
1557 bottomPanel.remove(closePanel);
1559 else if (e.getPropertyName().equals(JFileChooser.ACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY))
1561 if (filechooser.isAcceptAllFileFilterUsed())
1562 filechooser.addChoosableFileFilter(getAcceptAllFileFilter(filechooser));
1564 filechooser.removeChoosableFileFilter(getAcceptAllFileFilter(filechooser));
1566 else if (e.getPropertyName().equals(JFileChooser.ACCESSORY_CHANGED_PROPERTY))
1568 JComponent old = (JComponent) e.getOldValue();
1570 getAccessoryPanel().remove(old);
1571 JComponent newval = (JComponent) e.getNewValue();
1573 getAccessoryPanel().add(newval);
1575 if (e.getPropertyName().equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)
1576 || e.getPropertyName().equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY)
1577 || e.getPropertyName().equals(JFileChooser.FILE_HIDING_CHANGED_PROPERTY))
1578 rescanCurrentDirectory(filechooser);
1580 filechooser.revalidate();
1581 filechooser.repaint();
1589 * @return DOCUMENT ME!
1591 public String getFileName()
1599 * @return DOCUMENT ME!
1601 public String getDirectoryName()
1603 // XXX: I don't see a case where the thing returns something non-null..
1610 * @param filename DOCUMENT ME!
1612 public void setFileName(String filename)
1614 this.filename = filename;
1620 * @param dirname DOCUMENT ME!
1622 public void setDirectoryName(String dirname)
1630 * @param fc DOCUMENT ME!
1632 public void rescanCurrentDirectory(JFileChooser fc)
1634 getModel().validateFileCache();
1635 filelist.revalidate();
1641 * @param fc DOCUMENT ME!
1642 * @param f DOCUMENT ME!
1644 public void ensureFileIsVisible(JFileChooser fc, File f)
1646 // XXX: Not sure what this does.
1652 * @return DOCUMENT ME!
1654 public JFileChooser getFileChooser()
1662 * @return DOCUMENT ME!
1664 public JPanel getAccessoryPanel()
1666 return accessoryPanel;
1672 * @param fc DOCUMENT ME!
1674 * @return DOCUMENT ME!
1676 public JButton getApproveButton(JFileChooser fc)
1678 accept = new JButton(getApproveButtonText(fc));
1679 accept.setMnemonic(getApproveButtonMnemonic(fc));
1680 accept.setToolTipText(getApproveButtonToolTipText(fc));
1687 * @param fc DOCUMENT ME!
1689 * @return DOCUMENT ME!
1691 public String getApproveButtonToolTipText(JFileChooser fc)
1693 if (fc.getApproveButtonToolTipText() != null)
1694 return fc.getApproveButtonToolTipText();
1695 else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1696 return saveButtonToolTipText;
1698 return openButtonToolTipText;
1704 public void clearIconCache()
1706 if (fv instanceof BasicFileView)
1707 ((BasicFileView) fv).clearIconCache();
1713 * @param fc DOCUMENT ME!
1715 * @return DOCUMENT ME!
1717 public ListSelectionListener createListSelectionListener(JFileChooser fc)
1719 return new SelectionListener();
1725 * @param fc DOCUMENT ME!
1726 * @param list DOCUMENT ME!
1728 * @return DOCUMENT ME!
1730 protected MouseListener createDoubleClickListener(JFileChooser fc, JList list)
1732 return new DoubleClickListener(list);
1738 * @return DOCUMENT ME!
1740 protected boolean isDirectorySelected()
1748 * @param selected DOCUMENT ME!
1750 protected void setDirectorySelected(boolean selected)
1752 dirSelected = selected;
1758 * @return DOCUMENT ME!
1760 protected File getDirectory()
1768 * @param f DOCUMENT ME!
1770 protected void setDirectory(File f)
1778 * @param fc DOCUMENT ME!
1780 * @return DOCUMENT ME!
1782 public FileFilter getAcceptAllFileFilter(JFileChooser fc)
1790 * @param fc DOCUMENT ME!
1792 * @return DOCUMENT ME!
1794 public FileView getFileView(JFileChooser fc)
1796 if (fc.getFileView() != null)
1797 return fc.getFileView();
1804 * @param fc DOCUMENT ME!
1806 * @return DOCUMENT ME!
1808 public String getDialogTitle(JFileChooser fc)
1810 String ret = fc.getDialogTitle();
1813 switch (fc.getDialogType())
1815 case JFileChooser.OPEN_DIALOG:
1816 ret = openButtonText;
1818 case JFileChooser.SAVE_DIALOG:
1819 ret = saveButtonText;
1822 ret = fc.getApproveButtonText();
1826 ret = openButtonText;
1833 * @param fc DOCUMENT ME!
1835 * @return DOCUMENT ME!
1837 public int getApproveButtonMnemonic(JFileChooser fc)
1839 if (fc.getApproveButtonMnemonic() != 0)
1840 return fc.getApproveButtonMnemonic();
1841 else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1842 return saveButtonMnemonic;
1844 return openButtonMnemonic;
1850 * @param fc DOCUMENT ME!
1852 * @return DOCUMENT ME!
1854 public String getApproveButtonText(JFileChooser fc)
1856 if (fc.getApproveButtonText() != null)
1857 return fc.getApproveButtonText();
1858 else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1859 return saveButtonText;
1861 return openButtonText;
1867 * @return DOCUMENT ME!
1869 public Action getNewFolderAction()
1871 return new NewFolderAction();
1877 * @return DOCUMENT ME!
1879 public Action getGoHomeAction()
1881 return new GoHomeAction();
1887 * @return DOCUMENT ME!
1889 public Action getChangeToParentDirectoryAction()
1891 return new ChangeToParentDirectoryAction();
1897 * @return DOCUMENT ME!
1899 public Action getApproveSelectionAction()
1901 return new ApproveSelectionAction();
1907 * @return DOCUMENT ME!
1909 public Action getCancelSelectionAction()
1911 return new CancelSelectionAction();
1917 * @return DOCUMENT ME!
1919 public Action getUpdateAction()
1921 return new UpdateAction();