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: private void boxEntries()
1094: {
1095: ArrayList parentFiles = new ArrayList();
1096: File parent = filechooser.getCurrentDirectory();
1097: if (parent == null)
1098: parent = filechooser.getFileSystemView().getDefaultDirectory();
1099: while (parent != null)
1100: {
1101: String name = parent.getName();
1102: if (name.equals(""))
1103: name = parent.getAbsolutePath();
1104:
1105: parentFiles.add(parentFiles.size(), name);
1106: parent = parent.getParentFile();
1107: }
1108:
1109: if (parentFiles.size() == 0)
1110: return;
1111:
1112: if (parents.getItemCount() > 0)
1113: parents.removeAllItems();
1114: for (int i = parentFiles.size() - 1; i >= 0; i--)
1115: parents.addItem(parentFiles.get(i));
1116: parents.setSelectedIndex(parentFiles.size() - 1);
1117: parents.revalidate();
1118: parents.repaint();
1119: }
1120:
1121:
1126: private ItemListener createBoxListener()
1127: {
1128: return new ItemListener()
1129: {
1130: public void itemStateChanged(ItemEvent e)
1131: {
1132: if (parents.getItemCount() - 1 == parents.getSelectedIndex())
1133: return;
1134: StringBuffer dir = new StringBuffer();
1135: for (int i = 0; i <= parents.getSelectedIndex(); i++)
1136: {
1137: dir.append(parents.getItemAt(i));
1138: dir.append(File.separatorChar);
1139: }
1140: filechooser.setCurrentDirectory(filechooser.getFileSystemView()
1141: .createFileObject(dir
1142: .toString()));
1143: }
1144: };
1145: }
1146:
1147:
1152: private ItemListener createFilterListener()
1153: {
1154: return new ItemListener()
1155: {
1156: public void itemStateChanged(ItemEvent e)
1157: {
1158: int index = filters.getSelectedIndex();
1159: if (index == -1)
1160: return;
1161: filechooser.setFileFilter(filechooser.getChoosableFileFilters()[index]);
1162: }
1163: };
1164: }
1165:
1166: void filterEntries()
1167: {
1168: FileFilter[] list = filechooser.getChoosableFileFilters();
1169: if (filters.getItemCount() > 0)
1170: filters.removeAllItems();
1171:
1172: int index = -1;
1173: String selected = filechooser.getFileFilter().getDescription();
1174: for (int i = 0; i < list.length; i++)
1175: {
1176: if (selected.equals(list[i].getDescription()))
1177: index = i;
1178: filters.addItem(list[i].getDescription());
1179: }
1180: filters.setSelectedIndex(index);
1181: filters.revalidate();
1182: filters.repaint();
1183: }
1184:
1185:
1190: public void installComponents(JFileChooser fc)
1191: {
1192: JLabel look = new JLabel("Look In:");
1193:
1194: parents = new JComboBox();
1195: parents.setRenderer(new CBLabelRenderer());
1196: boxEntries();
1197: look.setLabelFor(parents);
1198: JPanel parentsPanel = new JPanel();
1199: parentsPanel.add(look);
1200: parentsPanel.add(parents);
1201: JPanel buttonPanel = new JPanel();
1202:
1203: upFolderButton = new JButton();
1204: upFolderButton.setIcon(upFolderIcon);
1205: buttonPanel.add(upFolderButton);
1206:
1207: homeFolderButton = new JButton();
1208: homeFolderButton = new JButton(homeFolderIcon);
1209: buttonPanel.add(homeFolderButton);
1210:
1211: newFolderButton = new JButton();
1212: newFolderButton.setIcon(newFolderIcon);
1213: buttonPanel.add(newFolderButton);
1214:
1215: ButtonGroup toggles = new ButtonGroup();
1216: JToggleButton listViewButton = new JToggleButton();
1217: listViewButton.setIcon(listViewIcon);
1218: toggles.add(listViewButton);
1219: buttonPanel.add(listViewButton);
1220:
1221: JToggleButton detailsViewButton = new JToggleButton();
1222: detailsViewButton.setIcon(detailsViewIcon);
1223: toggles.add(detailsViewButton);
1224: buttonPanel.add(detailsViewButton);
1225:
1226: JPanel topPanel = new JPanel();
1227: topPanel.setLayout(new java.awt.FlowLayout());
1228: topPanel.add(parentsPanel);
1229: topPanel.add(buttonPanel);
1230:
1231: accessoryPanel = new JPanel();
1232: if (filechooser.getAccessory() != null)
1233: accessoryPanel.add(filechooser.getAccessory(), BorderLayout.CENTER);
1234:
1235: filelist = new JList(model);
1236: filelist.setVisibleRowCount(6);
1237: JScrollPane scrollp = new JScrollPane(filelist);
1238: scrollp.setPreferredSize(new Dimension(400, 175));
1239: filelist.setBackground(Color.WHITE);
1240:
1241: filelist.setLayoutOrientation(JList.VERTICAL_WRAP);
1242: filelist.setCellRenderer(new ListLabelRenderer());
1243:
1244: GridBagConstraints c = new GridBagConstraints();
1245: c.gridx = 0;
1246: c.gridy = 0;
1247: c.fill = GridBagConstraints.BOTH;
1248: c.weightx = 1;
1249: c.weighty = 1;
1250:
1251: JPanel centrePanel = new JPanel();
1252: centrePanel.setLayout(new GridBagLayout());
1253: centrePanel.add(scrollp, c);
1254:
1255: c.gridx = 1;
1256: centrePanel.add(accessoryPanel, c);
1257:
1258: JLabel fileNameLabel = new JLabel("File Name:");
1259: JLabel fileTypesLabel = new JLabel("Files of Type:");
1260:
1261: JTextField entry = new JTextField();
1262: filters = new JComboBox();
1263: filterEntries();
1264:
1265: fileNameLabel.setLabelFor(entry);
1266: fileNameLabel.setHorizontalTextPosition(SwingConstants.LEFT);
1267: fileTypesLabel.setLabelFor(filters);
1268: fileTypesLabel.setHorizontalTextPosition(SwingConstants.LEFT);
1269:
1270: closePanel = new JPanel();
1271: accept = getApproveButton(filechooser);
1272: cancel = new JButton(cancelButtonText);
1273: cancel.setMnemonic(cancelButtonMnemonic);
1274: cancel.setToolTipText(cancelButtonToolTipText);
1275: closePanel.add(accept);
1276: closePanel.add(cancel);
1277:
1278: c.anchor = GridBagConstraints.WEST;
1279: c.weighty = 0;
1280: c.weightx = 0;
1281: c.gridx = 0;
1282:
1283: bottomPanel = new JPanel();
1284: bottomPanel.setLayout(new GridBagLayout());
1285: bottomPanel.add(fileNameLabel, c);
1286:
1287: c.gridy = 1;
1288: bottomPanel.add(fileTypesLabel, c);
1289: c.gridx = 1;
1290: c.gridy = 0;
1291: c.weightx = 1;
1292: c.weighty = 1;
1293: bottomPanel.add(entry, c);
1294:
1295: c.gridy = 1;
1296: bottomPanel.add(filters, c);
1297:
1298: c.fill = GridBagConstraints.NONE;
1299: c.gridy = 2;
1300: c.anchor = GridBagConstraints.EAST;
1301: bottomPanel.add(closePanel, c);
1302:
1303: filechooser.setLayout(new BorderLayout());
1304: filechooser.add(topPanel, BorderLayout.NORTH);
1305: filechooser.add(centrePanel, BorderLayout.CENTER);
1306: filechooser.add(bottomPanel, BorderLayout.SOUTH);
1307: }
1308:
1309:
1314: public void uninstallComponents(JFileChooser fc)
1315: {
1316: parents = null;
1317:
1318: accept = null;
1319: cancel = null;
1320: upFolderButton = null;
1321: homeFolderButton = null;
1322: newFolderButton = null;
1323:
1324: filelist = null;
1325: }
1326:
1327:
1332: protected void installListeners(JFileChooser fc)
1333: {
1334: propertyChangeListener = createPropertyChangeListener(filechooser);
1335: filechooser.addPropertyChangeListener(propertyChangeListener);
1336:
1337:
1338: accept.addActionListener(getApproveSelectionAction());
1339: cancel.addActionListener(getCancelSelectionAction());
1340: upFolderButton.addActionListener(getChangeToParentDirectoryAction());
1341: homeFolderButton.addActionListener(getGoHomeAction());
1342: newFolderButton.addActionListener(getNewFolderAction());
1343: filters.addItemListener(createFilterListener());
1344:
1345: filelist.addMouseListener(createDoubleClickListener(filechooser, filelist));
1346: filelist.addListSelectionListener(createListSelectionListener(filechooser));
1347: }
1348:
1349:
1354: protected void uninstallListeners(JFileChooser fc)
1355: {
1356: filechooser.removePropertyChangeListener(propertyChangeListener);
1357: propertyChangeListener = null;
1358: }
1359:
1360:
1365: protected void installDefaults(JFileChooser fc)
1366: {
1367: installIcons(fc);
1368: installStrings(fc);
1369: }
1370:
1371:
1376: protected void uninstallDefaults(JFileChooser fc)
1377: {
1378: uninstallStrings(fc);
1379: uninstallIcons(fc);
1380: }
1381:
1382:
1387: protected void installIcons(JFileChooser fc)
1388: {
1389:
1390: }
1391:
1392:
1397: protected void uninstallIcons(JFileChooser fc)
1398: {
1399:
1400: }
1401:
1402:
1407: protected void installStrings(JFileChooser fc)
1408: {
1409: UIDefaults defaults = UIManager.getLookAndFeelDefaults();
1410:
1411: acceptAllFileFilterText = defaults.getString("FileChooser.acceptAllFileFilterText");
1412: cancelButtonMnemonic = defaults.getInt("FileChooser.cancelButtonMnemonic");
1413: cancelButtonText = defaults.getString("FileChooser.cancelButtonText");
1414: cancelButtonToolTipText = defaults.getString("FileChooser.cancelButtonToolTipText");
1415:
1416: dirDescText = defaults.getString("FileChooser.directoryDescriptionText");
1417: fileDescText = defaults.getString("FileChooser.fileDescriptionText");
1418:
1419: helpButtonMnemonic = defaults.getInt("FileChooser.helpButtonMnemonic");
1420: helpButtonText = defaults.getString("FileChooser.helpButtonText");
1421: helpButtonToolTipText = defaults.getString("FileChooser.helpButtonToolTipText");
1422:
1423: openButtonMnemonic = defaults.getInt("FileChooser.openButtonMnemonic");
1424: openButtonText = defaults.getString("FileChooser.openButtonText");
1425: openButtonToolTipText = defaults.getString("FileChooser.openButtonToolTipText");
1426:
1427: saveButtonMnemonic = defaults.getInt("FileChooser.saveButtonMnemonic");
1428: saveButtonText = defaults.getString("FileChooser.saveButtonText");
1429: saveButtonToolTipText = defaults.getString("FileChooser.saveButtonToolTipText");
1430: }
1431:
1432:
1437: protected void uninstallStrings(JFileChooser fc)
1438: {
1439: acceptAllFileFilterText = null;
1440: cancelButtonMnemonic = 0;
1441: cancelButtonText = null;
1442: cancelButtonToolTipText = null;
1443:
1444: dirDescText = null;
1445: fileDescText = null;
1446:
1447: helpButtonMnemonic = 0;
1448: helpButtonText = null;
1449: helpButtonToolTipText = null;
1450:
1451: openButtonMnemonic = 0;
1452: openButtonText = null;
1453: openButtonToolTipText = null;
1454:
1455: saveButtonMnemonic = 0;
1456: saveButtonText = null;
1457: saveButtonToolTipText = null;
1458: }
1459:
1460:
1463: protected void createModel()
1464: {
1465: model = new BasicDirectoryModel(filechooser);
1466: }
1467:
1468:
1473: public BasicDirectoryModel getModel()
1474: {
1475: return model;
1476: }
1477:
1478:
1485: public PropertyChangeListener createPropertyChangeListener(JFileChooser fc)
1486: {
1487: return new PropertyChangeListener()
1488: {
1489: public void propertyChange(PropertyChangeEvent e)
1490: {
1491:
1492: if (e.getPropertyName().equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY))
1493: {
1494: if (filechooser.getSelectedFile() == null)
1495: setFileName(null);
1496: else
1497: setFileName(filechooser.getSelectedFile().toString());
1498: int index = -1;
1499: File file = filechooser.getSelectedFile();
1500: for (index = 0; index < model.getSize(); index++)
1501: if (((File) model.getElementAt(index)).equals(file))
1502: break;
1503: if (index == -1)
1504: return;
1505: filelist.setSelectedIndex(index);
1506: filelist.ensureIndexIsVisible(index);
1507: filelist.revalidate();
1508: filelist.repaint();
1509: }
1510: else if (e.getPropertyName().equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY))
1511: {
1512:
1513: filelist.clearSelection();
1514: filelist.revalidate();
1515: filelist.repaint();
1516: setDirectorySelected(false);
1517: setDirectory(filechooser.getCurrentDirectory());
1518: }
1519: else if (e.getPropertyName().equals(JFileChooser.CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY)
1520: || e.getPropertyName().equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY))
1521: filterEntries();
1522: else if (e.getPropertyName().equals(JFileChooser.DIALOG_TYPE_CHANGED_PROPERTY)
1523: || e.getPropertyName().equals(JFileChooser.DIALOG_TITLE_CHANGED_PROPERTY))
1524: {
1525: Window owner = SwingUtilities.windowForComponent(filechooser);
1526: if (owner instanceof JDialog)
1527: ((JDialog) owner).setTitle(getDialogTitle(filechooser));
1528: accept.setText(getApproveButtonText(filechooser));
1529: accept.setToolTipText(getApproveButtonToolTipText(filechooser));
1530: accept.setMnemonic(getApproveButtonMnemonic(filechooser));
1531: }
1532: else if (e.getPropertyName().equals(JFileChooser.APPROVE_BUTTON_TEXT_CHANGED_PROPERTY))
1533: accept.setText(getApproveButtonText(filechooser));
1534: else if (e.getPropertyName().equals(JFileChooser.APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY))
1535: accept.setToolTipText(getApproveButtonToolTipText(filechooser));
1536: else if (e.getPropertyName().equals(JFileChooser.APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY))
1537: accept.setMnemonic(getApproveButtonMnemonic(filechooser));
1538: else if (e.getPropertyName().equals(JFileChooser.CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY))
1539: {
1540: if (filechooser.getControlButtonsAreShown())
1541: {
1542: GridBagConstraints c = new GridBagConstraints();
1543: c.gridy = 1;
1544: bottomPanel.add(filters, c);
1545:
1546: c.fill = GridBagConstraints.BOTH;
1547: c.gridy = 2;
1548: c.anchor = GridBagConstraints.EAST;
1549: bottomPanel.add(closePanel, c);
1550: bottomPanel.revalidate();
1551: bottomPanel.repaint();
1552: bottomPanel.doLayout();
1553: }
1554: else
1555: bottomPanel.remove(closePanel);
1556: }
1557: else if (e.getPropertyName().equals(JFileChooser.ACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY))
1558: {
1559: if (filechooser.isAcceptAllFileFilterUsed())
1560: filechooser.addChoosableFileFilter(getAcceptAllFileFilter(filechooser));
1561: else
1562: filechooser.removeChoosableFileFilter(getAcceptAllFileFilter(filechooser));
1563: }
1564: else if (e.getPropertyName().equals(JFileChooser.ACCESSORY_CHANGED_PROPERTY))
1565: {
1566: JComponent old = (JComponent) e.getOldValue();
1567: if (old != null)
1568: getAccessoryPanel().remove(old);
1569: JComponent newval = (JComponent) e.getNewValue();
1570: if (newval != null)
1571: getAccessoryPanel().add(newval);
1572: }
1573: if (e.getPropertyName().equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)
1574: || e.getPropertyName().equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY)
1575: || e.getPropertyName().equals(JFileChooser.FILE_HIDING_CHANGED_PROPERTY))
1576: rescanCurrentDirectory(filechooser);
1577:
1578: filechooser.revalidate();
1579: filechooser.repaint();
1580: }
1581: };
1582: }
1583:
1584:
1589: public String getFileName()
1590: {
1591: return filename;
1592: }
1593:
1594:
1599: public String getDirectoryName()
1600: {
1601:
1602: return null;
1603: }
1604:
1605:
1610: public void setFileName(String filename)
1611: {
1612: this.filename = filename;
1613: }
1614:
1615:
1620: public void setDirectoryName(String dirname)
1621: {
1622:
1623: }
1624:
1625:
1630: public void rescanCurrentDirectory(JFileChooser fc)
1631: {
1632: getModel().validateFileCache();
1633: filelist.revalidate();
1634: }
1635:
1636:
1642: public void ensureFileIsVisible(JFileChooser fc, File f)
1643: {
1644:
1645: }
1646:
1647:
1652: public JFileChooser getFileChooser()
1653: {
1654: return filechooser;
1655: }
1656:
1657:
1662: public JPanel getAccessoryPanel()
1663: {
1664: return accessoryPanel;
1665: }
1666:
1667:
1674: public JButton getApproveButton(JFileChooser fc)
1675: {
1676: accept = new JButton(getApproveButtonText(fc));
1677: accept.setMnemonic(getApproveButtonMnemonic(fc));
1678: accept.setToolTipText(getApproveButtonToolTipText(fc));
1679: return accept;
1680: }
1681:
1682:
1689: public String getApproveButtonToolTipText(JFileChooser fc)
1690: {
1691: if (fc.getApproveButtonToolTipText() != null)
1692: return fc.getApproveButtonToolTipText();
1693: else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1694: return saveButtonToolTipText;
1695: else
1696: return openButtonToolTipText;
1697: }
1698:
1699:
1702: public void clearIconCache()
1703: {
1704: if (fv instanceof BasicFileView)
1705: ((BasicFileView) fv).clearIconCache();
1706: }
1707:
1708:
1715: public ListSelectionListener createListSelectionListener(JFileChooser fc)
1716: {
1717: return new SelectionListener();
1718: }
1719:
1720:
1728: protected MouseListener createDoubleClickListener(JFileChooser fc, JList list)
1729: {
1730: return new DoubleClickListener(list);
1731: }
1732:
1733:
1738: protected boolean isDirectorySelected()
1739: {
1740: return dirSelected;
1741: }
1742:
1743:
1748: protected void setDirectorySelected(boolean selected)
1749: {
1750: dirSelected = selected;
1751: }
1752:
1753:
1758: protected File getDirectory()
1759: {
1760: return currDir;
1761: }
1762:
1763:
1768: protected void setDirectory(File f)
1769: {
1770: currDir = f;
1771: }
1772:
1773:
1780: public FileFilter getAcceptAllFileFilter(JFileChooser fc)
1781: {
1782: return acceptAll;
1783: }
1784:
1785:
1792: public FileView getFileView(JFileChooser fc)
1793: {
1794: if (fc.getFileView() != null)
1795: return fc.getFileView();
1796: return fv;
1797: }
1798:
1799:
1806: public String getDialogTitle(JFileChooser fc)
1807: {
1808: String ret = fc.getDialogTitle();
1809: if (ret != null)
1810: return ret;
1811: switch (fc.getDialogType())
1812: {
1813: case JFileChooser.OPEN_DIALOG:
1814: ret = openButtonText;
1815: break;
1816: case JFileChooser.SAVE_DIALOG:
1817: ret = saveButtonText;
1818: break;
1819: default:
1820: ret = fc.getApproveButtonText();
1821: break;
1822: }
1823: if (ret == null)
1824: ret = openButtonText;
1825: return ret;
1826: }
1827:
1828:
1835: public int getApproveButtonMnemonic(JFileChooser fc)
1836: {
1837: if (fc.getApproveButtonMnemonic() != 0)
1838: return fc.getApproveButtonMnemonic();
1839: else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1840: return saveButtonMnemonic;
1841: else
1842: return openButtonMnemonic;
1843: }
1844:
1845:
1852: public String getApproveButtonText(JFileChooser fc)
1853: {
1854: if (fc.getApproveButtonText() != null)
1855: return fc.getApproveButtonText();
1856: else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG)
1857: return saveButtonText;
1858: else
1859: return openButtonText;
1860: }
1861:
1862:
1867: public Action getNewFolderAction()
1868: {
1869: return new NewFolderAction();
1870: }
1871:
1872:
1877: public Action getGoHomeAction()
1878: {
1879: return new GoHomeAction();
1880: }
1881:
1882:
1887: public Action getChangeToParentDirectoryAction()
1888: {
1889: return new ChangeToParentDirectoryAction();
1890: }
1891:
1892:
1897: public Action getApproveSelectionAction()
1898: {
1899: return new ApproveSelectionAction();
1900: }
1901:
1902:
1907: public Action getCancelSelectionAction()
1908: {
1909: return new CancelSelectionAction();
1910: }
1911:
1912:
1917: public Action getUpdateAction()
1918: {
1919: return new UpdateAction();
1920: }
1921: }