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