KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > gui > util > MenuFactory


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/gui/util/MenuFactory.java,v 1.29.2.2 2004/09/26 15:37:58 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.gui.util;
20
21 import java.awt.event.KeyEvent JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31
32 import javax.swing.JMenu JavaDoc;
33 import javax.swing.JMenuItem JavaDoc;
34 import javax.swing.JPopupMenu JavaDoc;
35 import javax.swing.KeyStroke JavaDoc;
36 import javax.swing.MenuElement JavaDoc;
37
38 import org.apache.jmeter.gui.GuiPackage;
39 import org.apache.jmeter.gui.JMeterGUIComponent;
40 import org.apache.jmeter.gui.action.ActionRouter;
41 import org.apache.jmeter.junit.JMeterTestCase;
42 import org.apache.jmeter.testbeans.TestBean;
43 import org.apache.jmeter.testbeans.gui.TestBeanGUI;
44 import org.apache.jmeter.util.JMeterUtils;
45 import org.apache.jorphan.reflect.ClassFinder;
46 import org.apache.jorphan.util.JOrphanUtils;
47 import org.apache.jorphan.logging.LoggingManager;
48 import org.apache.log.Logger;
49
50 /**
51  * @author Michael Stover
52  * @author <a HREF="mailto:klancast@swbell.net">Keith Lancaster</a>
53  * @version $Revision: 1.29.2.2 $ updated on $Date: 2004/09/26 15:37:58 $
54  */

55 public final class MenuFactory
56 {
57     transient private static Logger log = LoggingManager.getLoggerForClass();
58
59     public final static String JavaDoc TIMERS = "menu_timer";// $NON-NLS-1$
60
public final static String JavaDoc CONTROLLERS = "menu_logic_controller";// $NON-NLS-1$
61
public final static String JavaDoc SAMPLERS = "menu_generative_controller";// $NON-NLS-1$
62
public final static String JavaDoc CONFIG_ELEMENTS = "menu_config_element";// $NON-NLS-1$
63
public final static String JavaDoc POST_PROCESSORS = "menu_post_processors";// $NON-NLS-1$
64
public final static String JavaDoc PRE_PROCESSORS = "menu_pre_processors";// $NON-NLS-1$
65
public final static String JavaDoc ASSERTIONS = "menu_assertions";// $NON-NLS-1$
66
public final static String JavaDoc NON_TEST_ELEMENTS = "menu_non_test_elements";// $NON-NLS-1$
67
public final static String JavaDoc LISTENERS = "menu_listener";// $NON-NLS-1$
68
private static Map JavaDoc menuMap = new HashMap JavaDoc();
69     private static Set JavaDoc elementsToSkip = new HashSet JavaDoc();
70
71     // MENU_ADD_xxx - controls which items are in the ADD menu
72
// MENU_PARENT_xxx - controls which items are in the Insert Parent menu
73
private static final String JavaDoc[] MENU_ADD_CONTROLLER = new String JavaDoc[] {
74             MenuFactory.CONTROLLERS,
75             MenuFactory.SAMPLERS,
76             MenuFactory.CONFIG_ELEMENTS,
77             MenuFactory.TIMERS,
78             MenuFactory.LISTENERS,
79             MenuFactory.PRE_PROCESSORS,
80             MenuFactory.POST_PROCESSORS };
81     private static final String JavaDoc[] MENU_PARENT_CONTROLLER = new String JavaDoc[] {
82             MenuFactory.CONTROLLERS };
83     
84     private static final String JavaDoc[] MENU_ADD_SAMPLER = new String JavaDoc[] {
85             MenuFactory.CONFIG_ELEMENTS,
86             MenuFactory.ASSERTIONS,
87             MenuFactory.TIMERS,
88             MenuFactory.LISTENERS,
89             MenuFactory.PRE_PROCESSORS,
90             MenuFactory.POST_PROCESSORS };
91     private static final String JavaDoc[] MENU_PARENT_SAMPLER = new String JavaDoc[] {
92             MenuFactory.CONTROLLERS };
93
94     
95     private static List JavaDoc timers,
96         controllers,
97         samplers,
98         configElements,
99         assertions,
100         listeners,
101         nonTestElements,
102         postProcessors,
103         preProcessors;
104
105     //private static JMenu timerMenu;
106
//private static JMenu controllerMenu;
107
//private static JMenu generativeControllerMenu;
108
//private static JMenu listenerMenu;
109
//private static JMenu assertionMenu;
110
//private static JMenu configMenu;
111
//private static JMenu insertControllerMenu;
112
//private static JMenu postProcessorMenu;
113
//private static JMenu preProcessorMenu;
114

115     static {
116         try
117         {
118             String JavaDoc[] classesToSkip =
119                 JOrphanUtils.split(
120                     JMeterUtils.getPropDefault("not_in_menu", ""),// $NON-NLS-1$
121
",");// $NON-NLS-1$
122
for (int i = 0; i < classesToSkip.length; i++)
123             {
124                 elementsToSkip.add(classesToSkip[i].trim());
125             }
126
127             initializeMenus();
128         }
129         catch (Throwable JavaDoc e)
130         {
131             log.error("", e);
132         }
133     }
134
135     /**
136      * Private constructor to prevent instantiation.
137      */

138     private MenuFactory()
139     {
140     }
141
142     public static String JavaDoc doNothing()
143     {
144         return "doing nothing";
145     }
146
147     public static void addEditMenu(JPopupMenu JavaDoc menu, boolean removable)
148     {
149         addSeparator(menu);
150         if (removable)
151         {
152             menu.add(
153                 makeMenuItem(
154                     JMeterUtils.getResString("remove"),// $NON-NLS-1$
155
"Remove", // TODO are these other params language dependent?
156
"remove", // Indeed, should they be made into constants?
157
KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)));
158             menu.add(
159                 makeMenuItem(
160                     JMeterUtils.getResString("cut"),// $NON-NLS-1$
161
"Cut",
162                     "Cut",
163                     KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_MASK)));
164         }
165         menu.add(
166             makeMenuItem(
167                 JMeterUtils.getResString("copy"),// $NON-NLS-1$
168
"Copy",
169                 "Copy",
170                 KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK)));
171         menu.add(
172             makeMenuItem(
173                 JMeterUtils.getResString("paste"),// $NON-NLS-1$
174
"Paste",
175                 "Paste",
176                 KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.CTRL_MASK)));
177         menu.add(
178             makeMenuItem(
179                 JMeterUtils.getResString("paste_insert"),// $NON-NLS-1$
180
"Paste Insert",
181                 "Paste Insert"));
182     }
183
184     public static void addFileMenu(JPopupMenu JavaDoc menu)
185     {
186         addSeparator(menu);
187         menu.add(
188             makeMenuItem(
189                 JMeterUtils.getResString("menu_merge"),// $NON-NLS-1$
190
"Merge",
191                 "merge"));
192         menu.add(
193             makeMenuItem(
194                 JMeterUtils.getResString("save_as"),// $NON-NLS-1$
195
"Save As",
196                 "save_as"));
197         JMenuItem JavaDoc disabled =
198             makeMenuItem(
199                 JMeterUtils.getResString("disable"),// $NON-NLS-1$
200
"Disable",
201                 "disable");
202         JMenuItem JavaDoc enabled =
203             makeMenuItem(
204                 JMeterUtils.getResString("enable"),// $NON-NLS-1$
205
"Enable",
206                 "enable");
207         boolean isEnabled =
208             GuiPackage
209                 .getInstance()
210                 .getTreeListener()
211                 .getCurrentNode()
212                 .isEnabled();
213         if (isEnabled)
214         {
215             disabled.setEnabled(true);
216             enabled.setEnabled(false);
217         }
218         else
219         {
220             disabled.setEnabled(false);
221             enabled.setEnabled(true);
222         }
223         menu.add(enabled);
224         menu.add(disabled);
225         addSeparator(menu);
226         menu.add(
227             makeMenuItem(
228                 JMeterUtils.getResString("help"),// $NON-NLS-1$
229
"Help",
230                 "help"));
231     }
232
233     public static JMenu JavaDoc makeMenus(
234         String JavaDoc[] categories,
235         String JavaDoc label,
236         String JavaDoc actionCommand)
237     {
238         JMenu JavaDoc addMenu = new JMenu JavaDoc(label);
239         for (int i = 0; i < categories.length; i++)
240         {
241             addMenu.add(makeMenu(categories[i], actionCommand));
242         }
243         return addMenu;
244     }
245
246     public static JPopupMenu JavaDoc getDefaultControllerMenu()
247     {
248         JPopupMenu JavaDoc pop = new JPopupMenu JavaDoc();
249         pop.add(
250             MenuFactory.makeMenus(
251                 MENU_ADD_CONTROLLER,
252                 JMeterUtils.getResString("Add"),// $NON-NLS-1$
253
"Add"));
254         pop.add(
255             makeMenus(
256                 MENU_PARENT_CONTROLLER,
257                 JMeterUtils.getResString("insert_parent"),// $NON-NLS-1$
258
"Add Parent"));
259         MenuFactory.addEditMenu(pop, true);
260         MenuFactory.addFileMenu(pop);
261         return pop;
262     }
263
264     public static JPopupMenu JavaDoc getDefaultSamplerMenu()
265     {
266         JPopupMenu JavaDoc pop = new JPopupMenu JavaDoc();
267         pop.add(
268             MenuFactory.makeMenus(
269                 MENU_ADD_SAMPLER,
270                 JMeterUtils.getResString("Add"),// $NON-NLS-1$
271
"Add"));
272         pop.add(
273             makeMenus(
274                 MENU_PARENT_SAMPLER,
275                 JMeterUtils.getResString("insert_parent"),// $NON-NLS-1$
276
"Add Parent"));
277         MenuFactory.addEditMenu(pop, true);
278         MenuFactory.addFileMenu(pop);
279         return pop;
280     }
281
282     public static JPopupMenu JavaDoc getDefaultConfigElementMenu()
283     {
284         JPopupMenu JavaDoc pop = new JPopupMenu JavaDoc();
285         MenuFactory.addEditMenu(pop, true);
286         MenuFactory.addFileMenu(pop);
287         return pop;
288     }
289
290     public static JPopupMenu JavaDoc getDefaultVisualizerMenu()
291     {
292         JPopupMenu JavaDoc pop = new JPopupMenu JavaDoc();
293         MenuFactory.addEditMenu(pop, true);
294         MenuFactory.addFileMenu(pop);
295         return pop;
296     }
297
298     public static JPopupMenu JavaDoc getDefaultTimerMenu()
299     {
300         JPopupMenu JavaDoc pop = new JPopupMenu JavaDoc();
301         MenuFactory.addEditMenu(pop, true);
302         MenuFactory.addFileMenu(pop);
303         return pop;
304     }
305
306     public static JPopupMenu JavaDoc getDefaultAssertionMenu()
307     {
308         JPopupMenu JavaDoc pop = new JPopupMenu JavaDoc();
309         MenuFactory.addEditMenu(pop, true);
310         MenuFactory.addFileMenu(pop);
311         return pop;
312     }
313
314     public static JPopupMenu JavaDoc getDefaultExtractorMenu()
315     {
316         JPopupMenu JavaDoc pop = new JPopupMenu JavaDoc();
317         MenuFactory.addEditMenu(pop, true);
318         MenuFactory.addFileMenu(pop);
319         return pop;
320     }
321
322     public static JMenu JavaDoc makeMenu(String JavaDoc category, String JavaDoc actionCommand)
323     {
324         return makeMenu(
325             (Collection JavaDoc) menuMap.get(category),
326             actionCommand,
327             JMeterUtils.getResString(category));
328     }
329
330     public static JMenu JavaDoc makeMenu(
331         Collection JavaDoc menuInfo,
332         String JavaDoc actionCommand,
333         String JavaDoc menuName)
334     {
335         Iterator JavaDoc iter = menuInfo.iterator();
336         JMenu JavaDoc menu = new JMenu JavaDoc(menuName);
337         while (iter.hasNext())
338         {
339             MenuInfo info = (MenuInfo) iter.next();
340             menu.add(makeMenuItem(info.label, info.className, actionCommand));
341         }
342         return menu;
343     }
344
345     public static void setEnabled(JMenu JavaDoc menu)
346     {
347         if (menu.getSubElements().length == 0)
348         {
349             menu.setEnabled(false);
350         }
351     }
352
353     public static JMenuItem JavaDoc makeMenuItem(
354         String JavaDoc label,
355         String JavaDoc name,
356         String JavaDoc actionCommand)
357     {
358         JMenuItem JavaDoc newMenuChoice = new JMenuItem JavaDoc(label);
359         newMenuChoice.setName(name);
360         newMenuChoice.addActionListener(ActionRouter.getInstance());
361         if (actionCommand != null)
362         {
363             newMenuChoice.setActionCommand(actionCommand);
364         }
365
366         return newMenuChoice;
367     }
368
369     public static JMenuItem JavaDoc makeMenuItem(
370         String JavaDoc label,
371         String JavaDoc name,
372         String JavaDoc actionCommand,
373         KeyStroke JavaDoc accel)
374     {
375         JMenuItem JavaDoc item = makeMenuItem(label, name, actionCommand);
376         item.setAccelerator(accel);
377         return item;
378     }
379
380     private static void initializeMenus()
381     {
382         try
383         {
384             List JavaDoc guiClasses =
385                 ClassFinder.findClassesThatExtend(
386                     JMeterUtils.getSearchPaths(),
387                     new Class JavaDoc[] { JMeterGUIComponent.class, TestBean.class });
388             timers = new LinkedList JavaDoc();
389             controllers = new LinkedList JavaDoc();
390             samplers = new LinkedList JavaDoc();
391             configElements = new LinkedList JavaDoc();
392             assertions = new LinkedList JavaDoc();
393             listeners = new LinkedList JavaDoc();
394             postProcessors = new LinkedList JavaDoc();
395             preProcessors = new LinkedList JavaDoc();
396             nonTestElements = new LinkedList JavaDoc();
397             menuMap.put(TIMERS, timers);
398             menuMap.put(ASSERTIONS, assertions);
399             menuMap.put(CONFIG_ELEMENTS, configElements);
400             menuMap.put(CONTROLLERS, controllers);
401             menuMap.put(LISTENERS, listeners);
402             menuMap.put(NON_TEST_ELEMENTS, nonTestElements);
403             menuMap.put(SAMPLERS, samplers);
404             menuMap.put(POST_PROCESSORS, postProcessors);
405             menuMap.put(PRE_PROCESSORS, preProcessors);
406             Collections.sort(guiClasses);
407             Iterator JavaDoc iter = guiClasses.iterator();
408             while (iter.hasNext())
409             {
410                 String JavaDoc name= (String JavaDoc)iter.next();
411
412                 /*
413                  * JMeterTreeNode and TestBeanGUI are special GUI classes,
414                  * and aren't intended to be added to menus
415                  *
416                  * TODO: find a better way of checking this
417                  */

418                 if (name.endsWith("JMeterTreeNode")// $NON-NLS-1$
419
|| name.endsWith("TestBeanGUI"))// $NON-NLS-1$
420
{
421                     continue;// Don't try to instantiate these
422
}
423                 
424                 JMeterGUIComponent item;
425                 try
426                 {
427                     Class JavaDoc c = Class.forName(name);
428                     if (TestBean.class.isAssignableFrom(c))
429                     {
430                         item = new TestBeanGUI(c);
431                     }
432                     else
433                     {
434                         item = (JMeterGUIComponent) c.newInstance();
435                     }
436                 }
437                 catch (Throwable JavaDoc e)
438                 {
439                     log.info("Could not instantiate "+name, e);
440                     continue;
441                 }
442                 if (elementsToSkip.contains(name)
443                     || elementsToSkip.contains(item.getStaticLabel()))
444                 {
445                     log.info("Skipping "+name);
446                     continue;
447                 }
448                 else
449                 {
450                     elementsToSkip.add(name);
451                 }
452                 Collection JavaDoc categories = item.getMenuCategories();
453                 if (categories == null)
454                 {
455                     log.debug(name+" participates in no menus.");
456                     continue;
457                 }
458                 if (categories.contains(TIMERS))
459                 {
460                     timers.add(
461                         new MenuInfo(
462                             item.getStaticLabel(),
463                             name));
464                 }
465
466                 if (categories.contains(POST_PROCESSORS))
467                 {
468                     postProcessors.add(
469                         new MenuInfo(
470                             item.getStaticLabel(),
471                             name));
472                 }
473
474                 if (categories.contains(PRE_PROCESSORS))
475                 {
476                     preProcessors.add(
477                         new MenuInfo(
478                             item.getStaticLabel(),
479                             name));
480                 }
481
482                 if (categories.contains(CONTROLLERS))
483                 {
484                     controllers.add(
485                         new MenuInfo(
486                             item.getStaticLabel(),
487                             name));
488                 }
489
490                 if (categories.contains(SAMPLERS))
491                 {
492                     samplers.add(
493                         new MenuInfo(
494                             item.getStaticLabel(),
495                             name));
496                 }
497
498                 if (categories.contains(NON_TEST_ELEMENTS))
499                 {
500                     nonTestElements.add(
501                         new MenuInfo(
502                             item.getStaticLabel(),
503                             name));
504                 }
505
506                 if (categories.contains(LISTENERS))
507                 {
508                     listeners.add(
509                         new MenuInfo(
510                             item.getStaticLabel(),
511                             name));
512                 }
513
514                 if (categories.contains(CONFIG_ELEMENTS))
515                 {
516                     configElements.add(
517                         new MenuInfo(
518                             item.getStaticLabel(),
519                             name));
520                 }
521                 if (categories.contains(ASSERTIONS))
522                 {
523                     assertions.add(
524                         new MenuInfo(
525                             item.getStaticLabel(),
526                             name));
527                 }
528
529             }
530         }
531         catch (Exception JavaDoc e)
532         {
533             log.error("", e);
534         }
535     }
536
537     private static void addSeparator(JPopupMenu JavaDoc menu)
538     {
539         MenuElement JavaDoc[] elements = menu.getSubElements();
540         if ((elements.length > 0)
541             && !(elements[elements.length - 1] instanceof JPopupMenu.Separator JavaDoc))
542         {
543             menu.addSeparator();
544         }
545     }
546
547 ////////////////////////////// Test code ////////////////////////////////////
548

549     public static class Test extends JMeterTestCase
550     {
551
552         public Test() {
553             super();
554         }
555
556         public Test(String JavaDoc name) {
557             super(name);
558         }
559
560         private static void check(String JavaDoc s,int i) throws Exception JavaDoc
561         {
562             assertFalse("The number of "+s+" should not be 0",0==i);
563         }
564         
565         public void testMenu() throws Exception JavaDoc
566         {
567             check("menumap",menuMap.size());
568             
569             check("assertions",assertions.size());
570             check("configElements",configElements.size());
571             check("controllers",controllers.size());
572             check("listeners",listeners.size());
573             check("nonTestElements",nonTestElements.size());
574             check("postProcessors",postProcessors.size());
575             check("preProcessors",preProcessors.size());
576             check("samplers",samplers.size());
577             check("timers",timers.size());
578             
579             
580             check("elementstoskip",elementsToSkip.size());
581
582
583         }
584     }
585 }
586
Popular Tags