KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Abbreviations.java
22  *
23  * Created on 1/2/03 4:04 PM
24  */

25 package org.netbeans.jellytools.modules.editor;
26
27 import java.util.*;
28 import javax.swing.table.TableModel JavaDoc;
29 import org.netbeans.jellytools.OptionsOperator;
30 import org.netbeans.jellytools.properties.Property;
31 import org.netbeans.jellytools.properties.PropertySheetOperator;
32 import org.netbeans.jemmy.EventTool;
33 import org.netbeans.jemmy.operators.*;
34
35 /**
36  * Class implementing all necessary methods for handling "Abbreviations" NbDialog.
37  *
38  * @author Jan Lahoda
39  * @author Max Sauer
40  * @version 1.1
41  */

42 public class Abbreviations extends JDialogOperator {
43     
44     /** Creates new Abbreviations that can handle it.
45      */

46     public Abbreviations() {
47         super(java.util.ResourceBundle.getBundle("org/netbeans/modules/editor/options/Bundle").getString("PROP_Abbreviations"));
48     }
49     
50     private JTableOperator _tabAbbreviations;
51     private JButtonOperator _btMetalScrollButton;
52     private JButtonOperator _btMetalScrollButton2;
53     private JButtonOperator _btAdd;
54     private JButtonOperator _btEdit;
55     private JButtonOperator _btRemove;
56     private JButtonOperator _btOK;
57     private JButtonOperator _btCancel;
58     private JButtonOperator _btHelp;
59     
60     
61     //******************************
62
// Subcomponents definition part
63
//******************************
64

65     /** Tries to find null JTable in this dialog.
66      * @return JTableOperator
67      */

68     public JTableOperator tabAbbreviations() {
69         if (_tabAbbreviations==null) {
70             _tabAbbreviations = new JTableOperator(this);
71         }
72         return _tabAbbreviations;
73     }
74     
75     /** Tries to find "" MetalScrollButton in this dialog.
76      * @return JButtonOperator
77      */

78     public JButtonOperator btMetalScrollButton() {
79         if (_btMetalScrollButton==null) {
80             _btMetalScrollButton = new JButtonOperator(this, "");
81         }
82         return _btMetalScrollButton;
83     }
84     
85     /** Tries to find "" MetalScrollButton in this dialog.
86      * @return JButtonOperator
87      */

88     public JButtonOperator btMetalScrollButton2() {
89         if (_btMetalScrollButton2==null) {
90             _btMetalScrollButton2 = new JButtonOperator(this, "", 1);
91         }
92         return _btMetalScrollButton2;
93     }
94     
95     /** Tries to find "Add..." JButton in this dialog.
96      * @return JButtonOperator
97      */

98     public JButtonOperator btAdd() {
99         if (_btAdd==null) {
100             _btAdd = new JButtonOperator(this, java.util.ResourceBundle.getBundle("org.netbeans.modules.editor.options.Bundle").getString("KBEP_Add"));
101         }
102         return _btAdd;
103     }
104     
105     /** Tries to find "Edit..." JButton in this dialog.
106      * @return JButtonOperator
107      */

108     public JButtonOperator btEdit() {
109         if (_btEdit==null) {
110             _btEdit = new JButtonOperator(this, java.util.ResourceBundle.getBundle("org/netbeans/modules/editor/options/Bundle").getString("AEP_Edit"));
111         }
112         return _btEdit;
113     }
114     
115     /** Tries to find "Remove" JButton in this dialog.
116      * @return JButtonOperator
117      */

118     public JButtonOperator btRemove() {
119         if (_btRemove==null) {
120             _btRemove = new JButtonOperator(this, java.util.ResourceBundle.getBundle("org/netbeans/modules/editor/options/Bundle").getString("Remove"));
121         }
122         return _btRemove;
123     }
124     
125     /** Tries to find "OK" JButton in this dialog.
126      * @return JButtonOperator
127      */

128     public JButtonOperator btOK() {
129         if (_btOK==null) {
130             _btOK = new JButtonOperator(this, java.util.ResourceBundle.getBundle("org/netbeans/modules/editor/options/Bundle").getString("KBEP_OK_LABEL"));
131         }
132         return _btOK;
133     }
134     
135     /** Tries to find "Cancel" JButton in this dialog.
136      * @return JButtonOperator
137      */

138     public JButtonOperator btCancel() {
139         if (_btCancel==null) {
140             _btCancel = new JButtonOperator(this, java.util.ResourceBundle.getBundle("org.openide.explorer.propertysheet.Bundle").getString("CTL_Cancel"));
141         }
142         return _btCancel;
143     }
144     
145     /** Tries to find "Help" JButton in this dialog.
146      * @return JButtonOperator
147      */

148     public JButtonOperator btHelp() {
149         if (_btHelp==null) {
150             _btHelp = new JButtonOperator(this, java.util.ResourceBundle.getBundle("org.openide.explorer.propertysheet.Bundle").getString("CTL_Help"));
151         }
152         return _btHelp;
153     }
154     
155     
156     //****************************************
157
// Low-level functionality definition part
158
//****************************************
159

160     /** clicks on "" MetalScrollButton
161      */

162     public void metalScrollButton() {
163         btMetalScrollButton().push();
164     }
165     
166     /** clicks on "" MetalScrollButton
167      */

168     public void metalScrollButton2() {
169         btMetalScrollButton2().push();
170     }
171     
172     /** clicks on "Add..." JButton
173      */

174     public void add() {
175         btAdd().push();
176     }
177     
178     /** clicks on "Edit..." JButton
179      */

180     public void edit() {
181         btEdit().push();
182     }
183     
184     /** clicks on "Remove" JButton
185      */

186     public void remove() {
187         btRemove().push();
188     }
189     
190     /** clicks on "OK" JButton
191      */

192     public void oK() {
193         btOK().push();
194     }
195     
196     /** clicks on "Cancel" JButton
197      */

198     public void cancel() {
199         btCancel().push();
200     }
201     
202     /** clicks on "Help" JButton
203      */

204     public void help() {
205         btHelp().push();
206     }
207     
208     
209     //*****************************************
210
// High-level functionality definition part
211
//*****************************************
212

213     /** Performs verification of Abbreviations by accessing all its components.
214      */

215     public void verify() {
216         tabAbbreviations();
217         btMetalScrollButton();
218         btMetalScrollButton2();
219         btAdd();
220         btEdit();
221         btRemove();
222         btOK();
223         btCancel();
224         btHelp();
225     }
226     
227     public void addAbbreviation(String JavaDoc abbreviation, String JavaDoc expansion) {
228         new EventTool().waitNoEvent(500);
229         btAdd().pushNoBlock();
230         EnterAbbreviation enter = new EnterAbbreviation();
231         enter.fillAbbreviation(abbreviation, expansion);
232         enter.oK();
233     }
234     
235     public boolean editAbbreviation(String JavaDoc abbreviationName, String JavaDoc newAbbreviationName, String JavaDoc newExpansion) {
236         int row = tabAbbreviations().findCellRow(abbreviationName);
237         
238         if (row == (-1))
239             return false;
240         
241         tabAbbreviations().selectCell(row, 0);
242         btEdit().pushNoBlock();
243         
244         EnterAbbreviation enter = new EnterAbbreviation();
245         
246         enter.fillAbbreviation(newAbbreviationName, newExpansion);
247         enter.oK();
248         
249         return true;
250     }
251     
252     public void addOrEditAbbreviation(String JavaDoc abbreviationName, String JavaDoc newAbbreviationName, String JavaDoc newExpansion) {
253         if (!editAbbreviation(abbreviationName, newAbbreviationName, newExpansion))
254             addAbbreviation(newAbbreviationName, newExpansion);
255     }
256     
257     public boolean removeAbbreviation(String JavaDoc abbreviation) {
258         int row = tabAbbreviations().findCellRow(abbreviation,
259                 new Operator.DefaultStringComparator(true, true));
260         
261         if (row == (-1)) {
262             System.out.println("Didn't find "+abbreviation);
263             TableModel JavaDoc model = tabAbbreviations().getModel();
264             int rowCount = model.getRowCount();
265             for (int cntr = 0; cntr < rowCount; cntr++) {
266                 System.out.print(model.getValueAt(cntr, 0)+" ");
267             }
268             System.out.println("");
269             return false;
270         }
271         tabAbbreviations().selectCell(row, 0);
272         btRemove().pushNoBlock();
273         
274         return true;
275     }
276     
277     public Map listAbbreviations() {
278         TableModel JavaDoc model = tabAbbreviations().getModel();
279         int rowCount = model.getRowCount();
280         Map result = new HashMap();
281         
282         for (int cntr = 0; cntr < rowCount; cntr++) {
283             result.put((String JavaDoc) model.getValueAt(cntr, 0), (String JavaDoc) model.getValueAt(cntr, 1));
284         }
285         
286         return result;
287     }
288     
289     public static Abbreviations invoke(String JavaDoc editorName) {
290         OptionsOperator options = OptionsOperator.invoke();
291         options.switchToClassicView(); //use switchToClassic, do not push the button
292
options.selectOption(ResourceBundle.getBundle("org/netbeans/core/Bundle").getString("UI/Services/Editing")+
293                 "|"+ResourceBundle.getBundle("org/netbeans/modules/editor/options/Bundle").getString("OPTIONS_all")+
294                 "|" + editorName);
295         new EventTool().waitNoEvent(500);
296         PropertySheetOperator pso = new PropertySheetOperator(options);
297         new Property(pso,ResourceBundle.getBundle("org/netbeans/modules/editor/options/Bundle").getString("PROP_Abbreviations")).openEditor();
298         
299         Abbreviations abbs = new Abbreviations();
300         
301         options.btClose().push();
302         return abbs;
303     }
304     
305     public static void addAbbreviation(String JavaDoc editorName, String JavaDoc abbreviation, String JavaDoc expansion) {
306         Abbreviations instance = invoke(editorName);
307         
308         instance.addAbbreviation(abbreviation, expansion);
309         instance.oK();
310     }
311     
312     public static void addOrEditAbbreviation(String JavaDoc editorName, String JavaDoc abbreviationName, String JavaDoc newAbbreviationName, String JavaDoc newExpansion) {
313         Abbreviations instance = invoke(editorName);
314         
315         instance.addOrEditAbbreviation(abbreviationName, newAbbreviationName, newExpansion);
316         instance.oK();
317     }
318     
319     public static boolean removeAbbreviation(String JavaDoc editorName, String JavaDoc abbreviation) {
320         Abbreviations instance = invoke(editorName);
321         boolean result = instance.removeAbbreviation(abbreviation);
322         
323         instance.oK();
324         return result;
325     }
326     
327     public static boolean editAbbreviation(String JavaDoc editorName, String JavaDoc abbreviationName, String JavaDoc newAbbreviationName, String JavaDoc newExpansion) {
328         Abbreviations instance = invoke(editorName);
329         boolean result = instance.editAbbreviation(abbreviationName, newAbbreviationName, newExpansion);
330         
331         instance.oK();
332         return result;
333     }
334     
335     public static Map listAbbreviations(String JavaDoc editorName) {
336         Abbreviations instance = invoke(editorName);
337         Map result = instance.listAbbreviations();
338         
339         instance.oK();
340         
341         return result;
342     }
343     
344     /** Performs simple test of Abbreviations
345      * @param args the command line arguments
346      */

347     public static void main(String JavaDoc args[]) {
348         Abbreviations.addAbbreviation("Java Editor","aaa","All abbrev");
349         try {
350             Thread.currentThread().sleep(5000);
351         } catch (Exception JavaDoc ex) {
352             ex.printStackTrace();
353         }
354         Abbreviations.removeAbbreviation("Java Editor","aaa");
355         /*
356         try {
357             //java.io.PrintWriter pw= new java.io.PrintWriter(new java.io.FileWriter("/tmp/abbrevs.java"));
358             Map map=Abbreviations.listAbbreviations(java.util.ResourceBundle.getBundle("org/netbeans/modules/editor/options/Bundle").getString("OPTIONS_java"));
359             java.util.Iterator keys=map.keySet().iterator();
360             String key;
361             while (keys.hasNext()) {
362                 key=(String)(keys.next());
363                 System.out.println(key+" "+map.get(key));
364                 // pw.println("new Abbreviation(\""+key+"\", \""+map.get(key)+"\", \"\", \"\"),");
365             }
366             //pw.close();
367         } catch (Exception ex) {
368             ex.printStackTrace();
369         }*/

370         
371     }
372 }
373
374
Popular Tags