KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gui > windowsystem > MenuChecker


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 package gui.windowsystem;
21
22 import javax.swing.JMenuBar JavaDoc;
23 import javax.swing.JPopupMenu JavaDoc;
24 import javax.swing.JMenu JavaDoc;
25 import javax.swing.JMenuItem JavaDoc;
26 import javax.swing.MenuElement JavaDoc;
27
28 import java.util.Collection JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.TreeSet JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35
36 import java.io.PrintStream JavaDoc;
37
38 import org.netbeans.jemmy.operators.JMenuBarOperator;
39
40 import org.netbeans.jellytools.MainWindowOperator;
41
42
43 /**
44  * @author lhasik@netbeans.org, mmirilovic@netbeans.org
45  */

46 public class MenuChecker {
47
48     /** Creates a new instance of MenuChecker */
49     public MenuChecker() {
50     }
51
52     /** Check whether JPopupMenu <b>popup</b> contains <B>item</B> ?
53      * @param popup looking for menu item in this popup menu
54      * @param item looking for this item
55      * @return true-popup contains item, false-doesn't contain item */

56     public boolean containsMenuItem(javax.swing.JPopupMenu JavaDoc popup, String JavaDoc item) {
57         MenuElement JavaDoc [] elements = popup.getSubElements();
58         for(int k=0; k < elements.length; k++) {
59
60             if(elements[k] instanceof JMenuItem JavaDoc) {
61                 if(item.equals(((JMenuItem JavaDoc)elements[k]).getText())) return true;
62             }
63         }
64         return false;
65     }
66
67     /** Open all menus in menubar
68      * @param menu to be visited */

69     public static void visitMenuBar(JMenuBar JavaDoc menu) {
70         MenuElement JavaDoc [] elements = menu.getSubElements();
71
72         JMenuBarOperator op = new JMenuBarOperator(menu);
73
74         for(int k=0; k < elements.length; k++) {
75             if(elements[k] instanceof JMenuItem JavaDoc) {
76                 op.pushMenu(((JMenuItem JavaDoc)elements[k]).getText(), "/", true, true);
77                 try {
78                     op.wait(200);
79                 }catch(Exception JavaDoc e) {}
80             }
81         }
82     }
83
84     /** Get MenuBar and tranfer it to ArrayList.
85      * @param menu menu to be tranfered
86      * @return tranfered menubar */

87     public static ArrayList JavaDoc getMenuBarArrayList(JMenuBar JavaDoc menu) {
88         visitMenuBar(menu);
89
90         MenuElement JavaDoc [] elements = menu.getSubElements();
91
92         ArrayList JavaDoc list = new ArrayList JavaDoc();
93         for(int k=0; k < elements.length; k++) {
94             if(elements[k] instanceof JMenuItem JavaDoc) {
95                 list.add(NbMenu.getNbMenu((JMenuItem JavaDoc)elements[k]));
96                 JMenuBarOperator menuOp = new JMenuBarOperator(menu);
97                 list.add(getMenuArrayList(menuOp.getMenu(k)));
98             }
99             /*if(elements[k] instanceof JMenuBar) {
100                 JMenuBarOperator menuOp = new JMenuBarOperator(menu);
101                 list.add(getMenuArrayList(menuOp.getMenu(0)));
102             }
103              */

104         }
105         return list;
106     }
107
108     /** Get Menu and tranfer it to ArrayList.
109      * @param menu menu to be tranfered
110      * @return tranfered menu */

111     public static ArrayList JavaDoc getMenuArrayList(JMenu JavaDoc menu) {
112         MenuElement JavaDoc [] elements = menu.getSubElements();
113         ArrayList JavaDoc list = new ArrayList JavaDoc();
114
115         for(int k=0; k < elements.length; k++) {
116
117             if(elements[k] instanceof JPopupMenu JavaDoc)
118                 list.add(getPopupMenuArrayList((JPopupMenu JavaDoc)elements[k]));
119
120             if(elements[k] instanceof JMenuItem JavaDoc)
121                 list.add(NbMenu.getNbMenu((JMenuItem JavaDoc)elements[k]));
122
123         }
124         return list;
125     }
126
127     /** Get PopupMenu and transfer it to ArrayList.
128      * @param popup menu to be tranfered
129      * @return transfered menu */

130     public static ArrayList JavaDoc getPopupMenuArrayList(JPopupMenu JavaDoc popup) {
131         MenuElement JavaDoc [] elements = popup.getSubElements();
132         ArrayList JavaDoc list = new ArrayList JavaDoc();
133
134         for(int k=0; k < elements.length; k++) {
135             if(elements[k] instanceof JMenu JavaDoc)
136                 list.add(getMenuArrayList((JMenu JavaDoc)elements[k]));
137
138             if(elements[k] instanceof JMenuItem JavaDoc)
139                 list.add(NbMenu.getNbMenu((JMenuItem JavaDoc)elements[k]));
140         }
141         return list;
142     }
143
144     /**
145      * @param a aarray to be printed
146      * @param stream where
147      * @param x level of array */

148     public static void printArray(ArrayList JavaDoc a, PrintStream JavaDoc stream, int x) {
149         Iterator JavaDoc it = a.iterator();
150         while(it.hasNext()) {
151             Object JavaDoc o = it.next();
152
153             if(o instanceof NbMenu) {
154
155                 for(int i=0;i<x;i++)
156                     stream.print("-");
157
158                 stream.println(((NbMenu)o).name);
159             }
160
161             if(o instanceof ArrayList JavaDoc) {
162                 printArray((ArrayList JavaDoc)o, stream, x + 1);
163             }
164         }
165     }
166
167     /**
168      * @param menu
169      * @return */

170     public static TreeSet JavaDoc getSortedMenuBar(JMenuBar JavaDoc menu, String JavaDoc menuToTest) {
171
172         StringTokenizer JavaDoc menuT = new StringTokenizer JavaDoc(menuToTest, ", ");
173         HashSet JavaDoc menuTT = new HashSet JavaDoc();
174
175         while(menuT.hasMoreTokens())
176             menuTT.add(menuT.nextToken());
177
178         MenuElement JavaDoc [] elements = menu.getSubElements();
179         TreeSet JavaDoc list = new TreeSet JavaDoc();
180
181         for(int k=0; k < elements.length; k++) {
182             if(elements[k] instanceof JMenuItem JavaDoc) {
183                 //NbMenu m = NbMenu.getNbMenu((JMenuItem)elements[k]);
184
JMenuBarOperator menuOp = new JMenuBarOperator(menu);
185                 //m.addSubMenu(getMenuArrayList(menuOp.getMenu(k)));
186
JMenu JavaDoc m = menuOp.getMenu(k);
187                 if(menuTT.contains(m.getLabel())){
188                     list.addAll((Collection JavaDoc)getSortedMenu(menuOp.getMenu(k)));
189                 }
190             }
191             /*if(elements[k] instanceof JMenuBar) {
192                 JMenuBarOperator menuOp = new JMenuBarOperator(menu);
193                 list.add(getMenuArrayList(menuOp.getMenu(0)));
194             }
195              */

196         }
197         return list;
198     }
199
200     /**
201      * @param menu
202      * @return */

203     public static TreeSet JavaDoc getSortedMenu(JMenu JavaDoc menu) {
204         menu.list();
205         MenuElement JavaDoc [] elements = menu.getSubElements();
206         TreeSet JavaDoc list = new TreeSet JavaDoc();
207         NbMenu last = NbMenu.getNbMenu(menu);
208         list.add(last);
209
210         for(int k=0; k < elements.length; k++) {
211             if(elements[k] instanceof JPopupMenu JavaDoc) {
212                 //NbMenu last = (NbMenu)list.get(list.size() - 1);
213
last.addSubMenu(getSortedPopupMenu((JPopupMenu JavaDoc)elements[k], ""));
214             }
215             if(elements[k] instanceof JMenuItem JavaDoc) {
216                 last = NbMenu.getNbMenu((JMenuItem JavaDoc)elements[k]);
217                 list.add(last);
218             }
219
220         }
221         return list;
222     }
223
224     /**
225      * @param popup
226      * @return */

227     public static TreeSet JavaDoc getSortedPopupMenu(JPopupMenu JavaDoc popup, String JavaDoc menuNotTest) {
228         StringTokenizer JavaDoc menuT = new StringTokenizer JavaDoc(menuNotTest, ", ");
229         HashSet JavaDoc menuTT = new HashSet JavaDoc();
230
231         while(menuT.hasMoreTokens())
232             menuTT.add(menuT.nextToken());
233
234         MenuElement JavaDoc [] elements = popup.getSubElements();
235         TreeSet JavaDoc list = new TreeSet JavaDoc();
236
237         for(int k=0; k < elements.length; k++) {
238
239             if(elements[k] instanceof JMenu JavaDoc) {
240                 JMenu JavaDoc m = (JMenu JavaDoc) elements[k];
241                 if(!menuTT.contains(m.getLabel()))
242                     list.addAll(getSortedMenu(m));
243             }
244
245             if(elements[k] instanceof JMenuItem JavaDoc) {
246                 list.add(NbMenu.getNbMenu((JMenuItem JavaDoc)elements[k]));
247             }
248         }
249         return list;
250     }
251
252     /** Print (unsorted) structure of menu - as it really looks
253      * @param menu
254      * @param stream */

255     public static void printMenuBarStructure(JMenuBar JavaDoc menu, PrintStream JavaDoc stream, String JavaDoc menuToBeTested, boolean printEnabledOnly, boolean sorted) {
256         if(sorted){
257             printSorted(getSortedMenuBar(menu, menuToBeTested), stream, 1, printEnabledOnly);
258             stream.close();
259         }else
260             printArray(getMenuBarArrayList(menu), stream, 1);
261     }
262
263     /** Print (unsorted) structure of menu - as it really looks
264      * @param menu
265      * @param stream */

266     public static void printPopupMenuStructure(JPopupMenu JavaDoc menu, PrintStream JavaDoc stream, String JavaDoc menuNotToBeTested, boolean printEnabledOnly, boolean sorted) {
267         if(sorted){
268             printSorted(getSortedPopupMenu(menu, menuNotToBeTested), stream, 1, printEnabledOnly);
269             stream.close();
270         }else{
271             printArray(getPopupMenuArrayList(menu), stream, 1);
272         }
273     }
274
275     /** Print Sorted collection.
276      * @param a Collection to be sorted.
277      * @param stream output stream
278      * @param x indentation */

279     public static void printSorted(Collection JavaDoc a, PrintStream JavaDoc stream, int x, boolean printEnabledOnly) {
280         Iterator JavaDoc it = a.iterator();
281
282         while(it.hasNext()) {
283             Object JavaDoc o = it.next();
284             if(o instanceof NbMenu) {
285                 NbMenu item = (NbMenu)o;
286
287                 if(!(printEnabledOnly ^ item.enabled)){
288                     for(int i=0;i<x;i++) stream.print("-");
289                     stream.println(item.name);
290                 }
291
292                 if(item.submenu != null) {
293                     printSorted(item.getSubMenu(), stream, x+1, printEnabledOnly);
294                 }
295
296             }
297         }
298     }
299
300     public static String JavaDoc checkMnemonicCollision() {
301         return checkMnemonicCollision(getMenuBarArrayList(MainWindowOperator.getDefault().getJMenuBar())).toString();
302     }
303
304
305     /** Check mnemonics in menu structure.
306      * @param list
307      * @return */

308     private static StringBuffer JavaDoc checkCollision(ArrayList JavaDoc list, boolean checkShortCuts) {
309         StringBuffer JavaDoc collisions = new StringBuffer JavaDoc("");
310         Iterator JavaDoc it = list.iterator();
311
312         HashMap JavaDoc check = new HashMap JavaDoc();
313
314         while(it.hasNext()) {
315             Object JavaDoc o = it.next();
316
317             if(o instanceof NbMenu) {
318                 NbMenu item = (NbMenu)o;
319
320                 if(checkShortCuts){
321                     if(item.mnemo != 0) {
322                         Integer JavaDoc mnemonic = new Integer JavaDoc(item.mnemo);
323                         //stream.println("checking : " + item.name + " - " + item.mnemo);
324
if(check.containsKey(mnemonic)) {
325                             char k = (char) item.mnemo;
326                             collisions.append("\n !!!!!! Collision! mnemonic='" + k + "' : " + item.name + " is in collision with " + check.get(mnemonic));
327                         } else {
328                             check.put(mnemonic, item.name);
329                         }
330                     }
331                 }else{
332                     if(item.mnemo != 0) {
333                         Integer JavaDoc mnemonic = new Integer JavaDoc(item.mnemo);
334                         //stream.println("checking : " + item.name + " - " + item.mnemo);
335
if(check.containsKey(mnemonic)) {
336                             char k = (char) item.mnemo;
337                             collisions.append("\n !!!!!! Collision! mnemonic='" + k + "' : " + item.name + " is in collision with " + check.get(mnemonic));
338                         } else {
339                             check.put(mnemonic, item.name);
340                         }
341                     }
342                 }
343             }
344
345             if(o instanceof ArrayList JavaDoc) {
346                 collisions.append(checkMnemonicCollision((ArrayList JavaDoc)o));
347             }
348         }
349
350         return collisions;
351     }
352
353
354
355     /** Check mnemonics in menu structure.
356      * @param list
357      * @return */

358     private static StringBuffer JavaDoc checkMnemonicCollision(ArrayList JavaDoc list) {
359         StringBuffer JavaDoc collisions = new StringBuffer JavaDoc("");
360         Iterator JavaDoc it = list.iterator();
361
362         HashMap JavaDoc check = new HashMap JavaDoc();
363
364         while(it.hasNext()) {
365             Object JavaDoc o = it.next();
366
367             if(o instanceof NbMenu) {
368                 NbMenu item = (NbMenu)o;
369                 if(item.mnemo != 0) {
370                     Integer JavaDoc mnemonic = new Integer JavaDoc(item.mnemo);
371                     //stream.println("checking : " + item.name + " - " + item.mnemo);
372
if(check.containsKey(mnemonic)) {
373                         char k = (char) item.mnemo;
374                         collisions.append("\n !!!!!! Collision! mnemonic='" + k + "' : " + item.name + " is in collision with " + check.get(mnemonic));
375                     } else {
376                         check.put(mnemonic, item.name);
377                     }
378                 }
379             }
380
381             if(o instanceof ArrayList JavaDoc) {
382                 collisions.append(checkMnemonicCollision((ArrayList JavaDoc)o));
383             }
384         }
385
386         return collisions;
387     }
388
389
390
391     public static String JavaDoc checkShortCutCollision() {
392         return checkShortCutCollision(getMenuBarArrayList(MainWindowOperator.getDefault().getJMenuBar())).toString();
393     }
394
395     /** check shortcuts in menu structure
396      * @param a
397      * @return */

398     private static StringBuffer JavaDoc checkShortCutCollision(ArrayList JavaDoc a) {
399         StringBuffer JavaDoc collisions = new StringBuffer JavaDoc("");
400         Iterator JavaDoc it = a.iterator();
401         HashMap JavaDoc check = new HashMap JavaDoc();
402
403         while(it.hasNext()) {
404             Object JavaDoc o = it.next();
405
406             if(o instanceof NbMenu) {
407                 NbMenu item = (NbMenu)o;
408
409                 if(item.accelerator != null) {
410                     //stream.println("checking : " + item.name + " - " + item.accelerator);
411
if(check.containsKey(item.accelerator)) {
412                         collisions.append("\n !!!!!! Collision! accelerator ='" + item.accelerator + "' : " + item.name + " is in collision with " + check.get(item.accelerator));
413                     } else {
414                         check.put(item.accelerator, item.name);
415                     }
416                 }
417             }
418
419             if(o instanceof ArrayList JavaDoc) {
420                 collisions.append(checkShortCutCollision((ArrayList JavaDoc)o));
421             }
422         }
423
424         return collisions;
425     }
426
427 }
428
429
430 class NbMenu implements Comparable JavaDoc {
431     /** label of menuitem */
432     public String JavaDoc name;
433     /** mnemonic in int */
434     public int mnemo;
435     /** jasne ? */
436     public String JavaDoc accelerator;
437     public boolean enabled;
438     TreeSet JavaDoc submenu = null;
439
440     /**
441      * @param it
442      * @return instance of NbMenu constructed from parameter it */

443     public static NbMenu getNbMenu(JMenuItem JavaDoc it) {
444         NbMenu item = new NbMenu();
445         item.name = it.getText();//getLabel();
446
item.accelerator = (it.getAccelerator() == null) ? null : it.getAccelerator().toString();
447         item.mnemo = it.getMnemonic();
448         item.enabled = it.isEnabled();
449         return item;
450     }
451     /**
452      * @param m */

453     public void addSubMenu(TreeSet JavaDoc m) {
454         submenu = m;
455     }
456
457     /**
458      * @return */

459     public TreeSet JavaDoc getSubMenu() {
460         return submenu;
461     }
462
463     /** needed for comparing in TreeSet
464      * @param obj
465      * @return */

466     public int compareTo(Object JavaDoc obj) {
467         NbMenu n = (NbMenu)obj;
468         return (name != null) ? name.compareTo(n.name) : n.name.compareTo(name);
469     }
470 }
471
472
Popular Tags