1 19 20 package org.objectweb.jac.aspects.gui.swing; 21 22 import java.awt.event.KeyEvent ; 23 import java.awt.event.KeyListener ; 24 25 28 public class SearchTool implements KeyListener { 29 30 SHEditor editor; 31 int savedPosition; 32 int searchFrom; 33 34 38 public SearchTool(SHEditor editor, int searchFrom) { 39 this.editor = editor; 40 this.searchFrom = searchFrom; 41 this.savedPosition = editor.getCaretPosition(); 42 } 43 44 45 String searchedText = ""; 46 47 50 protected void searchNext() { 51 int found = editor.getText().indexOf(searchedText,searchFrom+1); 52 if (found!=-1) { 53 found(found); 54 } 55 } 56 57 60 protected void search() { 61 int found = editor.getText().indexOf(searchedText,searchFrom); 62 if (found!=-1) { 63 found(found); 64 } 65 } 66 67 70 protected void found(int found) { 71 searchFrom = found; 72 editor.setSelection(found,found+searchedText.length()); 73 editor.selectionVisible(); 74 editor.repaint(); 75 } 76 77 81 protected void done() { 82 editor.setCaretPosition(searchFrom); 83 editor.toolDone(); 84 } 85 86 89 protected void abort() { 90 editor.setCaretPosition(savedPosition); 91 editor.resetSelection(); 92 editor.toolDone(); 93 } 94 95 public void keyPressed(KeyEvent e) 96 { 97 if (e.isControlDown()) { 98 switch (e.getKeyCode()) { 99 case KeyEvent.VK_S: 100 searchNext(); 101 e.consume(); 102 break; 103 case KeyEvent.VK_ESCAPE: 104 e.consume(); 105 abort(); 106 break; 107 case KeyEvent.VK_LEFT: 108 case KeyEvent.VK_RIGHT: 109 case KeyEvent.VK_UP: 110 case KeyEvent.VK_DOWN: 111 case KeyEvent.VK_END: 112 case KeyEvent.VK_HOME: 113 e.consume(); 114 done(); 115 break; 116 default: 117 e.consume(); 118 return; 119 } 120 } else { 121 switch (e.getKeyCode()) { 122 case KeyEvent.VK_ENTER: 123 e.consume(); 124 break; 125 case KeyEvent.VK_ESCAPE: 126 e.consume(); 127 abort(); 128 break; 129 case KeyEvent.VK_LEFT: 130 case KeyEvent.VK_RIGHT: 131 case KeyEvent.VK_UP: 132 case KeyEvent.VK_DOWN: 133 case KeyEvent.VK_END: 134 case KeyEvent.VK_HOME: 135 e.consume(); 136 done(); 137 break; 138 default: 139 } 140 } 141 } 142 143 boolean isFirst = true; 144 public void keyTyped(KeyEvent e) 145 { 146 if (isFirst) { 147 isFirst = false; 149 return; 150 } 151 if (e.isControlDown()) { 152 switch (e.getKeyCode()) { 153 case KeyEvent.VK_S: 154 searchNext(); 155 e.consume(); 156 break; 157 default: 158 e.consume(); 159 return; 160 } 161 } else { 162 switch (e.getKeyChar()) { 163 case KeyEvent.VK_ENTER: 164 e.consume(); 165 done(); 166 break; 167 default: 168 searchedText += e.getKeyChar(); 169 search(); 170 e.consume(); 171 } 172 } 173 } 174 175 public void keyReleased(KeyEvent e) 176 { 177 } 178 179 } 180 | Popular Tags |