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: else
161: {
162: File f = new File(filechooser.getCurrentDirectory(), getFileName());
163: if ( selectedDir != null )
164: f = selectedDir;
165: if (filechooser.isTraversable(f))
166: {
167: filechooser.setCurrentDirectory(f);
168: filechooser.rescanCurrentDirectory();
169: }
170: else
171: {
172: filechooser.setSelectedFile(f);
173: filechooser.approveSelection();
174: closeDialog();
175: }
176: }
177: }
178: }
179:
180:
183: protected class BasicFileView extends FileView
184: {
185:
186: protected Hashtable<File, Icon> iconCache = new Hashtable<File, Icon>();
187:
188:
191: public BasicFileView()
192: {
193:
194: }
195:
196:
202: public void cacheIcon(File f, Icon i)
203: {
204: iconCache.put(f, i);
205: }
206:
207:
210: public void clearIconCache()
211: {
212: iconCache.clear();
213: }
214:
215:
223: public Icon getCachedIcon(File f)
224: {
225: return (Icon) iconCache.get(f);
226: }
227:
228:
237: public String getDescription(File f)
238: {
239: return getName(f);
240: }
241:
242:
249: public Icon getIcon(File f)
250: {
251: Icon val = getCachedIcon(f);
252: if (val != null)
253: return val;
254: if (filechooser.isTraversable(f))
255: val = directoryIcon;
256: else
257: val = fileIcon;
258: cacheIcon(f, val);
259: return val;
260: }
261:
262:
269: public String getName(File f)
270: {
271: String name = null;
272: if (f != null)
273: {
274: JFileChooser c = getFileChooser();
275: FileSystemView v = c.getFileSystemView();
276: name = v.getSystemDisplayName(f);
277: }
278: return name;
279: }
280:
281:
288: public String getTypeDescription(File f)
289: {
290: if (filechooser.isTraversable(f))
291: return dirDescText;
292: else
293: return fileDescText;
294: }
295:
296:
304: public Boolean isHidden(File f)
305: {
306: return Boolean.valueOf(filechooser.getFileSystemView().isHiddenFile(f));
307: }
308: }
309:
310:
315: protected class CancelSelectionAction extends AbstractAction
316: {
317:
320: protected CancelSelectionAction()
321: {
322: super(null);
323: }
324:
325:
330: public void actionPerformed(ActionEvent e)
331: {
332: filechooser.setSelectedFile(null);
333: filechooser.setSelectedFiles(null);
334: filechooser.cancelSelection();
335: closeDialog();
336: }
337: }
338:
339:
345: protected class ChangeToParentDirectoryAction extends AbstractAction
346: {
347:
350: protected ChangeToParentDirectoryAction()
351: {
352: super("Go Up");
353: }
354:
355:
360: public void actionPerformed(ActionEvent e)
361: {
362: filechooser.changeToParentDirectory();
363: filechooser.revalidate();
364: filechooser.repaint();
365: }
366: }
367:
368:
373: protected class DoubleClickListener extends MouseAdapter
374: {
375:
376:
377: private Object lastSelected;
378:
379:
380: private JList list;
381:
382:
387: public DoubleClickListener(JList list)
388: {
389: this.list = list;
390: lastSelected = list.getSelectedValue();
391: setDirectorySelected(false);
392: }
393:
394:
399: public void mouseClicked(MouseEvent e)
400: {
401: Object p = list.getSelectedValue();
402: if (p == null)
403: return;
404: FileSystemView fsv = filechooser.getFileSystemView();
405: if (e.getClickCount() >= 2 && lastSelected != null &&
406: p.toString().equals(lastSelected.toString()))
407: {
408: File f = fsv.createFileObject(lastSelected.toString());
409: if (filechooser.isTraversable(f))
410: {
411: filechooser.setCurrentDirectory(f);
412: filechooser.rescanCurrentDirectory();
413: }
414: else
415: {
416: filechooser.setSelectedFile(f);
417: filechooser.approveSelection();
418: closeDialog();
419: }
420: }
421: else
422: {
423: String path = p.toString();
424: File f = fsv.createFileObject(path);
425: filechooser.setSelectedFile(f);
426:
427: if (filechooser.isMultiSelectionEnabled())
428: {
429: int[] inds = list.getSelectedIndices();
430: File[] allFiles = new File[inds.length];
431: for (int i = 0; i < inds.length; i++)
432: allFiles[i] = (File) list.getModel().getElementAt(inds[i]);
433: filechooser.setSelectedFiles(allFiles);
434: }
435:
436: if (filechooser.isTraversable(f))
437: {
438: setDirectorySelected(true);
439: setDirectory(f);
440: }
441: else
442: {
443: setDirectorySelected(false);
444: setDirectory(null);
445: }
446: lastSelected = path;
447: parentPath = f.getParent();
448:
449: if (f.isFile())
450: setFileName(f.getName());
451: else if (filechooser.getFileSelectionMode() !=
452: JFileChooser.FILES_ONLY)
453: setFileName(path);
454: }
455: }
456:
457:
462: public void mouseEntered(MouseEvent e)
463: {
464:
465: }
466: }
467:
468:
474: protected class GoHomeAction extends AbstractAction
475: {
476:
479: protected GoHomeAction()
480: {
481: super("Go Home");
482: }
483:
484:
490: public void actionPerformed(ActionEvent e)
491: {
492: filechooser.setCurrentDirectory(filechooser.getFileSystemView()
493: .getHomeDirectory());
494: filechooser.revalidate();
495: filechooser.repaint();
496: }
497: }
498:
499:
504: protected class NewFolderAction extends AbstractAction
505: {
506:
509: protected NewFolderAction()
510: {
511: super("New Folder");
512: }
513:
514:
519: public void actionPerformed(ActionEvent e)
520: {
521: try
522: {
523: filechooser.getFileSystemView().createNewFolder(filechooser
524: .getCurrentDirectory());
525: }
526: catch (IOException ioe)
527: {
528: return;
529: }
530: filechooser.rescanCurrentDirectory();
531: filechooser.repaint();
532: }
533: }
534:
535:
540: protected class SelectionListener implements ListSelectionListener
541: {
542:
545: protected SelectionListener()
546: {
547:
548: }
549:
550:
555: public void valueChanged(ListSelectionEvent e)
556: {
557: JList list = (JList) e.getSource();
558: Object f = list.getSelectedValue();
559: if (f == null)
560: return;
561: File file = filechooser.getFileSystemView().createFileObject(f.toString());
562: if (! filechooser.isTraversable(file))
563: {
564: selectedDir = null;
565: filechooser.setSelectedFile(file);
566: }
567: else
568: {
569: selectedDir = file;
570: filechooser.setSelectedFile(null);
571: }
572: }
573: }
574:
575:
580: protected class UpdateAction extends AbstractAction
581: {
582:
585: protected UpdateAction()
586: {
587: super(null);
588: }
589:
590:
595: public void actionPerformed(ActionEvent e)
596: {
597:
598: }
599: }
600:
601:
602: protected int cancelButtonMnemonic;
603:
604:
605: protected String cancelButtonText;
606:
607:
608: protected String cancelButtonToolTipText;
609:
610:
611: protected Icon computerIcon;
612:
613:
614: protected Icon detailsViewIcon;
615:
616:
617: protected Icon directoryIcon;
618:
619:
620: protected int directoryOpenButtonMnemonic;
621:
622:
623: protected String directoryOpenButtonText;
624:
625:
626: protected String directoryOpenButtonToolTipText;
627:
628:
629: protected Icon fileIcon;
630:
631:
632: protected Icon floppyDriveIcon;
633:
634:
635: protected Icon hardDriveIcon;
636:
637:
638: protected int helpButtonMnemonic;
639:
640:
641: protected String helpButtonText;
642:
643:
644: protected String helpButtonToolTipText;
645:
646:
647: protected Icon homeFolderIcon;
648:
649:
650: protected Icon listViewIcon;
651:
652:
653: protected Icon newFolderIcon = directoryIcon;
654:
655:
656: protected int openButtonMnemonic;
657:
658:
659: protected String openButtonText;
660:
661:
662: protected String openButtonToolTipText;
663:
664:
665: protected int saveButtonMnemonic;
666:
667:
668: protected String saveButtonText;
669:
670:
671: protected String saveButtonToolTipText;
672:
673:
674: protected int updateButtonMnemonic;
675:
676:
677: protected String updateButtonText;
678:
679:
680: protected String updateButtonToolTipText;
681:
682:
683: protected Icon upFolderIcon;
684:
685:
686:
687:
688: JFileChooser filechooser;
689:
690:
691: BasicDirectoryModel model;
692:
693:
694: FileFilter acceptAll = new AcceptAllFileFilter();
695:
696:
697: FileView fv = new BasicFileView();
698:
699:
700: JButton accept;
701:
702:
703: JPanel accessoryPanel = new JPanel();
704:
705:
706: PropertyChangeListener propertyChangeListener;
707:
708:
709: String acceptAllFileFilterText;
710:
711:
712: String dirDescText;
713:
714:
715: String fileDescText;
716:
717:
718: boolean dirSelected;
719:
720:
721: File currDir;
722:
723:
724:
725: JPanel bottomPanel;
726:
727:
728: JPanel closePanel;
729:
730:
731: JTextField entry;
732:
733:
734: String parentPath;
735:
736:
740: private ApproveSelectionAction approveSelectionAction;
741:
742:
746: private CancelSelectionAction cancelSelectionAction;
747:
748:
752: private GoHomeAction goHomeAction;
753:
754:
758: private ChangeToParentDirectoryAction changeToParentDirectoryAction;
759:
760:
764: private NewFolderAction newFolderAction;
765:
766:
770: private UpdateAction updateAction;
771:
772:
777: private File selectedDir;
778:
779:
780:
781:
784: void closeDialog()
785: {
786: Window owner = SwingUtilities.windowForComponent(filechooser);
787: if (owner instanceof JDialog)
788: ((JDialog) owner).dispose();
789: }
790:
791:
796: public BasicFileChooserUI(JFileChooser b)
797: {
798: }
799:
800:
807: public static ComponentUI createUI(JComponent c)
808: {
809: return new BasicFileChooserUI((JFileChooser) c);
810: }
811:
812:
817: public void installUI(JComponent c)
818: {
819: if (c instanceof JFileChooser)
820: {
821: JFileChooser fc = (JFileChooser) c;
822: this.filechooser = fc;
823: fc.resetChoosableFileFilters();
824: createModel();
825: clearIconCache();
826: installDefaults(fc);
827: installComponents(fc);
828: installListeners(fc);
829:
830: File path = filechooser.getCurrentDirectory();
831: if (path != null)
832: parentPath = path.getParent();
833: }
834: }
835:
836:
841: public void uninstallUI(JComponent c)
842: {
843: model = null;
844: uninstallListeners(filechooser);
845: uninstallComponents(filechooser);
846: uninstallDefaults(filechooser);
847: filechooser = null;
848: }
849:
850:
851:
852:
853: void boxEntries()
854: {
855: ArrayList parentFiles = new ArrayList();
856: File parent = filechooser.getCurrentDirectory();
857: if (parent == null)
858: parent = filechooser.getFileSystemView().getDefaultDirectory();
859: while (parent != null)
860: {
861: String name = parent.getName();
862: if (name.equals(""))
863: name = parent.getAbsolutePath();
864:
865: parentFiles.add(parentFiles.size(), name);
866: parent = parent.getParentFile();
867: }
868:
869: if (parentFiles.size() == 0)
870: return;
871:
872: }
873:
874:
879: public void installComponents(JFileChooser fc)
880: {
881: }
882:
883:
888: public void uninstallComponents(JFileChooser fc)
889: {
890: }
891:
892:
897: protected void installListeners(JFileChooser fc)
898: {
899: propertyChangeListener = createPropertyChangeListener(filechooser);
900: if (propertyChangeListener != null)
901: filechooser.addPropertyChangeListener(propertyChangeListener);
902: fc.addPropertyChangeListener(getModel());
903: }
904:
905:
910: protected void uninstallListeners(JFileChooser fc)
911: {
912: if (propertyChangeListener != null)
913: {
914: filechooser.removePropertyChangeListener(propertyChangeListener);
915: propertyChangeListener = null;
916: }
917: fc.removePropertyChangeListener(getModel());
918: }
919:
920:
925: protected void installDefaults(JFileChooser fc)
926: {
927: installIcons(fc);
928: installStrings(fc);
929: }
930:
931:
936: protected void uninstallDefaults(JFileChooser fc)
937: {
938: uninstallStrings(fc);
939: uninstallIcons(fc);
940: }
941:
942:
947: protected void installIcons(JFileChooser fc)
948: {
949: UIDefaults defaults = UIManager.getLookAndFeelDefaults();
950: computerIcon = MetalIconFactory.getTreeComputerIcon();
951: detailsViewIcon = defaults.getIcon("FileChooser.detailsViewIcon");
952: directoryIcon = new MetalIconFactory.TreeFolderIcon();
953: fileIcon = new MetalIconFactory.TreeLeafIcon();
954: floppyDriveIcon = MetalIconFactory.getTreeFloppyDriveIcon();
955: hardDriveIcon = MetalIconFactory.getTreeHardDriveIcon();
956: homeFolderIcon = defaults.getIcon("FileChooser.homeFolderIcon");
957: listViewIcon = defaults.getIcon("FileChooser.listViewIcon");
958: newFolderIcon = defaults.getIcon("FileChooser.newFolderIcon");
959: upFolderIcon = defaults.getIcon("FileChooser.upFolderIcon");
960: }
961:
962:
967: protected void uninstallIcons(JFileChooser fc)
968: {
969: computerIcon = null;
970: detailsViewIcon = null;
971: directoryIcon = null;
972: fileIcon = null;
973: floppyDriveIcon = null;
974: hardDriveIcon = null;
975: homeFolderIcon = null;
976: listViewIcon = null;
977: newFolderIcon = null;
978: upFolderIcon = null;
979: }
980:
981:
986: protected void installStrings(JFileChooser fc)
987: {
988: UIDefaults defaults = UIManager.getLookAndFeelDefaults();
989:
990: dirDescText = defaults.getString("FileChooser.directoryDescriptionText");
991: fileDescText = defaults.getString("FileChooser.fileDescriptionText");
992:
993: acceptAllFileFilterText = defaults.getString("FileChooser.acceptAllFileFilterText");
994: cancelButtonText = "Cancel";
995: cancelButtonToolTipText = "Abort file chooser dialog";
996: cancelButtonMnemonic = new Integer((String) UIManager.get("FileChooser.cancelButtonMnemonic")).intValue();
997:
998: directoryOpenButtonText = "Open";
999: directoryOpenButtonToolTipText = "Open selected directory";
1000: directoryOpenButtonMnemonic
1001: = new Integer((String) UIManager.get("FileChooser.directoryOpenButtonMnemonic")).intValue();
1002:
1003: helpButtonText = "Help";
1004: helpButtonToolTipText = "FileChooser help";
1005: helpButtonMnemonic = new Integer((String) UIManager.get("FileChooser.helpButtonMnemonic")).intValue();
1006:
1007: openButtonText = "Open";
1008: openButtonToolTipText = "Open selected file";
1009: openButtonMnemonic = new Integer((String) UIManager.get("FileChooser.openButtonMnemonic")).intValue();
1010:
1011: saveButtonText = "Save";
1012: saveButtonToolTipText = "Save selected file";
1013: saveButtonMnemonic = new Integer((String) UIManager.get("FileChooser.saveButtonMnemonic")).intValue();
1014:
1015: updateButtonText = "Update";
1016: updateButtonToolTipText = "Update directory listing";
1017: updateButtonMnemonic = new Integer((String) UIManager.get("FileChooser.updateButtonMnemonic")).intValue();
1018: }
1019:
1020:
1025: protected void uninstallStrings(JFileChooser fc)
1026: {
1027: acceptAllFileFilterText = null;
1028: dirDescText = null;
1029: fileDescText = null;
1030:
1031: cancelButtonText = null;
1032: cancelButtonToolTipText = null;
1033:
1034: directoryOpenButtonText = null;
1035: directoryOpenButtonToolTipText = null;
1036:
1037: helpButtonText = null;
1038: helpButtonToolTipText = null;
1039:
1040: openButtonText = null;
1041: openButtonToolTipText = null;
1042:
1043: saveButtonText = null;
1044: saveButtonToolTipText = null;
1045:
1046: updateButtonText = null;
1047: updateButtonToolTipText = null;
1048: }
1049:
1050:
1053: protected void createModel()
1054: {
1055: model = new BasicDirectoryModel(filechooser);
1056: }
1057:
1058:
1063: public BasicDirectoryModel getModel()
1064: {
1065: return model;
1066: }
1067:
1068:
1076: public PropertyChangeListener createPropertyChangeListener(JFileChooser fc)
1077: {
1078:
1079: return null;
1080: }
1081:
1082:
1087: public String getFileName()
1088: {
1089: return entry.getText();
1090: }
1091:
1092:
1099: public String getDirectoryName()
1100: {
1101:
1102: return null;
1103: }
1104:
1105:
1112: public void setFileName(String filename)
1113: {
1114:
1115:
1116:
1117: }
1118:
1119:
1126: public void setDirectoryName(String dirname)
1127: {
1128:
1129: }
1130:
1131:
1136: public void rescanCurrentDirectory(JFileChooser fc)
1137: {
1138: getModel().validateFileCache();
1139: }
1140:
1141:
1147: public void ensureFileIsVisible(JFileChooser fc, File f)
1148: {
1149:
1150: }
1151:
1152:
1158: public JFileChooser getFileChooser()
1159: {
1160: return filechooser;
1161: }
1162:
1163:
1168: public JPanel getAccessoryPanel()
1169: {
1170: return accessoryPanel;
1171: }
1172:
1173:
1180: protected JButton getApproveButton(JFileChooser fc)
1181: {
1182: return accept;
1183: }
1184:
1185:
1195: public String getApproveButtonToolTipText(JFileChooser fc)
1196: {
1197: if (fc.getApproveButtonToolTipText() != null)
1198: return fc.getApproveButtonToolTipText();
1199: else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1200: return saveButtonToolTipText;
1201: else
1202: return openButtonToolTipText;
1203: }
1204:
1205:
1208: public void clearIconCache()
1209: {
1210: if (fv instanceof BasicFileView)
1211: ((BasicFileView) fv).clearIconCache();
1212: }
1213:
1214:
1221: public ListSelectionListener createListSelectionListener(JFileChooser fc)
1222: {
1223: return new SelectionListener();
1224: }
1225:
1226:
1234: protected MouseListener createDoubleClickListener(JFileChooser fc, JList list)
1235: {
1236: return new DoubleClickListener(list);
1237: }
1238:
1239:
1245: protected boolean isDirectorySelected()
1246: {
1247: return dirSelected;
1248: }
1249:
1250:
1255: protected void setDirectorySelected(boolean selected)
1256: {
1257: dirSelected = selected;
1258: }
1259:
1260:
1265: protected File getDirectory()
1266: {
1267: return currDir;
1268: }
1269:
1270:
1275: protected void setDirectory(File f)
1276: {
1277: currDir = f;
1278: }
1279:
1280:
1287: public FileFilter getAcceptAllFileFilter(JFileChooser fc)
1288: {
1289: return acceptAll;
1290: }
1291:
1292:
1302: public FileView getFileView(JFileChooser fc)
1303: {
1304: return fv;
1305: }
1306:
1307:
1316: public String getDialogTitle(JFileChooser fc)
1317: {
1318: String result = fc.getDialogTitle();
1319: if (result == null)
1320: result = getApproveButtonText(fc);
1321: return result;
1322: }
1323:
1324:
1333: public int getApproveButtonMnemonic(JFileChooser fc)
1334: {
1335: if (fc.getApproveButtonMnemonic() != 0)
1336: return fc.getApproveButtonMnemonic();
1337: else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1338: return saveButtonMnemonic;
1339: else
1340: return openButtonMnemonic;
1341: }
1342:
1343:
1352: public String getApproveButtonText(JFileChooser fc)
1353: {
1354: String result = fc.getApproveButtonText();
1355: if (result == null)
1356: {
1357: if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1358: result = saveButtonText;
1359: else
1360: result = openButtonText;
1361: }
1362: return result;
1363: }
1364:
1365:
1371: public Action getNewFolderAction()
1372: {
1373: if (newFolderAction == null)
1374: newFolderAction = new NewFolderAction();
1375: return newFolderAction;
1376: }
1377:
1378:
1384: public Action getGoHomeAction()
1385: {
1386: if (goHomeAction == null)
1387: goHomeAction = new GoHomeAction();
1388: return goHomeAction;
1389: }
1390:
1391:
1396: public Action getChangeToParentDirectoryAction()
1397: {
1398: if (changeToParentDirectoryAction == null)
1399: changeToParentDirectoryAction = new ChangeToParentDirectoryAction();
1400: return changeToParentDirectoryAction;
1401: }
1402:
1403:
1408: public Action getApproveSelectionAction()
1409: {
1410: if (approveSelectionAction == null)
1411: approveSelectionAction = new ApproveSelectionAction();
1412: return approveSelectionAction;
1413: }
1414:
1415:
1420: public Action getCancelSelectionAction()
1421: {
1422: if (cancelSelectionAction == null)
1423: cancelSelectionAction = new CancelSelectionAction();
1424: return cancelSelectionAction;
1425: }
1426:
1427:
1432: public Action getUpdateAction()
1433: {
1434: if (updateAction == null)
1435: updateAction = new UpdateAction();
1436: return updateAction;
1437: }
1438: }