📄 findaccessory.java
字号:
/* * 19:48:05 05/05/00 * * FindAccessory.java - a powerful find accessory for open dialogs * (C) 2000 Ken Klinner, portions copyright (C) 2000 Romain Guy * * 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.jext.misc;import java.io.*;import java.util.*;import java.text.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;import javax.swing.filechooser.*;import java.beans.*;import org.jext.Jext;import org.jext.Utilities;import org.jext.gui.*;/** * A threaded file search accessory for JFileChooser. * <P> * Presents JFileChooser users with a tabbed panel interface for * specifying file search criteria including (1) search by name, * (2) search by date of modification, and (3) search by file content. * Finded are performed "in the background" with found files displayed * dynamically as they are found. Only one search can be active at * a time. FindResults are displayed in a scrolling list within a results * tab panel. * <P> * Findes are performed asynchronously so the user can continue * browsing the file system. The user may stop the search at any time. * Accepting or cancelling the file chooser or closing the dialog window * will automatically stop a search in progress. * <P> * The starting folder of the search (the search base) is displayed * at the top of the accessory panel. The search base dsiplay will * not change while a search is running. Thes search base display * will change to reflect the current directory of JFileChooser * when a search is not running. * <P> * Changing the search options does not affect a search in progress. * * @version 1.0, 2000/01/19 * @author Ken Klinner, kklinner@opiom.com */public class FindAccessory extends JPanel implements Runnable, ActionListener, FindProgressCallback{ /** Label for this accessory. */ static public final String ACCESSORY_NAME = Jext.getProperty("find.accessory.find"); //" Find "; /** Default max number of found items. Prevents overloading results list. */ static public final int DEFAULT_MAX_SEARCH_HITS = 500; /** Find start action name */ static public final String ACTION_START = Jext.getProperty("find.accessory.start"); //"Start"; /** Find stop action name */ static public final String ACTION_STOP = Jext.getProperty("find.accessory.stop"); //"Stop"; /** Parent JFileChooser component */ protected JFileChooser chooser = null; protected FindAction actionStart = null; protected FindAction actionStop = null; /** This version of FindAccesory supports only one active search thread */ protected Thread searchThread = null; /** Set to true to stop current search */ protected boolean killFind = false; /** Find options with results list */ protected FindTabs searchTabs = null; /** Find controls with progress display */ protected FindControls controlPanel = null; /** Number of items inspected by current/last search */ protected int total = 0; /** Number of items found by current/last search */ protected int matches = 0; /** Max number of found items to prevent overloading the results list. */ protected int maxMatches = DEFAULT_MAX_SEARCH_HITS; /** Construct a search panel with start and stop actions, option panes and a results list pane that can display up to DEFAULT_MAX_SEARCH_HITS items. */ public FindAccessory () { super(); setBorder(new TitledBorder(ACCESSORY_NAME)); setLayout(new BorderLayout()); actionStart = new FindAction(ACTION_START, null); actionStop = new FindAction(ACTION_STOP, null); add(searchTabs = new FindTabs(), BorderLayout.CENTER); add(controlPanel = new FindControls(actionStart, actionStop, true), BorderLayout.SOUTH); setMinimumSize(getPreferredSize()); } /** Construct a search panel with start and stop actions and "attach" it to the specified JFileChooser component. Calls register() to establish FindAccessory as a PropertyChangeListener of JFileChooser. @param parent JFileChooser containing this accessory */ public FindAccessory (JFileChooser parent) { this(); chooser = parent; register(chooser); } /** Construct a search panel with start and stop actions and "attach" it to the specified JFileChooser component. Calls register() to establish FindAccessory as a PropertyChangeListener of JFileChooser. Sets maximum number of found items to limit the load in the results list. @param parent JFileChooser containing this accessory @param max Max number of items for results list. Find stops when max number of items found. */ public FindAccessory (JFileChooser c, int max) { this(c); setMaxFindHits(max); } /** Sets maximum capacity of the results list. Find stops when max number of items found. @param max Max capacity of results list. */ public void setMaxFindHits (int max) { maxMatches = max; } /** Returns maximum capacity of results list. @return Max capacity of results list. */ public int getMaxFindHits () { return maxMatches; } /** Called by JFileChooser when the user provokes an action like "cancel" or "open". Listens for APPROVE_SELECTION and CANCEL_SELECTION action and stops the current search, if there is one. @param e ActionEvent from parent JFileChooser. */ public void actionPerformed (ActionEvent e) { String command = e.getActionCommand(); if (command == null) return; // Can this happen? Probably not. Call me paranoid. if (command.equals(JFileChooser.APPROVE_SELECTION)) quit(); else if (command.equals(JFileChooser.CANCEL_SELECTION)) quit(); } /** Set parent's current directory to the parent folder of the specified file and select the specified file. This method is invoked when the user double clicks on an item in the results list. @param f File to select in parent JFileChooser */ public void goTo (File f) { if (f == null) return; if (!f.exists()) return; if (chooser == null) return; // Make sure that files and directories can be displayed chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // Make sure that parent file chooser will show the type of file // specified javax.swing.filechooser.FileFilter filter = chooser.getFileFilter(); if (filter != null) { if (!filter.accept(f)) { // The current filter will not display the specified file. // Set the file filter to the built-in accept-all filter (*.*) javax.swing.filechooser.FileFilter all = chooser.getAcceptAllFileFilter(); chooser.setFileFilter(all); } } // Tell parent file chooser to display contents of parentFolder. // Prior to Java 1.2.2 setSelectedFile() did not set the current // directory the folder containing the file to be selected. File parentFolder = new File(f.getParent()); if (parentFolder != null) chooser.setCurrentDirectory(parentFolder); // Nullify the current selection, if any. // Why is this necessary? // Emperical evidence suggests that JFileChooser gets "sticky" (i.e. it // does not always relinquish the current selection). Nullifying the // current selection seems to yield better results. chooser.setSelectedFile(null); // Select the file chooser.setSelectedFile(f); // Refresh file chooser display. // Is this really necessary? Testing on a variety of systems with // Java 1.2.2 suggests that this helps. Sometimes it doesn't work, // but it doesn't do any harm. chooser.invalidate(); chooser.repaint(); } /** Start a search. The path display will show the starting folder of the search. Finds are recursive and will span the entire folder hierarchy below the base folder. The user may continue to browse with JFileChooser. */ public synchronized void startThread () { if (searchTabs != null) searchTabs.showFindResults(); killFind = false; if (searchThread == null) { searchThread = new Thread(this); } if (searchThread != null) searchThread.start(); } /** Stop the active search. */ public synchronized void stop () { killFind = true; } /** @return true if a search is currently running */ public boolean isRunning () { if (searchThread == null) return false; return searchThread.isAlive(); } /** Find thread */ public void run () { if (searchThread == null) return; if (Thread.currentThread() != searchThread) return; try { actionStart.setEnabled(false); actionStop.setEnabled(true); runFind(chooser.getCurrentDirectory(), newFind()); } catch (InterruptedException e) { } finally { actionStart.setEnabled(true); actionStop.setEnabled(false); searchThread = null; } } /** Recursive search beginning at folder <b>base</b> for files and folders matching each filter in the <b>filters</b> array. To interrupt set <b>killFind</b> to true. Also stops when number of search hits (matches) equals <b>maxMatches</b>. <P> <b>Note:</b> Convert this to a nonrecursive search algorithm on systems where stack space might be limited and/or the search hierarchy might be very deep. @param base starting folder of search @param filters matches must pass each filters in array @exception InterruptedException if thread is interrupted */ protected void runFind (File base, FindFilter[] filters) throws InterruptedException { if (base == null) return; if (!base.exists()) return; // Not likely to happen if (filters == null) return; if (killFind) return; File folder = null; if (base.isDirectory()) folder = base; else folder = new File(base.getParent()); File[] files = folder.listFiles();//Utilities.listFiles(folder.list(), false); for (int i = 0; i < files.length; i++) { total++; if (accept(files[i], filters)) { matches++; searchTabs.addFoundFile(files[i]); } updateProgress(); if (killFind) return; Thread.currentThread().sleep(0); if (files[i].isDirectory()) runFind(files[i], filters); if ((maxMatches > 0) && (matches >= maxMatches)) { return;// stopgap measure so that we don't overload } } } /** @param file file to pass to each filter's accept method @param filters array of selection criteria @return true if specified file matches each filter's selection criteria */ protected boolean accept (File file, FindFilter[] filters) { if (file == null) return false; if (filters == null) return false; for (int i = 0; i < filters.length; i++) { if (!filters[i].accept(file, this)) return false; } return true; } /** Called by FindFilter to report progress of a search. Purely a voluntary report. This really should be implemented as a property change listener. Percentage completion = (current/total)*100.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -