📄 findaccessory.java
字号:
@param filter FindFilter reporting progress @param file file being searched @param current current "location" of search @param total expected maximum value of current @return true to continue search, false to abort */ public boolean reportProgress (FindFilter filter, File file, long current, long total) { return !killFind; } /** Begins a new search by resetting the <b>total</b> and <b>matches</b> progress variables and retrieves the search filter array from the options panel. Each tab in the options panel is responsible for generating a FindFilter based on its current settings. @return Array of search filters from the options panel. */ protected FindFilter[] newFind () { total = matches = 0; updateProgress(); if (searchTabs != null) return searchTabs.newFind(); return null; } /** Display progress of running search. */ protected void updateProgress () { controlPanel.showProgress(matches, total); } /** Add this component to the specified JFileChooser's list of property change listeners and action listeners. @param c parent JFileChooser */ protected void register (JFileChooser c) { if (c == null) return; c.addActionListener(this); } /** Remove this component from the specified JFileChooser's list of property change listeners and action listeners. @param c parent JFileChooser */ protected void unregister (JFileChooser c) { if (c == null) return; c.removeActionListener(this); } /** Stop the current search and unregister in preparation for parent shutdown. */ public void quit () { stop(); unregister(chooser); } /** Invoked by FindAction objects to start and stop searches. */ public void action (String command) { if (command == null) return; if (command.equals(ACTION_START)) startThread(); else if (command.equals(ACTION_STOP)) stop(); } /** Convenience class for adding action objects to the control panel. */ class FindAction extends AbstractAction { /** 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()); } } /** 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 = Jext.getProperty("find.accessory.name"); //"Name"; protected String TAB_DATE = Jext.getProperty("find.accessory.date"); //"Date"; protected String TAB_RESULTS = Jext.getProperty("find.accessory.found"); //"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("Monospaced",Font.BOLD, 10)); // Add search-by-name panel addTab(TAB_NAME, new FindByName()); // Add search-by-date panel addTab(TAB_DATE, new FindByDate()); // 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()); goTo((File) model.elementAt(index)); } 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; } setOpaque(isSelected); 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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -