jfilechooser.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 876 行 · 第 1/2 页
JAVA
876 行
/* class JFileChooser
*
* Copyright (C) 2003 R M Pitman
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Modified Jul 14, 2003 by Tadpole Computer, Inc.
* Modifications Copyright 2003 by Tadpole Computer, Inc.
*
* Modifications are hereby licensed to all parties at no charge under
* the same terms as the original.
*
* Fixed bug to allow save dialog to work when files do not exist.
* Added setSelectedFile method. Fixed fileSelectionMode to mean
* that when FILES_ONLY, entry of a directory name in the textfield now
* causes the appropriate setCurrentDirectory() call.
*/
package charvax.swing;
import java.io.File;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.Vector;
import charva.awt.BorderLayout;
import charva.awt.Component;
import charva.awt.Dialog;
import charva.awt.Dimension;
import charva.awt.FlowLayout;
import charva.awt.Insets;
import charva.awt.Point;
import charva.awt.Toolkit;
import charva.awt.event.ActionEvent;
import charva.awt.event.ActionListener;
import charva.awt.event.EventListener;
import charva.awt.event.KeyEvent;
import charva.awt.event.KeyListener;
import charvax.swing.border.EmptyBorder;
import charvax.swing.border.TitledBorder;
import charvax.swing.event.ListSelectionEvent;
import charvax.swing.event.ListSelectionListener;
import charvax.swing.filechooser.FileFilter;
/**
* <p>The JFileChooser class displays a dialog from which the user can choose
* a file. The dialog is always modal (i.e. the user cannot interact
* with any other windows until he closes the dialog).</p>
*
* <p>The dialog is displayed by calling its showDialog() method, which blocks
* until the dialog is closed (by the user pressing the Approve or Cancel
* buttons, or by pressing ENTER while the focus is in the "Filename" field).
* After the dialog has been closed, the program can find out what
* File was selected by calling the getSelectedFile() method.</p>
*
* The labels of the buttons that are displayed in the JFileChooser
* can be customized by changing the following static variables:
* <ul>
* <li> <code>PARENT_DIRECTORY_LABEL</code>
* <li> <code>NEW_DIRECTORY_LABEL</code>
* <li> <code>APPROVE_LABEL</code>
* <li> <code>CANCEL_LABEL</code>
* </ul>
* <p>"Accelerator keys" can also be set for the buttons. For example,
* to set the F1 key to have the same effect as pressing the CANCEL
* button, call the following code before using the JFileChooser:</p>
* <pre>
* JFileChooser.CANCEL_LABEL = "Cancel (F1)";
* JFileChooser.CANCEL_ACCELERATOR = KeyEvent.VK_F1;
* </pre>
* Note that after the buttons have been customized, they stay customized
* for all future invocations of JFileChooser (until they are re-customized
* to some other value).
*/
public class JFileChooser
extends JComponent
{
/**
* Constructs a JFileChooser pointing to the user's home directory.
*/
public JFileChooser()
{
this((File) null);
}
/**
* Constructs a JFileChooser pointing to the specified directory.
* Passing in a null parameter causes the JFileChooser to point to
* the user's home directory.
*/
public JFileChooser(File currentDirectory_)
{
setCurrentDirectory(currentDirectory_);
}
/**
* Constructs a JFileChooser with the specified pathname. Passing a value
* of <code>null</code> causes the file chooser to point to the user's
* home directory.
*/
public JFileChooser(String currentDirectoryPath_)
{
if (currentDirectoryPath_ == null)
setCurrentDirectory(null);
else
setCurrentDirectory(new File(currentDirectoryPath_));
}
/** Set the current directory. Passing a parameter of <code>null</code>
* cause the JFileChooser to point to the user's home directory.
*/
public void setCurrentDirectory(File dir_) {
if (dir_ == null) {
String home = System.getProperty("user.home");
dir_ = new File(home);
}
if (dir_.isDirectory() == false) {
throw new IllegalArgumentException("not a directory");
}
_currentDirectory = dir_;
_location = dir_.getAbsolutePath();
}
/**
* Returns the currently displayed directory.
*/
public File getCurrentDirectory() {
return _currentDirectory;
}
/**
* Get the File selected by the user. If the user pressed Cancel,
* the return value is null.
*/
public File getSelectedFile() {
if (_cancelWasPressed)
return null;
return new File(_location);
}
public void setSelectedFile(File file_) {
if (!file_.isAbsolute()) {
file_ = new File(_currentDirectory, file_.getPath());
}
File parent = file_.getParentFile();
if (!file_.isDirectory() && (parent != null)) {
_currentDirectory = parent;
_location = file_.getAbsolutePath();
} else if (file_.isDirectory()) {
_currentDirectory = file_;
_location = file_.getAbsolutePath();
}
fireFileChooserEvent();
}
/**
* Pops up a custom file chooser dialog with a custom approve button.
* @param parent_ the parent component of the dialog; can be
* <code>null</code>.
* @param approveButtonText_ the custom text string to display in the
* Approve button.
* @return the return state of the file chooser on popdown:
* <ul><li>JFileChooser.CANCEL_OPTION
* <li>JFileChooser.APPROVE_OPTION
* <li>JFileChooser.ERROR_OPTION</ul>
*/
public int showDialog(Component parent_, String approveButtonText_)
{
_approveButtonText = approveButtonText_;
JDialog chooserDialog = new ChooserDialog(parent_);
chooserDialog.setLocationRelativeTo(parent_);
chooserDialog.show();
if (_cancelWasPressed)
return CANCEL_OPTION;
else
return APPROVE_OPTION;
}
/**
* Pops up a "Save File" file chooser dialog; this is a convenience
* method and is equivalent to showDialog(Component, "Save").
* @return the return state of the file chooser on popdown:
* <ul><li>JFileChooser.CANCEL_OPTION
* <li>JFileChooser.APPROVE_OPTION
* <li>JFileChooser.ERROR_OPTION</ul>
*/
public int showSaveDialog(Component parent_)
{
return showDialog(parent_, "Save");
}
/**
* Pops up a "Open File" file chooser dialog; this is a convenience
* method and is equivalent to showDialog(Component, "Open").
* @return the return state of the file chooser on popdown:
* <ul>
* <li>JFileChooser.CANCEL_OPTION
* <li>JFileChooser.APPROVE_OPTION
* <li>JFileChooser.ERROR_OPTION
* </ul>
*/
public int showOpenDialog(Component parent_)
{
return showDialog(parent_, "Open");
}
/**
* Sets the <code>JFileChooser</code> to allow the user to select
* files only directories only, or files and directories. The default
* is JFileChooser.FILES_ONLY.
*/
public void setFileSelectionMode(int mode_)
{
if (mode_ < FILES_ONLY || mode_ > FILES_AND_DIRECTORIES)
throw new IllegalArgumentException("invalid file selection mode");
_fileSelectionMode = mode_;
}
/** Returns the current file-selection mode.
* @return the file-selection mode, one of the following:<p>
* <ul>
* <li><code>JFileChooser.FILES_ONLY</code>
* <li><code>JFileChooser.DIRECTORIES_ONLY</code>
* <li><code>JFileChooser.FILES_AND_DIRECTORIES</code>
* </ul>
*/
public int getFileSelectionMode() {
return _fileSelectionMode;
}
public void setDialogTitle(String title_) {
_title = title_;
}
/**
* Sets the current file filter. The file filter is used by the
* file chooser to filter out files from the user's view.
*/
public void setFileFilter(FileFilter filter_)
{
_fileFilter = filter_;
}
/**
* Returns the currently selected file filter.
*/
public FileFilter getFileFilter()
{
return _fileFilter;
}
public void debug(int level_) {
System.err.println("JFileChooser origin=" + _origin +
" title=" + _title );
}
/** Required to implement abstract method of JComponent (never used).
*/
public Dimension minimumSize() {
return null;
}
/** Required to implement abstract method of JComponent (never used).
*/
public Dimension getSize() {
return null;
}
/** Required to implement abstract method of JComponent (never used).
*/
public int getHeight() {
return 0;
}
/** Required to implement abstract method of JComponent (never used).
*/
public int getWidth() {
return 0;
}
protected void addFileChooserListener(FileChooserListener l)
{
_filelisteners.addElement(l);
}
protected void fireFileChooserEvent()
{
Enumeration e = _filelisteners.elements();
while (e.hasMoreElements()) {
FileChooserListener l = (FileChooserListener) e.nextElement();
l.fileChanged(new FileChooserEvent(this));
}
}
//====================================================================
// INSTANCE VARIABLES
protected String _title;
protected String _approveButtonText = "Open File";
/** The current directory shown in the dialog.
*/
protected File _currentDirectory = null;
protected JFileChooser.DirList _dirList = this.new DirList();
protected String _location = "";
protected boolean _cancelWasPressed = true;
protected int _fileSelectionMode = FILES_ONLY;
protected FileFilter _fileFilter = null;
protected Vector _filelisteners = new Vector();
protected static final int _COLS = 50;
protected static final int _ROWS = 20;
public static final int FILES_ONLY = 200;
public static final int DIRECTORIES_ONLY = 201;
public static final int FILES_AND_DIRECTORIES = 202;
public static final int CANCEL_OPTION = 300;
public static final int APPROVE_OPTION = 301;
public static final int ERROR_OPTION = 302;
// Default button labels - can be customized.
public static String CANCEL_LABEL = "Cancel";
public static String APPROVE_LABEL = "Approve";
public static String PARENT_DIRECTORY_LABEL = "Parent Directory";
public static String NEW_DIRECTORY_LABEL = "New Directory";
// Button accelerators (disabled by default).
public static int CANCEL_ACCELERATOR = -1;
public static int APPROVE_ACCELERATOR = -1;
public static int PARENT_DIRECTORY_ACCELERATOR = -1;
public static int NEW_DIRECTORY_ACCELERATOR = -1;
/*====================================================================
* This is a nonstatic inner class used by JFileChooser to display
* a popup dialog.
*/
private class ChooserDialog
extends JDialog
implements ActionListener, ListSelectionListener, KeyListener,
FileChooserListener
{
ChooserDialog(Component parent_) {
setTitle(_title);
setSize(_COLS, _ROWS);
// Inherit colors from the parent component unless they have
// been set already.
if (JFileChooser.this.getForeground() == null)
setForeground(parent_.getForeground());
else
setForeground(JFileChooser.this.getForeground());
if (JFileChooser.this.getBackground() == null)
setBackground(parent_.getBackground());
else
setBackground(JFileChooser.this.getBackground());
/* Insert the directory list in the west.
*/
_dirList.setVisibleRowCount(12);
_dirList.setColumns(45);
_dirList.addListSelectionListener(this);
displayCurrentDirectory();
_scrollPane = new JScrollPane(_dirList);
_scrollPane.setViewportBorder(new TitledBorder("Files"));
add(_scrollPane, BorderLayout.WEST);
/* Insert a north panel that contains the Parent and New buttons.
*/
JPanel toppanel = new JPanel();
toppanel.setBorder(new EmptyBorder(0,1,0,1));
toppanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 1, 0));
toppanel.add(_parentButton);
_parentButton.setText(PARENT_DIRECTORY_LABEL);
_parentButton.addActionListener(this);
toppanel.add(_newButton);
_newButton.setText(NEW_DIRECTORY_LABEL);
_newButton.addActionListener(this);
add(toppanel, BorderLayout.NORTH);
/* Insert a panel in the south for the textfield and the
* Approve and Cancel buttons.
*/
JPanel southpanel = new JPanel();
southpanel.setLayout(new BorderLayout());
JPanel topsouth = new JPanel();
topsouth.add(new JLabel("Pathname:"));
topsouth.add(_locationField);
_locationField.setText(_location);
_locationField.setActionCommand("locationField");
_locationField.addActionListener(this);
southpanel.add(topsouth, BorderLayout.NORTH);
JPanel bottomsouth = new JPanel();
bottomsouth.setLayout(new FlowLayout(FlowLayout.RIGHT, 1, 0));
bottomsouth.setBorder(new EmptyBorder(1,1,0,1));
bottomsouth.add(_approveButton);
bottomsouth.add(_cancelButton);
_approveButton.setText(_approveButtonText);
_cancelButton.setText(CANCEL_LABEL);
_approveButton.addActionListener(this);
_cancelButton.addActionListener(this);
southpanel.add(bottomsouth, BorderLayout.SOUTH);
add(southpanel, BorderLayout.SOUTH);
pack();
Insets insets = getInsets();
_dirList.setColumns(getWidth() - insets.left - insets.right - 2);
addKeyListener(this);
addFileChooserListener(this);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?