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 (filechooser.isTraversable(f))
164: {
165: filechooser.setCurrentDirectory(f);
166: filechooser.rescanCurrentDirectory();
167: }
168: else
169: {
170: filechooser.setSelectedFile(f);
171: filechooser.approveSelection();
172: closeDialog();
173: }
174: }
175: }
176: }
177:
178:
181: protected class BasicFileView extends FileView
182: {
183:
184: protected Hashtable iconCache = new Hashtable();
185:
186:
189: public BasicFileView()
190: {
191:
192: }
193:
194:
200: public void cacheIcon(File f, Icon i)
201: {
202: iconCache.put(f, i);
203: }
204:
205:
208: public void clearIconCache()
209: {
210: iconCache.clear();
211: }
212:
213:
221: public Icon getCachedIcon(File f)
222: {
223: return (Icon) iconCache.get(f);
224: }
225:
226:
235: public String getDescription(File f)
236: {
237: return getName(f);
238: }
239:
240:
247: public Icon getIcon(File f)
248: {
249: Icon val = getCachedIcon(f);
250: if (val != null)
251: return val;
252: if (filechooser.isTraversable(f))
253: val = directoryIcon;
254: else
255: val = fileIcon;
256: cacheIcon(f, val);
257: return val;
258: }
259:
260:
267: public String getName(File f)
268: {
269: return f.getName();
270: }
271:
272:
279: public String getTypeDescription(File f)
280: {
281: if (filechooser.isTraversable(f))
282: return dirDescText;
283: else
284: return fileDescText;
285: }
286:
287:
295: public Boolean isHidden(File f)
296: {
297: return Boolean.valueOf(filechooser.getFileSystemView().isHiddenFile(f));
298: }
299: }
300:
301:
306: protected class CancelSelectionAction extends AbstractAction
307: {
308:
311: protected CancelSelectionAction()
312: {
313: super(null);
314: }
315:
316:
321: public void actionPerformed(ActionEvent e)
322: {
323: filechooser.setSelectedFile(null);
324: filechooser.setSelectedFiles(null);
325: filechooser.cancelSelection();
326: closeDialog();
327: }
328: }
329:
330:
336: protected class ChangeToParentDirectoryAction extends AbstractAction
337: {
338:
341: protected ChangeToParentDirectoryAction()
342: {
343: super("Go Up");
344: }
345:
346:
351: public void actionPerformed(ActionEvent e)
352: {
353: filechooser.changeToParentDirectory();
354: filechooser.revalidate();
355: filechooser.repaint();
356: }
357: }
358:
359:
364: protected class DoubleClickListener extends MouseAdapter
365: {
366:
367:
368: private Object lastSelected;
369:
370:
371: private JList list;
372:
373:
378: public DoubleClickListener(JList list)
379: {
380: this.list = list;
381: lastSelected = list.getSelectedValue();
382: setDirectorySelected(false);
383: }
384:
385:
390: public void mouseClicked(MouseEvent e)
391: {
392: Object p = list.getSelectedValue();
393: if (p == null)
394: return;
395: FileSystemView fsv = filechooser.getFileSystemView();
396: if (e.getClickCount() >= 2 && lastSelected != null &&
397: p.toString().equals(lastSelected.toString()))
398: {
399: File f = fsv.createFileObject(lastSelected.toString());
400: if (filechooser.isTraversable(f))
401: {
402: filechooser.setCurrentDirectory(f);
403: filechooser.rescanCurrentDirectory();
404: }
405: else
406: {
407: filechooser.setSelectedFile(f);
408: filechooser.approveSelection();
409: closeDialog();
410: }
411: }
412: else
413: {
414: String path = p.toString();
415: File f = fsv.createFileObject(path);
416: filechooser.setSelectedFile(f);
417:
418: if (filechooser.isMultiSelectionEnabled())
419: {
420: int[] inds = list.getSelectedIndices();
421: File[] allFiles = new File[inds.length];
422: for (int i = 0; i < inds.length; i++)
423: allFiles[i] = (File) list.getModel().getElementAt(inds[i]);
424: filechooser.setSelectedFiles(allFiles);
425: }
426:
427: if (filechooser.isTraversable(f))
428: {
429: setDirectorySelected(true);
430: setDirectory(f);
431: }
432: else
433: {
434: setDirectorySelected(false);
435: setDirectory(null);
436: }
437: lastSelected = path;
438: parentPath = path.substring(0, path.lastIndexOf("/") + 1);
439: if (f.isFile())
440: setFileName(path.substring(path.lastIndexOf("/") + 1));
441: else if (filechooser.getFileSelectionMode() ==
442: JFileChooser.DIRECTORIES_ONLY)
443: setFileName(path);
444: }
445: }
446:
447:
452: public void mouseEntered(MouseEvent e)
453: {
454:
455: }
456: }
457:
458:
464: protected class GoHomeAction extends AbstractAction
465: {
466:
469: protected GoHomeAction()
470: {
471: super("Go Home");
472: }
473:
474:
480: public void actionPerformed(ActionEvent e)
481: {
482: filechooser.setCurrentDirectory(filechooser.getFileSystemView()
483: .getHomeDirectory());
484: filechooser.revalidate();
485: filechooser.repaint();
486: }
487: }
488:
489:
494: protected class NewFolderAction extends AbstractAction
495: {
496:
499: protected NewFolderAction()
500: {
501: super("New Folder");
502: }
503:
504:
509: public void actionPerformed(ActionEvent e)
510: {
511: try
512: {
513: filechooser.getFileSystemView().createNewFolder(filechooser
514: .getCurrentDirectory());
515: }
516: catch (IOException ioe)
517: {
518: return;
519: }
520: filechooser.rescanCurrentDirectory();
521: filechooser.repaint();
522: }
523: }
524:
525:
530: protected class SelectionListener implements ListSelectionListener
531: {
532:
535: protected SelectionListener()
536: {
537:
538: }
539:
540:
545: public void valueChanged(ListSelectionEvent e)
546: {
547: JList list = (JList) e.getSource();
548: Object f = list.getSelectedValue();
549: if (f == null)
550: return;
551: File file = filechooser.getFileSystemView().createFileObject(f.toString());
552: if (! filechooser.isTraversable(file))
553: filechooser.setSelectedFile(file);
554: else
555: filechooser.setSelectedFile(null);
556: }
557: }
558:
559:
564: protected class UpdateAction extends AbstractAction
565: {
566:
569: protected UpdateAction()
570: {
571: super(null);
572: }
573:
574:
579: public void actionPerformed(ActionEvent e)
580: {
581:
582: }
583: }
584:
585:
586: protected int cancelButtonMnemonic;
587:
588:
589: protected String cancelButtonText;
590:
591:
592: protected String cancelButtonToolTipText;
593:
594:
595: protected Icon computerIcon;
596:
597:
598: protected Icon detailsViewIcon;
599:
600:
601: protected Icon directoryIcon;
602:
603:
604: protected int directoryOpenButtonMnemonic;
605:
606:
607: protected String directoryOpenButtonText;
608:
609:
610: protected String directoryOpenButtonToolTipText;
611:
612:
613: protected Icon fileIcon;
614:
615:
616: protected Icon floppyDriveIcon;
617:
618:
619: protected Icon hardDriveIcon;
620:
621:
622: protected int helpButtonMnemonic;
623:
624:
625: protected String helpButtonText;
626:
627:
628: protected String helpButtonToolTipText;
629:
630:
631: protected Icon homeFolderIcon;
632:
633:
634: protected Icon listViewIcon;
635:
636:
637: protected Icon newFolderIcon = directoryIcon;
638:
639:
640: protected int openButtonMnemonic;
641:
642:
643: protected String openButtonText;
644:
645:
646: protected String openButtonToolTipText;
647:
648:
649: protected int saveButtonMnemonic;
650:
651:
652: protected String saveButtonText;
653:
654:
655: protected String saveButtonToolTipText;
656:
657:
658: protected int updateButtonMnemonic;
659:
660:
661: protected String updateButtonText;
662:
663:
664: protected String updateButtonToolTipText;
665:
666:
667: protected Icon upFolderIcon;
668:
669:
670:
671:
672: JFileChooser filechooser;
673:
674:
675: BasicDirectoryModel model;
676:
677:
678: FileFilter acceptAll = new AcceptAllFileFilter();
679:
680:
681: FileView fv = new BasicFileView();
682:
683:
684: JButton accept;
685:
686:
687: JPanel accessoryPanel = new JPanel();
688:
689:
690: PropertyChangeListener propertyChangeListener;
691:
692:
693: String acceptAllFileFilterText;
694:
695:
696: String dirDescText;
697:
698:
699: String fileDescText;
700:
701:
702: boolean dirSelected;
703:
704:
705: File currDir;
706:
707:
708:
709: JPanel bottomPanel;
710:
711:
712: JPanel closePanel;
713:
714:
715: JTextField entry;
716:
717:
718: String parentPath;
719:
720:
724: private ApproveSelectionAction approveSelectionAction;
725:
726:
730: private CancelSelectionAction cancelSelectionAction;
731:
732:
736: private GoHomeAction goHomeAction;
737:
738:
742: private ChangeToParentDirectoryAction changeToParentDirectoryAction;
743:
744:
748: private NewFolderAction newFolderAction;
749:
750:
754: private UpdateAction updateAction;
755:
756:
757:
758:
761: void closeDialog()
762: {
763: Window owner = SwingUtilities.windowForComponent(filechooser);
764: if (owner instanceof JDialog)
765: ((JDialog) owner).dispose();
766: }
767:
768:
773: public BasicFileChooserUI(JFileChooser b)
774: {
775: }
776:
777:
784: public static ComponentUI createUI(JComponent c)
785: {
786: return new BasicFileChooserUI((JFileChooser) c);
787: }
788:
789:
794: public void installUI(JComponent c)
795: {
796: if (c instanceof JFileChooser)
797: {
798: JFileChooser fc = (JFileChooser) c;
799: this.filechooser = fc;
800: fc.resetChoosableFileFilters();
801: createModel();
802: clearIconCache();
803: installDefaults(fc);
804: installComponents(fc);
805: installListeners(fc);
806:
807: Object path = filechooser.getCurrentDirectory();
808: if (path != null)
809: parentPath = path.toString().substring(path.toString().lastIndexOf("/"));
810: }
811: }
812:
813:
818: public void uninstallUI(JComponent c)
819: {
820: model = null;
821: uninstallListeners(filechooser);
822: uninstallComponents(filechooser);
823: uninstallDefaults(filechooser);
824: filechooser = null;
825: }
826:
827:
828:
829:
830: void boxEntries()
831: {
832: ArrayList parentFiles = new ArrayList();
833: File parent = filechooser.getCurrentDirectory();
834: if (parent == null)
835: parent = filechooser.getFileSystemView().getDefaultDirectory();
836: while (parent != null)
837: {
838: String name = parent.getName();
839: if (name.equals(""))
840: name = parent.getAbsolutePath();
841:
842: parentFiles.add(parentFiles.size(), name);
843: parent = parent.getParentFile();
844: }
845:
846: if (parentFiles.size() == 0)
847: return;
848:
849: }
850:
851:
856: public void installComponents(JFileChooser fc)
857: {
858: }
859:
860:
865: public void uninstallComponents(JFileChooser fc)
866: {
867: }
868:
869:
874: protected void installListeners(JFileChooser fc)
875: {
876: propertyChangeListener = createPropertyChangeListener(filechooser);
877: filechooser.addPropertyChangeListener(propertyChangeListener);
878: }
879:
880:
885: protected void uninstallListeners(JFileChooser fc)
886: {
887: filechooser.removePropertyChangeListener(propertyChangeListener);
888: propertyChangeListener = null;
889: }
890:
891:
896: protected void installDefaults(JFileChooser fc)
897: {
898: installIcons(fc);
899: installStrings(fc);
900: }
901:
902:
907: protected void uninstallDefaults(JFileChooser fc)
908: {
909: uninstallStrings(fc);
910: uninstallIcons(fc);
911: }
912:
913:
918: protected void installIcons(JFileChooser fc)
919: {
920: UIDefaults defaults = UIManager.getLookAndFeelDefaults();
921: computerIcon = MetalIconFactory.getTreeComputerIcon();
922: detailsViewIcon = defaults.getIcon("FileChooser.detailsViewIcon");
923: directoryIcon = new MetalIconFactory.TreeFolderIcon();
924: fileIcon = new MetalIconFactory.TreeLeafIcon();
925: floppyDriveIcon = MetalIconFactory.getTreeFloppyDriveIcon();
926: hardDriveIcon = MetalIconFactory.getTreeHardDriveIcon();
927: homeFolderIcon = defaults.getIcon("FileChooser.homeFolderIcon");
928: listViewIcon = defaults.getIcon("FileChooser.listViewIcon");
929: newFolderIcon = defaults.getIcon("FileChooser.newFolderIcon");
930: upFolderIcon = defaults.getIcon("FileChooser.upFolderIcon");
931: }
932:
933:
938: protected void uninstallIcons(JFileChooser fc)
939: {
940: computerIcon = null;
941: detailsViewIcon = null;
942: directoryIcon = null;
943: fileIcon = null;
944: floppyDriveIcon = null;
945: hardDriveIcon = null;
946: homeFolderIcon = null;
947: listViewIcon = null;
948: newFolderIcon = null;
949: upFolderIcon = null;
950: }
951:
952:
957: protected void installStrings(JFileChooser fc)
958: {
959: UIDefaults defaults = UIManager.getLookAndFeelDefaults();
960:
961: dirDescText = defaults.getString("FileChooser.directoryDescriptionText");
962: fileDescText = defaults.getString("FileChooser.fileDescriptionText");
963:
964: acceptAllFileFilterText = defaults.getString("FileChooser.acceptAllFileFilterText");
965: cancelButtonText = "Cancel";
966: cancelButtonToolTipText = "Abort file chooser dialog";
967: cancelButtonMnemonic = new Integer((String) UIManager.get("FileChooser.cancelButtonMnemonic")).intValue();
968:
969: directoryOpenButtonText = "Open";
970: directoryOpenButtonToolTipText = "Open selected directory";
971: directoryOpenButtonMnemonic
972: = new Integer((String) UIManager.get("FileChooser.directoryOpenButtonMnemonic")).intValue();
973:
974: helpButtonText = "Help";
975: helpButtonToolTipText = "FileChooser help";
976: helpButtonMnemonic = new Integer((String) UIManager.get("FileChooser.helpButtonMnemonic")).intValue();
977:
978: openButtonText = "Open";
979: openButtonToolTipText = "Open selected file";
980: openButtonMnemonic = new Integer((String) UIManager.get("FileChooser.openButtonMnemonic")).intValue();
981:
982: saveButtonText = "Save";
983: saveButtonToolTipText = "Save selected file";
984: saveButtonMnemonic = new Integer((String) UIManager.get("FileChooser.saveButtonMnemonic")).intValue();
985:
986: updateButtonText = "Update";
987: updateButtonToolTipText = "Update directory listing";
988: updateButtonMnemonic = new Integer((String) UIManager.get("FileChooser.updateButtonMnemonic")).intValue();
989: }
990:
991:
996: protected void uninstallStrings(JFileChooser fc)
997: {
998: acceptAllFileFilterText = null;
999: dirDescText = null;
1000: fileDescText = null;
1001:
1002: cancelButtonText = null;
1003: cancelButtonToolTipText = null;
1004:
1005: directoryOpenButtonText = null;
1006: directoryOpenButtonToolTipText = null;
1007:
1008: helpButtonText = null;
1009: helpButtonToolTipText = null;
1010:
1011: openButtonText = null;
1012: openButtonToolTipText = null;
1013:
1014: saveButtonText = null;
1015: saveButtonToolTipText = null;
1016:
1017: updateButtonText = null;
1018: updateButtonToolTipText = null;
1019: }
1020:
1021:
1024: protected void createModel()
1025: {
1026: model = new BasicDirectoryModel(filechooser);
1027: }
1028:
1029:
1034: public BasicDirectoryModel getModel()
1035: {
1036: return model;
1037: }
1038:
1039:
1047: public PropertyChangeListener createPropertyChangeListener(JFileChooser fc)
1048: {
1049: return new PropertyChangeListener()
1050: {
1051: public void propertyChange(PropertyChangeEvent e)
1052: {
1053: }
1054: };
1055: }
1056:
1057:
1062: public String getFileName()
1063: {
1064: return entry.getText();
1065: }
1066:
1067:
1074: public String getDirectoryName()
1075: {
1076:
1077: return null;
1078: }
1079:
1080:
1087: public void setFileName(String filename)
1088: {
1089:
1090:
1091:
1092: }
1093:
1094:
1101: public void setDirectoryName(String dirname)
1102: {
1103:
1104: }
1105:
1106:
1111: public void rescanCurrentDirectory(JFileChooser fc)
1112: {
1113: getModel().validateFileCache();
1114: }
1115:
1116:
1122: public void ensureFileIsVisible(JFileChooser fc, File f)
1123: {
1124:
1125: }
1126:
1127:
1133: public JFileChooser getFileChooser()
1134: {
1135: return filechooser;
1136: }
1137:
1138:
1143: public JPanel getAccessoryPanel()
1144: {
1145: return accessoryPanel;
1146: }
1147:
1148:
1155: protected JButton getApproveButton(JFileChooser fc)
1156: {
1157: return accept;
1158: }
1159:
1160:
1170: public String getApproveButtonToolTipText(JFileChooser fc)
1171: {
1172: if (fc.getApproveButtonToolTipText() != null)
1173: return fc.getApproveButtonToolTipText();
1174: else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1175: return saveButtonToolTipText;
1176: else
1177: return openButtonToolTipText;
1178: }
1179:
1180:
1183: public void clearIconCache()
1184: {
1185: if (fv instanceof BasicFileView)
1186: ((BasicFileView) fv).clearIconCache();
1187: }
1188:
1189:
1196: public ListSelectionListener createListSelectionListener(JFileChooser fc)
1197: {
1198: return new SelectionListener();
1199: }
1200:
1201:
1209: protected MouseListener createDoubleClickListener(JFileChooser fc, JList list)
1210: {
1211: return new DoubleClickListener(list);
1212: }
1213:
1214:
1220: protected boolean isDirectorySelected()
1221: {
1222: return dirSelected;
1223: }
1224:
1225:
1230: protected void setDirectorySelected(boolean selected)
1231: {
1232: dirSelected = selected;
1233: }
1234:
1235:
1240: protected File getDirectory()
1241: {
1242: return currDir;
1243: }
1244:
1245:
1250: protected void setDirectory(File f)
1251: {
1252: currDir = f;
1253: }
1254:
1255:
1262: public FileFilter getAcceptAllFileFilter(JFileChooser fc)
1263: {
1264: return acceptAll;
1265: }
1266:
1267:
1277: public FileView getFileView(JFileChooser fc)
1278: {
1279: return fv;
1280: }
1281:
1282:
1291: public String getDialogTitle(JFileChooser fc)
1292: {
1293: String result = fc.getDialogTitle();
1294: if (result == null)
1295: result = getApproveButtonText(fc);
1296: return result;
1297: }
1298:
1299:
1308: public int getApproveButtonMnemonic(JFileChooser fc)
1309: {
1310: if (fc.getApproveButtonMnemonic() != 0)
1311: return fc.getApproveButtonMnemonic();
1312: else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1313: return saveButtonMnemonic;
1314: else
1315: return openButtonMnemonic;
1316: }
1317:
1318:
1327: public String getApproveButtonText(JFileChooser fc)
1328: {
1329: String result = fc.getApproveButtonText();
1330: if (result == null)
1331: {
1332: if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1333: result = saveButtonText;
1334: else
1335: result = openButtonText;
1336: }
1337: return result;
1338: }
1339:
1340:
1346: public Action getNewFolderAction()
1347: {
1348: if (newFolderAction == null)
1349: newFolderAction = new NewFolderAction();
1350: return newFolderAction;
1351: }
1352:
1353:
1359: public Action getGoHomeAction()
1360: {
1361: if (goHomeAction == null)
1362: goHomeAction = new GoHomeAction();
1363: return goHomeAction;
1364: }
1365:
1366:
1371: public Action getChangeToParentDirectoryAction()
1372: {
1373: if (changeToParentDirectoryAction == null)
1374: changeToParentDirectoryAction = new ChangeToParentDirectoryAction();
1375: return changeToParentDirectoryAction;
1376: }
1377:
1378:
1383: public Action getApproveSelectionAction()
1384: {
1385: if (approveSelectionAction == null)
1386: approveSelectionAction = new ApproveSelectionAction();
1387: return approveSelectionAction;
1388: }
1389:
1390:
1395: public Action getCancelSelectionAction()
1396: {
1397: if (cancelSelectionAction == null)
1398: cancelSelectionAction = new CancelSelectionAction();
1399: return cancelSelectionAction;
1400: }
1401:
1402:
1407: public Action getUpdateAction()
1408: {
1409: if (updateAction == null)
1410: updateAction = new UpdateAction();
1411: return updateAction;
1412: }
1413: }