KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > SearchTool


1 /*
2   Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopsys.com>,
3                           Laurent Martelli <laurent@aopsys.com>
4   
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU Lesser General Public License as
7   published by the Free Software Foundation; either version 2 of the
8   License, or (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18   USA */

19
20 package org.objectweb.jac.aspects.gui.swing;
21
22 import java.awt.event.KeyEvent JavaDoc;
23 import java.awt.event.KeyListener JavaDoc;
24
25 /**
26  * A search tool for the SHEditor.
27  */

28 public class SearchTool implements KeyListener JavaDoc {
29
30     SHEditor editor;
31     int savedPosition;
32     int searchFrom;
33  
34     /**
35      * @param editor search in this editor's text
36      * @param searchFrom position to start searching from
37      */

38     public SearchTool(SHEditor editor, int searchFrom) {
39         this.editor = editor;
40         this.searchFrom = searchFrom;
41         this.savedPosition = editor.getCaretPosition();
42     }
43
44     /** The text to find */
45     String JavaDoc searchedText = "";
46
47     /**
48      * Finds the next occurrence
49      */

50     protected void searchNext() {
51         int found = editor.getText().indexOf(searchedText,searchFrom+1);
52         if (found!=-1) {
53             found(found);
54         }
55     }
56
57     /**
58      * Finds an occurence
59      */

60     protected void search() {
61         int found = editor.getText().indexOf(searchedText,searchFrom);
62         if (found!=-1) {
63             found(found);
64         }
65     }
66
67     /**
68      * Repaint editor when an occurrence is found
69      */

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     /**
78      * Quit search tool, sets the position at the beginning of the
79      * found occurrence.
80      */

81     protected void done() {
82         editor.setCaretPosition(searchFrom);
83         editor.toolDone();
84     }
85
86     /**
87      * Quit search tool, reset position to saved one.
88      */

89     protected void abort() {
90         editor.setCaretPosition(savedPosition);
91         editor.resetSelection();
92         editor.toolDone();
93     }
94
95     public void keyPressed(KeyEvent JavaDoc 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 JavaDoc e)
145     {
146         if (isFirst) {
147             // Skip first event (it's the Ctrl+S that triggered the search tool)
148
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 JavaDoc e)
176     {
177     }
178
179 }
180
Popular Tags