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