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: import ;
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: import ;
91: import ;
92:
93:
94:
97: public class BasicFileChooserUI extends FileChooserUI
98: {
99:
102: protected class AcceptAllFileFilter extends FileFilter
103: {
104: public AcceptAllFileFilter()
105: {
106: }
107:
108:
115: public boolean accept(File f)
116: {
117: return true;
118: }
119:
120:
125: public String getDescription()
126: {
127: return acceptAllFileFilterText;
128: }
129: }
130:
131:
134: protected class ApproveSelectionAction extends AbstractAction
135: {
136:
139: protected ApproveSelectionAction()
140: {
141: }
142:
143:
148: public void actionPerformed(ActionEvent e)
149: {
150: Object obj = filelist.getSelectedValue();
151: if (obj != null)
152: {
153: File f = filechooser.getFileSystemView().createFileObject(obj
154: .toString());
155: if (filechooser.isTraversable(f) &&
156: filechooser.getFileSelectionMode() == JFileChooser.FILES_ONLY)
157: filechooser.setCurrentDirectory(f);
158: else
159: {
160: filechooser.setSelectedFile(f);
161: filechooser.approveSelection();
162: closeDialog();
163: }
164: }
165: }
166: }
167:
168:
171: protected class BasicFileView extends FileView
172: {
173:
174: protected Hashtable iconCache = new Hashtable();
175:
176: public BasicFileView()
177: {
178: }
179:
180:
186: public void cacheIcon(File f, Icon i)
187: {
188: iconCache.put(f, i);
189: }
190:
191:
194: public void clearIconCache()
195: {
196: iconCache.clear();
197: }
198:
199:
206: public Icon getCachedIcon(File f)
207: {
208: return (Icon) iconCache.get(f);
209: }
210:
211:
218: public String getDescription(File f)
219: {
220: return getName(f);
221: }
222:
223:
230: public Icon getIcon(File f)
231: {
232: Icon val = getCachedIcon(f);
233: if (val != null)
234: return val;
235: if (filechooser.isTraversable(f))
236: val = directoryIcon;
237: else
238: val = fileIcon;
239: cacheIcon(f, val);
240: return val;
241: }
242:
243:
250: public String getName(File f)
251: {
252: return f.getName();
253: }
254:
255:
262: public String getTypeDescription(File f)
263: {
264: if (filechooser.isTraversable(f))
265: return dirDescText;
266: else
267: return fileDescText;
268: }
269:
270:
277: public Boolean isHidden(File f)
278: {
279: return new Boolean(filechooser.getFileSystemView().isHiddenFile(f));
280: }
281: }
282:
283:
286: protected class CancelSelectionAction extends AbstractAction
287: {
288:
291: protected CancelSelectionAction()
292: {
293: }
294:
295:
300: public void actionPerformed(ActionEvent e)
301: {
302: filechooser.cancelSelection();
303: closeDialog();
304: }
305: }
306:
307:
310: protected class ChangeToParentDirectoryAction extends AbstractAction
311: {
312:
315: protected ChangeToParentDirectoryAction()
316: {
317: }
318:
319:
324: public void actionPerformed(ActionEvent e)
325: {
326: filechooser.changeToParentDirectory();
327: filechooser.revalidate();
328: filechooser.repaint();
329: }
330: }
331:
332:
335: protected class DoubleClickListener extends MouseAdapter
336: {
337:
338: private Timer timer = null;
339:
340:
341: private Object lastSelected = null;
342:
343:
344: private JList list = null;
345:
346:
351: public DoubleClickListener(JList list)
352: {
353: this.list = list;
354: timer = new Timer(1000, null);
355: timer.setRepeats(false);
356: lastSelected = list.getSelectedValue();
357: setDirectorySelected(false);
358: }
359:
360:
365: public void mouseClicked(MouseEvent e)
366: {
367: if (list.getSelectedValue() == null)
368: return;
369: FileSystemView fsv = filechooser.getFileSystemView();
370: if (timer.isRunning()
371: && list.getSelectedValue().toString().equals(lastSelected.toString()))
372: {
373: File f = fsv.createFileObject(lastSelected.toString());
374: timer.stop();
375: if (filechooser.isTraversable(f))
376: {
377: filechooser.setCurrentDirectory(f);
378: filechooser.rescanCurrentDirectory();
379: }
380: else
381: {
382: filechooser.setSelectedFile(f);
383: filechooser.approveSelection();
384: closeDialog();
385: }
386: }
387: else
388: {
389: File f = fsv.createFileObject(list.getSelectedValue().toString());
390: if (filechooser.isTraversable(f))
391: {
392: setDirectorySelected(true);
393: setDirectory(f);
394: }
395: else
396: {
397: setDirectorySelected(false);
398: setDirectory(null);
399: }
400: lastSelected = list.getSelectedValue().toString();
401: timer.restart();
402: }
403: }
404:
405:
410: public void mouseEntered(MouseEvent e)
411: {
412:
413: }
414: }
415:
416:
419: protected class GoHomeAction extends AbstractAction
420: {
421:
424: protected GoHomeAction()
425: {
426: }
427:
428:
433: public void actionPerformed(ActionEvent e)
434: {
435: filechooser.setCurrentDirectory(filechooser.getFileSystemView()
436: .getHomeDirectory());
437: filechooser.revalidate();
438: filechooser.repaint();
439: }
440: }
441:
442:
445: protected class NewFolderAction extends AbstractAction
446: {
447:
450: protected NewFolderAction()
451: {
452: }
453:
454:
459: public void actionPerformed(ActionEvent e)
460: {
461: try
462: {
463: filechooser.getFileSystemView().createNewFolder(filechooser
464: .getCurrentDirectory());
465: }
466: catch (IOException ioe)
467: {
468: return;
469: }
470: filechooser.rescanCurrentDirectory();
471: filechooser.repaint();
472: }
473: }
474:
475:
478: protected class SelectionListener implements ListSelectionListener
479: {
480:
483: protected SelectionListener()
484: {
485: }
486:
487:
492: public void valueChanged(ListSelectionEvent e)
493: {
494: Object f = filelist.getSelectedValue();
495: if (f == null)
496: return;
497: File file = filechooser.getFileSystemView().createFileObject(f.toString());
498: if (! filechooser.isTraversable(file))
499: filechooser.setSelectedFile(file);
500: else
501: filechooser.setSelectedFile(null);
502: }
503: }
504:
505:
508: protected class UpdateAction extends AbstractAction
509: {
510:
513: protected UpdateAction()
514: {
515: }
516:
517:
522: public void actionPerformed(ActionEvent e)
523: {
524: }
525: }
526:
527:
528: protected int cancelButtonMnemonic;
529:
530:
531: protected String cancelButtonText;
532:
533:
534: protected String cancelButtonToolTipText;
535:
536:
537: protected Icon computerIcon = new Icon()
538: {
539: public int getIconHeight()
540: {
541: return ICON_SIZE;
542: }
543:
544: public int getIconWidth()
545: {
546: return ICON_SIZE;
547: }
548:
549: public void paintIcon(Component c, Graphics g, int x, int y)
550: {
551: }
552: };
553:
554:
555: protected Icon detailsViewIcon = new Icon()
556: {
557: public int getIconHeight()
558: {
559: return ICON_SIZE;
560: }
561:
562: public int getIconWidth()
563: {
564: return ICON_SIZE;
565: }
566:
567: public void paintIcon(Component c, Graphics g, int x, int y)
568: {
569: Color saved = g.getColor();
570: g.translate(x, y);
571:
572: g.setColor(Color.GRAY);
573: g.drawRect(1, 1, 15, 20);
574: g.drawLine(17, 6, 23, 6);
575: g.drawLine(17, 12, 23, 12);
576: g.drawLine(17, 18, 23, 18);
577:
578: g.setColor(saved);
579: g.translate(-x, -y);
580: }
581: };
582:
583:
584: protected Icon directoryIcon = new Icon()
585: {
586: public int getIconHeight()
587: {
588: return ICON_SIZE;
589: }
590:
591: public int getIconWidth()
592: {
593: return ICON_SIZE;
594: }
595:
596: public void paintIcon(Component c, Graphics g, int x, int y)
597: {
598: Color saved = g.getColor();
599: g.translate(x, y);
600:
601: Point ap = new Point(3, 7);
602: Point bp = new Point(3, 21);
603: Point cp = new Point(21, 21);
604: Point dp = new Point(21, 12);
605: Point ep = new Point(16, 12);
606: Point fp = new Point(13, 7);
607:
608: Polygon dir = new Polygon(new int[] { ap.x, bp.x, cp.x, dp.x, ep.x, fp.x },
609: new int[] { ap.y, bp.y, cp.y, dp.y, ep.y, fp.y },
610: 6);
611:
612: g.setColor(new Color(153, 204, 255));
613: g.fillPolygon(dir);
614: g.setColor(Color.BLACK);
615: g.drawPolygon(dir);
616:
617: g.translate(-x, -y);
618: g.setColor(saved);
619: }
620: };
621:
622:
623: protected int directoryOpenButtonMnemonic;
624:
625:
626: protected String directoryOpenButtonText;
627:
628:
629: protected String directoryOpenButtonToolTipText;
630:
631:
632: protected Icon fileIcon = new Icon()
633: {
634: public int getIconHeight()
635: {
636: return ICON_SIZE;
637: }
638:
639: public int getIconWidth()
640: {
641: return ICON_SIZE;
642: }
643:
644: public void paintIcon(Component c, Graphics g, int x, int y)
645: {
646: Color saved = g.getColor();
647: g.translate(x, y);
648:
649: Point a = new Point(5, 4);
650: Point b = new Point(5, 20);
651: Point d = new Point(19, 20);
652: Point e = new Point(19, 7);
653: Point f = new Point(16, 4);
654:
655: Polygon p = new Polygon(new int[] { a.x, b.x, d.x, e.x, f.x, },
656: new int[] { a.y, b.y, d.y, e.y, f.y }, 5);
657:
658: g.setColor(Color.WHITE);
659: g.fillPolygon(p);
660: g.setColor(Color.BLACK);
661: g.drawPolygon(p);
662:
663: g.drawLine(16, 4, 14, 6);
664: g.drawLine(14, 6, 19, 7);
665:
666: g.setColor(saved);
667: g.translate(-x, -y);
668: }
669: };
670:
671:
672: protected Icon floppyDriveIcon = new Icon()
673: {
674: public int getIconHeight()
675: {
676: return ICON_SIZE;
677: }
678:
679: public int getIconWidth()
680: {
681: return ICON_SIZE;
682: }
683:
684: public void paintIcon(Component c, Graphics g, int x, int y)
685: {
686: }
687: };
688:
689:
690: protected Icon hardDriveIcon = new Icon()
691: {
692: public int getIconHeight()
693: {
694: return ICON_SIZE;
695: }
696:
697: public int getIconWidth()
698: {
699: return ICON_SIZE;
700: }
701:
702: public void paintIcon(Component c, Graphics g, int x, int y)
703: {
704: }
705: };
706:
707:
708: protected int helpButtonMnemonic;
709:
710:
711: protected String helpButtonText;
712:
713:
714: protected String helpButtonToolTipText;
715:
716:
717: protected Icon homeFolderIcon = new Icon()
718: {
719: public int getIconHeight()
720: {
721: return ICON_SIZE;
722: }
723:
724: public int getIconWidth()
725: {
726: return ICON_SIZE;
727: }
728:
729: public void paintIcon(Component c, Graphics g, int x, int y)
730: {
731: Color saved = g.getColor();
732: g.translate(x, y);
733:
734: Point a = new Point(12, 3);
735: Point b = new Point(4, 10);
736: Point d = new Point(20, 10);
737:
738: Polygon p = new Polygon(new int[] { a.x, b.x, d.x },
739: new int[] { a.y, b.y, d.y }, 3);
740:
741: g.setColor(new Color(104, 51, 0));
742: g.fillPolygon(p);
743: g.setColor(Color.BLACK);
744: g.drawPolygon(p);
745:
746: g.setColor(Color.WHITE);
747: g.fillRect(8, 10, 8, 10);
748: g.setColor(Color.BLACK);
749: g.drawRect(8, 10, 8, 10);
750:
751: g.setColor(saved);
752: g.translate(-x, -y);
753: }
754: };
755:
756:
757: protected Icon listViewIcon = new Icon()
758: {
759: public int getIconHeight()
760: {
761: return ICON_SIZE;
762: }
763:
764: public int getIconWidth()
765: {
766: return ICON_SIZE;
767: }
768:
769:
770: private void paintPartial(Graphics g, int x, int y)
771: {
772: Color saved = g.getColor();
773: g.translate(x, y);
774:
775: g.setColor(Color.GRAY);
776: g.drawRect(1, 1, 7, 10);
777: g.drawLine(8, 6, 11, 6);
778:
779: g.setColor(saved);
780: g.translate(-x, -y);
781: }
782:
783: public void paintIcon(Component c, Graphics g, int x, int y)
784: {
785: Color saved = g.getColor();
786: g.translate(x, y);
787:
788: paintPartial(g, 0, 0);
789: paintPartial(g, 12, 0);
790: paintPartial(g, 0, 12);
791: paintPartial(g, 12, 12);
792:
793: g.setColor(saved);
794: g.translate(-x, -y);
795: }
796: };
797:
798:
799: protected Icon newFolderIcon = directoryIcon;
800:
801:
802: protected int openButtonMnemonic;
803:
804:
805: protected String openButtonText;
806:
807:
808: protected String openButtonToolTipText;
809:
810:
811: protected int saveButtonMnemonic;
812:
813:
814: protected String saveButtonText;
815:
816:
817: protected String saveButtonToolTipText;
818:
819:
820: protected int updateButtonMnemonic;
821:
822:
823: protected String updateButtonText;
824:
825:
826: protected String updateButtonToolTipText;
827:
828:
829: protected Icon upFolderIcon = new Icon()
830: {
831: public int getIconHeight()
832: {
833: return ICON_SIZE;
834: }
835:
836: public int getIconWidth()
837: {
838: return ICON_SIZE;
839: }
840:
841: public void paintIcon(Component comp, Graphics g, int x, int y)
842: {
843: Color saved = g.getColor();
844: g.translate(x, y);
845:
846: Point a = new Point(3, 7);
847: Point b = new Point(3, 21);
848: Point c = new Point(21, 21);
849: Point d = new Point(21, 12);
850: Point e = new Point(16, 12);
851: Point f = new Point(13, 7);
852:
853: Polygon dir = new Polygon(new int[] { a.x, b.x, c.x, d.x, e.x, f.x },
854: new int[] { a.y, b.y, c.y, d.y, e.y, f.y }, 6);
855:
856: g.setColor(new Color(153, 204, 255));
857: g.fillPolygon(dir);
858: g.setColor(Color.BLACK);
859: g.drawPolygon(dir);
860:
861: a = new Point(12, 15);
862: b = new Point(9, 18);
863: c = new Point(15, 18);
864:
865: Polygon arrow = new Polygon(new int[] { a.x, b.x, c.x },
866: new int[] { a.y, b.y, c.y }, 3);
867:
868: g.fillPolygon(arrow);
869:
870: g.drawLine(12, 15, 12, 22);
871:
872: g.translate(-x, -y);
873: g.setColor(saved);
874: }
875: };
876:
877:
878:
879: JFileChooser filechooser;
880:
881:
882: JList filelist;
883:
884:
885: JComboBox filters;
886:
887:
888: BasicDirectoryModel model;
889:
890:
891: FileFilter acceptAll = new AcceptAllFileFilter();
892:
893:
894: FileView fv = new BasicFileView();
895:
896:
897: static final int ICON_SIZE = 24;
898:
899:
900: JComboBox parents;
901:
902:
903: String filename;
904:
905:
906: JButton accept;
907:
908:
909: JButton cancel;
910:
911:
912: JButton upFolderButton;
913:
914:
915: JButton newFolderButton;
916:
917:
918: JButton homeFolderButton;
919:
920:
921: JPanel accessoryPanel;
922:
923:
924: PropertyChangeListener propertyChangeListener;
925:
926:
927: String acceptAllFileFilterText;
928:
929:
930: String dirDescText;
931:
932:
933: String fileDescText;
934:
935:
936: boolean dirSelected = false;
937:
938:
939: File currDir = null;
940:
941: JPanel bottomPanel;
942:
943:
944: JPanel closePanel;
945:
946:
947: private class ListLabelRenderer
948: extends JLabel
949: implements ListCellRenderer
950: {
951:
952: final Color selected = new Color(153, 204, 255);
953:
954:
957: public ListLabelRenderer()
958: {
959: super();
960: setOpaque(true);
961: }
962:
963:
974: public Component getListCellRendererComponent(JList list, Object value,
975: int index,
976: boolean isSelected,
977: boolean cellHasFocus)
978: {
979: setHorizontalAlignment(SwingConstants.LEFT);
980: File file = (File) value;
981: setText(filechooser.getName(file));
982: setIcon(filechooser.getIcon(file));
983: setBackground(isSelected ? selected : Color.WHITE);
984: setForeground(Color.BLACK);
985:
986: return this;
987: }
988: }
989:
990:
993: public class CBLabelRenderer extends JLabel implements ListCellRenderer
994: {
995:
998: public CBLabelRenderer()
999: {
1000: super();
1001: setOpaque(true);
1002: }
1003:
1004:
1015: public Component getListCellRendererComponent(JList list, Object value,
1016: int index,
1017: boolean isSelected,
1018: boolean cellHasFocus)
1019: {
1020: setHorizontalAlignment(SwingConstants.LEFT);
1021: setIcon(directoryIcon);
1022: setText(value.toString());
1023: setForeground(Color.BLACK);
1024: setBackground(Color.WHITE);
1025:
1026: return this;
1027: }
1028: }
1029:
1030: void closeDialog()
1031: {
1032: Window owner = SwingUtilities.windowForComponent(filechooser);
1033: if (owner instanceof JDialog)
1034: ((JDialog) owner).dispose();
1035: }
1036:
1037:
1042: public BasicFileChooserUI(JFileChooser b)
1043: {
1044: this.filechooser = b;
1045: }
1046:
1047:
1054: public static ComponentUI createUI(JComponent c)
1055: {
1056: return new BasicFileChooserUI((JFileChooser) c);
1057: }
1058:
1059:
1064: public void installUI(JComponent c)
1065: {
1066: if (c instanceof JFileChooser)
1067: {
1068: JFileChooser fc = (JFileChooser) c;
1069: fc.resetChoosableFileFilters();
1070: createModel();
1071: clearIconCache();
1072: installDefaults(fc);
1073: installComponents(fc);
1074: installListeners(fc);
1075: }
1076: }
1077:
1078:
1083: public void uninstallUI(JComponent c)
1084: {
1085: model = null;
1086: uninstallListeners(filechooser);
1087: uninstallComponents(filechooser);
1088: uninstallDefaults(filechooser);
1089: filechooser = null;
1090: }
1091:
1092:
1093:
1094:
1095: void boxEntries()
1096: {
1097: ArrayList parentFiles = new ArrayList();
1098: File parent = filechooser.getCurrentDirectory();
1099: if (parent == null)
1100: parent = filechooser.getFileSystemView().getDefaultDirectory();
1101: while (parent != null)
1102: {
1103: String name = parent.getName();
1104: if (name.equals(""))
1105: name = parent.getAbsolutePath();
1106:
1107: parentFiles.add(parentFiles.size(), name);
1108: parent = parent.getParentFile();
1109: }
1110:
1111: if (parentFiles.size() == 0)
1112: return;
1113:
1114: if (parents.getItemCount() > 0)
1115: parents.removeAllItems();
1116: for (int i = parentFiles.size() - 1; i >= 0; i--)
1117: parents.addItem(parentFiles.get(i));
1118: parents.setSelectedIndex(parentFiles.size() - 1);
1119: parents.revalidate();
1120: parents.repaint();
1121: }
1122:
1123:
1128: private ItemListener createBoxListener()
1129: {
1130: return new ItemListener()
1131: {
1132: public void itemStateChanged(ItemEvent e)
1133: {
1134: if (parents.getItemCount() - 1 == parents.getSelectedIndex())
1135: return;
1136: StringBuffer dir = new StringBuffer();
1137: for (int i = 0; i <= parents.getSelectedIndex(); i++)
1138: {
1139: dir.append(parents.getItemAt(i));
1140: dir.append(File.separatorChar);
1141: }
1142: filechooser.setCurrentDirectory(filechooser.getFileSystemView()
1143: .createFileObject(dir
1144: .toString()));
1145: }
1146: };
1147: }
1148:
1149:
1154: private ItemListener createFilterListener()
1155: {
1156: return new ItemListener()
1157: {
1158: public void itemStateChanged(ItemEvent e)
1159: {
1160: int index = filters.getSelectedIndex();
1161: if (index == -1)
1162: return;
1163: filechooser.setFileFilter(filechooser.getChoosableFileFilters()[index]);
1164: }
1165: };
1166: }
1167:
1168: void filterEntries()
1169: {
1170: FileFilter[] list = filechooser.getChoosableFileFilters();
1171: if (filters.getItemCount() > 0)
1172: filters.removeAllItems();
1173:
1174: int index = -1;
1175: String selected = filechooser.getFileFilter().getDescription();
1176: for (int i = 0; i < list.length; i++)
1177: {
1178: if (selected.equals(list[i].getDescription()))
1179: index = i;
1180: filters.addItem(list[i].getDescription());
1181: }
1182: filters.setSelectedIndex(index);
1183: filters.revalidate();
1184: filters.repaint();
1185: }
1186:
1187:
1192: public void installComponents(JFileChooser fc)
1193: {
1194: JLabel look = new JLabel("Look In:");
1195:
1196: parents = new JComboBox();
1197: parents.setRenderer(new CBLabelRenderer());
1198: boxEntries();
1199: look.setLabelFor(parents);
1200: JPanel parentsPanel = new JPanel();
1201: parentsPanel.add(look);
1202: parentsPanel.add(parents);
1203: JPanel buttonPanel = new JPanel();
1204:
1205: upFolderButton = new JButton();
1206: upFolderButton.setIcon(upFolderIcon);
1207: buttonPanel.add(upFolderButton);
1208:
1209: homeFolderButton = new JButton();
1210: homeFolderButton = new JButton(homeFolderIcon);
1211: buttonPanel.add(homeFolderButton);
1212:
1213: newFolderButton = new JButton();
1214: newFolderButton.setIcon(newFolderIcon);
1215: buttonPanel.add(newFolderButton);
1216:
1217: ButtonGroup toggles = new ButtonGroup();
1218: JToggleButton listViewButton = new JToggleButton();
1219: listViewButton.setIcon(listViewIcon);
1220: toggles.add(listViewButton);
1221: buttonPanel.add(listViewButton);
1222:
1223: JToggleButton detailsViewButton = new JToggleButton();
1224: detailsViewButton.setIcon(detailsViewIcon);
1225: toggles.add(detailsViewButton);
1226: buttonPanel.add(detailsViewButton);
1227:
1228: JPanel topPanel = new JPanel();
1229: topPanel.setLayout(new java.awt.FlowLayout());
1230: topPanel.add(parentsPanel);
1231: topPanel.add(buttonPanel);
1232:
1233: accessoryPanel = new JPanel();
1234: if (filechooser.getAccessory() != null)
1235: accessoryPanel.add(filechooser.getAccessory(), BorderLayout.CENTER);
1236:
1237: filelist = new JList(model);
1238: filelist.setVisibleRowCount(6);
1239: JScrollPane scrollp = new JScrollPane(filelist);
1240: scrollp.setPreferredSize(new Dimension(400, 175));
1241: filelist.setBackground(Color.WHITE);
1242:
1243: filelist.setLayoutOrientation(JList.VERTICAL_WRAP);
1244: filelist.setCellRenderer(new ListLabelRenderer());
1245:
1246: GridBagConstraints c = new GridBagConstraints();
1247: c.gridx = 0;
1248: c.gridy = 0;
1249: c.fill = GridBagConstraints.BOTH;
1250: c.weightx = 1;
1251: c.weighty = 1;
1252:
1253: JPanel centrePanel = new JPanel();
1254: centrePanel.setLayout(new GridBagLayout());
1255: centrePanel.add(scrollp, c);
1256:
1257: c.gridx = 1;
1258: centrePanel.add(accessoryPanel, c);
1259:
1260: JLabel fileNameLabel = new JLabel("File Name:");
1261: JLabel fileTypesLabel = new JLabel("Files of Type:");
1262:
1263: JTextField entry = new JTextField();
1264: filters = new JComboBox();
1265: filterEntries();
1266:
1267: fileNameLabel.setLabelFor(entry);
1268: fileNameLabel.setHorizontalTextPosition(SwingConstants.LEFT);
1269: fileTypesLabel.setLabelFor(filters);
1270: fileTypesLabel.setHorizontalTextPosition(SwingConstants.LEFT);
1271:
1272: closePanel = new JPanel();
1273: accept = getApproveButton(filechooser);
1274: cancel = new JButton(cancelButtonText);
1275: cancel.setMnemonic(cancelButtonMnemonic);
1276: cancel.setToolTipText(cancelButtonToolTipText);
1277: closePanel.add(accept);
1278: closePanel.add(cancel);
1279:
1280: c.anchor = GridBagConstraints.WEST;
1281: c.weighty = 0;
1282: c.weightx = 0;
1283: c.gridx = 0;
1284:
1285: bottomPanel = new JPanel();
1286: bottomPanel.setLayout(new GridBagLayout());
1287: bottomPanel.add(fileNameLabel, c);
1288:
1289: c.gridy = 1;
1290: bottomPanel.add(fileTypesLabel, c);
1291: c.gridx = 1;
1292: c.gridy = 0;
1293: c.weightx = 1;
1294: c.weighty = 1;
1295: bottomPanel.add(entry, c);
1296:
1297: c.gridy = 1;
1298: bottomPanel.add(filters, c);
1299:
1300: c.fill = GridBagConstraints.NONE;
1301: c.gridy = 2;
1302: c.anchor = GridBagConstraints.EAST;
1303: bottomPanel.add(closePanel, c);
1304:
1305: filechooser.setLayout(new BorderLayout());
1306: filechooser.add(topPanel, BorderLayout.NORTH);
1307: filechooser.add(centrePanel, BorderLayout.CENTER);
1308: filechooser.add(bottomPanel, BorderLayout.SOUTH);
1309: }
1310:
1311:
1316: public void uninstallComponents(JFileChooser fc)
1317: {
1318: parents = null;
1319:
1320: accept = null;
1321: cancel = null;
1322: upFolderButton = null;
1323: homeFolderButton = null;
1324: newFolderButton = null;
1325:
1326: filelist = null;
1327: }
1328:
1329:
1334: protected void installListeners(JFileChooser fc)
1335: {
1336: propertyChangeListener = createPropertyChangeListener(filechooser);
1337: filechooser.addPropertyChangeListener(propertyChangeListener);
1338:
1339:
1340: accept.addActionListener(getApproveSelectionAction());
1341: cancel.addActionListener(getCancelSelectionAction());
1342: upFolderButton.addActionListener(getChangeToParentDirectoryAction());
1343: homeFolderButton.addActionListener(getGoHomeAction());
1344: newFolderButton.addActionListener(getNewFolderAction());
1345: filters.addItemListener(createFilterListener());
1346:
1347: filelist.addMouseListener(createDoubleClickListener(filechooser, filelist));
1348: filelist.addListSelectionListener(createListSelectionListener(filechooser));
1349: }
1350:
1351:
1356: protected void uninstallListeners(JFileChooser fc)
1357: {
1358: filechooser.removePropertyChangeListener(propertyChangeListener);
1359: propertyChangeListener = null;
1360: }
1361:
1362:
1367: protected void installDefaults(JFileChooser fc)
1368: {
1369: installIcons(fc);
1370: installStrings(fc);
1371: }
1372:
1373:
1378: protected void uninstallDefaults(JFileChooser fc)
1379: {
1380: uninstallStrings(fc);
1381: uninstallIcons(fc);
1382: }
1383:
1384:
1389: protected void installIcons(JFileChooser fc)
1390: {
1391:
1392: }
1393:
1394:
1399: protected void uninstallIcons(JFileChooser fc)
1400: {
1401:
1402: }
1403:
1404:
1409: protected void installStrings(JFileChooser fc)
1410: {
1411: UIDefaults defaults = UIManager.getLookAndFeelDefaults();
1412:
1413: acceptAllFileFilterText = defaults.getString("FileChooser.acceptAllFileFilterText");
1414: cancelButtonMnemonic = defaults.getInt("FileChooser.cancelButtonMnemonic");
1415: cancelButtonText = defaults.getString("FileChooser.cancelButtonText");
1416: cancelButtonToolTipText = defaults.getString("FileChooser.cancelButtonToolTipText");
1417:
1418: dirDescText = defaults.getString("FileChooser.directoryDescriptionText");
1419: fileDescText = defaults.getString("FileChooser.fileDescriptionText");
1420:
1421: helpButtonMnemonic = defaults.getInt("FileChooser.helpButtonMnemonic");
1422: helpButtonText = defaults.getString("FileChooser.helpButtonText");
1423: helpButtonToolTipText = defaults.getString("FileChooser.helpButtonToolTipText");
1424:
1425: openButtonMnemonic = defaults.getInt("FileChooser.openButtonMnemonic");
1426: openButtonText = defaults.getString("FileChooser.openButtonText");
1427: openButtonToolTipText = defaults.getString("FileChooser.openButtonToolTipText");
1428:
1429: saveButtonMnemonic = defaults.getInt("FileChooser.saveButtonMnemonic");
1430: saveButtonText = defaults.getString("FileChooser.saveButtonText");
1431: saveButtonToolTipText = defaults.getString("FileChooser.saveButtonToolTipText");
1432: }
1433:
1434:
1439: protected void uninstallStrings(JFileChooser fc)
1440: {
1441: acceptAllFileFilterText = null;
1442: cancelButtonMnemonic = 0;
1443: cancelButtonText = null;
1444: cancelButtonToolTipText = null;
1445:
1446: dirDescText = null;
1447: fileDescText = null;
1448:
1449: helpButtonMnemonic = 0;
1450: helpButtonText = null;
1451: helpButtonToolTipText = null;
1452:
1453: openButtonMnemonic = 0;
1454: openButtonText = null;
1455: openButtonToolTipText = null;
1456:
1457: saveButtonMnemonic = 0;
1458: saveButtonText = null;
1459: saveButtonToolTipText = null;
1460: }
1461:
1462:
1465: protected void createModel()
1466: {
1467: model = new BasicDirectoryModel(filechooser);
1468: }
1469:
1470:
1475: public BasicDirectoryModel getModel()
1476: {
1477: return model;
1478: }
1479:
1480:
1487: public PropertyChangeListener createPropertyChangeListener(JFileChooser fc)
1488: {
1489: return new PropertyChangeListener()
1490: {
1491: public void propertyChange(PropertyChangeEvent e)
1492: {
1493:
1494: if (e.getPropertyName().equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY))
1495: {
1496: if (filechooser.getSelectedFile() == null)
1497: setFileName(null);
1498: else
1499: setFileName(filechooser.getSelectedFile().toString());
1500: int index = -1;
1501: File file = filechooser.getSelectedFile();
1502: for (index = 0; index < model.getSize(); index++)
1503: if (((File) model.getElementAt(index)).equals(file))
1504: break;
1505: if (index == -1)
1506: return;
1507: filelist.setSelectedIndex(index);
1508: filelist.ensureIndexIsVisible(index);
1509: filelist.revalidate();
1510: filelist.repaint();
1511: }
1512: else if (e.getPropertyName().equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY))
1513: {
1514: filelist.clearSelection();
1515: filelist.revalidate();
1516: filelist.repaint();
1517: setDirectorySelected(false);
1518: setDirectory(filechooser.getCurrentDirectory());
1519: boxEntries();
1520: }
1521: else if (e.getPropertyName().equals(JFileChooser.CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY)
1522: || e.getPropertyName().equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY))
1523: filterEntries();
1524: else if (e.getPropertyName().equals(JFileChooser.DIALOG_TYPE_CHANGED_PROPERTY)
1525: || e.getPropertyName().equals(JFileChooser.DIALOG_TITLE_CHANGED_PROPERTY))
1526: {
1527: Window owner = SwingUtilities.windowForComponent(filechooser);
1528: if (owner instanceof JDialog)
1529: ((JDialog) owner).setTitle(getDialogTitle(filechooser));
1530: accept.setText(getApproveButtonText(filechooser));
1531: accept.setToolTipText(getApproveButtonToolTipText(filechooser));
1532: accept.setMnemonic(getApproveButtonMnemonic(filechooser));
1533: }
1534: else if (e.getPropertyName().equals(JFileChooser.APPROVE_BUTTON_TEXT_CHANGED_PROPERTY))
1535: accept.setText(getApproveButtonText(filechooser));
1536: else if (e.getPropertyName().equals(JFileChooser.APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY))
1537: accept.setToolTipText(getApproveButtonToolTipText(filechooser));
1538: else if (e.getPropertyName().equals(JFileChooser.APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY))
1539: accept.setMnemonic(getApproveButtonMnemonic(filechooser));
1540: else if (e.getPropertyName().equals(JFileChooser.CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY))
1541: {
1542: if (filechooser.getControlButtonsAreShown())
1543: {
1544: GridBagConstraints c = new GridBagConstraints();
1545: c.gridy = 1;
1546: bottomPanel.add(filters, c);
1547:
1548: c.fill = GridBagConstraints.BOTH;
1549: c.gridy = 2;
1550: c.anchor = GridBagConstraints.EAST;
1551: bottomPanel.add(closePanel, c);
1552: bottomPanel.revalidate();
1553: bottomPanel.repaint();
1554: bottomPanel.doLayout();
1555: }
1556: else
1557: bottomPanel.remove(closePanel);
1558: }
1559: else if (e.getPropertyName().equals(JFileChooser.ACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY))
1560: {
1561: if (filechooser.isAcceptAllFileFilterUsed())
1562: filechooser.addChoosableFileFilter(getAcceptAllFileFilter(filechooser));
1563: else
1564: filechooser.removeChoosableFileFilter(getAcceptAllFileFilter(filechooser));
1565: }
1566: else if (e.getPropertyName().equals(JFileChooser.ACCESSORY_CHANGED_PROPERTY))
1567: {
1568: JComponent old = (JComponent) e.getOldValue();
1569: if (old != null)
1570: getAccessoryPanel().remove(old);
1571: JComponent newval = (JComponent) e.getNewValue();
1572: if (newval != null)
1573: getAccessoryPanel().add(newval);
1574: }
1575: if (e.getPropertyName().equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)
1576: || e.getPropertyName().equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY)
1577: || e.getPropertyName().equals(JFileChooser.FILE_HIDING_CHANGED_PROPERTY))
1578: rescanCurrentDirectory(filechooser);
1579:
1580: filechooser.revalidate();
1581: filechooser.repaint();
1582: }
1583: };
1584: }
1585:
1586:
1591: public String getFileName()
1592: {
1593: return filename;
1594: }
1595:
1596:
1601: public String getDirectoryName()
1602: {
1603:
1604: return null;
1605: }
1606:
1607:
1612: public void setFileName(String filename)
1613: {
1614: this.filename = filename;
1615: }
1616:
1617:
1622: public void setDirectoryName(String dirname)
1623: {
1624:
1625: }
1626:
1627:
1632: public void rescanCurrentDirectory(JFileChooser fc)
1633: {
1634: getModel().validateFileCache();
1635: filelist.revalidate();
1636: }
1637:
1638:
1644: public void ensureFileIsVisible(JFileChooser fc, File f)
1645: {
1646:
1647: }
1648:
1649:
1654: public JFileChooser getFileChooser()
1655: {
1656: return filechooser;
1657: }
1658:
1659:
1664: public JPanel getAccessoryPanel()
1665: {
1666: return accessoryPanel;
1667: }
1668:
1669:
1676: public JButton getApproveButton(JFileChooser fc)
1677: {
1678: accept = new JButton(getApproveButtonText(fc));
1679: accept.setMnemonic(getApproveButtonMnemonic(fc));
1680: accept.setToolTipText(getApproveButtonToolTipText(fc));
1681: return accept;
1682: }
1683:
1684:
1691: public String getApproveButtonToolTipText(JFileChooser fc)
1692: {
1693: if (fc.getApproveButtonToolTipText() != null)
1694: return fc.getApproveButtonToolTipText();
1695: else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1696: return saveButtonToolTipText;
1697: else
1698: return openButtonToolTipText;
1699: }
1700:
1701:
1704: public void clearIconCache()
1705: {
1706: if (fv instanceof BasicFileView)
1707: ((BasicFileView) fv).clearIconCache();
1708: }
1709:
1710:
1717: public ListSelectionListener createListSelectionListener(JFileChooser fc)
1718: {
1719: return new SelectionListener();
1720: }
1721:
1722:
1730: protected MouseListener createDoubleClickListener(JFileChooser fc, JList list)
1731: {
1732: return new DoubleClickListener(list);
1733: }
1734:
1735:
1740: protected boolean isDirectorySelected()
1741: {
1742: return dirSelected;
1743: }
1744:
1745:
1750: protected void setDirectorySelected(boolean selected)
1751: {
1752: dirSelected = selected;
1753: }
1754:
1755:
1760: protected File getDirectory()
1761: {
1762: return currDir;
1763: }
1764:
1765:
1770: protected void setDirectory(File f)
1771: {
1772: currDir = f;
1773: }
1774:
1775:
1782: public FileFilter getAcceptAllFileFilter(JFileChooser fc)
1783: {
1784: return acceptAll;
1785: }
1786:
1787:
1794: public FileView getFileView(JFileChooser fc)
1795: {
1796: if (fc.getFileView() != null)
1797: return fc.getFileView();
1798: return fv;
1799: }
1800:
1801:
1808: public String getDialogTitle(JFileChooser fc)
1809: {
1810: String ret = fc.getDialogTitle();
1811: if (ret != null)
1812: return ret;
1813: switch (fc.getDialogType())
1814: {
1815: case JFileChooser.OPEN_DIALOG:
1816: ret = openButtonText;
1817: break;
1818: case JFileChooser.SAVE_DIALOG:
1819: ret = saveButtonText;
1820: break;
1821: default:
1822: ret = fc.getApproveButtonText();
1823: break;
1824: }
1825: if (ret == null)
1826: ret = openButtonText;
1827: return ret;
1828: }
1829:
1830:
1837: public int getApproveButtonMnemonic(JFileChooser fc)
1838: {
1839: if (fc.getApproveButtonMnemonic() != 0)
1840: return fc.getApproveButtonMnemonic();
1841: else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1842: return saveButtonMnemonic;
1843: else
1844: return openButtonMnemonic;
1845: }
1846:
1847:
1854: public String getApproveButtonText(JFileChooser fc)
1855: {
1856: if (fc.getApproveButtonText() != null)
1857: return fc.getApproveButtonText();
1858: else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1859: return saveButtonText;
1860: else
1861: return openButtonText;
1862: }
1863:
1864:
1869: public Action getNewFolderAction()
1870: {
1871: return new NewFolderAction();
1872: }
1873:
1874:
1879: public Action getGoHomeAction()
1880: {
1881: return new GoHomeAction();
1882: }
1883:
1884:
1889: public Action getChangeToParentDirectoryAction()
1890: {
1891: return new ChangeToParentDirectoryAction();
1892: }
1893:
1894:
1899: public Action getApproveSelectionAction()
1900: {
1901: return new ApproveSelectionAction();
1902: }
1903:
1904:
1909: public Action getCancelSelectionAction()
1910: {
1911: return new CancelSelectionAction();
1912: }
1913:
1914:
1919: public Action getUpdateAction()
1920: {
1921: return new UpdateAction();
1922: }
1923: }