1. /*
  2. * @(#)MotifFileChooserUI.java 1.42 04/03/11
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.java.swing.plaf.motif;
  8. import javax.swing.*;
  9. import javax.swing.filechooser.*;
  10. import javax.swing.event.*;
  11. import javax.swing.plaf.*;
  12. import javax.swing.plaf.basic.*;
  13. import java.awt.*;
  14. import java.awt.event.*;
  15. import java.beans.*;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.util.*;
  19. /**
  20. * Motif FileChooserUI.
  21. *
  22. * @version 1.42 03/11/04
  23. * @author Jeff Dinkins
  24. */
  25. public class MotifFileChooserUI extends BasicFileChooserUI {
  26. private FilterComboBoxModel filterComboBoxModel;
  27. protected JList directoryList = null;
  28. protected JList fileList = null;
  29. protected JTextField pathField = null;
  30. protected JComboBox filterComboBox = null;
  31. protected JTextField filenameTextField = null;
  32. private static final Dimension hstrut10 = new Dimension(10, 1);
  33. private static final Dimension vstrut10 = new Dimension(1, 10);
  34. private static final Insets insets = new Insets(10, 10, 10, 10);
  35. private static Dimension prefListSize = new Dimension(75, 150);
  36. private static Dimension WITH_ACCELERATOR_PREF_SIZE = new Dimension(650, 450);
  37. private static Dimension PREF_SIZE = new Dimension(350, 450);
  38. private static Dimension MIN_SIZE = new Dimension(200, 300);
  39. private static Dimension PREF_ACC_SIZE = new Dimension(10, 10);
  40. private static Dimension ZERO_ACC_SIZE = new Dimension(1, 1);
  41. private static Dimension MAX_SIZE = new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
  42. private static final Insets buttonMargin = new Insets(3, 3, 3, 3);
  43. private JPanel directoryPanel = new JPanel();
  44. private JPanel bottomPanel;
  45. protected JButton approveButton;
  46. private String enterFileNameLabelText = null;
  47. private int enterFileNameLabelMnemonic = 0;
  48. private String filesLabelText = null;
  49. private int filesLabelMnemonic = 0;
  50. private String foldersLabelText = null;
  51. private int foldersLabelMnemonic = 0;
  52. private String pathLabelText = null;
  53. private int pathLabelMnemonic = 0;
  54. private String filterLabelText = null;
  55. private int filterLabelMnemonic = 0;
  56. private String fileNameString(File file) {
  57. if (file == null) {
  58. return null;
  59. } else {
  60. JFileChooser fc = getFileChooser();
  61. if (fc.isDirectorySelectionEnabled() && !fc.isFileSelectionEnabled()) {
  62. return file.getPath();
  63. } else {
  64. return file.getName();
  65. }
  66. }
  67. }
  68. private String fileNameString(File[] files) {
  69. StringBuffer buf = new StringBuffer();
  70. for (int i = 0; files != null && i < files.length; i++) {
  71. if (i > 0) {
  72. buf.append(" ");
  73. }
  74. if (files.length > 1) {
  75. buf.append("\"");
  76. }
  77. buf.append(fileNameString(files[i]));
  78. if (files.length > 1) {
  79. buf.append("\"");
  80. }
  81. }
  82. return buf.toString();
  83. }
  84. public MotifFileChooserUI(JFileChooser filechooser) {
  85. super(filechooser);
  86. }
  87. public String getFileName() {
  88. if(filenameTextField != null) {
  89. return filenameTextField.getText();
  90. } else {
  91. return null;
  92. }
  93. }
  94. public void setFileName(String filename) {
  95. if(filenameTextField != null) {
  96. filenameTextField.setText(filename);
  97. }
  98. }
  99. public String getDirectoryName() {
  100. return pathField.getText();
  101. }
  102. public void setDirectoryName(String dirname) {
  103. pathField.setText(dirname);
  104. }
  105. public void ensureFileIsVisible(JFileChooser fc, File f) {
  106. // PENDING(jeff)
  107. }
  108. public void rescanCurrentDirectory(JFileChooser fc) {
  109. getModel().validateFileCache();
  110. }
  111. public PropertyChangeListener createPropertyChangeListener(JFileChooser fc) {
  112. return new PropertyChangeListener() {
  113. public void propertyChange(PropertyChangeEvent e) {
  114. String prop = e.getPropertyName();
  115. if(prop.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
  116. File f = (File) e.getNewValue();
  117. if(f != null) {
  118. setFileName(getFileChooser().getName(f));
  119. }
  120. }
  121. else if (prop.equals(JFileChooser.SELECTED_FILES_CHANGED_PROPERTY)) {
  122. File[] files = (File[]) e.getNewValue();
  123. JFileChooser fc = getFileChooser();
  124. if (files != null && files.length > 0 && (files.length > 1 || fc.isDirectorySelectionEnabled() || !files[0].isDirectory())) {
  125. setFileName(fileNameString(files));
  126. }
  127. } else if(prop.equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)) {
  128. directoryList.clearSelection();
  129. fileList.clearSelection();
  130. File currentDirectory = getFileChooser().getCurrentDirectory();
  131. if(currentDirectory != null) {
  132. try {
  133. setDirectoryName(((File)e.getNewValue()).getCanonicalPath());
  134. } catch (IOException ioe) {
  135. setDirectoryName(((File)e.getNewValue()).getAbsolutePath());
  136. }
  137. }
  138. } else if(prop.equals(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY)) {
  139. directoryList.clearSelection();
  140. } else if(prop == JFileChooser.MULTI_SELECTION_ENABLED_CHANGED_PROPERTY) {
  141. if(getFileChooser().isMultiSelectionEnabled()) {
  142. fileList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  143. } else {
  144. fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  145. fileList.clearSelection();
  146. getFileChooser().setSelectedFiles(null);
  147. }
  148. } else if(prop == JFileChooser.ACCESSORY_CHANGED_PROPERTY) {
  149. if(getAccessoryPanel() != null) {
  150. if(e.getOldValue() != null) {
  151. getAccessoryPanel().remove((JComponent) e.getOldValue());
  152. }
  153. JComponent accessory = (JComponent) e.getNewValue();
  154. if(accessory != null) {
  155. getAccessoryPanel().add(accessory, BorderLayout.CENTER);
  156. getAccessoryPanel().setPreferredSize(PREF_ACC_SIZE);
  157. getAccessoryPanel().setMaximumSize(MAX_SIZE);
  158. } else {
  159. getAccessoryPanel().setPreferredSize(ZERO_ACC_SIZE);
  160. getAccessoryPanel().setMaximumSize(ZERO_ACC_SIZE);
  161. }
  162. }
  163. } else if (prop == JFileChooser.APPROVE_BUTTON_TEXT_CHANGED_PROPERTY ||
  164. prop == JFileChooser.APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY ||
  165. prop == JFileChooser.DIALOG_TYPE_CHANGED_PROPERTY) {
  166. approveButton.setText(getApproveButtonText(getFileChooser()));
  167. approveButton.setToolTipText(getApproveButtonToolTipText(getFileChooser()));
  168. } else if (prop.equals(JFileChooser.CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY)) {
  169. doControlButtonsChanged(e);
  170. } else if (prop.equals("componentOrientation")) {
  171. ComponentOrientation o = (ComponentOrientation)e.getNewValue();
  172. JFileChooser cc = (JFileChooser)e.getSource();
  173. if (o != (ComponentOrientation)e.getOldValue()) {
  174. cc.applyComponentOrientation(o);
  175. }
  176. }
  177. }
  178. };
  179. }
  180. //
  181. // ComponentUI Interface Implementation methods
  182. //
  183. public static ComponentUI createUI(JComponent c) {
  184. return new MotifFileChooserUI((JFileChooser)c);
  185. }
  186. public void installUI(JComponent c) {
  187. super.installUI(c);
  188. }
  189. public void uninstallUI(JComponent c) {
  190. getFileChooser().removeAll();
  191. super.uninstallUI(c);
  192. }
  193. public void installComponents(JFileChooser fc) {
  194. fc.setLayout(new BorderLayout(10, 10));
  195. fc.setAlignmentX(JComponent.CENTER_ALIGNMENT);
  196. JPanel interior = new JPanel() {
  197. public Insets getInsets() {
  198. return insets;
  199. }
  200. };
  201. align(interior);
  202. interior.setLayout(new BoxLayout(interior, BoxLayout.PAGE_AXIS));
  203. fc.add(interior, BorderLayout.CENTER);
  204. // PENDING(jeff) - I18N
  205. JLabel l = new JLabel(pathLabelText);
  206. l.setDisplayedMnemonic(pathLabelMnemonic);
  207. align(l);
  208. interior.add(l);
  209. File currentDirectory = fc.getCurrentDirectory();
  210. String curDirName = null;
  211. if(currentDirectory != null) {
  212. curDirName = currentDirectory.getPath();
  213. }
  214. pathField = new JTextField(curDirName) {
  215. public Dimension getMaximumSize() {
  216. Dimension d = super.getMaximumSize();
  217. d.height = getPreferredSize().height;
  218. return d;
  219. }
  220. };
  221. l.setLabelFor(pathField);
  222. align(pathField);
  223. // Change to folder on return
  224. pathField.addActionListener(getUpdateAction());
  225. interior.add(pathField);
  226. interior.add(Box.createRigidArea(vstrut10));
  227. // CENTER: left, right accessory
  228. JPanel centerPanel = new JPanel();
  229. centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.LINE_AXIS));
  230. align(centerPanel);
  231. // left panel - Filter & folderList
  232. JPanel leftPanel = new JPanel();
  233. leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
  234. align(leftPanel);
  235. // add the filter PENDING(jeff) - I18N
  236. l = new JLabel(filterLabelText);
  237. l.setDisplayedMnemonic(filterLabelMnemonic);
  238. align(l);
  239. leftPanel.add(l);
  240. filterComboBox = new JComboBox() {
  241. public Dimension getMaximumSize() {
  242. Dimension d = super.getMaximumSize();
  243. d.height = getPreferredSize().height;
  244. return d;
  245. }
  246. };
  247. l.setLabelFor(filterComboBox);
  248. filterComboBoxModel = createFilterComboBoxModel();
  249. filterComboBox.setModel(filterComboBoxModel);
  250. filterComboBox.setRenderer(createFilterComboBoxRenderer());
  251. fc.addPropertyChangeListener(filterComboBoxModel);
  252. align(filterComboBox);
  253. leftPanel.add(filterComboBox);
  254. // leftPanel.add(Box.createRigidArea(vstrut10));
  255. // Add the Folder List PENDING(jeff) - I18N
  256. l = new JLabel(foldersLabelText);
  257. l.setDisplayedMnemonic(foldersLabelMnemonic);
  258. align(l);
  259. leftPanel.add(l);
  260. JScrollPane sp = createDirectoryList();
  261. sp.getVerticalScrollBar().setFocusable(false);
  262. sp.getHorizontalScrollBar().setFocusable(false);
  263. l.setLabelFor(sp.getViewport().getView());
  264. leftPanel.add(sp);
  265. // create files list
  266. JPanel rightPanel = new JPanel();
  267. align(rightPanel);
  268. rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.PAGE_AXIS));
  269. l = new JLabel(filesLabelText);
  270. l.setDisplayedMnemonic(filesLabelMnemonic);
  271. align(l);
  272. rightPanel.add(l);
  273. sp = createFilesList();
  274. l.setLabelFor(sp);
  275. rightPanel.add(sp);
  276. centerPanel.add(leftPanel);
  277. centerPanel.add(Box.createRigidArea(hstrut10));
  278. centerPanel.add(rightPanel);
  279. JComponent accessoryPanel = getAccessoryPanel();
  280. JComponent accessory = fc.getAccessory();
  281. if(accessoryPanel != null) {
  282. if(accessory == null) {
  283. accessoryPanel.setPreferredSize(ZERO_ACC_SIZE);
  284. accessoryPanel.setMaximumSize(ZERO_ACC_SIZE);
  285. } else {
  286. getAccessoryPanel().add(accessory, BorderLayout.CENTER);
  287. accessoryPanel.setPreferredSize(PREF_ACC_SIZE);
  288. accessoryPanel.setMaximumSize(MAX_SIZE);
  289. }
  290. align(accessoryPanel);
  291. centerPanel.add(accessoryPanel);
  292. }
  293. interior.add(centerPanel);
  294. interior.add(Box.createRigidArea(vstrut10));
  295. // add the filename field PENDING(jeff) - I18N
  296. l = new JLabel(enterFileNameLabelText);
  297. l.setDisplayedMnemonic(enterFileNameLabelMnemonic);
  298. align(l);
  299. interior.add(l);
  300. filenameTextField = new JTextField() {
  301. public Dimension getMaximumSize() {
  302. Dimension d = super.getMaximumSize();
  303. d.height = getPreferredSize().height;
  304. return d;
  305. }
  306. };
  307. l.setLabelFor(filenameTextField);
  308. filenameTextField.addActionListener(getApproveSelectionAction());
  309. align(filenameTextField);
  310. filenameTextField.setAlignmentX(JComponent.LEFT_ALIGNMENT);
  311. interior.add(filenameTextField);
  312. bottomPanel = getBottomPanel();
  313. bottomPanel.add(new JSeparator(), BorderLayout.NORTH);
  314. // Add buttons
  315. JPanel buttonPanel = new JPanel();
  316. align(buttonPanel);
  317. buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
  318. buttonPanel.add(Box.createGlue());
  319. approveButton = new JButton(getApproveButtonText(fc)) {
  320. public Dimension getMaximumSize() {
  321. return new Dimension(MAX_SIZE.width, this.getPreferredSize().height);
  322. }
  323. };
  324. approveButton.setMnemonic(getApproveButtonMnemonic(fc));
  325. approveButton.setToolTipText(getApproveButtonToolTipText(fc));
  326. align(approveButton);
  327. approveButton.setMargin(buttonMargin);
  328. approveButton.addActionListener(getApproveSelectionAction());
  329. buttonPanel.add(approveButton);
  330. buttonPanel.add(Box.createGlue());
  331. JButton updateButton = new JButton(updateButtonText) {
  332. public Dimension getMaximumSize() {
  333. return new Dimension(MAX_SIZE.width, this.getPreferredSize().height);
  334. }
  335. };
  336. updateButton.setMnemonic(updateButtonMnemonic);
  337. updateButton.setToolTipText(updateButtonToolTipText);
  338. align(updateButton);
  339. updateButton.setMargin(buttonMargin);
  340. updateButton.addActionListener(getUpdateAction());
  341. buttonPanel.add(updateButton);
  342. buttonPanel.add(Box.createGlue());
  343. JButton cancelButton = new JButton(cancelButtonText) {
  344. public Dimension getMaximumSize() {
  345. return new Dimension(MAX_SIZE.width, this.getPreferredSize().height);
  346. }
  347. };
  348. cancelButton.setMnemonic(cancelButtonMnemonic);
  349. cancelButton.setToolTipText(cancelButtonToolTipText);
  350. align(cancelButton);
  351. cancelButton.setMargin(buttonMargin);
  352. cancelButton.addActionListener(getCancelSelectionAction());
  353. buttonPanel.add(cancelButton);
  354. buttonPanel.add(Box.createGlue());
  355. JButton helpButton = new JButton(helpButtonText) {
  356. public Dimension getMaximumSize() {
  357. return new Dimension(MAX_SIZE.width, this.getPreferredSize().height);
  358. }
  359. };
  360. helpButton.setMnemonic(helpButtonMnemonic);
  361. helpButton.setToolTipText(helpButtonToolTipText);
  362. align(helpButton);
  363. helpButton.setMargin(buttonMargin);
  364. helpButton.setEnabled(false);
  365. buttonPanel.add(helpButton);
  366. buttonPanel.add(Box.createGlue());
  367. bottomPanel.add(buttonPanel, BorderLayout.SOUTH);
  368. if (fc.getControlButtonsAreShown()) {
  369. fc.add(bottomPanel, BorderLayout.SOUTH);
  370. }
  371. }
  372. protected JPanel getBottomPanel() {
  373. if (bottomPanel == null) {
  374. bottomPanel = new JPanel(new BorderLayout(0, 4));
  375. }
  376. return bottomPanel;
  377. }
  378. private void doControlButtonsChanged(PropertyChangeEvent e) {
  379. if (getFileChooser().getControlButtonsAreShown()) {
  380. getFileChooser().add(bottomPanel,BorderLayout.SOUTH);
  381. } else {
  382. getFileChooser().remove(getBottomPanel());
  383. }
  384. }
  385. public void uninstallComponents(JFileChooser fc) {
  386. fc.removeAll();
  387. }
  388. protected void installStrings(JFileChooser fc) {
  389. super.installStrings(fc);
  390. Locale l = fc.getLocale();
  391. enterFileNameLabelText = UIManager.getString("FileChooser.enterFileNameLabelText",l);
  392. enterFileNameLabelMnemonic = UIManager.getInt("FileChooser.enterFileNameLabelMnemonic");
  393. filesLabelText = UIManager.getString("FileChooser.filesLabelText",l);
  394. filesLabelMnemonic = UIManager.getInt("FileChooser.filesLabelMnemonic");
  395. foldersLabelText = UIManager.getString("FileChooser.foldersLabelText",l);
  396. foldersLabelMnemonic = UIManager.getInt("FileChooser.foldersLabelMnemonic");
  397. pathLabelText = UIManager.getString("FileChooser.pathLabelText",l);
  398. pathLabelMnemonic = UIManager.getInt("FileChooser.pathLabelMnemonic");
  399. filterLabelText = UIManager.getString("FileChooser.filterLabelText",l);
  400. filterLabelMnemonic = UIManager.getInt("FileChooser.filterLabelMnemonic");
  401. }
  402. protected void installIcons(JFileChooser fc) {
  403. // Since motif doesn't have button icons, leave this empty
  404. // which overrides the supertype icon loading
  405. }
  406. protected void uninstallIcons(JFileChooser fc) {
  407. // Since motif doesn't have button icons, leave this empty
  408. // which overrides the supertype icon loading
  409. }
  410. protected JScrollPane createFilesList() {
  411. fileList = new JList();
  412. if(getFileChooser().isMultiSelectionEnabled()) {
  413. fileList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  414. } else {
  415. fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  416. }
  417. fileList.setModel(new MotifFileListModel());
  418. fileList.setCellRenderer(new FileCellRenderer());
  419. fileList.addListSelectionListener(createListSelectionListener(getFileChooser()));
  420. fileList.addMouseListener(createDoubleClickListener(getFileChooser(), fileList));
  421. align(fileList);
  422. JScrollPane scrollpane = new JScrollPane(fileList);
  423. scrollpane.setPreferredSize(prefListSize);
  424. scrollpane.setMaximumSize(MAX_SIZE);
  425. align(scrollpane);
  426. return scrollpane;
  427. }
  428. protected JScrollPane createDirectoryList() {
  429. directoryList = new JList();
  430. align(directoryList);
  431. directoryList.setCellRenderer(new DirectoryCellRenderer());
  432. directoryList.setModel(new MotifDirectoryListModel());
  433. directoryList.addMouseListener(createDoubleClickListener(getFileChooser(), directoryList));
  434. directoryList.addListSelectionListener(createListSelectionListener(getFileChooser()));
  435. JScrollPane scrollpane = new JScrollPane(directoryList);
  436. scrollpane.setMaximumSize(MAX_SIZE);
  437. scrollpane.setPreferredSize(prefListSize);
  438. align(scrollpane);
  439. return scrollpane;
  440. }
  441. public Dimension getPreferredSize(JComponent c) {
  442. Dimension prefSize =
  443. (getFileChooser().getAccessory() != null) ? WITH_ACCELERATOR_PREF_SIZE : PREF_SIZE;
  444. Dimension d = c.getLayout().preferredLayoutSize(c);
  445. if (d != null) {
  446. return new Dimension(d.width < prefSize.width ? prefSize.width : d.width,
  447. d.height < prefSize.height ? prefSize.height : d.height);
  448. } else {
  449. return prefSize;
  450. }
  451. }
  452. public Dimension getMinimumSize(JComponent x) {
  453. return MIN_SIZE;
  454. }
  455. public Dimension getMaximumSize(JComponent x) {
  456. return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
  457. }
  458. protected void align(JComponent c) {
  459. c.setAlignmentX(JComponent.LEFT_ALIGNMENT);
  460. c.setAlignmentY(JComponent.TOP_ALIGNMENT);
  461. }
  462. protected class FileCellRenderer extends DefaultListCellRenderer {
  463. public Component getListCellRendererComponent(JList list, Object value, int index,
  464. boolean isSelected, boolean cellHasFocus) {
  465. super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
  466. setText(getFileChooser().getName((File) value));
  467. return this;
  468. }
  469. }
  470. protected class DirectoryCellRenderer extends DefaultListCellRenderer {
  471. public Component getListCellRendererComponent(JList list, Object value, int index,
  472. boolean isSelected, boolean cellHasFocus) {
  473. super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
  474. setText(getFileChooser().getName((File) value));
  475. return this;
  476. }
  477. }
  478. protected class MotifDirectoryListModel extends AbstractListModel implements ListDataListener {
  479. public MotifDirectoryListModel() {
  480. getModel().addListDataListener(this);
  481. }
  482. public int getSize() {
  483. return getModel().getDirectories().size();
  484. }
  485. public Object getElementAt(int index) {
  486. return getModel().getDirectories().elementAt(index);
  487. }
  488. public void intervalAdded(ListDataEvent e) {
  489. fireIntervalAdded(this, e.getIndex0(), e.getIndex1());
  490. }
  491. public void intervalRemoved(ListDataEvent e) {
  492. fireIntervalRemoved(this, e.getIndex0(), e.getIndex1());
  493. }
  494. // PENDING(jeff) - this is inefficient - should sent out
  495. // incremental adjustment values instead of saying that the
  496. // whole list has changed.
  497. public void fireContentsChanged() {
  498. fireContentsChanged(this, 0, getModel().getDirectories().size()-1);
  499. }
  500. // PENDING(jeff) - fire the correct interval changed - currently sending
  501. // out that everything has changed
  502. public void contentsChanged(ListDataEvent e) {
  503. fireContentsChanged();
  504. }
  505. }
  506. protected class MotifFileListModel extends AbstractListModel implements ListDataListener {
  507. public MotifFileListModel() {
  508. getModel().addListDataListener(this);
  509. }
  510. public int getSize() {
  511. return getModel().getFiles().size();
  512. }
  513. public boolean contains(Object o) {
  514. return getModel().getFiles().contains(o);
  515. }
  516. public int indexOf(Object o) {
  517. return getModel().getFiles().indexOf(o);
  518. }
  519. public Object getElementAt(int index) {
  520. return getModel().getFiles().elementAt(index);
  521. }
  522. public void intervalAdded(ListDataEvent e) {
  523. fireIntervalAdded(this, e.getIndex0(), e.getIndex1());
  524. }
  525. public void intervalRemoved(ListDataEvent e) {
  526. fireIntervalRemoved(this, e.getIndex0(), e.getIndex1());
  527. }
  528. // PENDING(jeff) - this is inefficient - should sent out
  529. // incremental adjustment values instead of saying that the
  530. // whole list has changed.
  531. public void fireContentsChanged() {
  532. fireContentsChanged(this, 0, getModel().getFiles().size()-1);
  533. }
  534. // PENDING(jeff) - fire the interval changed
  535. public void contentsChanged(ListDataEvent e) {
  536. fireContentsChanged();
  537. }
  538. }
  539. //
  540. // DataModel for Types Comboxbox
  541. //
  542. protected FilterComboBoxModel createFilterComboBoxModel() {
  543. return new FilterComboBoxModel();
  544. }
  545. //
  546. // Renderer for Types ComboBox
  547. //
  548. protected FilterComboBoxRenderer createFilterComboBoxRenderer() {
  549. return new FilterComboBoxRenderer();
  550. }
  551. /**
  552. * Render different type sizes and styles.
  553. */
  554. public class FilterComboBoxRenderer extends DefaultListCellRenderer {
  555. public Component getListCellRendererComponent(JList list,
  556. Object value, int index, boolean isSelected,
  557. boolean cellHasFocus) {
  558. super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
  559. if (value != null && value instanceof FileFilter) {
  560. setText(((FileFilter)value).getDescription());
  561. }
  562. return this;
  563. }
  564. }
  565. /**
  566. * Data model for a type-face selection combo-box.
  567. */
  568. protected class FilterComboBoxModel extends AbstractListModel implements ComboBoxModel, PropertyChangeListener {
  569. protected FileFilter[] filters;
  570. protected FilterComboBoxModel() {
  571. super();
  572. filters = getFileChooser().getChoosableFileFilters();
  573. }
  574. public void propertyChange(PropertyChangeEvent e) {
  575. String prop = e.getPropertyName();
  576. if(prop == JFileChooser.CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY) {
  577. filters = (FileFilter[]) e.getNewValue();
  578. fireContentsChanged(this, -1, -1);
  579. } else if (prop == JFileChooser.FILE_FILTER_CHANGED_PROPERTY) {
  580. fireContentsChanged(this, -1, -1);
  581. }
  582. }
  583. public void setSelectedItem(Object filter) {
  584. if(filter != null) {
  585. getFileChooser().setFileFilter((FileFilter) filter);
  586. fireContentsChanged(this, -1, -1);
  587. }
  588. }
  589. public Object getSelectedItem() {
  590. // Ensure that the current filter is in the list.
  591. // NOTE: we shouldnt' have to do this, since JFileChooser adds
  592. // the filter to the choosable filters list when the filter
  593. // is set. Lets be paranoid just in case someone overrides
  594. // setFileFilter in JFileChooser.
  595. FileFilter currentFilter = getFileChooser().getFileFilter();
  596. boolean found = false;
  597. if(currentFilter != null) {
  598. for(int i=0; i < filters.length; i++) {
  599. if(filters[i] == currentFilter) {
  600. found = true;
  601. }
  602. }
  603. if(found == false) {
  604. getFileChooser().addChoosableFileFilter(currentFilter);
  605. }
  606. }
  607. return getFileChooser().getFileFilter();
  608. }
  609. public int getSize() {
  610. if(filters != null) {
  611. return filters.length;
  612. } else {
  613. return 0;
  614. }
  615. }
  616. public Object getElementAt(int index) {
  617. if(index > getSize() - 1) {
  618. // This shouldn't happen. Try to recover gracefully.
  619. return getFileChooser().getFileFilter();
  620. }
  621. if(filters != null) {
  622. return filters[index];
  623. } else {
  624. return null;
  625. }
  626. }
  627. }
  628. protected JButton getApproveButton(JFileChooser fc) {
  629. return approveButton;
  630. }
  631. }