📄 findaccessory.java
字号:
{
/**
Construct a search control action currently implements
FindAccesory.ACTION_START and FindAccessory.ACTION_STOP.
@param text command
@param icon button icon
*/
FindAction (String text, Icon icon)
{
super(text,icon);
}
/**
Invoke FindAction's action() method.
@param e action event
*/
public void actionPerformed (ActionEvent e)
{
action(e.getActionCommand());
}
}
/**
Displays the full path of the search starting folder.
*/
class FindFolder extends JPanel
{
protected JLabel searchDirectory = null;
FindFolder ()
{
super();
setLayout(new BorderLayout());
// Directory
searchDirectory = new JLabel();
searchDirectory.setForeground(Color.black);
searchDirectory.setFont(new Font("Helvetica",Font.PLAIN,9));
add(searchDirectory);
}
/**
Display the full path of the specified folder.
*/
public void setFindDirectory (File f)
{
if (searchDirectory == null) return;
if (f != null) searchDirectory.setText(f.getAbsolutePath());
else searchDirectory.setText(null);
}
}
/**
Find controls panel displays default action components for starting
and stopping a search. Also displays the search progress in the form of
a text display indicating the number of items found and the total number
of items encountered in the search.
*/
class FindControls extends JPanel
{
protected JLabel searchDirectory = null;
protected JLabel progress = null;
/**
Construct a simple search control panel with buttons for
starting and stopping a search and a simple display for
search progress.
*/
FindControls (FindAction find, FindAction stop, boolean recurse)
{
super();
setLayout(new BorderLayout());
JToolBar tools = new JToolBar();
tools.setFloatable(false);
tools.add(actionStart = new FindAction(ACTION_START,null));
tools.add(actionStop = new FindAction(ACTION_STOP,null));
add(tools,BorderLayout.WEST);
progress = new JLabel("",SwingConstants.RIGHT);
// So that frequent updates will appear smooth
progress.setDoubleBuffered(true);
progress.setForeground(Color.black);
progress.setFont(new Font("Helvetica",Font.PLAIN,9));
add(progress,BorderLayout.EAST);
}
/**
Display search progress as a text field
"no. of matches / total searched".
@param matches number of items found
@param total number of items investigated
*/
public void showProgress (int matches, int total)
{
if (progress == null) return;
progress.setText(String.valueOf(matches)+"/"+String.valueOf(total));
}
}
/**
Contains a collecton of search options displayed as tabbed panes and
at least one pane for displaying the search results. Each options tab
pane is a user interface for sprecifying the search criteria and a
factory for a FindFilter to implement the acceptance function. By making
the search option pane responsible for generating a FindFilter object,
the programmer can easily extend the search capabilities without
modifying the controlling search engine.
*/
class FindTabs extends JTabbedPane
{
protected String TAB_NAME = "Name";
protected String TAB_DATE = "Date";
protected String TAB_CONTENT = "Content";
protected String TAB_RESULTS = "Found";
protected FindResults resultsPanel = null;
protected JScrollPane resultsScroller = null;
/**
Construct a search tabbed pane with tab panels for seach by
filename, search by date, search by content and search results.
*/
FindTabs ()
{
super();
setForeground(Color.black);
setFont(new Font("Helvetica",Font.BOLD,10));
// Add search-by-name panel
addTab(TAB_NAME,new FindByName());
// Add search-by-date panel
addTab(TAB_DATE,new FindByDate());
// Add search-by-content panel
addTab(TAB_CONTENT,new FindByContent());
// Add results panel
resultsScroller = new JScrollPane(resultsPanel = new FindResults());
// so that updates will be smooth
resultsPanel.setDoubleBuffered(true);
resultsScroller.setDoubleBuffered(true);
addTab(TAB_RESULTS,resultsScroller);
}
/**
Adds the specified file to the results list.
@param f file to add to results list
*/
public void addFoundFile (File f)
{
if (resultsPanel != null) resultsPanel.append(f);
}
/**
Bring the search results tab panel to the front.
*/
public void showFindResults ()
{
if (resultsScroller != null) setSelectedComponent(resultsScroller);
}
/**
Prepares the panel for a new search by clearing the results list,
bringing the results tab panel to the front and generating an
array of search filters for each search options pane that
implements the FindFilterFactory interface.
@return array of FindFilters to be used by the controlling
search engine
*/
public FindFilter[] newFind ()
{
// Clear the results display
if (resultsPanel != null) resultsPanel.clear();
// Fix the width of the scrolling results panel so the layout
// managers don't try to make it too wide for JFileChooser
Dimension dim = resultsScroller.getSize();
resultsScroller.setMaximumSize(dim);
resultsScroller.setPreferredSize(dim);
// Return an array of FindFilters
Vector filters = new Vector();
for (int i=0; i<getTabCount(); i++)
{
try
{
FindFilterFactory fac = (FindFilterFactory)getComponentAt(i);
FindFilter f = fac.createFindFilter();
if (f != null) filters.addElement(f);
}
catch (Throwable e)
{
// The FindResults pane does not implement FindFilterFactory
}
}
if (filters.size() == 0) return null;
FindFilter[] filterArray = new FindFilter[filters.size()];
for (int i=0; i<filterArray.length; i++)
{
filterArray[i] = (FindFilter)filters.elementAt(i);
}
return filterArray;
}
}
/**
Appears as a special pane within the FindOptions tabbed panel.
The only one that does not generate a FindFilter.
*/
class FindResults extends JPanel
{
protected DefaultListModel model = null;
protected JList fileList = null;
/**
Construct a search results pane with a scrollable list of files.
When an item is double-clicked the FindAccessory controller will
be instructed to select the file in the parent JFileChooser's item
display.
*/
FindResults ()
{
super();
setLayout(new BorderLayout());
model = new DefaultListModel();
fileList = new JList(model);
fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fileList.setCellRenderer(new FindResultsCellRenderer());
add(fileList,BorderLayout.CENTER);
// Double click listener
MouseListener mouseListener = new MouseAdapter()
{
public void mouseClicked (MouseEvent e)
{
if (e.getClickCount() == 2)
{
try
{
int index = fileList.locationToIndex(e.getPoint());
File f = (File)model.elementAt(index);
goTo(f);
}
catch (Throwable err)
{
}
}
}
};
fileList.addMouseListener(mouseListener);
}
/**
Add a file to the results list.
@param f file found
*/
public void append (File f)
{
if (f == null) return;
model.addElement(f);
}
/**
Clear all items from the results list.
*/
public void clear ()
{
if (model != null)
{
model.removeAllElements();
invalidate();
repaint();
}
}
/**
Convenience class for rendering cells in the results list.
*/
class FindResultsCellRenderer extends JLabel implements ListCellRenderer
{
FindResultsCellRenderer()
{
setOpaque(true);
}
public Component getListCellRendererComponent (
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
if (index == -1)
{
// This shouldn't happen since we won't be using this
// renderer in a combo box
int selected = list.getSelectedIndex();
if (selected == -1) return this;
else index = selected;
}
setBorder(new EmptyBorder(1,2,1,2));
setFont(new Font("Helvetica",Font.PLAIN,10));
// show absolute path of file
File file = (File)model.elementAt(index);
setText(file.getAbsolutePath());
// selection characteristics
if(isSelected)
{
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
}
else
{
setBackground(Color.white);
setForeground(Color.black);
}
return this;
}
}
}
}
/**
Each search option tab that implements FindFilterFactory defines an
inner class that implements FindFilter. When a search is started
the search panel invokes createFindFilter() on each panel that
implements FindFilterFactory, thus causing the panel to create
a FindFilter object that implements its search settings.
*/
interface FindFilter
{
//public boolean accept (File f);
public boolean accept (File f, FindProgressCallback monitor);
}
interface FindProgressCallback
{
/**
Should be called by all time-consuming search filters at a reasonable
interval. Allows the search controller to report progress and to
abort the search in a clean and timely way.
@param filter FindFilter reporting the progress
@param file the file being searched
@param current current "location" of search
@param total maximum value
@return true if search should continue, false to abort
*/
public boolean reportProgress (FindFilter filter, File file,
long current, long total);
}
/**
Implemented by each search option panel. Each panel is responsible for
creating a FindFilter object that implements the search criteria
specified by its user interface.
*/
interface FindFilterFactory
{
public FindFilter createFindFilter ();
}
/**
Implements a user interface and generates FindFilter for selecting
files by date.
*/
class FindByDate extends JPanel implements FindFilterFactory
{
public static String THE_BIG_BANG = "The Big Bang";
public static String THE_BIG_CRUNCH = "The Big Crunch";
public static String YESTERDAY = "Yesterday";
public static String TODAY = "Today";
public static String NOW = "Now";
public static String MODIFIED_LABEL = "Modified";
public static String FORMAT_LABEL = "mm/dd/yyyy";
public static String FROM_DATE_LABEL = "between start of";
public static String TO_DATE_LABEL = "and end of";
protected JComboBox fromDateField = null;
protected JComboBox toDateField = null;
protected String[] fromDateItems = {THE_BIG_BANG,YESTERDAY,TODAY};
protected String[] toDateItems = {THE_BIG_CRUNCH,TODAY,NOW,YESTERDAY};
FindByDate ()
{
super();
setLayout(new BorderLayout());
Font font = new Font("Helvetica",Font.PLAIN,10);
// Grid Layout
JPanel p = new JPanel();
p.setLayout(new GridLayout(0,2,2,2));
// Date selection criteria
JLabel modified = new JLabel(MODIFIED_LABEL,SwingConstants.LEFT);
modified.setFont(font);
modified.setForeground(Color.black);
p.add(modified);
// format note
JLabel format = new JLabel(FORMAT_LABEL,SwingConstants.LEFT);
format.setFont(font);
format.setForeground(Color.black);
p.add(format);
// between
JLabel betweenLabel = new JLabel(FROM_DATE_LABEL,SwingConstants.RIGHT);
betweenLabel.setFont(font);
betweenLabel.setForeground(Color.black);
p.add(betweenLabel);
// from date
//fromDateField = new JTextField(8);
fromDateField = new JComboBox(fromDateItems);
fromDateField.setFont(font);
fromDateField.setEditable(true);
p.add(fromDateField);
// and
JLabel andLabel = new JLabel(TO_DATE_LABEL,SwingConstants.RIGHT);
andLabel.setFont(font);
andLabel.setForeground(Color.black);
p.add(andLabel);
//toDateField = new JTextField(8);
toDateField = new JComboBox(toDateItems);
toDateField.setFont(font);
toDateField.setEditable(true);
p.add(toDateField);
add(p,BorderLayout.NORTH);
}
/**
Generate a search filter object based on the setting of this UI
component.
@return a FindFilter object that implements the selection criteria
*/
public FindFilter createFindFilter ()
{
long from = -1;
long to = -1;
from = startDateToTime((String)fromDateField.getSelectedItem());
to = endDateToTime((String)toDateField.getSelectedItem());
return new DateFilter(from,to);
}
/**
Convenience method for converting the start date text to milliseconds
since January 1, 1970.
@return milliseconds since January 1, 1970
*/
protected long startDateToTime (String s)
{
if (s == null) return -1;
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date d = formatter.parse(s,new ParsePosition(0));
if (d == null)
{
if (s.equalsIgnoreCase(TODAY))
{
String today = formatter.format(new Date());
d = formatter.parse(today,new ParsePosition(0));
}
else if (s.equalsIgnoreCase(YESTERDAY))
{
String yesterday = formatter.format(
new Date(new Date().getTime() - 24*60*60*1000));
d = formatter.parse(yesterday,new ParsePosition(0));
}
else if (s.equalsIgnoreCase(THE_BIG_BANG))
{
return 0; // Not exactly the beginning of time, but
//close enough for computer work
}
}
if (d != null) return d.getTime();
return -1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -