PageRenderTime 204ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/GtkJFileChooser/demo/com/google/code/gtkjfilechooser/demo/FileChooserDemo.java

http://gtkjfilechooser.googlecode.com/
Java | 666 lines | 491 code | 89 blank | 86 comment | 99 complexity | 72c7ecfb1c8a14eee3ec819085c332b4 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2010 Costantino Cerbo.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the GNU Lesser Public License v2.1
  5. * which accompanies this distribution, and is available at
  6. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  7. *
  8. * Contributors:
  9. * Costantino Cerbo - initial API and implementation
  10. * Yuvi Masory - Issue 69
  11. ******************************************************************************/
  12. package com.google.code.gtkjfilechooser.demo;
  13. /*
  14. * @(#)FileChooserDemo.java 1.31 06/02/03
  15. *
  16. * Copyright 2006, 2009006 Sun Microsystems, Inc. All Rights Reserved.
  17. *
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted provided that the following conditions are met:
  20. *
  21. * -Redistribution of source code must retain the above copyright notice, this
  22. * list of conditions and the following disclaimer.
  23. *
  24. * -Redistribution in binary form must reproduce the above copyright notice,
  25. * this list of conditions and the following disclaimer in the documentation
  26. * and/or other materials provided with the distribution.
  27. *
  28. * Neither the name of Sun Microsystems, Inc. or the names of contributors may
  29. * be used to endorse or promote products derived from this software without
  30. * specific prior written permission.
  31. *
  32. * This software is provided "AS IS," without a warranty of any kind. ALL
  33. * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  34. * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  35. * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
  36. * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
  37. * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
  38. * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  39. * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  40. * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  41. * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
  42. * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  43. *
  44. * You acknowledge that this software is not designed, licensed or intended
  45. * for use in the design, construction, operation or maintenance of any
  46. * nuclear facility.
  47. */
  48. /*
  49. * @(#)FileChooserDemo.java 1.31 06/02/03
  50. */
  51. import static javax.swing.JFileChooser.*;
  52. import java.awt.BorderLayout;
  53. import java.awt.CardLayout;
  54. import java.awt.Component;
  55. import java.awt.Dimension;
  56. import java.awt.Insets;
  57. import java.awt.Toolkit;
  58. import java.awt.event.ActionEvent;
  59. import java.awt.event.ActionListener;
  60. import java.awt.event.WindowAdapter;
  61. import java.awt.event.WindowEvent;
  62. import java.io.File;
  63. import javax.swing.BorderFactory;
  64. import javax.swing.Box;
  65. import javax.swing.BoxLayout;
  66. import javax.swing.ButtonGroup;
  67. import javax.swing.JButton;
  68. import javax.swing.JCheckBox;
  69. import javax.swing.JComponent;
  70. import javax.swing.JDialog;
  71. import javax.swing.JFileChooser;
  72. import javax.swing.JFrame;
  73. import javax.swing.JLabel;
  74. import javax.swing.JOptionPane;
  75. import javax.swing.JPanel;
  76. import javax.swing.JRadioButton;
  77. import javax.swing.JTextField;
  78. import javax.swing.JToggleButton;
  79. import javax.swing.UIManager;
  80. import javax.swing.filechooser.FileFilter;
  81. import javax.swing.filechooser.FileNameExtensionFilter;
  82. import javax.swing.plaf.FileChooserUI;
  83. import javax.swing.plaf.basic.BasicFileChooserUI;
  84. import com.google.code.gtkjfilechooser.GtkFileChooserSettings;
  85. import com.google.code.gtkjfilechooser.ui.ImagePreviewer;
  86. import com.sun.java.swing.plaf.gtk.GTKLookAndFeel;
  87. /**
  88. *
  89. * A demo which makes extensive use of the file chooser.
  90. *
  91. * 1.31 02/03/06
  92. *
  93. * @author Jeff Dinkins
  94. */
  95. public class FileChooserDemo extends JPanel implements ActionListener {
  96. static JFrame frame;
  97. JButton showButton;
  98. JCheckBox showAllFilesFilterCheckBox;
  99. JCheckBox showImageFilesFilterCheckBox;
  100. JCheckBox showFullDescriptionCheckBox;
  101. JCheckBox previewCheckBox;
  102. JCheckBox setHiddenCheckBox;
  103. JCheckBox useEmbedInWizardCheckBox;
  104. JCheckBox useControlsCheckBox;
  105. JRadioButton singleSelectionRadioButton;
  106. JRadioButton multiSelectionRadioButton;
  107. JRadioButton openRadioButton;
  108. JRadioButton saveRadioButton;
  109. JRadioButton customButton;
  110. JRadioButton justFilesRadioButton;
  111. JRadioButton justDirectoriesRadioButton;
  112. JRadioButton bothFilesAndDirectoriesRadioButton;
  113. JTextField customField;
  114. FileFilter jpgFilter, gifFilter, bothFilter;
  115. JPanel buttonPanel;
  116. public final static Dimension hpad10 = new Dimension(10, 1);
  117. public final static Dimension vpad20 = new Dimension(1, 20);
  118. public final static Dimension vpad7 = new Dimension(1, 7);
  119. public final static Dimension vpad4 = new Dimension(1, 4);
  120. public final static Insets insets = new Insets(5, 10, 0, 10);
  121. ImagePreviewer previewer;
  122. JFileChooser chooser;
  123. public FileChooserDemo() {
  124. setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
  125. chooser = new JFileChooser();
  126. previewer = new ImagePreviewer(chooser);
  127. // create a radio listener to listen to option changes
  128. OptionListener optionListener = new OptionListener();
  129. // Create options
  130. openRadioButton = new JRadioButton("Open");
  131. openRadioButton.setSelected(true);
  132. openRadioButton.addActionListener(optionListener);
  133. saveRadioButton = new JRadioButton("Save");
  134. saveRadioButton.addActionListener(optionListener);
  135. customButton = new JRadioButton("Custom");
  136. customButton.addActionListener(optionListener);
  137. customField = new JTextField(8) {
  138. @Override
  139. public Dimension getMaximumSize() {
  140. return new Dimension(getPreferredSize().width, getPreferredSize().height);
  141. }
  142. };
  143. customField.setText("Doit");
  144. customField.setAlignmentY(JComponent.TOP_ALIGNMENT);
  145. customField.setEnabled(false);
  146. customField.addActionListener(optionListener);
  147. ButtonGroup group1 = new ButtonGroup();
  148. group1.add(openRadioButton);
  149. group1.add(saveRadioButton);
  150. group1.add(customButton);
  151. // filter buttons
  152. showAllFilesFilterCheckBox = new JCheckBox("Show \"All Files\" Filter");
  153. showAllFilesFilterCheckBox.addActionListener(optionListener);
  154. showAllFilesFilterCheckBox.setSelected(true);
  155. showImageFilesFilterCheckBox = new JCheckBox("Show JPG and GIF Filters");
  156. showImageFilesFilterCheckBox.addActionListener(optionListener);
  157. showImageFilesFilterCheckBox.setSelected(false);
  158. previewCheckBox = new JCheckBox("Show Preview");
  159. previewCheckBox.addActionListener(optionListener);
  160. previewCheckBox.setSelected(false);
  161. // more options
  162. setHiddenCheckBox = new JCheckBox("Show Hidden Files");
  163. setHiddenCheckBox.setSelected(GtkFileChooserSettings.get().getShowHidden());
  164. setHiddenCheckBox.addActionListener(optionListener);
  165. showFullDescriptionCheckBox = new JCheckBox("With File Extensions");
  166. showFullDescriptionCheckBox.addActionListener(optionListener);
  167. showFullDescriptionCheckBox.setSelected(true);
  168. showFullDescriptionCheckBox.setEnabled(false);
  169. useEmbedInWizardCheckBox = new JCheckBox("Embed in Wizard");
  170. useEmbedInWizardCheckBox.addActionListener(optionListener);
  171. useEmbedInWizardCheckBox.setSelected(false);
  172. useControlsCheckBox = new JCheckBox("Show Control Buttons");
  173. useControlsCheckBox.addActionListener(optionListener);
  174. useControlsCheckBox.setSelected(true);
  175. // File or Directory chooser options
  176. ButtonGroup group3 = new ButtonGroup();
  177. justFilesRadioButton = new JRadioButton("Just Select Files");
  178. justFilesRadioButton.setSelected(true);
  179. group3.add(justFilesRadioButton);
  180. justFilesRadioButton.addActionListener(optionListener);
  181. justDirectoriesRadioButton = new JRadioButton("Just Select Directories");
  182. group3.add(justDirectoriesRadioButton);
  183. justDirectoriesRadioButton.addActionListener(optionListener);
  184. bothFilesAndDirectoriesRadioButton = new JRadioButton(
  185. "Select Files or Directories");
  186. group3.add(bothFilesAndDirectoriesRadioButton);
  187. bothFilesAndDirectoriesRadioButton.addActionListener(optionListener);
  188. singleSelectionRadioButton = new JRadioButton("Single Selection", true);
  189. singleSelectionRadioButton.addActionListener(optionListener);
  190. multiSelectionRadioButton = new JRadioButton("Multi Selection");
  191. multiSelectionRadioButton.addActionListener(optionListener);
  192. ButtonGroup group4 = new ButtonGroup();
  193. group4.add(singleSelectionRadioButton);
  194. group4.add(multiSelectionRadioButton);
  195. // Create show button
  196. showButton = new JButton("Show FileChooser");
  197. showButton.addActionListener(this);
  198. showButton.setMnemonic('s');
  199. // ********************************************************
  200. // ******************** Dialog Type ***********************
  201. // ********************************************************
  202. JPanel control1 = new InsetPanel(insets);
  203. control1.setBorder(BorderFactory.createTitledBorder("Dialog Type"));
  204. control1.setLayout(new BoxLayout(control1, BoxLayout.Y_AXIS));
  205. control1.add(Box.createRigidArea(vpad20));
  206. control1.add(openRadioButton);
  207. control1.add(Box.createRigidArea(vpad7));
  208. control1.add(saveRadioButton);
  209. control1.add(Box.createRigidArea(vpad7));
  210. control1.add(customButton);
  211. control1.add(Box.createRigidArea(vpad4));
  212. JPanel fieldWrapper = new JPanel();
  213. fieldWrapper.setLayout(new BoxLayout(fieldWrapper, BoxLayout.X_AXIS));
  214. fieldWrapper.setAlignmentX(Component.LEFT_ALIGNMENT);
  215. fieldWrapper.add(Box.createRigidArea(hpad10));
  216. fieldWrapper.add(Box.createRigidArea(hpad10));
  217. fieldWrapper.add(customField);
  218. control1.add(fieldWrapper);
  219. control1.add(Box.createRigidArea(vpad20));
  220. control1.add(Box.createGlue());
  221. // ********************************************************
  222. // ***************** Filter Controls **********************
  223. // ********************************************************
  224. JPanel control2 = new InsetPanel(insets);
  225. control2.setBorder(BorderFactory.createTitledBorder("Filter Controls"));
  226. control2.setLayout(new BoxLayout(control2, BoxLayout.Y_AXIS));
  227. control2.add(Box.createRigidArea(vpad20));
  228. control2.add(showAllFilesFilterCheckBox);
  229. control2.add(Box.createRigidArea(vpad7));
  230. control2.add(showImageFilesFilterCheckBox);
  231. control2.add(Box.createRigidArea(vpad4));
  232. JPanel checkWrapper = new JPanel();
  233. checkWrapper.setLayout(new BoxLayout(checkWrapper, BoxLayout.X_AXIS));
  234. checkWrapper.setAlignmentX(Component.LEFT_ALIGNMENT);
  235. checkWrapper.add(Box.createRigidArea(hpad10));
  236. checkWrapper.add(Box.createRigidArea(hpad10));
  237. checkWrapper.add(showFullDescriptionCheckBox);
  238. control2.add(checkWrapper);
  239. control2.add(Box.createRigidArea(vpad20));
  240. control2.add(Box.createGlue());
  241. // ********************************************************
  242. // ****************** Display Options *********************
  243. // ********************************************************
  244. JPanel control3 = new InsetPanel(insets);
  245. control3.setBorder(BorderFactory.createTitledBorder("Display Options"));
  246. control3.setLayout(new BoxLayout(control3, BoxLayout.Y_AXIS));
  247. control3.add(Box.createRigidArea(vpad20));
  248. control3.add(setHiddenCheckBox);
  249. control3.add(Box.createRigidArea(vpad7));
  250. control3.add(previewCheckBox);
  251. control3.add(Box.createRigidArea(vpad7));
  252. control3.add(useEmbedInWizardCheckBox);
  253. control3.add(Box.createRigidArea(vpad7));
  254. control3.add(useControlsCheckBox);
  255. control3.add(Box.createGlue());
  256. // ********************************************************
  257. // ************* File & Directory Options *****************
  258. // ********************************************************
  259. JPanel control4 = new InsetPanel(insets);
  260. control4
  261. .setBorder(BorderFactory.createTitledBorder("File and Directory Options"));
  262. control4.setLayout(new BoxLayout(control4, BoxLayout.Y_AXIS));
  263. control4.add(Box.createRigidArea(vpad20));
  264. control4.add(justFilesRadioButton);
  265. control4.add(Box.createRigidArea(vpad7));
  266. control4.add(justDirectoriesRadioButton);
  267. control4.add(Box.createRigidArea(vpad7));
  268. control4.add(bothFilesAndDirectoriesRadioButton);
  269. control4.add(Box.createRigidArea(vpad20));
  270. control4.add(singleSelectionRadioButton);
  271. control4.add(Box.createRigidArea(vpad7));
  272. control4.add(multiSelectionRadioButton);
  273. control4.add(Box.createRigidArea(vpad20));
  274. control4.add(Box.createGlue());
  275. JPanel panel = new JPanel();
  276. panel.add(showButton);
  277. // ********************************************************
  278. // ****************** Wrap 'em all up *********************
  279. // ********************************************************
  280. JPanel wrapper = new JPanel();
  281. wrapper.setLayout(new BoxLayout(wrapper, BoxLayout.X_AXIS));
  282. add(Box.createRigidArea(vpad20));
  283. wrapper.add(Box.createRigidArea(hpad10));
  284. wrapper.add(Box.createRigidArea(hpad10));
  285. wrapper.add(control1);
  286. wrapper.add(Box.createRigidArea(hpad10));
  287. wrapper.add(control2);
  288. wrapper.add(Box.createRigidArea(hpad10));
  289. wrapper.add(control3);
  290. wrapper.add(Box.createRigidArea(hpad10));
  291. wrapper.add(control4);
  292. wrapper.add(Box.createRigidArea(hpad10));
  293. wrapper.add(Box.createRigidArea(hpad10));
  294. add(wrapper);
  295. add(Box.createRigidArea(vpad20));
  296. add(panel);
  297. add(Box.createRigidArea(vpad20));
  298. }
  299. public void actionPerformed(ActionEvent e) {
  300. if (customButton.isSelected()) {
  301. chooser.setApproveButtonText(customField.getText());
  302. }
  303. if (chooser.isMultiSelectionEnabled()) {
  304. chooser.setSelectedFiles(null);
  305. } else {
  306. chooser.setSelectedFile(null);
  307. }
  308. // clear the preview from the previous display of the chooser
  309. JComponent accessory = chooser.getAccessory();
  310. if (accessory != null) {
  311. ((ImagePreviewer) accessory).loadImage(null);
  312. }
  313. if (useEmbedInWizardCheckBox.isSelected()) {
  314. WizardDialog wizard = new WizardDialog(frame, true);
  315. wizard.setVisible(true);
  316. wizard.dispose();
  317. return;
  318. }
  319. int retval = chooser.showDialog(frame, null);
  320. if (retval == APPROVE_OPTION) {
  321. JOptionPane.showMessageDialog(frame, getResultString());
  322. } else if (retval == CANCEL_OPTION) {
  323. JOptionPane.showMessageDialog(frame,
  324. "User cancelled operation. No file was chosen.");
  325. } else if (retval == ERROR_OPTION) {
  326. JOptionPane.showMessageDialog(frame, "An error occured. No file was chosen.");
  327. } else {
  328. JOptionPane.showMessageDialog(frame, "Unknown operation occured.");
  329. }
  330. }
  331. private void resetFileFilters(boolean enableFilters,
  332. boolean showExtensionInDescription) {
  333. chooser.resetChoosableFileFilters();
  334. if (enableFilters) {
  335. jpgFilter = createFileFilter("JPEG Compressed Image Files",
  336. showExtensionInDescription, "jpg");
  337. gifFilter = createFileFilter("GIF Image Files", showExtensionInDescription,
  338. "gif");
  339. bothFilter = createFileFilter("JPEG and GIF Image Files",
  340. showExtensionInDescription, "jpg", "gif");
  341. chooser.addChoosableFileFilter(bothFilter);
  342. chooser.addChoosableFileFilter(jpgFilter);
  343. chooser.addChoosableFileFilter(gifFilter);
  344. }
  345. }
  346. private FileFilter createFileFilter(String description,
  347. boolean showExtensionInDescription, String... extensions) {
  348. if (showExtensionInDescription) {
  349. description = createFileNameFilterDescriptionFromExtensions(description,
  350. extensions);
  351. }
  352. return new FileNameExtensionFilter(description, extensions);
  353. }
  354. private String createFileNameFilterDescriptionFromExtensions(String description,
  355. String[] extensions) {
  356. String fullDescription = (description == null) ? "(" : description + " (";
  357. // build the description from the extension list
  358. fullDescription += "." + extensions[0];
  359. for (int i = 1; i < extensions.length; i++) {
  360. fullDescription += ", .";
  361. fullDescription += extensions[i];
  362. }
  363. fullDescription += ")";
  364. return fullDescription;
  365. }
  366. class WizardDialog extends JDialog implements ActionListener {
  367. CardLayout cardLayout;
  368. JPanel cardPanel;
  369. JLabel messageLabel;
  370. JButton backButton, nextButton, closeButton;
  371. WizardDialog(JFrame frame, boolean modal) {
  372. super(frame, "Embedded JFileChooser Demo", modal);
  373. cardLayout = new CardLayout();
  374. cardPanel = new JPanel(cardLayout);
  375. getContentPane().add(cardPanel, BorderLayout.CENTER);
  376. messageLabel = new JLabel("", JLabel.CENTER);
  377. cardPanel.add(chooser, "fileChooser");
  378. cardPanel.add(messageLabel, "label");
  379. cardLayout.show(cardPanel, "fileChooser");
  380. chooser.addActionListener(this);
  381. JPanel buttonPanel = new JPanel();
  382. backButton = new JButton("< Back");
  383. nextButton = new JButton("Next >");
  384. closeButton = new JButton("Close");
  385. buttonPanel.add(backButton);
  386. buttonPanel.add(nextButton);
  387. buttonPanel.add(closeButton);
  388. getContentPane().add(buttonPanel, BorderLayout.SOUTH);
  389. backButton.setEnabled(false);
  390. getRootPane().setDefaultButton(nextButton);
  391. backButton.addActionListener(this);
  392. nextButton.addActionListener(this);
  393. closeButton.addActionListener(this);
  394. pack();
  395. setLocationRelativeTo(frame);
  396. }
  397. public void actionPerformed(ActionEvent evt) {
  398. Object src = evt.getSource();
  399. String cmd = evt.getActionCommand();
  400. if (src == backButton) {
  401. back();
  402. } else if (src == nextButton) {
  403. FileChooserUI ui = chooser.getUI();
  404. if (ui instanceof BasicFileChooserUI) {
  405. // Workaround for bug 4528663. This is necessary to
  406. // pick up the contents of the file chooser text field.
  407. // This will trigger an APPROVE_SELECTION action.
  408. ((BasicFileChooserUI) ui).getApproveSelectionAction()
  409. .actionPerformed(null);
  410. } else {
  411. next();
  412. }
  413. } else if (src == closeButton) {
  414. close();
  415. } else if (cmd == APPROVE_SELECTION) {
  416. next();
  417. } else if (cmd == CANCEL_SELECTION) {
  418. close();
  419. }
  420. }
  421. private void back() {
  422. backButton.setEnabled(false);
  423. nextButton.setEnabled(true);
  424. cardLayout.show(cardPanel, "fileChooser");
  425. getRootPane().setDefaultButton(nextButton);
  426. chooser.requestFocus();
  427. }
  428. private void next() {
  429. backButton.setEnabled(true);
  430. nextButton.setEnabled(false);
  431. messageLabel.setText(getResultString());
  432. cardLayout.show(cardPanel, "label");
  433. getRootPane().setDefaultButton(closeButton);
  434. closeButton.requestFocus();
  435. }
  436. private void close() {
  437. setVisible(false);
  438. }
  439. @Override
  440. public void dispose() {
  441. chooser.removeActionListener(this);
  442. // The chooser is hidden by CardLayout on remove
  443. // so fix it here
  444. cardPanel.remove(chooser);
  445. chooser.setVisible(true);
  446. super.dispose();
  447. }
  448. }
  449. private String getResultString() {
  450. String resultString = null;
  451. String filter = chooser.getFileFilter().getDescription();
  452. String path = null;
  453. boolean isDirMode = (chooser.getFileSelectionMode() == DIRECTORIES_ONLY);
  454. boolean isMulti = chooser.isMultiSelectionEnabled();
  455. if (isMulti) {
  456. File[] files = chooser.getSelectedFiles();
  457. if (files != null && files.length > 0) {
  458. path = "";
  459. for (int i = 0; i < files.length; i++) {
  460. path = path + "<br>" + files[i].getAbsolutePath();
  461. }
  462. }
  463. } else {
  464. File file = chooser.getSelectedFile();
  465. if (file != null) {
  466. path = "<br>" + file.getAbsolutePath();
  467. }
  468. }
  469. if (path != null) {
  470. path = path.replace(" ", "&nbsp;");
  471. filter = filter.replace(" ", "&nbsp;");
  472. resultString = "<html>You chose "
  473. + (isMulti ? "these" : "this")
  474. + " "
  475. + (isDirMode ? (isMulti ? "directories" : "directory")
  476. : (isMulti ? "files" : "file")) + ": <code>" + path
  477. + "</code><br><br>with filter: <br><code>" + filter;
  478. } else {
  479. resultString = "Nothing was chosen";
  480. }
  481. return resultString;
  482. }
  483. /** An ActionListener that listens to the radio buttons. */
  484. class OptionListener implements ActionListener {
  485. public void actionPerformed(ActionEvent e) {
  486. JComponent c = (JComponent) e.getSource();
  487. boolean selected = false;
  488. if (c instanceof JToggleButton) {
  489. selected = ((JToggleButton) c).isSelected();
  490. }
  491. if (c == openRadioButton) {
  492. chooser.setDialogType(OPEN_DIALOG);
  493. customField.setEnabled(false);
  494. repaint();
  495. } else if (c == useEmbedInWizardCheckBox) {
  496. useControlsCheckBox.setEnabled(!selected);
  497. useControlsCheckBox.setSelected(!selected);
  498. chooser.setControlButtonsAreShown(!selected);
  499. } else if (c == useControlsCheckBox) {
  500. chooser.setControlButtonsAreShown(selected);
  501. } else if (c == saveRadioButton) {
  502. chooser.setDialogType(SAVE_DIALOG);
  503. customField.setEnabled(false);
  504. repaint();
  505. } else if (c == customButton || c == customField) {
  506. customField.setEnabled(true);
  507. chooser.setDialogType(CUSTOM_DIALOG);
  508. repaint();
  509. } else if (c == showAllFilesFilterCheckBox) {
  510. chooser.setAcceptAllFileFilterUsed(selected);
  511. } else if (c == showImageFilesFilterCheckBox) {
  512. resetFileFilters(selected, showFullDescriptionCheckBox.isSelected());
  513. showFullDescriptionCheckBox.setEnabled(selected);
  514. } else if (c == setHiddenCheckBox) {
  515. chooser.setFileHidingEnabled(!selected);
  516. } else if (c == previewCheckBox) {
  517. if (selected) {
  518. chooser.setAccessory(previewer);
  519. } else {
  520. chooser.setAccessory(null);
  521. }
  522. } else if (c == showFullDescriptionCheckBox) {
  523. resetFileFilters(showImageFilesFilterCheckBox.isSelected(), selected);
  524. } else if (c == justFilesRadioButton) {
  525. chooser.setFileSelectionMode(FILES_ONLY);
  526. } else if (c == justDirectoriesRadioButton) {
  527. chooser.setFileSelectionMode(DIRECTORIES_ONLY);
  528. } else if (c == bothFilesAndDirectoriesRadioButton) {
  529. chooser.setFileSelectionMode(FILES_AND_DIRECTORIES);
  530. } else if (c == singleSelectionRadioButton) {
  531. if (selected) {
  532. chooser.setMultiSelectionEnabled(false);
  533. }
  534. } else if (c == multiSelectionRadioButton) {
  535. if (selected) {
  536. chooser.setMultiSelectionEnabled(true);
  537. }
  538. }
  539. }
  540. }
  541. class InsetPanel extends JPanel {
  542. Insets i;
  543. InsetPanel(Insets i) {
  544. this.i = i;
  545. }
  546. @Override
  547. public Insets getInsets() {
  548. return i;
  549. }
  550. }
  551. public static void main(String s[]) {
  552. try {
  553. UIManager.setLookAndFeel(GTKLookAndFeel.class.getName());
  554. } catch (Exception exc) {
  555. JOptionPane.showMessageDialog(null, exc.getLocalizedMessage(),
  556. "Look and Feel error", JOptionPane.ERROR_MESSAGE);
  557. return;
  558. }
  559. if ("GTK look and feel".equals(UIManager.getLookAndFeel().getName())) {
  560. UIManager.put("FileChooserUI",
  561. com.google.code.gtkjfilechooser.ui.GtkFileChooserUI.class.getName());
  562. }
  563. FileChooserDemo panel = new FileChooserDemo();
  564. frame = new JFrame("FileChooserDemo");
  565. frame.setIconImage(Toolkit.getDefaultToolkit().getImage(
  566. FileChooserDemo.class.getResource("/gnome_foot.png")));
  567. frame.addWindowListener(new WindowAdapter() {
  568. @Override
  569. public void windowClosing(WindowEvent e) {
  570. System.exit(0);
  571. }
  572. });
  573. frame.getContentPane().add("Center", panel);
  574. frame.pack();
  575. frame.setVisible(true);
  576. }
  577. }