1 7 8 package org.jdesktop.swing; 9 10 import java.awt.BorderLayout ; 11 import java.awt.Component ; 12 import java.awt.Frame ; 13 import java.awt.GraphicsConfiguration ; 14 import java.awt.GraphicsEnvironment ; 15 import java.awt.Rectangle ; 16 17 import java.awt.event.ActionEvent ; 18 import java.awt.event.ActionListener ; 19 import java.awt.event.KeyEvent ; 20 21 import java.beans.EventHandler ; 22 23 import java.util.regex.Pattern ; 24 import java.util.regex.Matcher ; 25 26 import javax.swing.*; 27 28 import javax.swing.event.DocumentEvent ; 29 import javax.swing.event.DocumentListener ; 30 31 public class JXFindDialog extends JDialog { 32 33 private Searchable searchable; 34 35 private JTextField findText; 36 private JCheckBox matchCheck; 37 private JCheckBox wrapCheck; 38 private JCheckBox backCheck; 39 40 private Pattern pattern; 41 private Matcher matcher; 42 43 private boolean DEBUG = true; 44 45 public JXFindDialog(Searchable searchable) { 46 super((Frame )SwingUtilities.getWindowAncestor((Component )searchable), 47 "Find in this component"); 48 this.searchable = searchable; 49 50 GraphicsConfiguration gc = 51 GraphicsEnvironment.getLocalGraphicsEnvironment(). 52 getDefaultScreenDevice().getDefaultConfiguration(); 53 Rectangle bounds = gc.getBounds(); 54 int x = bounds.x+bounds.width/3; 55 int y = bounds.y+bounds.height/3; 56 57 setLocation(x, y); 58 59 initUI(); 60 pack(); 61 } 62 63 66 public void setDebug(boolean debug) { 67 this.DEBUG = debug; 68 } 69 70 private void initUI() { 71 getContentPane().add(createFieldPanel(), BorderLayout.CENTER); 72 getContentPane().add(createButtonPanel(), BorderLayout.SOUTH); 73 74 89 } 90 91 94 private JComponent createFieldPanel() { 95 96 JLabel label = new JLabel("Find Text: "); 98 label.setDisplayedMnemonicIndex(2); 99 label.setLabelFor(findText); 100 101 findText = new JTextField(); 102 matchCheck = new JCheckBox(new MatchAction()); 103 wrapCheck = new JCheckBox(new WrapAction()); 104 backCheck = new JCheckBox(new BackwardAction()); 105 106 Box lBox = Box.createVerticalBox(); 107 lBox.add(label); 108 lBox.add(Box.createGlue()); 109 110 Box rBox = Box.createVerticalBox(); 111 rBox.add(findText); 112 rBox.add(matchCheck); 113 rBox.add(wrapCheck); 114 rBox.add(backCheck); 115 116 Box box = Box.createHorizontalBox(); 117 box.add(lBox); 118 box.add(rBox); 119 120 box.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 121 122 return box; 123 } 124 125 private JPanel createButtonPanel() { 126 JPanel panel = new JPanel(); 127 128 Action findAction = new FindAction(); 129 Action closeAction = new CloseAction(); 130 131 JButton findButton; 132 panel.add(findButton = new JButton(findAction)); 133 panel.add(new JButton(closeAction)); 134 135 String CANCEL_ACTION_KEY = "CANCEL_ACTION_KEY"; 137 String ENTER_ACTION_KEY = "ENTER_ACTION_KEY"; 138 139 KeyStroke enterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false); 140 KeyStroke escapeKey = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false); 141 142 InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 143 inputMap.put(enterKey, ENTER_ACTION_KEY); 144 inputMap.put(escapeKey, CANCEL_ACTION_KEY); 145 146 getRootPane().setDefaultButton(findButton); 147 148 ActionMap actionMap = getRootPane().getActionMap(); 149 actionMap.put(ENTER_ACTION_KEY, findAction); 150 actionMap.put(CANCEL_ACTION_KEY, closeAction); 151 152 return panel; 153 } 154 155 private int lastIndex = -1; 156 157 160 public void doFind() { 161 doFind(getBackwardsFlag()); 162 } 163 164 public void doFind(boolean backwards) { 165 Pattern pattern = getPattern(); 166 lastIndex = searchable.search(getPattern(), lastIndex, backwards); 169 if (lastIndex == -1) { 170 JOptionPane.showMessageDialog(this, "Value not found"); 171 } 172 } 173 174 177 public void doClose() { 178 JXFindDialog.this.dispose(); 179 } 180 181 public boolean getMatchFlag() { 182 return matchCheck.isSelected(); 183 } 184 185 191 public void setMatchFlag(boolean flag) { 192 matchCheck.setSelected(flag); 193 } 194 195 public boolean getWrapFlag() { 196 return wrapCheck.isSelected(); 197 } 198 199 public void setWrapFlag(boolean flag) { 200 wrapCheck.setSelected(flag); 201 } 202 203 public boolean getBackwardsFlag() { 204 return backCheck.isSelected(); 205 } 206 207 public void setBackwardsFlag(boolean flag) { 208 backCheck.setSelected(flag); 209 } 210 211 private Pattern getPattern() { 212 String searchString = findText.getText(); 213 if (searchString.length() == 0) { 214 return null; 215 } 216 if (pattern == null || !pattern.pattern().equals(searchString)) { 217 pattern = Pattern.compile(searchString, 220 getMatchFlag() ? 0 : Pattern.CASE_INSENSITIVE); 221 lastIndex = -1; 223 } 224 return pattern; 225 } 226 227 private class FindAction extends AbstractAction { 228 public FindAction() { 229 super("Find"); 230 } 231 public void actionPerformed(ActionEvent evt) { 232 doFind(); 233 } 234 } 235 236 private class CloseAction extends AbstractAction { 237 public CloseAction() { 238 super("Close"); 239 } 240 241 public void actionPerformed(ActionEvent evt) { 242 if (DEBUG) { 243 System.err.println(this.getValue(Action.NAME)); 244 } 245 doClose(); 246 } 247 } 248 249 private abstract class CheckAction extends AbstractAction { 250 251 public CheckAction(String name) { 252 super(name); 253 } 254 255 public void actionPerformed(ActionEvent evt) { 256 if (DEBUG) { 257 System.err.println(this.getValue(Action.NAME)); 258 } 259 } 260 } 261 262 private class MatchAction extends CheckAction { 263 public MatchAction() { 264 super("Match upper/lower case"); 265 putValue(Action.MNEMONIC_KEY, new Integer ('M')); 266 } 267 } 268 269 private class WrapAction extends CheckAction { 270 public WrapAction() { 271 super("Wrap around"); 272 putValue(Action.MNEMONIC_KEY, new Integer ('W')); 273 } 274 } 275 276 private class BackwardAction extends CheckAction { 277 public BackwardAction() { 278 super("Search Backwards"); 279 putValue(Action.MNEMONIC_KEY, new Integer ('B')); 280 } 281 } 282 283 } 284 | Popular Tags |