📄 rdirectorychooser.java
字号:
/*
* 12/10/2004
*
* RDirectoryChooser.java - A dialog allowing the user to select a single
* directory on their system.
* Copyright (C) 2004 Robert Futrell
* email@address.com
* www.website.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.fife.ui.rtextfilechooser;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ResourceBundle;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import org.fife.RUtilities;
import org.fife.ui.RButton;
import org.fife.ui.RScrollPane;
import org.fife.ui.UIUtilities;
/**
* A directory chooser component. This component is preferable to using
* either <code>RTextFileChooser</code> or <code>JFileChooser</code>
* in directory-mode because that design simply isn't intuitive.<p>
*
* An <code>RDirectoryChooser</code> is simply a tree representing
* all directories in the user's file system. System icons are placed
* beside directory names if available (e.g. Windows directory choosers).<p>
*
* NOTE: If you experience long load times when expanding a directory
* node for a network directory, it's not my fault, I swear; Java's
* file I/O performance over a network (especially for calls such as
* <code>File.isDirectory()</code>) is horrendous.
*
* @author Robert Futrell
* @version 0.1
*/
public class RDirectoryChooser extends JDialog {
/**
*
*/
private static final long serialVersionUID = 6601835164978070425L;
private RButton okButton;
private RButton cancelButton;
private DirectoryTree directoryTree;
private String chosenDirectory;
/*****************************************************************************/
/**
* Constructor.
*
* @param parent The dialog that owns this directory chooser.
*/
public RDirectoryChooser(Dialog parent) {
this(parent, null);
}
/*****************************************************************************/
/**
* Constructor.
*
* @param parent The window that owns this directory chooser.
*/
public RDirectoryChooser(Frame parent) {
this(parent, null);
}
/*****************************************************************************/
/**
* Constructor.
*
* @param parent The dialog that owns this directory chooser.
* @param title The title for this directory chooser. If
* <code>null</code>, a default title will be used.
*/
public RDirectoryChooser(Dialog parent, String title) {
super(parent);
init(parent, title);
}
/*****************************************************************************/
/**
* Constructor.
*
* @param parent The window that owns this directory chooser.
* @param title The title for this directory chooser.
* If <code>null</code>, a default title will be used.
*/
public RDirectoryChooser(Frame parent, String title) {
super(parent);
init(parent, title);
}
/*****************************************************************************/
/**
* Returns the directory chosen by the user.
*
* @return The chosen directory. If the user canceled the dialog, then
* <code>null</code> is returned.
*/
public String getChosenDirectory() {
return chosenDirectory;
}
/*****************************************************************************/
/**
* Initializes this directory chooser. This is here because JDialog's
* parent must be either a Frame or a Dialog, and not a Window; thus,
* we need a separate constructor for each of these cases...
* very unfortunate...
*
* @param parent The window that owns this directory chooser.
* @param title The title for this directory chooser.
*/
private void init(Window parent, String title) {
Listener listener = new Listener();
// Get our localized messages.
ResourceBundle msg = ResourceBundle.getBundle(
"org.fife.ui.rtextfilechooser.DirectoryChooser");
// The panel that will contain everything.
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(UIUtilities.getEmpty5Border());
// Add a panel with the directory tree.
JPanel treePanel = new JPanel(new GridLayout(1,1));
directoryTree = new DirectoryTree();
directoryTree.getSelectionModel().addTreeSelectionListener(listener);
directoryTree.addPropertyChangeListener(listener);
RScrollPane scrollPane = new RScrollPane(directoryTree);
scrollPane.setHorizontalScrollBarPolicy(
RScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(
RScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
treePanel.add(scrollPane);
contentPane.add(treePanel);
// Add a panel with the OK and Cancel buttons.
JPanel buttonPanel = new JPanel();
JPanel temp = new JPanel(new GridLayout(1,2, 5,0));
okButton = RUtilities.createRButton(msg, "OK", "OKMnemonic");
okButton.setEnabled(false);
okButton.setActionCommand("OK");
okButton.addActionListener(listener);
temp.add(okButton);
cancelButton = RUtilities.createRButton(msg, "Cancel",
"CancelMnemonic");
cancelButton.setActionCommand("Cancel");
cancelButton.addActionListener(listener);
temp.add(cancelButton);
buttonPanel.add(temp);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
// Get ready to go!
setContentPane(contentPane);
setTitle(title==null ? msg.getString("DefaultTitle") : title);
pack();
setModal(true);
setLocationRelativeTo(parent);
}
/*****************************************************************************/
/*********************** PRIVATE INNER CLASSES *******************************/
/*****************************************************************************/
/**
* Listens for all events in this directory chooser.
*/
private class Listener implements ActionListener, TreeSelectionListener,
PropertyChangeListener {
public Listener() {
}
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (actionCommand.equals("OK")) {
chosenDirectory = directoryTree.getSelectedFileName();
setVisible(false);
}
else if (actionCommand.equals("Cancel")) {
chosenDirectory = null;
setVisible(false);
}
}
public void propertyChange(PropertyChangeEvent e) {
String property = e.getPropertyName();
if (property.equals(DirectoryTree.WILL_EXPAND_PROPERTY)) {
RDirectoryChooser.this.setCursor(
Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
}
else if (property.equals(DirectoryTree.EXPANDED_PROPERTY)) {
RDirectoryChooser.this.setCursor(
Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
public void valueChanged(TreeSelectionEvent e) {
okButton.setEnabled(e.getNewLeadSelectionPath()!=null);
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -