532 if (readOnly) {
533 rfa.setEnabled(false);
534 }
535 renameFileButton.setText(renameFileButtonText);
536 renameFileButton.setName("GTKFileChooser.renameFileButton");
537 renameFileButton.setMnemonic(renameFileButtonMnemonic);
538 renameFileButton.setToolTipText(renameFileButtonToolTipText);
539 topButtonPanel.add(renameFileButton);
540
541 fc.add(topButtonPanel, BorderLayout.NORTH);
542
543
544 JPanel interior = new JPanel();
545 interior.setBorder(new EmptyBorder(0, 10, 10, 10));
546 interior.setName("GTKFileChooser.interiorPanel");
547 align(interior);
548 interior.setLayout(new BoxLayout(interior, BoxLayout.PAGE_AXIS));
549
550 fc.add(interior, BorderLayout.CENTER);
551
552 JPanel comboBoxPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,
553 0, 0) {
554 public void layoutContainer(Container target) {
555 super.layoutContainer(target);
556 JComboBox comboBox = directoryComboBox;
557 if (comboBox.getWidth() > target.getWidth()) {
558 comboBox.setBounds(0, comboBox.getY(), target.getWidth(),
559 comboBox.getHeight());
560 }
561 }
562 });
563 comboBoxPanel.setBorder(new EmptyBorder(0, 0, 4, 0));
564 comboBoxPanel.setName("GTKFileChooser.directoryComboBoxPanel");
565 // CurrentDir ComboBox
566 directoryComboBoxModel = createDirectoryComboBoxModel(fc);
567 directoryComboBox = new JComboBox(directoryComboBoxModel);
568 directoryComboBox.setName("GTKFileChooser.directoryComboBox");
569 directoryComboBox.putClientProperty( "JComboBox.lightweightKeyboardNavigation", "Lightweight" );
570 directoryComboBox.addActionListener(directoryComboBoxAction);
571 directoryComboBox.setMaximumRowCount(8);
643 centerPanel.add(accessoryPanel, BorderLayout.AFTER_LINE_ENDS);
644 }
645 interior.add(centerPanel);
646 interior.add(Box.createRigidArea(vstrut10));
647
648 JPanel pathFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEADING,
649 0, 0));
650 pathFieldPanel.setBorder(new EmptyBorder(0, 0, 4, 0));
651 JLabel pathFieldLabel = new JLabel(pathLabelText);
652 pathFieldLabel.setName("GTKFileChooser.pathFieldLabel");
653 pathFieldLabel.setDisplayedMnemonic(pathLabelMnemonic);
654 align(pathFieldLabel);
655 pathFieldPanel.add(pathFieldLabel);
656 pathFieldPanel.add(Box.createRigidArea(hstrut3));
657
658 File currentDirectory = fc.getCurrentDirectory();
659 String curDirName = null;
660 if (currentDirectory != null) {
661 curDirName = currentDirectory.getPath();
662 }
663 pathField = new JLabel(curDirName) {
664 public Dimension getMaximumSize() {
665 Dimension d = super.getMaximumSize();
666 d.height = getPreferredSize().height;
667 return d;
668 }
669 };
670 pathField.setName("GTKFileChooser.pathField");
671 align(pathField);
672 pathFieldPanel.add(pathField);
673 interior.add(pathFieldPanel);
674
675 // add the fileName field
676 fileNameTextField = new JTextField() {
677 public Dimension getMaximumSize() {
678 Dimension d = super.getMaximumSize();
679 d.height = getPreferredSize().height;
680 return d;
681 }
682 };
683
684 pathFieldLabel.setLabelFor(fileNameTextField);
685
686 Set<AWTKeyStroke> forwardTraversalKeys = fileNameTextField.getFocusTraversalKeys(
687 KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
688 forwardTraversalKeys = new HashSet<AWTKeyStroke>(forwardTraversalKeys);
689 forwardTraversalKeys.remove(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
690 fileNameTextField.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, forwardTraversalKeys);
691
692 fileNameTextField.setName("GTKFileChooser.fileNameTextField");
693 fileNameTextField.getActionMap().put("fileNameCompletionAction", getFileNameCompletionAction());
694 fileNameTextField.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "fileNameCompletionAction");
695 interior.add(fileNameTextField);
696
697 // Add the filter combo box
698 JPanel panel = new JPanel();
699 panel.setLayout(new FlowLayout(FlowLayout.LEADING, 0, 0));
700 panel.setBorder(new EmptyBorder(0, 0, 4, 0));
701 JLabel filterLabel = new JLabel(filterLabelText);
702 filterLabel.setName("GTKFileChooser.filterLabel");
888 scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
889 scrollpane.setName("GTKFileChooser.directoryListScrollPane");
890 scrollpane.setMaximumSize(MAX_SIZE);
891 scrollpane.setPreferredSize(prefListSize);
892 align(scrollpane);
893 return scrollpane;
894 }
895
896 protected void createModel() {
897 model = new GTKDirectoryModel();
898 }
899
900 public BasicDirectoryModel getModel() {
901 return model;
902 }
903
904 public Action getApproveSelectionAction() {
905 return approveSelectionAction;
906 }
907
908 private class GTKDirectoryModel extends BasicDirectoryModel {
909 FileSystemView fsv;
910 private Comparator<File> fileComparator = new Comparator<File>() {
911 public int compare(File o, File o1) {
912 return fsv.getSystemDisplayName(o).compareTo(fsv.getSystemDisplayName(o1));
913 }
914 };
915
916 public GTKDirectoryModel() {
917 super(getFileChooser());
918 }
919
920 protected void sort(Vector<? extends File> v) {
921 fsv = getFileChooser().getFileSystemView();
922 Collections.sort(v, fileComparator);
923 }
924 }
925
926 protected class GTKDirectoryListModel extends AbstractListModel implements ListDataListener {
927 File curDir;
928 public GTKDirectoryListModel() {
929 getModel().addListDataListener(this);
930 directoryChanged();
931 }
932
933 public int getSize() {
934 return getModel().getDirectories().size() + 1;
935 }
936
937 public Object getElementAt(int index) {
938 return index > 0 ? getModel().getDirectories().elementAt(index - 1):
939 curDir;
940 }
941
942 public void intervalAdded(ListDataEvent e) {
943 fireIntervalAdded(this, e.getIndex0(), e.getIndex1());
944 }
945
949
950 // PENDING - this is inefficient - should sent out
951 // incremental adjustment values instead of saying that the
952 // whole list has changed.
953 public void fireContentsChanged() {
954 fireContentsChanged(this, 0, getModel().getDirectories().size()-1);
955 }
956
957 // PENDING - fire the correct interval changed - currently sending
958 // out that everything has changed
959 public void contentsChanged(ListDataEvent e) {
960 fireContentsChanged();
961 }
962
963 private void directoryChanged() {
964 curDir = getFileChooser().getFileSystemView().createFileObject(
965 getFileChooser().getCurrentDirectory(), ".");
966 }
967 }
968
969 protected class GTKFileListModel extends AbstractListModel implements ListDataListener {
970 public GTKFileListModel() {
971 getModel().addListDataListener(this);
972 }
973
974 public int getSize() {
975 return getModel().getFiles().size();
976 }
977
978 public boolean contains(Object o) {
979 return getModel().getFiles().contains(o);
980 }
981
982 public int indexOf(Object o) {
983 return getModel().getFiles().indexOf(o);
984 }
985
986 public Object getElementAt(int index) {
987 return getModel().getFiles().elementAt(index);
988 }
992 }
993
994 public void intervalRemoved(ListDataEvent e) {
995 fireIntervalRemoved(this, e.getIndex0(), e.getIndex1());
996 }
997
998 // PENDING - this is inefficient - should sent out
999 // incremental adjustment values instead of saying that the
1000 // whole list has changed.
1001 public void fireContentsChanged() {
1002 fireContentsChanged(this, 0, getModel().getFiles().size()-1);
1003 }
1004
1005 // PENDING - fire the interval changed
1006 public void contentsChanged(ListDataEvent e) {
1007 fireContentsChanged();
1008 }
1009 }
1010
1011
1012 protected class FileCellRenderer extends DefaultListCellRenderer {
1013 public Component getListCellRendererComponent(JList list, Object value, int index,
1014 boolean isSelected, boolean cellHasFocus) {
1015
1016 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
1017 setText(getFileChooser().getName((File) value));
1018 if (showFileIcons) {
1019 setIcon(getFileChooser().getIcon((File)value));
1020 }
1021 return this;
1022 }
1023 }
1024
1025 protected class DirectoryCellRenderer extends DefaultListCellRenderer {
1026 public Component getListCellRendererComponent(JList list, Object value, int index,
1027 boolean isSelected, boolean cellHasFocus) {
1028
1029 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
1030
1031 if (showDirectoryIcons) {
1032 setIcon(getFileChooser().getIcon((File)value));
1033 setText(getFileChooser().getName((File)value));
1034 } else {
1035 setText(getFileChooser().getName((File)value) + "/");
1036 }
1037 return this;
1038 }
1039 }
1040
1041 public Dimension getPreferredSize(JComponent c) {
1042 Dimension prefSize = new Dimension(PREF_SIZE);
1043 JComponent accessory = getFileChooser().getAccessory();
1044 if (accessory != null) {
1067 }
1068
1069 public Action getNewFolderAction() {
1070 if (newFolderAction == null) {
1071 newFolderAction = new NewFolderAction();
1072 newFolderAction.setEnabled(!readOnly);
1073 }
1074 return newFolderAction;
1075 }
1076
1077 //
1078 // DataModel for DirectoryComboxbox
1079 //
1080 protected DirectoryComboBoxModel createDirectoryComboBoxModel(JFileChooser fc) {
1081 return new DirectoryComboBoxModel();
1082 }
1083
1084 /**
1085 * Data model for a type-face selection combo-box.
1086 */
1087 protected class DirectoryComboBoxModel extends AbstractListModel implements ComboBoxModel {
1088 Vector<File> directories = new Vector<File>();
1089 File selectedDirectory = null;
1090 JFileChooser chooser = getFileChooser();
1091 FileSystemView fsv = chooser.getFileSystemView();
1092
1093 public DirectoryComboBoxModel() {
1094 // Add the current directory to the model, and make it the
1095 // selectedDirectory
1096 File dir = getFileChooser().getCurrentDirectory();
1097 if (dir != null) {
1098 addItem(dir);
1099 }
1100 }
1101
1102 /**
1103 * Adds the directory to the model and sets it to be selected,
1104 * additionally clears out the previous selected directory and
1105 * the paths leading up to it, if any.
1106 */
1143 this.selectedDirectory = (File)selectedDirectory;
1144 fireContentsChanged(this, -1, -1);
1145 }
1146
1147 public Object getSelectedItem() {
1148 return selectedDirectory;
1149 }
1150
1151 public int getSize() {
1152 return directories.size();
1153 }
1154
1155 public Object getElementAt(int index) {
1156 return directories.elementAt(index);
1157 }
1158 }
1159
1160 /**
1161 * Acts when DirectoryComboBox has changed the selected item.
1162 */
1163 protected class DirectoryComboBoxAction extends AbstractAction {
1164 protected DirectoryComboBoxAction() {
1165 super("DirectoryComboBoxAction");
1166 }
1167
1168 public void actionPerformed(ActionEvent e) {
1169 File f = (File)directoryComboBox.getSelectedItem();
1170 getFileChooser().setCurrentDirectory(f);
1171 }
1172 }
1173
1174 /**
1175 * Creates a new folder.
1176 */
1177 private class NewFolderAction extends AbstractAction {
1178 protected NewFolderAction() {
1179 super(FilePane.ACTION_NEW_FOLDER);
1180 }
1181 public void actionPerformed(ActionEvent e) {
1182 if (readOnly) {
1183 return;
1184 }
1185 JFileChooser fc = getFileChooser();
1186 File currentDirectory = fc.getCurrentDirectory();
1187 String dirName = JOptionPane.showInputDialog(fc,
1188 newFolderDialogText, newFolderButtonText,
1189 JOptionPane.PLAIN_MESSAGE);
1190
1191 if (dirName != null) {
1192 if (!currentDirectory.exists()) {
1193 JOptionPane.showMessageDialog(fc,
1194 MessageFormat.format(newFolderNoDirectoryErrorText, dirName),
1195 newFolderNoDirectoryErrorTitleText, JOptionPane.ERROR_MESSAGE);
1196 return;
1197 }
1198
1199 File newDir = fc.getFileSystemView().createFileObject
1200 (currentDirectory, dirName);
1201 if (newDir == null || !newDir.mkdir()) {
1202 JOptionPane.showMessageDialog(fc,
1203 newFolderErrorText + newFolderErrorSeparator + " \"" +
1204 dirName + "\"",
1205 newFolderErrorText, JOptionPane.ERROR_MESSAGE);
1206 }
1207 fc.rescanCurrentDirectory();
1208 }
1209 }
1210 }
1211
1212 private class GTKApproveSelectionAction extends ApproveSelectionAction {
1213 public void actionPerformed(ActionEvent e) {
1214 if (isDirectorySelected()) {
1215 File dir = getDirectory();
1216 try {
1217 // Strip trailing ".."
1218 if (dir != null) {
1219 dir = ShellFolder.getNormalizedFile(dir);
1220 }
1221 } catch (IOException ex) {
1222 // Ok, use f as is
1223 }
1224 if (getFileChooser().getCurrentDirectory().equals(dir)) {
1225 directoryList.clearSelection();
1226 fileList.clearSelection();
1227 ListSelectionModel sm = fileList.getSelectionModel();
1228 if (sm instanceof DefaultListSelectionModel) {
1229 ((DefaultListSelectionModel)sm).moveLeadSelectionIndex(0);
1230 sm.setAnchorSelectionIndex(0);
1231 }
1232 rescanCurrentDirectory(getFileChooser());
1233 return;
1234 }
1235 }
1236 super.actionPerformed(e);
1237 }
1238 }
1239
1240 /**
1241 * Renames file
1242 */
1243 private class RenameFileAction extends AbstractAction {
1244 protected RenameFileAction() {
1245 super(FilePane.ACTION_EDIT_FILE_NAME);
1246 }
1247 public void actionPerformed(ActionEvent e) {
1248 if (getFileName().equals("")) {
1249 return;
1250 }
1251 JFileChooser fc = getFileChooser();
1252 File currentDirectory = fc.getCurrentDirectory();
1253 String newFileName = (String) JOptionPane.showInputDialog
1254 (fc, new MessageFormat(renameFileDialogText).format
1255 (new Object[] { getFileName() }),
1256 renameFileButtonText, JOptionPane.PLAIN_MESSAGE, null, null,
1257 getFileName());
1258
1259 if (newFileName != null) {
1260 File oldFile = fc.getFileSystemView().createFileObject
1261 (currentDirectory, getFileName());
1262 File newFile = fc.getFileSystemView().createFileObject
1268 format(new Object[] { getFileName(), newFileName}),
1269 renameFileErrorTitle, JOptionPane.ERROR_MESSAGE);
1270 } else {
1271 setFileName(getFileChooser().getName(newFile));
1272 fc.rescanCurrentDirectory();
1273 }
1274 }
1275 }
1276 }
1277
1278 //
1279 // Renderer for Filter ComboBox
1280 //
1281 protected FilterComboBoxRenderer createFilterComboBoxRenderer() {
1282 return new FilterComboBoxRenderer();
1283 }
1284
1285 /**
1286 * Render different filters
1287 */
1288 public class FilterComboBoxRenderer extends DefaultListCellRenderer implements UIResource {
1289 public String getName() {
1290 // As SynthComboBoxRenderer's are asked for a size BEFORE they
1291 // are parented getName is overriden to force the name to be
1292 // ComboBox.renderer if it isn't set. If we didn't do this the
1293 // wrong style could be used for size calculations.
1294 String name = super.getName();
1295 if (name == null) {
1296 return "ComboBox.renderer";
1297 }
1298 return name;
1299 }
1300
1301 public Component getListCellRendererComponent(JList list, Object value,
1302 int index, boolean isSelected,
1303 boolean cellHasFocus) {
1304
1305 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
1306
1307 setName("ComboBox.listRenderer");
1311 setText(((FileFilter) value).getDescription());
1312 }
1313 } else {
1314 setText("");
1315 }
1316
1317 return this;
1318 }
1319 }
1320
1321 //
1322 // DataModel for Filter Combobox
1323 //
1324 protected FilterComboBoxModel createFilterComboBoxModel() {
1325 return new FilterComboBoxModel();
1326 }
1327
1328 /**
1329 * Data model for filter combo-box.
1330 */
1331 protected class FilterComboBoxModel extends AbstractListModel
1332 implements ComboBoxModel, PropertyChangeListener {
1333 protected FileFilter[] filters;
1334
1335 protected FilterComboBoxModel() {
1336 super();
1337 filters = getFileChooser().getChoosableFileFilters();
1338 }
1339
1340 public void propertyChange(PropertyChangeEvent e) {
1341 String prop = e.getPropertyName();
1342 if (prop == JFileChooser.CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY) {
1343 filters = (FileFilter[]) e.getNewValue();
1344 fireContentsChanged(this, -1, -1);
1345 } else if (prop == JFileChooser.FILE_FILTER_CHANGED_PROPERTY) {
1346 fireContentsChanged(this, -1, -1);
1347 }
1348 }
1349
1350 public void setSelectedItem(Object filter) {
|
532 if (readOnly) {
533 rfa.setEnabled(false);
534 }
535 renameFileButton.setText(renameFileButtonText);
536 renameFileButton.setName("GTKFileChooser.renameFileButton");
537 renameFileButton.setMnemonic(renameFileButtonMnemonic);
538 renameFileButton.setToolTipText(renameFileButtonToolTipText);
539 topButtonPanel.add(renameFileButton);
540
541 fc.add(topButtonPanel, BorderLayout.NORTH);
542
543
544 JPanel interior = new JPanel();
545 interior.setBorder(new EmptyBorder(0, 10, 10, 10));
546 interior.setName("GTKFileChooser.interiorPanel");
547 align(interior);
548 interior.setLayout(new BoxLayout(interior, BoxLayout.PAGE_AXIS));
549
550 fc.add(interior, BorderLayout.CENTER);
551
552 @SuppressWarnings("serial") // anonymous class
553 JPanel comboBoxPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,
554 0, 0) {
555 public void layoutContainer(Container target) {
556 super.layoutContainer(target);
557 JComboBox comboBox = directoryComboBox;
558 if (comboBox.getWidth() > target.getWidth()) {
559 comboBox.setBounds(0, comboBox.getY(), target.getWidth(),
560 comboBox.getHeight());
561 }
562 }
563 });
564 comboBoxPanel.setBorder(new EmptyBorder(0, 0, 4, 0));
565 comboBoxPanel.setName("GTKFileChooser.directoryComboBoxPanel");
566 // CurrentDir ComboBox
567 directoryComboBoxModel = createDirectoryComboBoxModel(fc);
568 directoryComboBox = new JComboBox(directoryComboBoxModel);
569 directoryComboBox.setName("GTKFileChooser.directoryComboBox");
570 directoryComboBox.putClientProperty( "JComboBox.lightweightKeyboardNavigation", "Lightweight" );
571 directoryComboBox.addActionListener(directoryComboBoxAction);
572 directoryComboBox.setMaximumRowCount(8);
644 centerPanel.add(accessoryPanel, BorderLayout.AFTER_LINE_ENDS);
645 }
646 interior.add(centerPanel);
647 interior.add(Box.createRigidArea(vstrut10));
648
649 JPanel pathFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEADING,
650 0, 0));
651 pathFieldPanel.setBorder(new EmptyBorder(0, 0, 4, 0));
652 JLabel pathFieldLabel = new JLabel(pathLabelText);
653 pathFieldLabel.setName("GTKFileChooser.pathFieldLabel");
654 pathFieldLabel.setDisplayedMnemonic(pathLabelMnemonic);
655 align(pathFieldLabel);
656 pathFieldPanel.add(pathFieldLabel);
657 pathFieldPanel.add(Box.createRigidArea(hstrut3));
658
659 File currentDirectory = fc.getCurrentDirectory();
660 String curDirName = null;
661 if (currentDirectory != null) {
662 curDirName = currentDirectory.getPath();
663 }
664 @SuppressWarnings("serial") // anonymous class
665 JLabel tmp = new JLabel(curDirName) {
666 public Dimension getMaximumSize() {
667 Dimension d = super.getMaximumSize();
668 d.height = getPreferredSize().height;
669 return d;
670 }
671 };
672 pathField = tmp;
673 pathField.setName("GTKFileChooser.pathField");
674 align(pathField);
675 pathFieldPanel.add(pathField);
676 interior.add(pathFieldPanel);
677
678 // add the fileName field
679 @SuppressWarnings("serial") // anonymous class
680 JTextField tmp2 = new JTextField() {
681 public Dimension getMaximumSize() {
682 Dimension d = super.getMaximumSize();
683 d.height = getPreferredSize().height;
684 return d;
685 }
686 };
687 fileNameTextField = tmp2;
688
689 pathFieldLabel.setLabelFor(fileNameTextField);
690
691 Set<AWTKeyStroke> forwardTraversalKeys = fileNameTextField.getFocusTraversalKeys(
692 KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
693 forwardTraversalKeys = new HashSet<AWTKeyStroke>(forwardTraversalKeys);
694 forwardTraversalKeys.remove(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
695 fileNameTextField.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, forwardTraversalKeys);
696
697 fileNameTextField.setName("GTKFileChooser.fileNameTextField");
698 fileNameTextField.getActionMap().put("fileNameCompletionAction", getFileNameCompletionAction());
699 fileNameTextField.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "fileNameCompletionAction");
700 interior.add(fileNameTextField);
701
702 // Add the filter combo box
703 JPanel panel = new JPanel();
704 panel.setLayout(new FlowLayout(FlowLayout.LEADING, 0, 0));
705 panel.setBorder(new EmptyBorder(0, 0, 4, 0));
706 JLabel filterLabel = new JLabel(filterLabelText);
707 filterLabel.setName("GTKFileChooser.filterLabel");
893 scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
894 scrollpane.setName("GTKFileChooser.directoryListScrollPane");
895 scrollpane.setMaximumSize(MAX_SIZE);
896 scrollpane.setPreferredSize(prefListSize);
897 align(scrollpane);
898 return scrollpane;
899 }
900
901 protected void createModel() {
902 model = new GTKDirectoryModel();
903 }
904
905 public BasicDirectoryModel getModel() {
906 return model;
907 }
908
909 public Action getApproveSelectionAction() {
910 return approveSelectionAction;
911 }
912
913 @SuppressWarnings("serial") // Superclass is not serializable across versions
914 private class GTKDirectoryModel extends BasicDirectoryModel {
915 FileSystemView fsv;
916 private Comparator<File> fileComparator = new Comparator<File>() {
917 public int compare(File o, File o1) {
918 return fsv.getSystemDisplayName(o).compareTo(fsv.getSystemDisplayName(o1));
919 }
920 };
921
922 public GTKDirectoryModel() {
923 super(getFileChooser());
924 }
925
926 protected void sort(Vector<? extends File> v) {
927 fsv = getFileChooser().getFileSystemView();
928 Collections.sort(v, fileComparator);
929 }
930 }
931
932 @SuppressWarnings("serial") // Superclass is not serializable across versions
933 protected class GTKDirectoryListModel extends AbstractListModel implements ListDataListener {
934 File curDir;
935 public GTKDirectoryListModel() {
936 getModel().addListDataListener(this);
937 directoryChanged();
938 }
939
940 public int getSize() {
941 return getModel().getDirectories().size() + 1;
942 }
943
944 public Object getElementAt(int index) {
945 return index > 0 ? getModel().getDirectories().elementAt(index - 1):
946 curDir;
947 }
948
949 public void intervalAdded(ListDataEvent e) {
950 fireIntervalAdded(this, e.getIndex0(), e.getIndex1());
951 }
952
956
957 // PENDING - this is inefficient - should sent out
958 // incremental adjustment values instead of saying that the
959 // whole list has changed.
960 public void fireContentsChanged() {
961 fireContentsChanged(this, 0, getModel().getDirectories().size()-1);
962 }
963
964 // PENDING - fire the correct interval changed - currently sending
965 // out that everything has changed
966 public void contentsChanged(ListDataEvent e) {
967 fireContentsChanged();
968 }
969
970 private void directoryChanged() {
971 curDir = getFileChooser().getFileSystemView().createFileObject(
972 getFileChooser().getCurrentDirectory(), ".");
973 }
974 }
975
976 @SuppressWarnings("serial") // Superclass is not serializable across versions
977 protected class GTKFileListModel extends AbstractListModel implements ListDataListener {
978 public GTKFileListModel() {
979 getModel().addListDataListener(this);
980 }
981
982 public int getSize() {
983 return getModel().getFiles().size();
984 }
985
986 public boolean contains(Object o) {
987 return getModel().getFiles().contains(o);
988 }
989
990 public int indexOf(Object o) {
991 return getModel().getFiles().indexOf(o);
992 }
993
994 public Object getElementAt(int index) {
995 return getModel().getFiles().elementAt(index);
996 }
1000 }
1001
1002 public void intervalRemoved(ListDataEvent e) {
1003 fireIntervalRemoved(this, e.getIndex0(), e.getIndex1());
1004 }
1005
1006 // PENDING - this is inefficient - should sent out
1007 // incremental adjustment values instead of saying that the
1008 // whole list has changed.
1009 public void fireContentsChanged() {
1010 fireContentsChanged(this, 0, getModel().getFiles().size()-1);
1011 }
1012
1013 // PENDING - fire the interval changed
1014 public void contentsChanged(ListDataEvent e) {
1015 fireContentsChanged();
1016 }
1017 }
1018
1019
1020 @SuppressWarnings("serial") // Superclass is not serializable across versions
1021 protected class FileCellRenderer extends DefaultListCellRenderer {
1022 public Component getListCellRendererComponent(JList list, Object value, int index,
1023 boolean isSelected, boolean cellHasFocus) {
1024
1025 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
1026 setText(getFileChooser().getName((File) value));
1027 if (showFileIcons) {
1028 setIcon(getFileChooser().getIcon((File)value));
1029 }
1030 return this;
1031 }
1032 }
1033
1034 @SuppressWarnings("serial") // Superclass is not serializable across versions
1035 protected class DirectoryCellRenderer extends DefaultListCellRenderer {
1036 public Component getListCellRendererComponent(JList list, Object value, int index,
1037 boolean isSelected, boolean cellHasFocus) {
1038
1039 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
1040
1041 if (showDirectoryIcons) {
1042 setIcon(getFileChooser().getIcon((File)value));
1043 setText(getFileChooser().getName((File)value));
1044 } else {
1045 setText(getFileChooser().getName((File)value) + "/");
1046 }
1047 return this;
1048 }
1049 }
1050
1051 public Dimension getPreferredSize(JComponent c) {
1052 Dimension prefSize = new Dimension(PREF_SIZE);
1053 JComponent accessory = getFileChooser().getAccessory();
1054 if (accessory != null) {
1077 }
1078
1079 public Action getNewFolderAction() {
1080 if (newFolderAction == null) {
1081 newFolderAction = new NewFolderAction();
1082 newFolderAction.setEnabled(!readOnly);
1083 }
1084 return newFolderAction;
1085 }
1086
1087 //
1088 // DataModel for DirectoryComboxbox
1089 //
1090 protected DirectoryComboBoxModel createDirectoryComboBoxModel(JFileChooser fc) {
1091 return new DirectoryComboBoxModel();
1092 }
1093
1094 /**
1095 * Data model for a type-face selection combo-box.
1096 */
1097 @SuppressWarnings("serial") // Superclass is not serializable across versions
1098 protected class DirectoryComboBoxModel extends AbstractListModel implements ComboBoxModel {
1099 Vector<File> directories = new Vector<File>();
1100 File selectedDirectory = null;
1101 JFileChooser chooser = getFileChooser();
1102 FileSystemView fsv = chooser.getFileSystemView();
1103
1104 public DirectoryComboBoxModel() {
1105 // Add the current directory to the model, and make it the
1106 // selectedDirectory
1107 File dir = getFileChooser().getCurrentDirectory();
1108 if (dir != null) {
1109 addItem(dir);
1110 }
1111 }
1112
1113 /**
1114 * Adds the directory to the model and sets it to be selected,
1115 * additionally clears out the previous selected directory and
1116 * the paths leading up to it, if any.
1117 */
1154 this.selectedDirectory = (File)selectedDirectory;
1155 fireContentsChanged(this, -1, -1);
1156 }
1157
1158 public Object getSelectedItem() {
1159 return selectedDirectory;
1160 }
1161
1162 public int getSize() {
1163 return directories.size();
1164 }
1165
1166 public Object getElementAt(int index) {
1167 return directories.elementAt(index);
1168 }
1169 }
1170
1171 /**
1172 * Acts when DirectoryComboBox has changed the selected item.
1173 */
1174 @SuppressWarnings("serial") // Superclass is not serializable across versions
1175 protected class DirectoryComboBoxAction extends AbstractAction {
1176 protected DirectoryComboBoxAction() {
1177 super("DirectoryComboBoxAction");
1178 }
1179
1180 public void actionPerformed(ActionEvent e) {
1181 File f = (File)directoryComboBox.getSelectedItem();
1182 getFileChooser().setCurrentDirectory(f);
1183 }
1184 }
1185
1186 /**
1187 * Creates a new folder.
1188 */
1189 @SuppressWarnings("serial") // Superclass is not serializable across versions
1190 private class NewFolderAction extends AbstractAction {
1191 protected NewFolderAction() {
1192 super(FilePane.ACTION_NEW_FOLDER);
1193 }
1194 public void actionPerformed(ActionEvent e) {
1195 if (readOnly) {
1196 return;
1197 }
1198 JFileChooser fc = getFileChooser();
1199 File currentDirectory = fc.getCurrentDirectory();
1200 String dirName = JOptionPane.showInputDialog(fc,
1201 newFolderDialogText, newFolderButtonText,
1202 JOptionPane.PLAIN_MESSAGE);
1203
1204 if (dirName != null) {
1205 if (!currentDirectory.exists()) {
1206 JOptionPane.showMessageDialog(fc,
1207 MessageFormat.format(newFolderNoDirectoryErrorText, dirName),
1208 newFolderNoDirectoryErrorTitleText, JOptionPane.ERROR_MESSAGE);
1209 return;
1210 }
1211
1212 File newDir = fc.getFileSystemView().createFileObject
1213 (currentDirectory, dirName);
1214 if (newDir == null || !newDir.mkdir()) {
1215 JOptionPane.showMessageDialog(fc,
1216 newFolderErrorText + newFolderErrorSeparator + " \"" +
1217 dirName + "\"",
1218 newFolderErrorText, JOptionPane.ERROR_MESSAGE);
1219 }
1220 fc.rescanCurrentDirectory();
1221 }
1222 }
1223 }
1224
1225 @SuppressWarnings("serial") // Superclass is not serializable across versions
1226 private class GTKApproveSelectionAction extends ApproveSelectionAction {
1227 public void actionPerformed(ActionEvent e) {
1228 if (isDirectorySelected()) {
1229 File dir = getDirectory();
1230 try {
1231 // Strip trailing ".."
1232 if (dir != null) {
1233 dir = ShellFolder.getNormalizedFile(dir);
1234 }
1235 } catch (IOException ex) {
1236 // Ok, use f as is
1237 }
1238 if (getFileChooser().getCurrentDirectory().equals(dir)) {
1239 directoryList.clearSelection();
1240 fileList.clearSelection();
1241 ListSelectionModel sm = fileList.getSelectionModel();
1242 if (sm instanceof DefaultListSelectionModel) {
1243 ((DefaultListSelectionModel)sm).moveLeadSelectionIndex(0);
1244 sm.setAnchorSelectionIndex(0);
1245 }
1246 rescanCurrentDirectory(getFileChooser());
1247 return;
1248 }
1249 }
1250 super.actionPerformed(e);
1251 }
1252 }
1253
1254 /**
1255 * Renames file
1256 */
1257 @SuppressWarnings("serial") // Superclass is not serializable across versions
1258 private class RenameFileAction extends AbstractAction {
1259 protected RenameFileAction() {
1260 super(FilePane.ACTION_EDIT_FILE_NAME);
1261 }
1262 public void actionPerformed(ActionEvent e) {
1263 if (getFileName().equals("")) {
1264 return;
1265 }
1266 JFileChooser fc = getFileChooser();
1267 File currentDirectory = fc.getCurrentDirectory();
1268 String newFileName = (String) JOptionPane.showInputDialog
1269 (fc, new MessageFormat(renameFileDialogText).format
1270 (new Object[] { getFileName() }),
1271 renameFileButtonText, JOptionPane.PLAIN_MESSAGE, null, null,
1272 getFileName());
1273
1274 if (newFileName != null) {
1275 File oldFile = fc.getFileSystemView().createFileObject
1276 (currentDirectory, getFileName());
1277 File newFile = fc.getFileSystemView().createFileObject
1283 format(new Object[] { getFileName(), newFileName}),
1284 renameFileErrorTitle, JOptionPane.ERROR_MESSAGE);
1285 } else {
1286 setFileName(getFileChooser().getName(newFile));
1287 fc.rescanCurrentDirectory();
1288 }
1289 }
1290 }
1291 }
1292
1293 //
1294 // Renderer for Filter ComboBox
1295 //
1296 protected FilterComboBoxRenderer createFilterComboBoxRenderer() {
1297 return new FilterComboBoxRenderer();
1298 }
1299
1300 /**
1301 * Render different filters
1302 */
1303 @SuppressWarnings("serial") // Superclass is not serializable across versions
1304 public class FilterComboBoxRenderer extends DefaultListCellRenderer implements UIResource {
1305 public String getName() {
1306 // As SynthComboBoxRenderer's are asked for a size BEFORE they
1307 // are parented getName is overriden to force the name to be
1308 // ComboBox.renderer if it isn't set. If we didn't do this the
1309 // wrong style could be used for size calculations.
1310 String name = super.getName();
1311 if (name == null) {
1312 return "ComboBox.renderer";
1313 }
1314 return name;
1315 }
1316
1317 public Component getListCellRendererComponent(JList list, Object value,
1318 int index, boolean isSelected,
1319 boolean cellHasFocus) {
1320
1321 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
1322
1323 setName("ComboBox.listRenderer");
1327 setText(((FileFilter) value).getDescription());
1328 }
1329 } else {
1330 setText("");
1331 }
1332
1333 return this;
1334 }
1335 }
1336
1337 //
1338 // DataModel for Filter Combobox
1339 //
1340 protected FilterComboBoxModel createFilterComboBoxModel() {
1341 return new FilterComboBoxModel();
1342 }
1343
1344 /**
1345 * Data model for filter combo-box.
1346 */
1347 @SuppressWarnings("serial") // JDK implementation class
1348 protected class FilterComboBoxModel extends AbstractListModel
1349 implements ComboBoxModel, PropertyChangeListener {
1350 protected FileFilter[] filters;
1351
1352 protected FilterComboBoxModel() {
1353 super();
1354 filters = getFileChooser().getChoosableFileFilters();
1355 }
1356
1357 public void propertyChange(PropertyChangeEvent e) {
1358 String prop = e.getPropertyName();
1359 if (prop == JFileChooser.CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY) {
1360 filters = (FileFilter[]) e.getNewValue();
1361 fireContentsChanged(this, -1, -1);
1362 } else if (prop == JFileChooser.FILE_FILTER_CHANGED_PROPERTY) {
1363 fireContentsChanged(this, -1, -1);
1364 }
1365 }
1366
1367 public void setSelectedItem(Object filter) {
|