📄 findaccessory.java
字号:
*/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 = Jext.getProperty("find.accessory.bang"); //"The Big Bang"; public static String THE_BIG_CRUNCH = Jext.getProperty("find.accessory.crunch"); //"The Big Crunch"; public static String YESTERDAY = Jext.getProperty("find.accessory.yesterday"); //"Yesterday"; public static String TODAY = Jext.getProperty("find.accessory.today"); //"Today"; public static String NOW = Jext.getProperty("find.accessory.now"); //"Now"; public static String MODIFIED_LABEL = Jext.getProperty("find.accessory.modified"); //"Modified"; public static String FORMAT_LABEL = Jext.getProperty("find.accessory.format"); //"mm/dd/yyyy"; public static String FROM_DATE_LABEL = Jext.getProperty("find.accessory.from"); //"between start of"; public static String TO_DATE_LABEL = Jext.getProperty("find.accessory.to"); //"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); fromDateField.setRenderer(new ModifiedCellRenderer()); 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); toDateField.setRenderer(new ModifiedCellRenderer()); 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; } /** Convenience method for converting the end date text to milliseconds since January 1, 1970. The end time is the end of the specified day. @return milliseconds since January 1, 1970 */ protected long endDateToTime (String s) { if (s == null) return -1; SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy"); long time = -1; Date d = dateFormatter.parse(s, new ParsePosition(0)); if (d == null) { if (s.equalsIgnoreCase(TODAY)) { String today = dateFormatter.format(new Date()); d = dateFormatter.parse(today, new ParsePosition(0)); if (d != null) time = d.getTime() + (24L * 3600L * 1000L); } else if (s.equalsIgnoreCase(YESTERDAY)) { String yesterday = dateFormatter.format( new Date(new Date().getTime() - 24 * 60 * 60 * 1000)); d = dateFormatter.parse(yesterday, new ParsePosition(0)); if (d != null) time = d.getTime() + (24L * 3600L * 1000L); } else if (s.equalsIgnoreCase(NOW)) { d = new Date(); if (d != null) time = d.getTime(); } else if (s.equalsIgnoreCase(THE_BIG_CRUNCH)) { time = Long.MAX_VALUE; } } else { // Valid date. Now add 24 hours to make sure that the // date is inclusive time = d.getTime() + (24L * 3600L * 1000L); } return time; } /** Filter object for selecting files by the date range specified by the UI. */ class DateFilter implements FindFilter { protected long startTime = -1; protected long endTime = -1; DateFilter (long from, long to) { startTime = from; endTime = to; } public boolean accept (File f, FindProgressCallback callback) { if (f == null) return false; long t = f.lastModified(); if (startTime >= 0) { if (t < startTime) return false; } if (endTime >= 0) { if (t > endTime) return false; } return true; } }}/** Implements user interface and generates FindFilter for selecting files by name. */class FindByName extends JPanel implements FindFilterFactory{ protected String NAME_PATTERN = Jext.getProperty("find.accessory.pattern"); //"pattern"; protected String NAME_CONTAINS = Jext.getProperty("find.accessory.contains"); //"contains"; protected String NAME_IS = Jext.getProperty("find.accessory.is"); //"is"; protected String NAME_STARTS_WITH = Jext.getProperty("find.accessory.starts"); //"starts with"; protected String NAME_ENDS_WITH = Jext.getProperty("find.accessory.ends"); //"ends with"; protected int NAME_PATTERN_INDEX = 0; protected int NAME_CONTAINS_INDEX = 1; protected int NAME_IS_INDEX = 2; protected int NAME_STARTS_WITH_INDEX = 3; protected int NAME_ENDS_WITH_INDEX = 4; protected String[] criteria = {NAME_PATTERN, NAME_CONTAINS, NAME_IS, NAME_STARTS_WITH, NAME_ENDS_WITH}; protected JTextField nameField = null; protected JComboBox combo = null; protected JCheckBox ignoreCaseCheck = null; FindByName () { super(); setLayout(new BorderLayout()); // Grid Layout JPanel p = new JPanel(); p.setLayout(new GridLayout(0, 2, 2, 2)); // Name combo = new JComboBox(criteria); //combo.setFont(new Font("Helvetica",Font.PLAIN, 10)); combo.setPreferredSize(combo.getPreferredSize()); combo.setRenderer(new ModifiedCellRenderer()); p.add(combo); nameField = new JTextField(12); //nameField.setFont(new Font("Helvetica",Font.PLAIN, 10)); p.add(nameField); // ignore case p.add(new JLabel("",SwingConstants.RIGHT)); ignoreCaseCheck = new JCheckBox(Jext.getProperty("find.accessory.ignorecase"),true); ignoreCaseCheck.setForeground(Color.black); //ignoreCaseCheck.setFont(new Font("Helvetica",Font.PLAIN, 10)); p.add(ignoreCaseCheck); add(p, BorderLayout.NORTH); } public FindFilter createFindFilter () { return new NameFilter(nameField.getText(), combo.getSelectedIndex(), ignoreCaseCheck.isSelected()); } /** Filter object for selecting files by name. */ class NameFilter implements FindFilter { protected String match = null; protected int howToMatch = -1; protected boolean ignoreCase = true; NameFilter (String name, int how, boolean ignore) { match = name; howToMatch = how; ignoreCase = ignore; } public boolean accept (File f, FindProgressCallback callback) { if (f == null) return false; if ((match == null) || (match.length() == 0)) return true; if (howToMatch < 0) return true; String filename = f.getName(); if (howToMatch == NAME_PATTERN_INDEX) { return Utilities.match(match, filename); } else if (howToMatch == NAME_CONTAINS_INDEX) { if (ignoreCase) { if (filename.toLowerCase().indexOf(match.toLowerCase()) >= 0) return true; else return false; } else { if (filename.indexOf(match) >= 0) return true; else return false; } } else if (howToMatch == NAME_IS_INDEX) { if (ignoreCase) { if (filename.equalsIgnoreCase(match)) return true; else return false; } else { if (filename.equals(match)) return true; else return false; } } else if (howToMatch == NAME_STARTS_WITH_INDEX) { if (ignoreCase) { if (filename.toLowerCase().startsWith(match.toLowerCase())) return true; else return false; } else { if (filename.startsWith(match)) return true; else return false; } } else if (howToMatch == NAME_ENDS_WITH_INDEX) { if (ignoreCase) { if (filename.toLowerCase().endsWith(match.toLowerCase())) return true; else return false; } else { if (filename.endsWith(match)) return true; else return false; } } return true; } }}// End of FindAccessory.java
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -