001    /*
002      Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopsys.com>, 
003                              Laurent Martelli <laurent@aopsys.com>
004      
005      This program is free software; you can redistribute it and/or modify
006      it under the terms of the GNU Lesser General Public License as
007      published by the Free Software Foundation; either version 2 of the
008      License, or (at your option) any later version.
009    
010      This program is distributed in the hope that it will be useful, but
011      WITHOUT ANY WARRANTY; without even the implied warranty of
012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013      Lesser General Public License for more details.
014    
015      You should have received a copy of the GNU Lesser General Public
016      License along with this program; if not, write to the Free Software
017      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
018      USA */
019    
020    package org.objectweb.jac.aspects.gui.swing;
021    
022    import java.awt.event.KeyEvent;
023    import java.awt.event.KeyListener;
024    
025    /**
026     * A search tool for the SHEditor. 
027     */
028    public class SearchTool implements KeyListener {
029    
030        SHEditor editor;
031        int savedPosition;
032        int searchFrom;
033     
034        /**
035         * @param editor search in this editor's text
036         * @param searchFrom position to start searching from
037         */
038        public SearchTool(SHEditor editor, int searchFrom) {
039            this.editor = editor;
040            this.searchFrom = searchFrom;
041            this.savedPosition = editor.getCaretPosition();
042        }
043    
044        /** The text to find */
045        String searchedText = "";
046    
047        /**
048         * Finds the next occurrence 
049         */
050        protected void searchNext() {
051            int found = editor.getText().indexOf(searchedText,searchFrom+1);
052            if (found!=-1) {
053                found(found);
054            }
055        }
056    
057        /**
058         * Finds an occurence
059         */
060        protected void search() {
061            int found = editor.getText().indexOf(searchedText,searchFrom);
062            if (found!=-1) {
063                found(found);
064            }
065        }
066    
067        /**
068         * Repaint editor when an occurrence is found
069         */
070        protected void found(int found) {
071            searchFrom = found;
072            editor.setSelection(found,found+searchedText.length());
073            editor.selectionVisible();
074            editor.repaint();
075        }
076    
077        /**
078         * Quit search tool, sets the position at the beginning of the
079         * found occurrence.
080         */
081        protected void done() {
082            editor.setCaretPosition(searchFrom);
083            editor.toolDone();
084        }
085    
086        /**
087         * Quit search tool, reset position to saved one.
088         */
089        protected void abort() {
090            editor.setCaretPosition(savedPosition);
091            editor.resetSelection();
092            editor.toolDone();
093        }
094    
095        public void keyPressed(KeyEvent e)
096        {
097            if (e.isControlDown()) {
098                switch (e.getKeyCode()) {
099                    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                // 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 e)
176        {
177        }
178    
179    }