1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51:
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72: import ;
73:
74:
75:
79: public class BasicFileChooserUI extends FileChooserUI
80: {
81:
84: protected class AcceptAllFileFilter extends FileFilter
85: {
86:
89: public AcceptAllFileFilter()
90: {
91:
92: }
93:
94:
102: public boolean accept(File f)
103: {
104: return true;
105: }
106:
107:
112: public String getDescription()
113: {
114: return acceptAllFileFilterText;
115: }
116: }
117:
118:
123: protected class ApproveSelectionAction extends AbstractAction
124: {
125:
128: protected ApproveSelectionAction()
129: {
130: super("approveSelection");
131: }
132:
133:
138: public void actionPerformed(ActionEvent e)
139: {
140: Object obj = null;
141: if (parentPath != null)
142: obj = new String(parentPath + getFileName());
143: else
144: obj = filechooser.getSelectedFile();
145: if (obj != null)
146: {
147: File f = filechooser.getFileSystemView().createFileObject(obj.toString());
148: File currSelected = filechooser.getSelectedFile();
149: if (filechooser.isTraversable(f))
150: {
151: filechooser.setCurrentDirectory(currSelected);
152: filechooser.rescanCurrentDirectory();
153: }
154: else
155: {
156: filechooser.approveSelection();
157: closeDialog();
158: }
159: }
160: }
161: }
162:
163:
166: protected class BasicFileView extends FileView
167: {
168:
169: protected Hashtable iconCache = new Hashtable();
170:
171:
174: public BasicFileView()
175: {
176:
177: }
178:
179:
185: public void cacheIcon(File f, Icon i)
186: {
187: iconCache.put(f, i);
188: }
189:
190:
193: public void clearIconCache()
194: {
195: iconCache.clear();
196: }
197:
198:
206: public Icon getCachedIcon(File f)
207: {
208: return (Icon) iconCache.get(f);
209: }
210:
211:
220: public String getDescription(File f)
221: {
222: return getName(f);
223: }
224:
225:
232: public Icon getIcon(File f)
233: {
234: Icon val = getCachedIcon(f);
235: if (val != null)
236: return val;
237: if (filechooser.isTraversable(f))
238: val = directoryIcon;
239: else
240: val = fileIcon;
241: cacheIcon(f, val);
242: return val;
243: }
244:
245:
252: public String getName(File f)
253: {
254: return f.getName();
255: }
256:
257:
264: public String getTypeDescription(File f)
265: {
266: if (filechooser.isTraversable(f))
267: return dirDescText;
268: else
269: return fileDescText;
270: }
271:
272:
280: public Boolean isHidden(File f)
281: {
282: return Boolean.valueOf(filechooser.getFileSystemView().isHiddenFile(f));
283: }
284: }
285:
286:
291: protected class CancelSelectionAction extends AbstractAction
292: {
293:
296: protected CancelSelectionAction()
297: {
298: super(null);
299: }
300:
301:
306: public void actionPerformed(ActionEvent e)
307: {
308: filechooser.setSelectedFile(null);
309: filechooser.setSelectedFiles(null);
310: filechooser.cancelSelection();
311: closeDialog();
312: }
313: }
314:
315:
321: protected class ChangeToParentDirectoryAction extends AbstractAction
322: {
323:
326: protected ChangeToParentDirectoryAction()
327: {
328: super("Go Up");
329: }
330:
331:
336: public void actionPerformed(ActionEvent e)
337: {
338: filechooser.changeToParentDirectory();
339: filechooser.revalidate();
340: filechooser.repaint();
341: }
342: }
343:
344:
349: protected class DoubleClickListener extends MouseAdapter
350: {
351:
352:
353: private Object lastSelected = null;
354:
355:
356: private JList list = null;
357:
358:
363: public DoubleClickListener(JList list)
364: {
365: this.list = list;
366: lastSelected = list.getSelectedValue();
367: setDirectorySelected(false);
368: }
369:
370:
375: public void mouseClicked(MouseEvent e)
376: {
377: Object p = list.getSelectedValue();
378: if (p == null)
379: return;
380: FileSystemView fsv = filechooser.getFileSystemView();
381: if (e.getClickCount() >= 2 && lastSelected != null &&
382: p.toString().equals(lastSelected.toString()))
383: {
384: File f = fsv.createFileObject(lastSelected.toString());
385: if (filechooser.isTraversable(f))
386: {
387: filechooser.setCurrentDirectory(f);
388: filechooser.rescanCurrentDirectory();
389: }
390: else
391: {
392: filechooser.setSelectedFile(f);
393: filechooser.approveSelection();
394: closeDialog();
395: }
396: }
397: else
398: {
399: String path = p.toString();
400: File f = fsv.createFileObject(path);
401: filechooser.setSelectedFile(f);
402:
403: if (filechooser.isMultiSelectionEnabled())
404: {
405: int[] inds = list.getSelectedIndices();
406: File[] allFiles = new File[inds.length];
407: for (int i = 0; i < inds.length; i++)
408: allFiles[i] = (File) list.getModel().getElementAt(inds[i]);
409: filechooser.setSelectedFiles(allFiles);
410: }
411:
412: if (filechooser.isTraversable(f))
413: {
414: setDirectorySelected(true);
415: setDirectory(f);
416: }
417: else
418: {
419: setDirectorySelected(false);
420: setDirectory(null);
421: }
422: lastSelected = path;
423: parentPath = path.substring(0, path.lastIndexOf("/") + 1);
424: if (f.isFile())
425: setFileName(path.substring(path.lastIndexOf("/") + 1));
426: else if (filechooser.getFileSelectionMode() ==
427: JFileChooser.DIRECTORIES_ONLY)
428: setFileName(path);
429: }
430: }
431:
432:
437: public void mouseEntered(MouseEvent e)
438: {
439:
440: }
441: }
442:
443:
449: protected class GoHomeAction extends AbstractAction
450: {
451:
454: protected GoHomeAction()
455: {
456: super("Go Home");
457: }
458:
459:
465: public void actionPerformed(ActionEvent e)
466: {
467: filechooser.setCurrentDirectory(filechooser.getFileSystemView()
468: .getHomeDirectory());
469: filechooser.revalidate();
470: filechooser.repaint();
471: }
472: }
473:
474:
479: protected class NewFolderAction extends AbstractAction
480: {
481:
484: protected NewFolderAction()
485: {
486: super("New Folder");
487: }
488:
489:
494: public void actionPerformed(ActionEvent e)
495: {
496: try
497: {
498: filechooser.getFileSystemView().createNewFolder(filechooser
499: .getCurrentDirectory());
500: }
501: catch (IOException ioe)
502: {
503: return;
504: }
505: filechooser.rescanCurrentDirectory();
506: filechooser.repaint();
507: }
508: }
509:
510:
515: protected class SelectionListener implements ListSelectionListener
516: {
517:
520: protected SelectionListener()
521: {
522:
523: }
524:
525:
530: public void valueChanged(ListSelectionEvent e)
531: {
532: JList list = (JList) e.getSource();
533: Object f = list.getSelectedValue();
534: if (f == null)
535: return;
536: File file = filechooser.getFileSystemView().createFileObject(f.toString());
537: if (! filechooser.isTraversable(file))
538: filechooser.setSelectedFile(file);
539: else
540: filechooser.setSelectedFile(null);
541: }
542: }
543:
544:
549: protected class UpdateAction extends AbstractAction
550: {
551:
554: protected UpdateAction()
555: {
556: super(null);
557: }
558:
559:
564: public void actionPerformed(ActionEvent e)
565: {
566:
567: }
568: }
569:
570:
571: protected int cancelButtonMnemonic;
572:
573:
574: protected String cancelButtonText;
575:
576:
577: protected String cancelButtonToolTipText;
578:
579:
580: protected Icon computerIcon;
581:
582:
583: protected Icon detailsViewIcon;
584:
585:
586: protected Icon directoryIcon;
587:
588:
589: protected int directoryOpenButtonMnemonic;
590:
591:
592: protected String directoryOpenButtonText;
593:
594:
595: protected String directoryOpenButtonToolTipText;
596:
597:
598: protected Icon fileIcon;
599:
600:
601: protected Icon floppyDriveIcon;
602:
603:
604: protected Icon hardDriveIcon;
605:
606:
607: protected int helpButtonMnemonic;
608:
609:
610: protected String helpButtonText;
611:
612:
613: protected String helpButtonToolTipText;
614:
615:
616: protected Icon homeFolderIcon;
617:
618:
619: protected Icon listViewIcon;
620:
621:
622: protected Icon newFolderIcon = directoryIcon;
623:
624:
625: protected int openButtonMnemonic;
626:
627:
628: protected String openButtonText;
629:
630:
631: protected String openButtonToolTipText;
632:
633:
634: protected int saveButtonMnemonic;
635:
636:
637: protected String saveButtonText;
638:
639:
640: protected String saveButtonToolTipText;
641:
642:
643: protected int updateButtonMnemonic;
644:
645:
646: protected String updateButtonText;
647:
648:
649: protected String updateButtonToolTipText;
650:
651:
652: protected Icon upFolderIcon;
653:
654:
655:
656:
657: JFileChooser filechooser;
658:
659:
660: BasicDirectoryModel model;
661:
662:
663: FileFilter acceptAll = new AcceptAllFileFilter();
664:
665:
666: FileView fv = new BasicFileView();
667:
668:
669: JButton accept;
670:
671:
672: JPanel accessoryPanel = new JPanel();
673:
674:
675: PropertyChangeListener propertyChangeListener;
676:
677:
678: String acceptAllFileFilterText;
679:
680:
681: String dirDescText;
682:
683:
684: String fileDescText;
685:
686:
687: boolean dirSelected = false;
688:
689:
690: File currDir = null;
691:
692:
693:
694: JPanel bottomPanel;
695:
696:
697: JPanel closePanel;
698:
699:
700: JTextField entry;
701:
702:
703: String parentPath;
704:
705:
709: private ApproveSelectionAction approveSelectionAction;
710:
711:
715: private CancelSelectionAction cancelSelectionAction;
716:
717:
721: private GoHomeAction goHomeAction;
722:
723:
727: private ChangeToParentDirectoryAction changeToParentDirectoryAction;
728:
729:
733: private NewFolderAction newFolderAction;
734:
735:
739: private UpdateAction updateAction;
740:
741:
742:
743:
746: void closeDialog()
747: {
748: Window owner = SwingUtilities.windowForComponent(filechooser);
749: if (owner instanceof JDialog)
750: ((JDialog) owner).dispose();
751: }
752:
753:
758: public BasicFileChooserUI(JFileChooser b)
759: {
760: }
761:
762:
769: public static ComponentUI createUI(JComponent c)
770: {
771: return new BasicFileChooserUI((JFileChooser) c);
772: }
773:
774:
779: public void installUI(JComponent c)
780: {
781: if (c instanceof JFileChooser)
782: {
783: JFileChooser fc = (JFileChooser) c;
784: this.filechooser = fc;
785: fc.resetChoosableFileFilters();
786: createModel();
787: clearIconCache();
788: installDefaults(fc);
789: installComponents(fc);
790: installListeners(fc);
791:
792: Object path = filechooser.getCurrentDirectory();
793: if (path != null)
794: parentPath = path.toString().substring(path.toString().lastIndexOf("/"));
795: }
796: }
797:
798:
803: public void uninstallUI(JComponent c)
804: {
805: model = null;
806: uninstallListeners(filechooser);
807: uninstallComponents(filechooser);
808: uninstallDefaults(filechooser);
809: filechooser = null;
810: }
811:
812:
813:
814:
815: void boxEntries()
816: {
817: ArrayList parentFiles = new ArrayList();
818: File parent = filechooser.getCurrentDirectory();
819: if (parent == null)
820: parent = filechooser.getFileSystemView().getDefaultDirectory();
821: while (parent != null)
822: {
823: String name = parent.getName();
824: if (name.equals(""))
825: name = parent.getAbsolutePath();
826:
827: parentFiles.add(parentFiles.size(), name);
828: parent = parent.getParentFile();
829: }
830:
831: if (parentFiles.size() == 0)
832: return;
833:
834: }
835:
836:
841: public void installComponents(JFileChooser fc)
842: {
843: }
844:
845:
850: public void uninstallComponents(JFileChooser fc)
851: {
852: }
853:
854:
859: protected void installListeners(JFileChooser fc)
860: {
861: propertyChangeListener = createPropertyChangeListener(filechooser);
862: filechooser.addPropertyChangeListener(propertyChangeListener);
863: }
864:
865:
870: protected void uninstallListeners(JFileChooser fc)
871: {
872: filechooser.removePropertyChangeListener(propertyChangeListener);
873: propertyChangeListener = null;
874: }
875:
876:
881: protected void installDefaults(JFileChooser fc)
882: {
883: installIcons(fc);
884: installStrings(fc);
885: }
886:
887:
892: protected void uninstallDefaults(JFileChooser fc)
893: {
894: uninstallStrings(fc);
895: uninstallIcons(fc);
896: }
897:
898:
903: protected void installIcons(JFileChooser fc)
904: {
905: UIDefaults defaults = UIManager.getLookAndFeelDefaults();
906: computerIcon = MetalIconFactory.getTreeComputerIcon();
907: detailsViewIcon = defaults.getIcon("FileChooser.detailsViewIcon");
908: directoryIcon = new MetalIconFactory.TreeFolderIcon();
909: fileIcon = new MetalIconFactory.TreeLeafIcon();
910: floppyDriveIcon = MetalIconFactory.getTreeFloppyDriveIcon();
911: hardDriveIcon = MetalIconFactory.getTreeHardDriveIcon();
912: homeFolderIcon = defaults.getIcon("FileChooser.homeFolderIcon");
913: listViewIcon = defaults.getIcon("FileChooser.listViewIcon");
914: newFolderIcon = defaults.getIcon("FileChooser.newFolderIcon");
915: upFolderIcon = defaults.getIcon("FileChooser.upFolderIcon");
916: }
917:
918:
923: protected void uninstallIcons(JFileChooser fc)
924: {
925: computerIcon = null;
926: detailsViewIcon = null;
927: directoryIcon = null;
928: fileIcon = null;
929: floppyDriveIcon = null;
930: hardDriveIcon = null;
931: homeFolderIcon = null;
932: listViewIcon = null;
933: newFolderIcon = null;
934: upFolderIcon = null;
935: }
936:
937:
942: protected void installStrings(JFileChooser fc)
943: {
944: UIDefaults defaults = UIManager.getLookAndFeelDefaults();
945:
946: dirDescText = defaults.getString("FileChooser.directoryDescriptionText");
947: fileDescText = defaults.getString("FileChooser.fileDescriptionText");
948:
949: acceptAllFileFilterText = defaults.getString("FileChooser.acceptAllFileFilterText");
950: cancelButtonText = "Cancel";
951: cancelButtonToolTipText = "Abort file chooser dialog";
952: cancelButtonMnemonic = new Integer((String) UIManager.get("FileChooser.cancelButtonMnemonic")).intValue();
953:
954: directoryOpenButtonText = "Open";
955: directoryOpenButtonToolTipText = "Open selected directory";
956: directoryOpenButtonMnemonic
957: = new Integer((String) UIManager.get("FileChooser.directoryOpenButtonMnemonic")).intValue();
958:
959: helpButtonText = "Help";
960: helpButtonToolTipText = "FileChooser help";
961: helpButtonMnemonic = new Integer((String) UIManager.get("FileChooser.helpButtonMnemonic")).intValue();
962:
963: openButtonText = "Open";
964: openButtonToolTipText = "Open selected file";
965: openButtonMnemonic = new Integer((String) UIManager.get("FileChooser.openButtonMnemonic")).intValue();
966:
967: saveButtonText = "Save";
968: saveButtonToolTipText = "Save selected file";
969: saveButtonMnemonic = new Integer((String) UIManager.get("FileChooser.saveButtonMnemonic")).intValue();
970:
971: updateButtonText = "Update";
972: updateButtonToolTipText = "Update directory listing";
973: updateButtonMnemonic = new Integer((String) UIManager.get("FileChooser.updateButtonMnemonic")).intValue();
974: }
975:
976:
981: protected void uninstallStrings(JFileChooser fc)
982: {
983: acceptAllFileFilterText = null;
984: dirDescText = null;
985: fileDescText = null;
986:
987: cancelButtonText = null;
988: cancelButtonToolTipText = null;
989:
990: directoryOpenButtonText = null;
991: directoryOpenButtonToolTipText = null;
992:
993: helpButtonText = null;
994: helpButtonToolTipText = null;
995:
996: openButtonText = null;
997: openButtonToolTipText = null;
998:
999: saveButtonText = null;
1000: saveButtonToolTipText = null;
1001:
1002: updateButtonText = null;
1003: updateButtonToolTipText = null;
1004: }
1005:
1006:
1009: protected void createModel()
1010: {
1011: model = new BasicDirectoryModel(filechooser);
1012: }
1013:
1014:
1019: public BasicDirectoryModel getModel()
1020: {
1021: return model;
1022: }
1023:
1024:
1032: public PropertyChangeListener createPropertyChangeListener(JFileChooser fc)
1033: {
1034: return new PropertyChangeListener()
1035: {
1036: public void propertyChange(PropertyChangeEvent e)
1037: {
1038: }
1039: };
1040: }
1041:
1042:
1047: public String getFileName()
1048: {
1049:
1050:
1051: return null;
1052: }
1053:
1054:
1061: public String getDirectoryName()
1062: {
1063:
1064: return null;
1065: }
1066:
1067:
1074: public void setFileName(String filename)
1075: {
1076:
1077:
1078:
1079: }
1080:
1081:
1088: public void setDirectoryName(String dirname)
1089: {
1090:
1091: }
1092:
1093:
1098: public void rescanCurrentDirectory(JFileChooser fc)
1099: {
1100: getModel().validateFileCache();
1101: }
1102:
1103:
1109: public void ensureFileIsVisible(JFileChooser fc, File f)
1110: {
1111:
1112: }
1113:
1114:
1120: public JFileChooser getFileChooser()
1121: {
1122: return filechooser;
1123: }
1124:
1125:
1130: public JPanel getAccessoryPanel()
1131: {
1132: return accessoryPanel;
1133: }
1134:
1135:
1142: protected JButton getApproveButton(JFileChooser fc)
1143: {
1144: return accept;
1145: }
1146:
1147:
1157: public String getApproveButtonToolTipText(JFileChooser fc)
1158: {
1159: if (fc.getApproveButtonToolTipText() != null)
1160: return fc.getApproveButtonToolTipText();
1161: else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1162: return saveButtonToolTipText;
1163: else
1164: return openButtonToolTipText;
1165: }
1166:
1167:
1170: public void clearIconCache()
1171: {
1172: if (fv instanceof BasicFileView)
1173: ((BasicFileView) fv).clearIconCache();
1174: }
1175:
1176:
1183: public ListSelectionListener createListSelectionListener(JFileChooser fc)
1184: {
1185: return new SelectionListener();
1186: }
1187:
1188:
1196: protected MouseListener createDoubleClickListener(JFileChooser fc, JList list)
1197: {
1198: return new DoubleClickListener(list);
1199: }
1200:
1201:
1207: protected boolean isDirectorySelected()
1208: {
1209: return dirSelected;
1210: }
1211:
1212:
1217: protected void setDirectorySelected(boolean selected)
1218: {
1219: dirSelected = selected;
1220: }
1221:
1222:
1227: protected File getDirectory()
1228: {
1229: return currDir;
1230: }
1231:
1232:
1237: protected void setDirectory(File f)
1238: {
1239: currDir = f;
1240: }
1241:
1242:
1249: public FileFilter getAcceptAllFileFilter(JFileChooser fc)
1250: {
1251: return acceptAll;
1252: }
1253:
1254:
1264: public FileView getFileView(JFileChooser fc)
1265: {
1266: return fv;
1267: }
1268:
1269:
1278: public String getDialogTitle(JFileChooser fc)
1279: {
1280: String result = fc.getDialogTitle();
1281: if (result == null)
1282: result = getApproveButtonText(fc);
1283: return result;
1284: }
1285:
1286:
1295: public int getApproveButtonMnemonic(JFileChooser fc)
1296: {
1297: if (fc.getApproveButtonMnemonic() != 0)
1298: return fc.getApproveButtonMnemonic();
1299: else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1300: return saveButtonMnemonic;
1301: else
1302: return openButtonMnemonic;
1303: }
1304:
1305:
1314: public String getApproveButtonText(JFileChooser fc)
1315: {
1316: String result = fc.getApproveButtonText();
1317: if (result == null)
1318: {
1319: if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1320: result = saveButtonText;
1321: else
1322: result = openButtonText;
1323: }
1324: return result;
1325: }
1326:
1327:
1333: public Action getNewFolderAction()
1334: {
1335: if (newFolderAction == null)
1336: newFolderAction = new NewFolderAction();
1337: return newFolderAction;
1338: }
1339:
1340:
1346: public Action getGoHomeAction()
1347: {
1348: if (goHomeAction == null)
1349: goHomeAction = new GoHomeAction();
1350: return goHomeAction;
1351: }
1352:
1353:
1358: public Action getChangeToParentDirectoryAction()
1359: {
1360: if (changeToParentDirectoryAction == null)
1361: changeToParentDirectoryAction = new ChangeToParentDirectoryAction();
1362: return changeToParentDirectoryAction;
1363: }
1364:
1365:
1370: public Action getApproveSelectionAction()
1371: {
1372: if (approveSelectionAction == null)
1373: approveSelectionAction = new ApproveSelectionAction();
1374: return approveSelectionAction;
1375: }
1376:
1377:
1382: public Action getCancelSelectionAction()
1383: {
1384: if (cancelSelectionAction == null)
1385: cancelSelectionAction = new CancelSelectionAction();
1386: return cancelSelectionAction;
1387: }
1388:
1389:
1394: public Action getUpdateAction()
1395: {
1396: if (updateAction == null)
1397: updateAction = new UpdateAction();
1398: return updateAction;
1399: }
1400: }