1 18 19 package org.apache.jmeter.gui.util; 20 21 import java.awt.event.KeyEvent ; 22 import java.util.Collection ; 23 import java.util.Collections ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.LinkedList ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Set ; 31 32 import javax.swing.JMenu ; 33 import javax.swing.JMenuItem ; 34 import javax.swing.JPopupMenu ; 35 import javax.swing.KeyStroke ; 36 import javax.swing.MenuElement ; 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 55 public final class MenuFactory 56 { 57 transient private static Logger log = LoggingManager.getLoggerForClass(); 58 59 public final static String TIMERS = "menu_timer"; public final static String CONTROLLERS = "menu_logic_controller"; public final static String SAMPLERS = "menu_generative_controller"; public final static String CONFIG_ELEMENTS = "menu_config_element"; public final static String POST_PROCESSORS = "menu_post_processors"; public final static String PRE_PROCESSORS = "menu_pre_processors"; public final static String ASSERTIONS = "menu_assertions"; public final static String NON_TEST_ELEMENTS = "menu_non_test_elements"; public final static String LISTENERS = "menu_listener"; private static Map menuMap = new HashMap (); 69 private static Set elementsToSkip = new HashSet (); 70 71 private static final String [] MENU_ADD_CONTROLLER = new String [] { 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 [] MENU_PARENT_CONTROLLER = new String [] { 82 MenuFactory.CONTROLLERS }; 83 84 private static final String [] MENU_ADD_SAMPLER = new String [] { 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 [] MENU_PARENT_SAMPLER = new String [] { 92 MenuFactory.CONTROLLERS }; 93 94 95 private static List timers, 96 controllers, 97 samplers, 98 configElements, 99 assertions, 100 listeners, 101 nonTestElements, 102 postProcessors, 103 preProcessors; 104 105 115 static { 116 try 117 { 118 String [] classesToSkip = 119 JOrphanUtils.split( 120 JMeterUtils.getPropDefault("not_in_menu", ""), ","); for (int i = 0; i < classesToSkip.length; i++) 123 { 124 elementsToSkip.add(classesToSkip[i].trim()); 125 } 126 127 initializeMenus(); 128 } 129 catch (Throwable e) 130 { 131 log.error("", e); 132 } 133 } 134 135 138 private MenuFactory() 139 { 140 } 141 142 public static String doNothing() 143 { 144 return "doing nothing"; 145 } 146 147 public static void addEditMenu(JPopupMenu menu, boolean removable) 148 { 149 addSeparator(menu); 150 if (removable) 151 { 152 menu.add( 153 makeMenuItem( 154 JMeterUtils.getResString("remove"), "Remove", "remove", KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0))); 158 menu.add( 159 makeMenuItem( 160 JMeterUtils.getResString("cut"), "Cut", 162 "Cut", 163 KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_MASK))); 164 } 165 menu.add( 166 makeMenuItem( 167 JMeterUtils.getResString("copy"), "Copy", 169 "Copy", 170 KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK))); 171 menu.add( 172 makeMenuItem( 173 JMeterUtils.getResString("paste"), "Paste", 175 "Paste", 176 KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.CTRL_MASK))); 177 menu.add( 178 makeMenuItem( 179 JMeterUtils.getResString("paste_insert"), "Paste Insert", 181 "Paste Insert")); 182 } 183 184 public static void addFileMenu(JPopupMenu menu) 185 { 186 addSeparator(menu); 187 menu.add( 188 makeMenuItem( 189 JMeterUtils.getResString("menu_merge"), "Merge", 191 "merge")); 192 menu.add( 193 makeMenuItem( 194 JMeterUtils.getResString("save_as"), "Save As", 196 "save_as")); 197 JMenuItem disabled = 198 makeMenuItem( 199 JMeterUtils.getResString("disable"), "Disable", 201 "disable"); 202 JMenuItem enabled = 203 makeMenuItem( 204 JMeterUtils.getResString("enable"), "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"), "Help", 230 "help")); 231 } 232 233 public static JMenu makeMenus( 234 String [] categories, 235 String label, 236 String actionCommand) 237 { 238 JMenu addMenu = new JMenu (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 getDefaultControllerMenu() 247 { 248 JPopupMenu pop = new JPopupMenu (); 249 pop.add( 250 MenuFactory.makeMenus( 251 MENU_ADD_CONTROLLER, 252 JMeterUtils.getResString("Add"), "Add")); 254 pop.add( 255 makeMenus( 256 MENU_PARENT_CONTROLLER, 257 JMeterUtils.getResString("insert_parent"), "Add Parent")); 259 MenuFactory.addEditMenu(pop, true); 260 MenuFactory.addFileMenu(pop); 261 return pop; 262 } 263 264 public static JPopupMenu getDefaultSamplerMenu() 265 { 266 JPopupMenu pop = new JPopupMenu (); 267 pop.add( 268 MenuFactory.makeMenus( 269 MENU_ADD_SAMPLER, 270 JMeterUtils.getResString("Add"), "Add")); 272 pop.add( 273 makeMenus( 274 MENU_PARENT_SAMPLER, 275 JMeterUtils.getResString("insert_parent"), "Add Parent")); 277 MenuFactory.addEditMenu(pop, true); 278 MenuFactory.addFileMenu(pop); 279 return pop; 280 } 281 282 public static JPopupMenu getDefaultConfigElementMenu() 283 { 284 JPopupMenu pop = new JPopupMenu (); 285 MenuFactory.addEditMenu(pop, true); 286 MenuFactory.addFileMenu(pop); 287 return pop; 288 } 289 290 public static JPopupMenu getDefaultVisualizerMenu() 291 { 292 JPopupMenu pop = new JPopupMenu (); 293 MenuFactory.addEditMenu(pop, true); 294 MenuFactory.addFileMenu(pop); 295 return pop; 296 } 297 298 public static JPopupMenu getDefaultTimerMenu() 299 { 300 JPopupMenu pop = new JPopupMenu (); 301 MenuFactory.addEditMenu(pop, true); 302 MenuFactory.addFileMenu(pop); 303 return pop; 304 } 305 306 public static JPopupMenu getDefaultAssertionMenu() 307 { 308 JPopupMenu pop = new JPopupMenu (); 309 MenuFactory.addEditMenu(pop, true); 310 MenuFactory.addFileMenu(pop); 311 return pop; 312 } 313 314 public static JPopupMenu getDefaultExtractorMenu() 315 { 316 JPopupMenu pop = new JPopupMenu (); 317 MenuFactory.addEditMenu(pop, true); 318 MenuFactory.addFileMenu(pop); 319 return pop; 320 } 321 322 public static JMenu makeMenu(String category, String actionCommand) 323 { 324 return makeMenu( 325 (Collection ) menuMap.get(category), 326 actionCommand, 327 JMeterUtils.getResString(category)); 328 } 329 330 public static JMenu makeMenu( 331 Collection menuInfo, 332 String actionCommand, 333 String menuName) 334 { 335 Iterator iter = menuInfo.iterator(); 336 JMenu menu = new JMenu (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 menu) 346 { 347 if (menu.getSubElements().length == 0) 348 { 349 menu.setEnabled(false); 350 } 351 } 352 353 public static JMenuItem makeMenuItem( 354 String label, 355 String name, 356 String actionCommand) 357 { 358 JMenuItem newMenuChoice = new JMenuItem (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 makeMenuItem( 370 String label, 371 String name, 372 String actionCommand, 373 KeyStroke accel) 374 { 375 JMenuItem 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 guiClasses = 385 ClassFinder.findClassesThatExtend( 386 JMeterUtils.getSearchPaths(), 387 new Class [] { JMeterGUIComponent.class, TestBean.class }); 388 timers = new LinkedList (); 389 controllers = new LinkedList (); 390 samplers = new LinkedList (); 391 configElements = new LinkedList (); 392 assertions = new LinkedList (); 393 listeners = new LinkedList (); 394 postProcessors = new LinkedList (); 395 preProcessors = new LinkedList (); 396 nonTestElements = new LinkedList (); 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 iter = guiClasses.iterator(); 408 while (iter.hasNext()) 409 { 410 String name= (String )iter.next(); 411 412 418 if (name.endsWith("JMeterTreeNode") || name.endsWith("TestBeanGUI")) { 421 continue; } 423 424 JMeterGUIComponent item; 425 try 426 { 427 Class 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 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 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 e) 532 { 533 log.error("", e); 534 } 535 } 536 537 private static void addSeparator(JPopupMenu menu) 538 { 539 MenuElement [] elements = menu.getSubElements(); 540 if ((elements.length > 0) 541 && !(elements[elements.length - 1] instanceof JPopupMenu.Separator )) 542 { 543 menu.addSeparator(); 544 } 545 } 546 547 549 public static class Test extends JMeterTestCase 550 { 551 552 public Test() { 553 super(); 554 } 555 556 public Test(String name) { 557 super(name); 558 } 559 560 private static void check(String s,int i) throws Exception 561 { 562 assertFalse("The number of "+s+" should not be 0",0==i); 563 } 564 565 public void testMenu() throws Exception 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 |