| 264 | } |
| 265 | |
| 266 | public void search() { |
| 267 | |
| 268 | |
| 269 | String s = entry.getText().toLowerCase(); |
| 270 | if (!s.equalsIgnoreCase(oldEntry)) |
| 271 | { |
| 272 | hilit.removeAllHighlights(); |
| 273 | } |
| 274 | |
| 275 | if (s.length() <= 0) { |
| 276 | message("Nothing to search"); |
| 277 | hilit.removeAllHighlights(); |
| 278 | oldEntry = ""; |
| 279 | return; |
| 280 | } |
| 281 | oldEntry = s; |
| 282 | String content = null; |
| 283 | try { |
| 284 | Document d = textArea.getDocument(); |
| 285 | content = d.getText(0, d.getLength()).toLowerCase(); |
| 286 | } catch (BadLocationException e) {} |
| 287 | |
| 288 | int index = content.indexOf(s, pos); |
| 289 | hilit.removeAllHighlights(); |
| 290 | |
| 291 | if (index >= 0) { // match found |
| 292 | try { |
| 293 | end = index + s.length(); |
| 294 | hilit.addHighlight(index, end, painter2); |
| 295 | textArea.setCaretPosition(end); |
| 296 | entry.setBackground(entryBg); |
| 297 | message("'" + s + "' found. Press ESC to end search"); |
| 298 | } catch (BadLocationException e) { |
| 299 | e.printStackTrace(); |
| 300 | } |
| 301 | } else { |
| 302 | entry.setBackground(ERROR_COLOR); |
| 303 | IJ.beep(); |
| 304 | message("'" + s + "' not found. Press ESC to start a new search"); |
| 305 | pos = 0; |
| 306 | } |
| 307 | |
| 308 | int lastIndex = 0; |
| 309 | int firstOffset = -1; |
| 310 | int wordSize = s.length(); |
| 311 | while ((lastIndex = content.indexOf(s, lastIndex)) != -1) { |
| 312 | int endIndex = lastIndex + wordSize; |
| 313 | try { |
| 314 | hilit.addHighlight(lastIndex, endIndex, painter); |
| 315 | } catch (BadLocationException e) {} |
| 316 | if (firstOffset == -1) { |
| 317 | firstOffset = lastIndex; |
| 318 | } |
| 319 | lastIndex = endIndex; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | void message(String msg) { |