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: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62:
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72: import ;
73: import ;
74: import ;
75: import ;
76: import ;
77: import ;
78: import ;
79: import ;
80: import ;
81: import ;
82: import ;
83: import ;
84: import ;
85: import ;
86: import ;
87: import ;
88: import ;
89: import ;
90:
91:
92:
96: public class BasicFileChooserUI extends FileChooserUI
97: {
98:
101: protected class AcceptAllFileFilter extends FileFilter
102: {
103:
106: public AcceptAllFileFilter()
107: {
108:
109: }
110:
111:
119: public boolean accept(File f)
120: {
121: return true;
122: }
123:
124:
129: public String getDescription()
130: {
131: return acceptAllFileFilterText;
132: }
133: }
134:
135:
140: protected class ApproveSelectionAction extends AbstractAction
141: {
142:
145: protected ApproveSelectionAction()
146: {
147:
148: }
149:
150:
155: public void actionPerformed(ActionEvent e)
156: {
157: Object obj = new String(parentPath + entry.getText());
158: if (obj != null)
159: {
160: File f = filechooser.getFileSystemView().createFileObject(
161: obj.toString());
162: if (filechooser.isTraversable(f)
163: && filechooser.isDirectorySelectionEnabled())
164: filechooser.setCurrentDirectory(f);
165: else
166: {
167: filechooser.setSelectedFile(f);
168: filechooser.approveSelection();
169: closeDialog();
170: }
171: }
172: }
173: }
174:
175:
178: protected class BasicFileView extends FileView
179: {
180:
181: protected Hashtable iconCache = new Hashtable();
182:
183:
186: public BasicFileView()
187: {
188:
189: }
190:
191:
197: public void cacheIcon(File f, Icon i)
198: {
199: iconCache.put(f, i);
200: }
201:
202:
205: public void clearIconCache()
206: {
207: iconCache.clear();
208: }
209:
210:
218: public Icon getCachedIcon(File f)
219: {
220: return (Icon) iconCache.get(f);
221: }
222:
223:
232: public String getDescription(File f)
233: {
234: return getName(f);
235: }
236:
237:
244: public Icon getIcon(File f)
245: {
246: Icon val = getCachedIcon(f);
247: if (val != null)
248: return val;
249: if (filechooser.isTraversable(f))
250: val = directoryIcon;
251: else
252: val = fileIcon;
253: cacheIcon(f, val);
254: return val;
255: }
256:
257:
264: public String getName(File f)
265: {
266: return f.getName();
267: }
268:
269:
276: public String getTypeDescription(File f)
277: {
278: if (filechooser.isTraversable(f))
279: return dirDescText;
280: else
281: return fileDescText;
282: }
283:
284:
292: public Boolean isHidden(File f)
293: {
294: return Boolean.valueOf(filechooser.getFileSystemView().isHiddenFile(f));
295: }
296: }
297:
298:
303: protected class CancelSelectionAction extends AbstractAction
304: {
305:
308: protected CancelSelectionAction()
309: {
310:
311: }
312:
313:
318: public void actionPerformed(ActionEvent e)
319: {
320: filechooser.cancelSelection();
321: closeDialog();
322: }
323: }
324:
325:
331: protected class ChangeToParentDirectoryAction extends AbstractAction
332: {
333:
336: protected ChangeToParentDirectoryAction()
337: {
338:
339: }
340:
341:
346: public void actionPerformed(ActionEvent e)
347: {
348: filechooser.changeToParentDirectory();
349: filechooser.revalidate();
350: filechooser.repaint();
351: }
352: }
353:
354:
359: protected class DoubleClickListener extends MouseAdapter
360: {
361:
362: private Timer timer = null;
363:
364:
365: private Object lastSelected = null;
366:
367:
368: private JList list = null;
369:
370:
375: public DoubleClickListener(JList list)
376: {
377: this.list = list;
378: timer = new Timer(1000, null);
379: timer.setRepeats(false);
380: lastSelected = list.getSelectedValue();
381: setDirectorySelected(false);
382: }
383:
384:
389: public void mouseClicked(MouseEvent e)
390: {
391: if (list.getSelectedValue() == null)
392: return;
393: FileSystemView fsv = filechooser.getFileSystemView();
394: if (timer.isRunning()
395: && list.getSelectedValue().toString().equals(lastSelected.toString()))
396: {
397: File f = fsv.createFileObject(lastSelected.toString());
398: timer.stop();
399: if (filechooser.isTraversable(f))
400: {
401: filechooser.setCurrentDirectory(f);
402: filechooser.rescanCurrentDirectory();
403: }
404: else
405: {
406: filechooser.setSelectedFile(f);
407: filechooser.approveSelection();
408: closeDialog();
409: }
410: }
411: else
412: {
413: String path = list.getSelectedValue().toString();
414: File f = fsv.createFileObject(path);
415: if (filechooser.isTraversable(f))
416: {
417: setDirectorySelected(true);
418: setDirectory(f);
419: }
420: else
421: {
422: setDirectorySelected(false);
423: setDirectory(null);
424: }
425: lastSelected = path;
426: parentPath = path.substring(0, path.lastIndexOf("/") + 1);
427: entry.setText(path.substring(path.lastIndexOf("/") + 1));
428: timer.restart();
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:
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:
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: Object f = filelist.getSelectedValue();
533: if (f == null)
534: return;
535: File file = filechooser.getFileSystemView().createFileObject(f.toString());
536: if (! filechooser.isTraversable(file))
537: filechooser.setSelectedFile(file);
538: else
539: filechooser.setSelectedFile(null);
540: }
541: }
542:
543:
548: protected class UpdateAction extends AbstractAction
549: {
550:
553: protected UpdateAction()
554: {
555:
556: }
557:
558:
563: public void actionPerformed(ActionEvent e)
564: {
565:
566: }
567: }
568:
569:
570: protected int cancelButtonMnemonic;
571:
572:
573: protected String cancelButtonText;
574:
575:
576: protected String cancelButtonToolTipText;
577:
578:
579: protected Icon computerIcon = new Icon()
580: {
581: public int getIconHeight()
582: {
583: return ICON_SIZE;
584: }
585:
586: public int getIconWidth()
587: {
588: return ICON_SIZE;
589: }
590:
591: public void paintIcon(Component c, Graphics g, int x, int y)
592: {
593:
594: }
595: };
596:
597:
598: protected Icon detailsViewIcon = new Icon()
599: {
600: public int getIconHeight()
601: {
602: return ICON_SIZE;
603: }
604:
605: public int getIconWidth()
606: {
607: return ICON_SIZE;
608: }
609:
610: public void paintIcon(Component c, Graphics g, int x, int y)
611: {
612: Color saved = g.getColor();
613: g.translate(x, y);
614:
615: g.setColor(Color.GRAY);
616: g.drawRect(1, 1, 15, 20);
617: g.drawLine(17, 6, 23, 6);
618: g.drawLine(17, 12, 23, 12);
619: g.drawLine(17, 18, 23, 18);
620:
621: g.setColor(saved);
622: g.translate(-x, -y);
623: }
624: };
625:
626:
627: protected Icon directoryIcon = new Icon()
628: {
629: public int getIconHeight()
630: {
631: return ICON_SIZE;
632: }
633:
634: public int getIconWidth()
635: {
636: return ICON_SIZE;
637: }
638:
639: public void paintIcon(Component c, Graphics g, int x, int y)
640: {
641: Color saved = g.getColor();
642: g.translate(x, y);
643:
644: Point ap = new Point(3, 7);
645: Point bp = new Point(3, 21);
646: Point cp = new Point(21, 21);
647: Point dp = new Point(21, 12);
648: Point ep = new Point(16, 12);
649: Point fp = new Point(13, 7);
650:
651: Polygon dir = new Polygon(new int[] { ap.x, bp.x, cp.x, dp.x, ep.x, fp.x },
652: new int[] { ap.y, bp.y, cp.y, dp.y, ep.y, fp.y },
653: 6);
654:
655: g.setColor(new Color(153, 204, 255));
656: g.fillPolygon(dir);
657: g.setColor(Color.BLACK);
658: g.drawPolygon(dir);
659:
660: g.translate(-x, -y);
661: g.setColor(saved);
662: }
663: };
664:
665:
666: protected int directoryOpenButtonMnemonic;
667:
668:
669: protected String directoryOpenButtonText;
670:
671:
672: protected String directoryOpenButtonToolTipText;
673:
674:
675: protected Icon fileIcon = new Icon()
676: {
677: public int getIconHeight()
678: {
679: return ICON_SIZE;
680: }
681:
682: public int getIconWidth()
683: {
684: return ICON_SIZE;
685: }
686:
687: public void paintIcon(Component c, Graphics g, int x, int y)
688: {
689: Color saved = g.getColor();
690: g.translate(x, y);
691:
692: Point a = new Point(5, 4);
693: Point b = new Point(5, 20);
694: Point d = new Point(19, 20);
695: Point e = new Point(19, 7);
696: Point f = new Point(16, 4);
697:
698: Polygon p = new Polygon(new int[] { a.x, b.x, d.x, e.x, f.x, },
699: new int[] { a.y, b.y, d.y, e.y, f.y }, 5);
700:
701: g.setColor(Color.WHITE);
702: g.fillPolygon(p);
703: g.setColor(Color.BLACK);
704: g.drawPolygon(p);
705:
706: g.drawLine(16, 4, 14, 6);
707: g.drawLine(14, 6, 19, 7);
708:
709: g.setColor(saved);
710: g.translate(-x, -y);
711: }
712: };
713:
714:
715: protected Icon floppyDriveIcon = new Icon()
716: {
717: public int getIconHeight()
718: {
719: return ICON_SIZE;
720: }
721:
722: public int getIconWidth()
723: {
724: return ICON_SIZE;
725: }
726:
727: public void paintIcon(Component c, Graphics g, int x, int y)
728: {
729:
730: }
731: };
732:
733:
734: protected Icon hardDriveIcon = new Icon()
735: {
736: public int getIconHeight()
737: {
738: return ICON_SIZE;
739: }
740:
741: public int getIconWidth()
742: {
743: return ICON_SIZE;
744: }
745:
746: public void paintIcon(Component c, Graphics g, int x, int y)
747: {
748:
749: }
750: };
751:
752:
753: protected int helpButtonMnemonic;
754:
755:
756: protected String helpButtonText;
757:
758:
759: protected String helpButtonToolTipText;
760:
761:
762: protected Icon homeFolderIcon = new Icon()
763: {
764: public int getIconHeight()
765: {
766: return ICON_SIZE;
767: }
768:
769: public int getIconWidth()
770: {
771: return ICON_SIZE;
772: }
773:
774: public void paintIcon(Component c, Graphics g, int x, int y)
775: {
776: Color saved = g.getColor();
777: g.translate(x, y);
778:
779: Point a = new Point(12, 3);
780: Point b = new Point(4, 10);
781: Point d = new Point(20, 10);
782:
783: Polygon p = new Polygon(new int[] { a.x, b.x, d.x },
784: new int[] { a.y, b.y, d.y }, 3);
785:
786: g.setColor(new Color(104, 51, 0));
787: g.fillPolygon(p);
788: g.setColor(Color.BLACK);
789: g.drawPolygon(p);
790:
791: g.setColor(Color.WHITE);
792: g.fillRect(8, 10, 8, 10);
793: g.setColor(Color.BLACK);
794: g.drawRect(8, 10, 8, 10);
795:
796: g.setColor(saved);
797: g.translate(-x, -y);
798: }
799: };
800:
801:
802: protected Icon listViewIcon = new Icon()
803: {
804: public int getIconHeight()
805: {
806: return ICON_SIZE;
807: }
808:
809: public int getIconWidth()
810: {
811: return ICON_SIZE;
812: }
813:
814:
815: private void paintPartial(Graphics g, int x, int y)
816: {
817: Color saved = g.getColor();
818: g.translate(x, y);
819:
820: g.setColor(Color.GRAY);
821: g.drawRect(1, 1, 7, 10);
822: g.drawLine(8, 6, 11, 6);
823:
824: g.setColor(saved);
825: g.translate(-x, -y);
826: }
827:
828: public void paintIcon(Component c, Graphics g, int x, int y)
829: {
830: Color saved = g.getColor();
831: g.translate(x, y);
832:
833: paintPartial(g, 0, 0);
834: paintPartial(g, 12, 0);
835: paintPartial(g, 0, 12);
836: paintPartial(g, 12, 12);
837:
838: g.setColor(saved);
839: g.translate(-x, -y);
840: }
841: };
842:
843:
844: protected Icon newFolderIcon = directoryIcon;
845:
846:
847: protected int openButtonMnemonic;
848:
849:
850: protected String openButtonText;
851:
852:
853: protected String openButtonToolTipText;
854:
855:
856: protected int saveButtonMnemonic;
857:
858:
859: protected String saveButtonText;
860:
861:
862: protected String saveButtonToolTipText;
863:
864:
865: protected int updateButtonMnemonic;
866:
867:
868: protected String updateButtonText;
869:
870:
871: protected String updateButtonToolTipText;
872:
873:
874: protected Icon upFolderIcon = new Icon()
875: {
876: public int getIconHeight()
877: {
878: return ICON_SIZE;
879: }
880:
881: public int getIconWidth()
882: {
883: return ICON_SIZE;
884: }
885:
886: public void paintIcon(Component comp, Graphics g, int x, int y)
887: {
888: Color saved = g.getColor();
889: g.translate(x, y);
890:
891: Point a = new Point(3, 7);
892: Point b = new Point(3, 21);
893: Point c = new Point(21, 21);
894: Point d = new Point(21, 12);
895: Point e = new Point(16, 12);
896: Point f = new Point(13, 7);
897:
898: Polygon dir = new Polygon(new int[] { a.x, b.x, c.x, d.x, e.x, f.x },
899: new int[] { a.y, b.y, c.y, d.y, e.y, f.y }, 6);
900:
901: g.setColor(new Color(153, 204, 255));
902: g.fillPolygon(dir);
903: g.setColor(Color.BLACK);
904: g.drawPolygon(dir);
905:
906: a = new Point(12, 15);
907: b = new Point(9, 18);
908: c = new Point(15, 18);
909:
910: Polygon arrow = new Polygon(new int[] { a.x, b.x, c.x },
911: new int[] { a.y, b.y, c.y }, 3);
912:
913: g.fillPolygon(arrow);
914:
915: g.drawLine(12, 15, 12, 22);
916:
917: g.translate(-x, -y);
918: g.setColor(saved);
919: }
920: };
921:
922:
923:
924:
925: JFileChooser filechooser;
926:
927:
928: JList filelist;
929:
930:
931: JComboBox filters;
932:
933:
934: BasicDirectoryModel model;
935:
936:
937: FileFilter acceptAll = new AcceptAllFileFilter();
938:
939:
940: FileView fv = new BasicFileView();
941:
942:
943: static final int ICON_SIZE = 24;
944:
945:
946: JComboBox parents;
947:
948:
949: String filename;
950:
951:
952: JButton accept;
953:
954:
955: JButton cancel;
956:
957:
958: JButton upFolderButton;
959:
960:
961: JButton newFolderButton;
962:
963:
964: JButton homeFolderButton;
965:
966:
967: JPanel accessoryPanel;
968:
969:
970: PropertyChangeListener propertyChangeListener;
971:
972:
973: String acceptAllFileFilterText;
974:
975:
976: String dirDescText;
977:
978:
979: String fileDescText;
980:
981:
982: boolean dirSelected = false;
983:
984:
985: File currDir = null;
986:
987:
988:
989: JPanel bottomPanel;
990:
991:
992: JPanel closePanel;
993:
994:
995: JTextField entry;
996:
997:
998: String parentPath;
999:
1000:
1001: private class ListLabelRenderer extends JLabel implements ListCellRenderer
1002: {
1003:
1004: final Color selected = new Color(153, 204, 255);
1005:
1006:
1009: public ListLabelRenderer()
1010: {
1011: super();
1012: setOpaque(true);
1013: }
1014:
1015:
1026: public Component getListCellRendererComponent(JList list, Object value,
1027: int index,
1028: boolean isSelected,
1029: boolean cellHasFocus)
1030: {
1031: setHorizontalAlignment(SwingConstants.LEFT);
1032: File file = (File) value;
1033: setText(filechooser.getName(file));
1034: setIcon(filechooser.getIcon(file));
1035: setBackground(isSelected ? selected : Color.WHITE);
1036: setForeground(Color.BLACK);
1037:
1038: return this;
1039: }
1040: }
1041:
1042:
1045: void closeDialog()
1046: {
1047: Window owner = SwingUtilities.windowForComponent(filechooser);
1048: if (owner instanceof JDialog)
1049: ((JDialog) owner).dispose();
1050: }
1051:
1052:
1057: public BasicFileChooserUI(JFileChooser b)
1058: {
1059: this.filechooser = b;
1060: }
1061:
1062:
1069: public static ComponentUI createUI(JComponent c)
1070: {
1071: return new BasicFileChooserUI((JFileChooser) c);
1072: }
1073:
1074:
1079: public void installUI(JComponent c)
1080: {
1081: if (c instanceof JFileChooser)
1082: {
1083: JFileChooser fc = (JFileChooser) c;
1084: fc.resetChoosableFileFilters();
1085: createModel();
1086: clearIconCache();
1087: installDefaults(fc);
1088: installComponents(fc);
1089: installListeners(fc);
1090:
1091: Object path = filechooser.getCurrentDirectory();
1092: if (path != null)
1093: parentPath = path.toString().substring(path.toString().lastIndexOf("/"));
1094: }
1095: }
1096:
1097:
1102: public void uninstallUI(JComponent c)
1103: {
1104: model = null;
1105: uninstallListeners(filechooser);
1106: uninstallComponents(filechooser);
1107: uninstallDefaults(filechooser);
1108: filechooser = null;
1109: }
1110:
1111:
1112:
1113:
1114: void boxEntries()
1115: {
1116: ArrayList parentFiles = new ArrayList();
1117: File parent = filechooser.getCurrentDirectory();
1118: if (parent == null)
1119: parent = filechooser.getFileSystemView().getDefaultDirectory();
1120: while (parent != null)
1121: {
1122: String name = parent.getName();
1123: if (name.equals(""))
1124: name = parent.getAbsolutePath();
1125:
1126: parentFiles.add(parentFiles.size(), name);
1127: parent = parent.getParentFile();
1128: }
1129:
1130: if (parentFiles.size() == 0)
1131: return;
1132:
1133: if (parents.getItemCount() > 0)
1134: parents.removeAllItems();
1135: for (int i = parentFiles.size() - 1; i >= 0; i--)
1136: parents.addItem(parentFiles.get(i));
1137: parents.setSelectedIndex(parentFiles.size() - 1);
1138: parents.revalidate();
1139: parents.repaint();
1140: }
1141:
1142:
1147: private ItemListener createBoxListener()
1148: {
1149: return new ItemListener()
1150: {
1151: public void itemStateChanged(ItemEvent e)
1152: {
1153: if (parents.getItemCount() - 1 == parents.getSelectedIndex())
1154: return;
1155: StringBuffer dir = new StringBuffer();
1156: for (int i = 0; i <= parents.getSelectedIndex(); i++)
1157: {
1158: dir.append(parents.getItemAt(i));
1159: dir.append(File.separatorChar);
1160: }
1161: filechooser.setCurrentDirectory(filechooser.getFileSystemView()
1162: .createFileObject(dir
1163: .toString()));
1164: }
1165: };
1166: }
1167:
1168:
1173: private ItemListener createFilterListener()
1174: {
1175: return new ItemListener()
1176: {
1177: public void itemStateChanged(ItemEvent e)
1178: {
1179: int index = filters.getSelectedIndex();
1180: if (index == -1)
1181: return;
1182: filechooser.setFileFilter(filechooser.getChoosableFileFilters()[index]);
1183: }
1184: };
1185: }
1186:
1187: void filterEntries()
1188: {
1189: FileFilter[] list = filechooser.getChoosableFileFilters();
1190: if (filters.getItemCount() > 0)
1191: filters.removeAllItems();
1192:
1193: int index = -1;
1194: String selected = filechooser.getFileFilter().getDescription();
1195: for (int i = 0; i < list.length; i++)
1196: {
1197: if (selected.equals(list[i].getDescription()))
1198: index = i;
1199: filters.addItem(list[i].getDescription());
1200: }
1201: filters.setSelectedIndex(index);
1202: filters.revalidate();
1203: filters.repaint();
1204: }
1205:
1206:
1211: public void installComponents(JFileChooser fc)
1212: {
1213: JLabel look = new JLabel("Look In:");
1214:
1215: parents = new JComboBox();
1216: parents.setRenderer(new BasicComboBoxRenderer());
1217: boxEntries();
1218: look.setLabelFor(parents);
1219: JPanel parentsPanel = new JPanel();
1220: parentsPanel.add(look);
1221: parentsPanel.add(parents);
1222: JPanel buttonPanel = new JPanel();
1223:
1224: upFolderButton = new JButton();
1225: upFolderButton.setIcon(upFolderIcon);
1226: buttonPanel.add(upFolderButton);
1227:
1228: homeFolderButton = new JButton();
1229: homeFolderButton = new JButton(homeFolderIcon);
1230: buttonPanel.add(homeFolderButton);
1231:
1232: newFolderButton = new JButton();
1233: newFolderButton.setIcon(newFolderIcon);
1234: buttonPanel.add(newFolderButton);
1235:
1236: ButtonGroup toggles = new ButtonGroup();
1237: JToggleButton listViewButton = new JToggleButton();
1238: listViewButton.setIcon(listViewIcon);
1239: toggles.add(listViewButton);
1240: buttonPanel.add(listViewButton);
1241:
1242: JToggleButton detailsViewButton = new JToggleButton();
1243: detailsViewButton.setIcon(detailsViewIcon);
1244: toggles.add(detailsViewButton);
1245: buttonPanel.add(detailsViewButton);
1246:
1247: JPanel topPanel = new JPanel();
1248: parentsPanel.add(buttonPanel);
1249: topPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 0, 0));
1250: topPanel.add(parentsPanel);
1251:
1252: accessoryPanel = new JPanel();
1253: if (filechooser.getAccessory() != null)
1254: accessoryPanel.add(filechooser.getAccessory(), BorderLayout.CENTER);
1255:
1256: filelist = new JList(model);
1257: filelist.setVisibleRowCount(6);
1258: JScrollPane scrollp = new JScrollPane(filelist);
1259: scrollp.setPreferredSize(new Dimension(400, 175));
1260: filelist.setBackground(Color.WHITE);
1261:
1262: filelist.setLayoutOrientation(JList.VERTICAL_WRAP);
1263: filelist.setCellRenderer(new ListLabelRenderer());
1264:
1265: GridBagConstraints c = new GridBagConstraints();
1266: c.gridx = 0;
1267: c.gridy = 0;
1268: c.fill = GridBagConstraints.BOTH;
1269: c.weightx = 1;
1270: c.weighty = 1;
1271:
1272: JPanel centrePanel = new JPanel();
1273: centrePanel.setLayout(new GridBagLayout());
1274: centrePanel.add(scrollp, c);
1275:
1276: c.gridx = 1;
1277: centrePanel.add(accessoryPanel, c);
1278:
1279: JLabel fileNameLabel = new JLabel("File Name:");
1280: JLabel fileTypesLabel = new JLabel("Files of Type:");
1281:
1282: entry = new JTextField();
1283: filters = new JComboBox();
1284: filterEntries();
1285:
1286: fileNameLabel.setLabelFor(entry);
1287: fileNameLabel.setHorizontalTextPosition(SwingConstants.LEFT);
1288: fileTypesLabel.setLabelFor(filters);
1289: fileTypesLabel.setHorizontalTextPosition(SwingConstants.LEFT);
1290:
1291: closePanel = new JPanel();
1292: accept = getApproveButton(filechooser);
1293: cancel = new JButton(cancelButtonText);
1294: cancel.setMnemonic(cancelButtonMnemonic);
1295: cancel.setToolTipText(cancelButtonToolTipText);
1296: closePanel.add(accept);
1297: closePanel.add(cancel);
1298:
1299: c.anchor = GridBagConstraints.WEST;
1300: c.weighty = 0;
1301: c.weightx = 0;
1302: c.gridx = 0;
1303:
1304: bottomPanel = new JPanel();
1305: bottomPanel.setLayout(new GridBagLayout());
1306: bottomPanel.add(fileNameLabel, c);
1307:
1308: c.gridy = 1;
1309: bottomPanel.add(fileTypesLabel, c);
1310: c.gridx = 1;
1311: c.gridy = 0;
1312: c.weightx = 1;
1313: c.weighty = 1;
1314: bottomPanel.add(entry, c);
1315:
1316: c.gridy = 1;
1317: bottomPanel.add(filters, c);
1318:
1319: c.fill = GridBagConstraints.NONE;
1320: c.gridy = 2;
1321: c.anchor = GridBagConstraints.EAST;
1322: bottomPanel.add(closePanel, c);
1323:
1324: filechooser.setLayout(new BorderLayout());
1325: filechooser.add(topPanel, BorderLayout.NORTH);
1326: filechooser.add(centrePanel, BorderLayout.CENTER);
1327: filechooser.add(bottomPanel, BorderLayout.SOUTH);
1328: }
1329:
1330:
1335: public void uninstallComponents(JFileChooser fc)
1336: {
1337: parents = null;
1338:
1339: accept = null;
1340: cancel = null;
1341: upFolderButton = null;
1342: homeFolderButton = null;
1343: newFolderButton = null;
1344:
1345: filelist = null;
1346: }
1347:
1348:
1353: protected void installListeners(JFileChooser fc)
1354: {
1355: propertyChangeListener = createPropertyChangeListener(filechooser);
1356: filechooser.addPropertyChangeListener(propertyChangeListener);
1357:
1358:
1359: accept.addActionListener(getApproveSelectionAction());
1360: cancel.addActionListener(getCancelSelectionAction());
1361: upFolderButton.addActionListener(getChangeToParentDirectoryAction());
1362: homeFolderButton.addActionListener(getGoHomeAction());
1363: newFolderButton.addActionListener(getNewFolderAction());
1364: filters.addItemListener(createFilterListener());
1365:
1366: filelist.addMouseListener(createDoubleClickListener(filechooser, filelist));
1367: filelist.addListSelectionListener(createListSelectionListener(filechooser));
1368: }
1369:
1370:
1375: protected void uninstallListeners(JFileChooser fc)
1376: {
1377: filechooser.removePropertyChangeListener(propertyChangeListener);
1378: propertyChangeListener = null;
1379: }
1380:
1381:
1386: protected void installDefaults(JFileChooser fc)
1387: {
1388: installIcons(fc);
1389: installStrings(fc);
1390: }
1391:
1392:
1397: protected void uninstallDefaults(JFileChooser fc)
1398: {
1399: uninstallStrings(fc);
1400: uninstallIcons(fc);
1401: }
1402:
1403:
1408: protected void installIcons(JFileChooser fc)
1409: {
1410:
1411: }
1412:
1413:
1419: protected void uninstallIcons(JFileChooser fc)
1420: {
1421:
1422: }
1423:
1424:
1429: protected void installStrings(JFileChooser fc)
1430: {
1431: acceptAllFileFilterText = UIManager.getString("FileChooser.acceptAllFileFilterText");
1432: cancelButtonMnemonic = UIManager.getInt("FileChooser.cancelButtonMnemonic");
1433: cancelButtonText = UIManager.getString("FileChooser.cancelButtonText");
1434: cancelButtonToolTipText = UIManager.getString("FileChooser.cancelButtonToolTipText");
1435:
1436: dirDescText = UIManager.getString("FileChooser.directoryDescriptionText");
1437: fileDescText = UIManager.getString("FileChooser.fileDescriptionText");
1438:
1439: helpButtonMnemonic = UIManager.getInt("FileChooser.helpButtonMnemonic");
1440: helpButtonText = UIManager.getString("FileChooser.helpButtonText");
1441: helpButtonToolTipText = UIManager.getString("FileChooser.helpButtonToolTipText");
1442:
1443: openButtonMnemonic = UIManager.getInt("FileChooser.openButtonMnemonic");
1444: openButtonText = UIManager.getString("FileChooser.openButtonText");
1445: openButtonToolTipText = UIManager.getString("FileChooser.openButtonToolTipText");
1446:
1447: saveButtonMnemonic = UIManager.getInt("FileChooser.saveButtonMnemonic");
1448: saveButtonText = UIManager.getString("FileChooser.saveButtonText");
1449: saveButtonToolTipText = UIManager.getString("FileChooser.saveButtonToolTipText");
1450: }
1451:
1452:
1457: protected void uninstallStrings(JFileChooser fc)
1458: {
1459: acceptAllFileFilterText = null;
1460: cancelButtonMnemonic = 0;
1461: cancelButtonText = null;
1462: cancelButtonToolTipText = null;
1463:
1464: dirDescText = null;
1465: fileDescText = null;
1466:
1467: helpButtonMnemonic = 0;
1468: helpButtonText = null;
1469: helpButtonToolTipText = null;
1470:
1471: openButtonMnemonic = 0;
1472: openButtonText = null;
1473: openButtonToolTipText = null;
1474:
1475: saveButtonMnemonic = 0;
1476: saveButtonText = null;
1477: saveButtonToolTipText = null;
1478: }
1479:
1480:
1483: protected void createModel()
1484: {
1485: model = new BasicDirectoryModel(filechooser);
1486: }
1487:
1488:
1493: public BasicDirectoryModel getModel()
1494: {
1495: return model;
1496: }
1497:
1498:
1506: public PropertyChangeListener createPropertyChangeListener(JFileChooser fc)
1507: {
1508: return new PropertyChangeListener()
1509: {
1510: public void propertyChange(PropertyChangeEvent e)
1511: {
1512:
1513:
1514: if (e.getPropertyName().equals(
1515: JFileChooser.SELECTED_FILE_CHANGED_PROPERTY))
1516: {
1517: if (filechooser.getSelectedFile() == null)
1518: setFileName(null);
1519: else
1520: setFileName(filechooser.getSelectedFile().toString());
1521: int index = -1;
1522: File file = filechooser.getSelectedFile();
1523: for (index = 0; index < model.getSize(); index++)
1524: if (((File) model.getElementAt(index)).equals(file))
1525: break;
1526: if (index == -1)
1527: return;
1528: filelist.setSelectedIndex(index);
1529: filelist.ensureIndexIsVisible(index);
1530: filelist.revalidate();
1531: filelist.repaint();
1532: }
1533: else if (e.getPropertyName().equals(
1534: JFileChooser.DIRECTORY_CHANGED_PROPERTY))
1535: {
1536: filelist.clearSelection();
1537: filelist.revalidate();
1538: filelist.repaint();
1539: setDirectorySelected(false);
1540: setDirectory(filechooser.getCurrentDirectory());
1541: boxEntries();
1542: }
1543: else if (e.getPropertyName().equals(
1544: JFileChooser.CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY)
1545: || e.getPropertyName().equals(
1546: JFileChooser.FILE_FILTER_CHANGED_PROPERTY))
1547: filterEntries();
1548: else if (e.getPropertyName().equals(
1549: JFileChooser.DIALOG_TYPE_CHANGED_PROPERTY)
1550: || e.getPropertyName().equals(
1551: JFileChooser.DIALOG_TITLE_CHANGED_PROPERTY))
1552: {
1553: Window owner = SwingUtilities.windowForComponent(filechooser);
1554: if (owner instanceof JDialog)
1555: ((JDialog) owner).setTitle(getDialogTitle(filechooser));
1556: accept.setText(getApproveButtonText(filechooser));
1557: accept.setToolTipText(getApproveButtonToolTipText(filechooser));
1558: accept.setMnemonic(getApproveButtonMnemonic(filechooser));
1559: }
1560: else if (e.getPropertyName().equals(
1561: JFileChooser.APPROVE_BUTTON_TEXT_CHANGED_PROPERTY))
1562: accept.setText(getApproveButtonText(filechooser));
1563: else if (e.getPropertyName().equals(
1564: JFileChooser.APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY))
1565: accept.setToolTipText(getApproveButtonToolTipText(filechooser));
1566: else if (e.getPropertyName().equals(
1567: JFileChooser.APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY))
1568: accept.setMnemonic(getApproveButtonMnemonic(filechooser));
1569: else if (e.getPropertyName().equals(
1570: JFileChooser.CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY))
1571: {
1572: if (filechooser.getControlButtonsAreShown())
1573: {
1574: GridBagConstraints c = new GridBagConstraints();
1575: c.gridy = 1;
1576: bottomPanel.add(filters, c);
1577:
1578: c.fill = GridBagConstraints.BOTH;
1579: c.gridy = 2;
1580: c.anchor = GridBagConstraints.EAST;
1581: bottomPanel.add(closePanel, c);
1582: bottomPanel.revalidate();
1583: bottomPanel.repaint();
1584: bottomPanel.doLayout();
1585: }
1586: else
1587: bottomPanel.remove(closePanel);
1588: }
1589: else if (e.getPropertyName().equals(
1590: JFileChooser.ACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY))
1591: {
1592: if (filechooser.isAcceptAllFileFilterUsed())
1593: filechooser.addChoosableFileFilter(getAcceptAllFileFilter(filechooser));
1594: else
1595: filechooser.removeChoosableFileFilter(getAcceptAllFileFilter(filechooser));
1596: }
1597: else if (e.getPropertyName().equals(
1598: JFileChooser.ACCESSORY_CHANGED_PROPERTY))
1599: {
1600: JComponent old = (JComponent) e.getOldValue();
1601: if (old != null)
1602: getAccessoryPanel().remove(old);
1603: JComponent newval = (JComponent) e.getNewValue();
1604: if (newval != null)
1605: getAccessoryPanel().add(newval);
1606: }
1607: if (e.getPropertyName().equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)
1608: || e.getPropertyName().equals(
1609: JFileChooser.FILE_FILTER_CHANGED_PROPERTY)
1610: || e.getPropertyName().equals(
1611: JFileChooser.FILE_HIDING_CHANGED_PROPERTY))
1612: rescanCurrentDirectory(filechooser);
1613:
1614: filechooser.revalidate();
1615: filechooser.repaint();
1616: }
1617: };
1618: }
1619:
1620:
1625: public String getFileName()
1626: {
1627: return filename;
1628: }
1629:
1630:
1637: public String getDirectoryName()
1638: {
1639:
1640: return null;
1641: }
1642:
1643:
1650: public void setFileName(String filename)
1651: {
1652: this.filename = filename;
1653: }
1654:
1655:
1662: public void setDirectoryName(String dirname)
1663: {
1664:
1665: }
1666:
1667:
1672: public void rescanCurrentDirectory(JFileChooser fc)
1673: {
1674: getModel().validateFileCache();
1675: filelist.revalidate();
1676: }
1677:
1678:
1684: public void ensureFileIsVisible(JFileChooser fc, File f)
1685: {
1686:
1687: }
1688:
1689:
1695: public JFileChooser getFileChooser()
1696: {
1697: return filechooser;
1698: }
1699:
1700:
1705: public JPanel getAccessoryPanel()
1706: {
1707: return accessoryPanel;
1708: }
1709:
1710:
1717: public JButton getApproveButton(JFileChooser fc)
1718: {
1719: accept = new JButton(getApproveButtonText(fc));
1720: accept.setMnemonic(getApproveButtonMnemonic(fc));
1721: accept.setToolTipText(getApproveButtonToolTipText(fc));
1722: return accept;
1723: }
1724:
1725:
1735: public String getApproveButtonToolTipText(JFileChooser fc)
1736: {
1737: if (fc.getApproveButtonToolTipText() != null)
1738: return fc.getApproveButtonToolTipText();
1739: else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1740: return saveButtonToolTipText;
1741: else
1742: return openButtonToolTipText;
1743: }
1744:
1745:
1748: public void clearIconCache()
1749: {
1750: if (fv instanceof BasicFileView)
1751: ((BasicFileView) fv).clearIconCache();
1752: }
1753:
1754:
1761: public ListSelectionListener createListSelectionListener(JFileChooser fc)
1762: {
1763: return new SelectionListener();
1764: }
1765:
1766:
1774: protected MouseListener createDoubleClickListener(JFileChooser fc, JList list)
1775: {
1776: return new DoubleClickListener(list);
1777: }
1778:
1779:
1785: protected boolean isDirectorySelected()
1786: {
1787: return dirSelected;
1788: }
1789:
1790:
1795: protected void setDirectorySelected(boolean selected)
1796: {
1797: dirSelected = selected;
1798: }
1799:
1800:
1805: protected File getDirectory()
1806: {
1807: return currDir;
1808: }
1809:
1810:
1815: protected void setDirectory(File f)
1816: {
1817: currDir = f;
1818: }
1819:
1820:
1827: public FileFilter getAcceptAllFileFilter(JFileChooser fc)
1828: {
1829: return acceptAll;
1830: }
1831:
1832:
1843: public FileView getFileView(JFileChooser fc)
1844: {
1845: return fv;
1846: }
1847:
1848:
1857: public String getDialogTitle(JFileChooser fc)
1858: {
1859: String ret = fc.getDialogTitle();
1860: if (ret != null)
1861: return ret;
1862: switch (fc.getDialogType())
1863: {
1864: case JFileChooser.OPEN_DIALOG:
1865: ret = openButtonText;
1866: break;
1867: case JFileChooser.SAVE_DIALOG:
1868: ret = saveButtonText;
1869: break;
1870: default:
1871: ret = fc.getApproveButtonText();
1872: break;
1873: }
1874: if (ret == null)
1875: ret = openButtonText;
1876: return ret;
1877: }
1878:
1879:
1888: public int getApproveButtonMnemonic(JFileChooser fc)
1889: {
1890: if (fc.getApproveButtonMnemonic() != 0)
1891: return fc.getApproveButtonMnemonic();
1892: else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1893: return saveButtonMnemonic;
1894: else
1895: return openButtonMnemonic;
1896: }
1897:
1898:
1907: public String getApproveButtonText(JFileChooser fc)
1908: {
1909: if (fc.getApproveButtonText() != null)
1910: return fc.getApproveButtonText();
1911: else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1912: return saveButtonText;
1913: else
1914: return openButtonText;
1915: }
1916:
1917:
1923: public Action getNewFolderAction()
1924: {
1925: return new NewFolderAction();
1926: }
1927:
1928:
1934: public Action getGoHomeAction()
1935: {
1936: return new GoHomeAction();
1937: }
1938:
1939:
1945: public Action getChangeToParentDirectoryAction()
1946: {
1947: return new ChangeToParentDirectoryAction();
1948: }
1949:
1950:
1956: public Action getApproveSelectionAction()
1957: {
1958: return new ApproveSelectionAction();
1959: }
1960:
1961:
1967: public Action getCancelSelectionAction()
1968: {
1969: return new CancelSelectionAction();
1970: }
1971:
1972:
1977: public Action getUpdateAction()
1978: {
1979: return new UpdateAction();
1980: }
1981: }