/bundles/plugins-trunk/CommonControls/common/gui/ModalJFileChooser.java

# · Java · 164 lines · 90 code · 24 blank · 50 comment · 10 complexity · 2d2dec0b14bb6cb734527f8fb66b9a7a MD5 · raw file

  1. /*
  2. * :tabSize=4:indentSize=4:noTabs=false:
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package common.gui;
  20. import java.io.File;
  21. import java.awt.Component;
  22. import java.awt.event.ActionEvent;
  23. import java.awt.event.ActionListener;
  24. import javax.swing.JDialog;
  25. import javax.swing.JFileChooser;
  26. import javax.swing.JOptionPane;
  27. import javax.swing.filechooser.FileFilter;
  28. import javax.swing.filechooser.FileSystemView;
  29. import org.gjt.sp.jedit.jEdit;
  30. import org.gjt.sp.jedit.GUIUtilities;
  31. import org.gjt.sp.jedit.gui.EnhancedDialog;
  32. /**
  33. * An implementation of JFileChooser that fixes the modal dialog being
  34. * created from another JDialog. The original JFileChooser would let the
  35. * calling dialog be activated, even though the JFileChooser was modal, due
  36. * to the wrong parent widget being used for the dialog.
  37. *
  38. * <p>It will also show hidden files when the "All files (including hidden)"
  39. * filter is selected. This filter is added automatically when this chooser
  40. * is instantiated. This is to overcome the lack of a GUI option to do this
  41. * (at least on Linux) and me not willing to come up with a better
  42. * solution to this (like a checkbox in the UI).</p>
  43. *
  44. * <p>Copied from the ProjectViewer plugin.</p>
  45. *
  46. * @author Marcelo Vanzin
  47. * @version $Id$
  48. * @since CC 0.9.4
  49. */
  50. public class ModalJFileChooser extends JFileChooser
  51. implements ActionListener {
  52. //{{{ Private members
  53. private boolean defaultHiddenFileStatus;
  54. private FileFilter hiddenFileFilter;
  55. //}}}
  56. //{{{ +ModalJFileChooser() : <init>
  57. public ModalJFileChooser() {
  58. init();
  59. } //}}}
  60. //{{{ +ModalJFileChooser(File) : <init>
  61. public ModalJFileChooser(File currentDirectory) {
  62. super(currentDirectory);
  63. init();
  64. } //}}}
  65. //{{{ +ModalJFileChooser(File, FileSystemView) : <init>
  66. public ModalJFileChooser(File currentDirectory, FileSystemView fsv) {
  67. super(currentDirectory, fsv);
  68. init();
  69. } //}}}
  70. //{{{ +ModalJFileChooser(FileSystemView) : <init>
  71. public ModalJFileChooser(FileSystemView fsv) {
  72. super(fsv);
  73. init();
  74. } //}}}
  75. //{{{ +ModalJFileChooser(String) : <init>
  76. public ModalJFileChooser(String currentDirectoryPath) {
  77. super(currentDirectoryPath);
  78. init();
  79. } //}}}
  80. //{{{ +ModalJFileChooser(String, FileSystemView) : <init>
  81. public ModalJFileChooser(String currentDirectoryPath, FileSystemView fsv) {
  82. super(currentDirectoryPath, fsv);
  83. } //}}}
  84. //{{{ -init() : void
  85. private void init() {
  86. hiddenFileFilter = new HiddenFileFilter();
  87. defaultHiddenFileStatus = isFileHidingEnabled();
  88. addActionListener(this);
  89. addChoosableFileFilter(hiddenFileFilter);
  90. setFileFilter(getAcceptAllFileFilter());
  91. } //}}}
  92. //{{{ #createDialog(Component) : JDialog
  93. /** Creates the modal dialog to show this file chooser. */
  94. protected JDialog createDialog(Component parent) {
  95. JDialog parentDlg = null;
  96. if (parent == null) {
  97. parent = jEdit.getActiveView();
  98. } else {
  99. parentDlg = (parent instanceof JDialog)
  100. ? (JDialog) parent
  101. : GUIUtilities.getParentDialog(parent);
  102. }
  103. JDialog dialog;
  104. if (parentDlg != null) {
  105. parent = parentDlg;
  106. dialog = new JDialog(parentDlg, true);
  107. } else {
  108. dialog = new JDialog(JOptionPane.getFrameForComponent(parent), true);
  109. }
  110. dialog.setTitle(getUI().getDialogTitle(this));
  111. dialog.getContentPane().add(this);
  112. dialog.pack();
  113. dialog.setLocationRelativeTo(parent);
  114. return dialog;
  115. } //}}}
  116. //{{{ +setFileFilter(FileFilter) : void
  117. public void setFileFilter(FileFilter filter) {
  118. super.setFileFilter(filter);
  119. fireActionPerformed(FILE_FILTER_CHANGED_PROPERTY);
  120. } //}}}
  121. //{{{ +actionPerformed(ActionEvent) : void
  122. public void actionPerformed(ActionEvent ae) {
  123. if (ae.getActionCommand().equals(FILE_FILTER_CHANGED_PROPERTY)) {
  124. if (getFileFilter() == hiddenFileFilter) {
  125. setFileHidingEnabled(false);
  126. } else {
  127. setFileHidingEnabled(defaultHiddenFileStatus);
  128. }
  129. }
  130. } //}}}
  131. //{{{ +class _HiddenFileFilter_
  132. public static class HiddenFileFilter extends FileFilter {
  133. public boolean accept(File f) {
  134. return true;
  135. }
  136. public String getDescription() {
  137. return jEdit.getProperty("projectviewer.general.hidden_file_filter");
  138. }
  139. } //}}}
  140. }