KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jellytools > modules > editor > Find


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * Find.java
22  *
23  * Created on 1/17/05 5:08 PM
24  */

25 package org.netbeans.jellytools.modules.editor;
26
27 import java.awt.Robot JavaDoc;
28 import java.awt.event.InputEvent JavaDoc;
29 import java.awt.event.KeyEvent JavaDoc;
30 import org.netbeans.jemmy.operators.*;
31
32
33 /** Class implementing all necessary methods for handling "Find" NbDialog.
34  *
35  * @author rs155161
36  * @version 1.0
37  */

38 public class Find extends JDialogOperator {
39
40     /** Creates new Find that can handle it.
41      */

42     public Find() {
43         super(java.util.ResourceBundle.getBundle("org.netbeans.editor.Bundle").getString("find-title"));
44     }
45
46     private JLabelOperator _lblFindWhat;
47     private JComboBoxOperator _cboFindWhat;
48     private JCheckBoxOperator _cbHighlightSearch;
49     private JCheckBoxOperator _cbIncrementalSearch;
50     private JCheckBoxOperator _cbMatchCase;
51     //private JCheckBoxOperator _cbSmartCase;
52
private JCheckBoxOperator _cbMatchWholeWordsOnly;
53     private JCheckBoxOperator _cbBackwardSearch;
54     private JCheckBoxOperator _cbWrapSearch;
55     private JCheckBoxOperator _cbRegularExpressions;
56     private JCheckBoxOperator _cbBlockSearch;
57     private JButtonOperator _btFind;
58     private JButtonOperator _btClose;
59     private JButtonOperator _btHelp;
60
61
62     //******************************
63
// Subcomponents definition part
64
//******************************
65

66     /** Tries to find "Find What:" JLabel in this dialog.
67      * @return JLabelOperator
68      */

69     public JLabelOperator lblFindWhat() {
70         if (_lblFindWhat==null) {
71             _lblFindWhat = new JLabelOperator(this, java.util.ResourceBundle.getBundle("org.netbeans.editor.Bundle").getString("find-what"));
72         }
73         return _lblFindWhat;
74     }
75
76     /** Tries to find null JComboBox in this dialog.
77      * @return JComboBoxOperator
78      */

79     public JComboBoxOperator cboFindWhat() {
80         if (_cboFindWhat==null) {
81             _cboFindWhat = new JComboBoxOperator(this);
82         }
83         return _cboFindWhat;
84     }
85
86     /** Tries to find " Highlight Search" JCheckBox in this dialog.
87      * @return JCheckBoxOperator
88      */

89     public JCheckBoxOperator cbHighlightSearch() {
90         if (_cbHighlightSearch==null) {
91             _cbHighlightSearch = new JCheckBoxOperator(this, java.util.ResourceBundle.getBundle("org.netbeans.editor.Bundle").getString("find-highlight-search"));
92         }
93         return _cbHighlightSearch;
94     }
95
96     /** Tries to find " Incremental Search" JCheckBox in this dialog.
97      * @return JCheckBoxOperator
98      */

99     public JCheckBoxOperator cbIncrementalSearch() {
100         if (_cbIncrementalSearch==null) {
101             _cbIncrementalSearch = new JCheckBoxOperator(this, java.util.ResourceBundle.getBundle("org.netbeans.editor.Bundle").getString("find-inc-search"));
102         }
103         return _cbIncrementalSearch;
104     }
105
106     /** Tries to find " Match Case" JCheckBox in this dialog.
107      * @return JCheckBoxOperator
108      */

109     public JCheckBoxOperator cbMatchCase() {
110         if (_cbMatchCase==null) {
111             _cbMatchCase = new JCheckBoxOperator(this, java.util.ResourceBundle.getBundle("org.netbeans.editor.Bundle").getString("find-match-case"));
112         }
113         return _cbMatchCase;
114     }
115
116     /** Tries to find " Smart Case" JCheckBox in this dialog.
117      * @return JCheckBoxOperator
118      */

119     /*public JCheckBoxOperator cbSmartCase() {
120         if (_cbSmartCase==null) {
121             _cbSmartCase = new JCheckBoxOperator(this, java.util.ResourceBundle.getBundle("org.netbeans.editor.Bundle").getString("find-smart-case"));
122         }
123         return _cbSmartCase;
124     }*/

125
126     /** Tries to find " Match Whole Words Only" JCheckBox in this dialog.
127      * @return JCheckBoxOperator
128      */

129     public JCheckBoxOperator cbMatchWholeWordsOnly() {
130         if (_cbMatchWholeWordsOnly==null) {
131             _cbMatchWholeWordsOnly = new JCheckBoxOperator(this, java.util.ResourceBundle.getBundle("org.netbeans.editor.Bundle").getString("find-whole-words"));
132         }
133         return _cbMatchWholeWordsOnly;
134     }
135
136     /** Tries to find " Backward Search" JCheckBox in this dialog.
137      * @return JCheckBoxOperator
138      */

139     public JCheckBoxOperator cbBackwardSearch() {
140         if (_cbBackwardSearch==null) {
141             _cbBackwardSearch = new JCheckBoxOperator(this, java.util.ResourceBundle.getBundle("org.netbeans.editor.Bundle").getString("find-backward-search"));
142         }
143         return _cbBackwardSearch;
144     }
145
146     /** Tries to find " Wrap Search" JCheckBox in this dialog.
147      * @return JCheckBoxOperator
148      */

149     public JCheckBoxOperator cbWrapSearch() {
150         if (_cbWrapSearch==null) {
151             _cbWrapSearch = new JCheckBoxOperator(this, java.util.ResourceBundle.getBundle("org.netbeans.editor.Bundle").getString("find-wrap-search"));
152         }
153         return _cbWrapSearch;
154     }
155
156     /** Tries to find " Regular Expressions" JCheckBox in this dialog.
157      * @return JCheckBoxOperator
158      */

159     public JCheckBoxOperator cbRegularExpressions() {
160         if (_cbRegularExpressions==null) {
161             _cbRegularExpressions = new JCheckBoxOperator(this, java.util.ResourceBundle.getBundle("org.netbeans.editor.Bundle").getString("find-reg-exp"));
162         }
163         return _cbRegularExpressions;
164     }
165
166     /** Tries to find " Block Search" JCheckBox in this dialog.
167      * @return JCheckBoxOperator
168      */

169     public JCheckBoxOperator cbBlockSearch() {
170         if (_cbBlockSearch==null) {
171             _cbBlockSearch = new JCheckBoxOperator(this, java.util.ResourceBundle.getBundle("org.netbeans.editor.Bundle").getString("find-block-search"));
172         }
173         return _cbBlockSearch;
174     }
175
176     /** Tries to find "Find" JButton in this dialog.
177      * @return JButtonOperator
178      */

179     public JButtonOperator btFind() {
180         if (_btFind==null) {
181             _btFind = new JButtonOperator(this, java.util.ResourceBundle.getBundle("org.netbeans.editor.Bundle").getString("find-button-find"));
182         }
183         return _btFind;
184     }
185
186     /** Tries to find "Close" JButton in this dialog.
187      * @return JButtonOperator
188      */

189     public JButtonOperator btClose() {
190         if (_btClose==null) {
191             _btClose = new JButtonOperator(this, java.util.ResourceBundle.getBundle("org.netbeans.editor.Bundle").getString("find-button-cancel"));
192         }
193         return _btClose;
194     }
195
196     /** Tries to find "Help" JButton in this dialog.
197      * @return JButtonOperator
198      */

199     public JButtonOperator btHelp() {
200         if (_btHelp==null) {
201             _btHelp = new JButtonOperator(this, java.util.ResourceBundle.getBundle("org.openide.explorer.propertysheet.Bundle").getString("CTL_Help"));
202         }
203         return _btHelp;
204     }
205
206
207     //****************************************
208
// Low-level functionality definition part
209
//****************************************
210

211     /** returns selected item for cboFindWhat
212      * @return String item
213      */

214     public String JavaDoc getSelectedFindWhat() {
215         return cboFindWhat().getSelectedItem().toString();
216     }
217
218     /** selects item for cboFindWhat
219      * @param item String item
220      */

221     public void selectFindWhat(String JavaDoc item) {
222         cboFindWhat().selectItem(item);
223     }
224
225     /** types text for cboFindWhat
226      * @param text String text
227      */

228     public void typeFindWhat(String JavaDoc text) {
229         cboFindWhat().typeText(text);
230     }
231
232     /** checks or unchecks given JCheckBox
233      * @param state boolean requested state
234      */

235     public void checkHighlightSearch(boolean state) {
236         if (cbHighlightSearch().isSelected()!=state) {
237             cbHighlightSearch().push();
238         }
239     }
240
241     /** checks or unchecks given JCheckBox
242      * @param state boolean requested state
243      */

244     public void checkIncrementalSearch(boolean state) {
245         if (cbIncrementalSearch().isSelected()!=state) {
246             cbIncrementalSearch().push();
247         }
248     }
249
250     /** checks or unchecks given JCheckBox
251      * @param state boolean requested state
252      */

253     public void checkMatchCase(boolean state) {
254         if (cbMatchCase().isSelected()!=state) {
255             cbMatchCase().push();
256         }
257     }
258
259     /** checks or unchecks given JCheckBox
260      * @param state boolean requested state
261      */

262     /*public void checkSmartCase(boolean state) {
263         if (cbSmartCase().isSelected()!=state) {
264             cbSmartCase().push();
265         }
266     }*/

267
268     /** checks or unchecks given JCheckBox
269      * @param state boolean requested state
270      */

271     public void checkMatchWholeWordsOnly(boolean state) {
272         if (cbMatchWholeWordsOnly().isSelected()!=state) {
273             cbMatchWholeWordsOnly().push();
274         }
275     }
276
277     /** checks or unchecks given JCheckBox
278      * @param state boolean requested state
279      */

280     public void checkBackwardSearch(boolean state) {
281         if (cbBackwardSearch().isSelected()!=state) {
282             cbBackwardSearch().push();
283         }
284     }
285
286     /** checks or unchecks given JCheckBox
287      * @param state boolean requested state
288      */

289     public void checkWrapSearch(boolean state) {
290         if (cbWrapSearch().isSelected()!=state) {
291             cbWrapSearch().push();
292         }
293     }
294
295     /** checks or unchecks given JCheckBox
296      * @param state boolean requested state
297      */

298     public void checkRegularExpressions(boolean state) {
299         if (cbRegularExpressions().isSelected()!=state) {
300             cbRegularExpressions().push();
301         }
302     }
303
304     /** checks or unchecks given JCheckBox
305      * @param state boolean requested state
306      */

307     public void checkBlockSearch(boolean state) {
308         if (cbBlockSearch().isSelected()!=state) {
309             cbBlockSearch().push();
310         }
311     }
312
313     /** clicks on "Find" JButton
314      */

315     public void find() {
316         btFind().push();
317     }
318
319     /** clicks on "Close" JButton
320      */

321     public void close() {
322         btClose().push();
323     }
324
325     /** clicks on "Help" JButton
326      */

327     public void help() {
328         btHelp().push();
329     }
330
331
332     //*****************************************
333
// High-level functionality definition part
334
//*****************************************
335

336     /** Performs verification of Find by accessing all its components.
337      */

338     public void verify() {
339         lblFindWhat();
340         cboFindWhat();
341         cbHighlightSearch();
342         cbIncrementalSearch();
343         cbMatchCase();
344         //cbSmartCase();
345
cbMatchWholeWordsOnly();
346         cbBackwardSearch();
347         cbWrapSearch();
348         cbRegularExpressions();
349         cbBlockSearch();
350         btFind();
351         btClose();
352         btHelp();
353     }
354
355     public static void find(String JavaDoc text) {
356         Find op=new Find();
357         op.typeFindWhat(text);
358         op.find();
359     }
360     
361     public static void find(String JavaDoc text,Robot JavaDoc robot) {
362         Find op=new Find();
363         robot.waitForIdle();
364         java.awt.Point JavaDoc p=op.cboFindWhat().getLocationOnScreen();
365         int x=p.x+op.cboFindWhat().getWidth()/2;
366         int y=p.y+op.cboFindWhat().getHeight()/2;
367         robot.mouseMove(x,y);
368         robot.mousePress(InputEvent.BUTTON1_MASK);
369         robot.delay(50);
370         robot.mouseRelease(InputEvent.BUTTON1_MASK);
371         char c;
372         boolean upper;
373         for (int i=0;i < text.length();i++) {
374             c=text.charAt(i);
375             if (Character.isUpperCase(c)) {
376                 upper=true;
377             } else {
378                 upper=false;
379                 c=(char)(Character.toUpperCase((char)c));
380             }
381             if (upper) {
382                 robot.keyPress(KeyEvent.VK_SHIFT);
383             }
384             robot.keyPress((int)c);
385             robot.delay(5);
386             robot.keyRelease((int)c);
387             robot.delay(5);
388             if (upper) {
389                 robot.keyRelease(KeyEvent.VK_SHIFT);
390             }
391         }
392         p=op.btFind().getLocationOnScreen();
393         x=p.x+op.btFind().getWidth()/2;
394         y=p.y+op.btFind().getHeight()/2;
395         robot.mouseMove(x,y);
396         robot.mousePress(InputEvent.BUTTON1_MASK);
397         robot.delay(50);
398         robot.mouseRelease(InputEvent.BUTTON1_MASK);
399         robot.delay(50);
400         robot.waitForIdle();
401     }
402     
403     
404     /** Performs simple test of Find
405      * @param args the command line arguments
406      */

407     public static void main(String JavaDoc args[]) {
408         new Find().verify();
409         System.out.println("Find verification finished.");
410         try {
411             Robot JavaDoc robot=new Robot JavaDoc();
412             System.out.println("Start delay.");
413             robot.delay(2000);
414             System.out.println("Delayed");
415             robot.waitForIdle();
416             robot.keyPress(KeyEvent.VK_CONTROL);
417             robot.delay(1);
418             robot.keyPress(KeyEvent.VK_F);
419             robot.delay(20);
420             robot.keyRelease(KeyEvent.VK_F);
421             robot.delay(1);
422             robot.keyRelease(KeyEvent.VK_CONTROL);
423             System.out.println("1");
424             robot.waitForIdle();
425             System.out.println("2");
426             Find.find("aaa",robot);
427             System.out.println("3");
428             
429         } catch (Exception JavaDoc ex) {
430             ex.printStackTrace();
431         }
432     }
433 }
434
435
Popular Tags