1 7 8 package org.jdesktop.swing; 9 10 import java.util.regex.Pattern ; 11 12 import java.awt.Dimension ; 13 import java.awt.FlowLayout ; 14 import java.awt.LayoutManager ; 15 import java.awt.event.ActionEvent ; 16 import java.awt.event.ActionListener ; 17 import javax.swing.ComboBoxModel ; 18 import javax.swing.DefaultComboBoxModel ; 19 import javax.swing.JButton ; 20 import javax.swing.JCheckBox ; 21 import javax.swing.JComboBox ; 22 import javax.swing.JComponent ; 23 import javax.swing.JLabel ; 24 import javax.swing.JPanel ; 25 import javax.swing.JTextField ; 26 import javax.swing.event.DocumentEvent ; 27 import javax.swing.event.DocumentListener ; 28 29 import org.jdesktop.swing.decorator.FilterPipeline; 30 import org.jdesktop.swing.decorator.HighlighterPipeline; 31 import org.jdesktop.swing.decorator.PatternFilter; 32 import org.jdesktop.swing.decorator.PatternHighlighter; 33 import org.jdesktop.swing.decorator.PatternMatcher; 34 import org.jdesktop.swing.decorator.PipelineListener; 35 import org.jdesktop.swing.decorator.Sorter; 36 37 42 public class JXSearchPanel extends JPanel implements DocumentListener , ActionListener { 43 45 private final JLabel fieldName = new JLabel (); 47 private final JCheckBox matchCase = new JCheckBox (); 48 private final JComboBox searchCriteria = new JComboBox (); 49 private final JButton searchButton = new JButton (); 50 private final JTextField searchField = new JTextField (); 51 private final JComponent [] colleagues = { 53 matchCase, 54 searchCriteria, 55 searchButton, 56 searchField 57 }; 58 59 private PatternFilter patternFilter = null; 60 private PatternHighlighter patternHighlighter = null; 61 private JComponent targetComponent = null; 62 63 public JXSearchPanel() { 64 66 searchButton.setText("Search"); 67 matchCase.setText("Match Case"); 68 ComboBoxModel model = new DefaultComboBoxModel (new String [] { 69 "begins with", 70 "contains", 71 "ends with", 72 "equals" 73 }); 74 searchCriteria.setModel(model); 75 searchField.setPreferredSize(new Dimension (80, 20)); 76 81 add(fieldName); 82 add(searchCriteria); 83 add(searchField); 84 add(matchCase); 85 addActionListener(); 87 addEditListener(); 88 } 89 90 public int getMatchFlags() { 91 return matchCase.isSelected() ? 0 : Pattern.CASE_INSENSITIVE; 92 } 93 94 public Pattern getPattern() { 95 String searchString = searchField.getText(); 96 if (searchString.length() == 0) { 97 return Pattern.compile(".*", getMatchFlags()); 98 } 99 100 String patternString; 101 int criteria = searchCriteria.getSelectedIndex(); 102 103 switch (criteria) { 104 case 0: { 105 patternString = new String (searchString + ".*"); 106 break; 107 } 108 case 1: { 109 patternString = new String (".*" + searchString + ".*"); 110 break; 111 } 112 case 2: { 113 patternString = new String (".*" + searchString); 114 break; 115 } 116 default: { 117 patternString = searchString; 118 break; 119 } 120 } 121 return Pattern.compile(patternString, getMatchFlags()); 122 } 123 124 private void addActionListener() { 125 131 matchCase.addActionListener(this); 133 searchButton.addActionListener(this); 134 searchField.addActionListener(this); 135 searchCriteria.addActionListener(this); 136 } 137 138 private void removeActionListener() { 139 145 matchCase.removeActionListener(this); 147 searchButton.removeActionListener(this); 148 searchField.removeActionListener(this); 149 searchCriteria.removeActionListener(this); 150 } 151 152 private void addEditListener() { 153 searchField.getDocument().addDocumentListener(this); 154 } 155 156 private void removeEditListener() { 157 searchField.getDocument().removeDocumentListener(this); 158 } 159 160 public void setPatternFilter(PatternFilter filter) { 161 patternFilter = filter; 162 if (filter == null) { 163 fieldName.setText("Field"); 164 } 165 else { 166 fieldName.setText(filter.getColumnName()); 167 } 168 } 169 170 public PatternFilter getPatternFilter() { 171 return patternFilter; 172 } 173 174 public void setPatternHighlighter(PatternHighlighter highlighter) { 175 patternHighlighter = highlighter; 176 if (fieldName.getText().length() == 0) { fieldName.setText("Field"); 178 } 179 } 180 181 public PatternHighlighter getPatternHighlighter() { 182 return patternHighlighter; 183 } 184 185 public void setTargetComponent(JComponent target) { 186 188 this.targetComponent = target; 189 } 190 191 public JComponent getTargetComponent() { 192 return targetComponent; 193 } 194 195 public void setFieldName(String name) { 196 fieldName.setText(name); 197 } 198 199 public String getFieldName() { 200 return fieldName.getText(); 201 } 202 203 216 217 public void changedUpdate(DocumentEvent ev) { 218 refresh(); 219 } 220 221 public void insertUpdate(DocumentEvent ev) { 222 refresh(); 223 } 224 225 public void removeUpdate(DocumentEvent ev) { 226 refresh(); 227 } 228 229 public void actionPerformed(ActionEvent ev) { 230 refresh(); 231 } 232 233 protected void refresh() { 234 Pattern pattern = getPattern(); 235 236 PatternMatcher filter = getPatternFilter(); 237 if (filter != null) { 238 filter.setPattern(pattern); } 240 241 PatternMatcher highlighter = getPatternHighlighter(); 242 if (highlighter != null) { 243 highlighter.setPattern(pattern); 244 245 if (filter == null) { 246 JComponent target = getTargetComponent(); 248 if (target != null) { 249 target.repaint(); 250 } 251 } 252 } 253 } 254 255 269 } 270 271 | Popular Tags |