1 package snow.text; 2 3 import snow.utils.gui.*; 4 import javax.swing.*; 5 import javax.swing.event.*; 6 import javax.swing.text.*; 7 import java.awt.event.*; 8 import java.util.*; 9 import java.awt.*; 10 11 15 public class SearchPanel extends JPanel 16 { 17 final private JTextField searchTF = new JTextField(12); 18 19 final private JButton searchPrevious = new JButton(new Icons.UpIcon(10,10, true)); 20 final private JButton searchNext = new JButton(new Icons.DownIcon(10,10, true)); 21 22 final private JButton close = new JButton(new Icons.CrossIcon(10,10, true)); 23 final private JCheckBox searchFromLine = new JCheckBox("search from actual position", false); 24 final private JCheckBox ignoreCase = new JCheckBox("ignore case", true); 25 final private JCheckBox backward = new JCheckBox("backward", false); 26 27 final private JCheckBox viewAll = new JCheckBox("all", false); 29 final private JCheckBox replace = new JCheckBox("replace", false); 31 32 final private SimpleDocument doc; 33 final private JTextPane textPane; 34 35 36 private int actualPosition = -1; private String actualSearch = ""; 38 39 private Object actualHighlightTag = null; 41 42 43 public SearchPanel(final JTextPane textPane, SimpleDocument doc) 44 { 45 super(new FlowLayout(FlowLayout.LEFT, 5, 1)); 46 this.setBorder(null); 47 48 this.doc = doc; 49 this.textPane = textPane; 50 51 add(close); 52 53 add(new JLabel("Search: ")); 54 add(searchTF); 55 searchNext.setMargin(new Insets(0,0,0,0)); 56 add(searchNext); 57 searchNext.setToolTipText("Search next"); 58 searchPrevious.setMargin(new Insets(0,0,0,0)); 59 add(searchPrevious); 60 searchPrevious.setToolTipText("Search previous"); 61 add(ignoreCase); 62 add(searchFromLine); 63 add(backward); 64 66 searchTF.addKeyListener(new KeyAdapter() 67 { 68 @Override 69 public void keyReleased(KeyEvent ke) 70 { 71 if(ke.getKeyCode() == KeyEvent.VK_ENTER) 72 { 73 searchNext(); 75 } 76 else 77 { 78 actualPosition = -1; 80 searchNext(); 81 } 82 } 83 }); 84 85 textPane.registerKeyboardAction(new ActionListener() 86 { 87 public void actionPerformed(ActionEvent ae) 88 { 89 CTRL_F_pressed(); 90 } 91 }, "Search", KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.CTRL_MASK, true), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 92 93 textPane.registerKeyboardAction(new ActionListener() 94 { 95 public void actionPerformed(ActionEvent ae) 96 { 97 searchNext(); 98 } 99 }, "SearchNext", KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0, true), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 100 101 102 close.setMargin(new Insets(0,0,0,0)); 103 close.setFocusPainted(false); 104 105 close.addActionListener(new ActionListener() 106 { 107 public void actionPerformed(ActionEvent ae) 108 { 109 setVisible(false); 110 textPane.requestFocus(); 111 } 112 }); 113 114 searchNext.addActionListener(new ActionListener() 115 { 116 public void actionPerformed(ActionEvent ae) 117 { 118 searchNext(true); 119 } 120 }); 121 122 searchPrevious.addActionListener(new ActionListener() 123 { 124 public void actionPerformed(ActionEvent ae) 125 { 126 searchNext(false); 127 } 128 }); 129 130 setVisible(false); 132 } 134 136 public void setSearchPositionStart(int pos) 137 { 138 actualPosition = pos; 139 } 140 141 142 public void CTRL_F_pressed() 143 { 144 if(!this.isVisible()) 146 { 147 this.setVisible(true); 148 } 149 searchTF.selectAll(); 150 searchTF.requestFocus(); 151 } 152 153 154 public void searchNext() 155 { 156 searchNext( !this.backward.isSelected() ); 157 } 158 159 161 public void searchNext(boolean forward) 162 { 163 if(!this.isVisible()) 165 { 166 this.setVisible(true); 167 return; 168 } 169 searchTF.requestFocus(); 170 171 172 if(actualHighlightTag!=null) 174 { 175 textPane.getHighlighter().removeHighlight(actualHighlightTag); 176 } 177 178 179 this.actualSearch = searchTF.getText(); 180 if(ignoreCase.isSelected()) actualSearch = actualSearch.toUpperCase(); 181 if(actualSearch.length()==0) return; 183 184 int incr = 1; 185 if(!forward) incr = -1; 186 if(this.searchFromLine.isSelected()) 187 { 188 actualPosition = search(actualSearch, textPane.getCaretPosition() + incr, !forward); 189 } 190 else 191 { 192 actualPosition = search(actualSearch, this.actualPosition + incr, !forward); 193 } 194 195 if(actualPosition>=0) 196 { 197 searchTF.setBackground(UIManager.getColor("TextField.background")); 198 201 try 202 { 203 actualHighlightTag = textPane.getHighlighter().addHighlight( 204 actualPosition, actualPosition+actualSearch.length(), doc.searchHighlighter); 205 textPane.setCaretPosition(actualPosition); 207 textPane.scrollRectToVisible( textPane.modelToView(actualPosition) ); 210 } catch(Exception e) { e.printStackTrace(); } 211 } 212 else 213 { 214 searchTF.setBackground(Color.red); 216 } 217 } 218 219 220 private int search(String str, int from, boolean backward) 221 { 222 if(backward) 223 { 224 return doc.searchBackward(str, from, ignoreCase.isSelected()); 225 } 226 else 227 { 228 return doc.search(str, from, ignoreCase.isSelected()); 229 } 230 } 231 232 234 public void searchNext_calledFromPopup(String str, int from) 235 { 236 this.actualSearch = str; 237 this.searchTF.setText(str); 238 this.backward.setSelected(false); 239 240 if(this.ignoreCase.isSelected()) str = str.toUpperCase(); 241 if(actualHighlightTag!=null) 243 { 244 textPane.getHighlighter().removeHighlight(actualHighlightTag); 245 } 246 247 int pos = doc.search(str, from, ignoreCase.isSelected()); 248 if(pos>=0) 249 { 250 try 251 { 252 actualHighlightTag = textPane.getHighlighter().addHighlight( 253 pos, pos+str.length(), doc.searchHighlighter); 254 textPane.setCaretPosition(pos); 256 textPane.scrollRectToVisible( textPane.modelToView(pos) ); 258 } catch(Exception e) { e.printStackTrace(); } 259 260 } 261 else 262 { 263 System.out.println(str+" not found from "+from); 264 } 265 } 266 267 269 public void searchPrevious_calledFromPopup(String str, int from) 270 { 271 this.searchTF.setText(str); 272 this.backward.setSelected(true); 273 274 if(this.ignoreCase.isSelected()) str = str.toUpperCase(); 275 if(actualHighlightTag!=null) 277 { 278 textPane.getHighlighter().removeHighlight(actualHighlightTag); 279 } 280 281 int pos = doc.searchBackward(str, from, ignoreCase.isSelected()); 282 if(pos>=0) 283 { 284 try 285 { 286 actualHighlightTag = textPane.getHighlighter().addHighlight( 287 pos, pos+str.length(), doc.searchHighlighter); 288 textPane.setCaretPosition(pos); 290 textPane.scrollRectToVisible( textPane.modelToView(pos) ); 292 } catch(Exception e) { e.printStackTrace(); } 293 294 } 295 else 296 { 297 System.out.println(str+" not found backward from "+from); 298 } 299 } 300 301 302 303 304 } | Popular Tags |