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