1 18 19 package org.apache.jmeter.gui; 20 21 import java.awt.Component ; 22 import java.awt.event.MouseEvent ; 23 import java.beans.Introspector ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 import javax.swing.JPopupMenu ; 28 29 import org.apache.jmeter.engine.util.ValueReplacer; 30 import org.apache.jmeter.exceptions.IllegalUserActionException; 31 import org.apache.jmeter.gui.tree.JMeterTreeListener; 32 import org.apache.jmeter.gui.tree.JMeterTreeModel; 33 import org.apache.jmeter.gui.tree.JMeterTreeNode; 34 import org.apache.jmeter.testbeans.TestBean; 35 import org.apache.jmeter.testbeans.gui.TestBeanGUI; 36 import org.apache.jmeter.testelement.TestElement; 37 import org.apache.jmeter.testelement.TestPlan; 38 import org.apache.jmeter.util.JMeterUtils; 39 import org.apache.jmeter.util.LocaleChangeEvent; 40 import org.apache.jmeter.util.LocaleChangeListener; 41 import org.apache.jmeter.visualizers.gui.AbstractVisualizer; 42 import org.apache.jorphan.collections.HashTree; 43 import org.apache.jorphan.logging.LoggingManager; 44 import org.apache.log.Logger; 45 46 58 public final class GuiPackage implements LocaleChangeListener 59 { 60 61 private static transient Logger log = 62 LoggingManager.getLoggerForClass(); 63 64 65 private static GuiPackage guiPack; 66 67 71 private boolean dirty = false; 72 73 77 private Map nodesToGui = new HashMap (); 78 79 83 private Map guis = new HashMap (); 84 85 89 private Map testBeanGUIs= new HashMap (); 90 91 92 private JMeterTreeNode currentNode = null; 93 94 95 private JMeterTreeModel treeModel; 96 97 98 private JMeterTreeListener treeListener; 99 100 101 private MainFrame mainFrame; 102 103 104 108 private GuiPackage() 109 { 110 JMeterUtils.addLocaleChangeListener(this); 111 } 112 113 118 public static GuiPackage getInstance() 119 { 120 return guiPack; 121 } 122 123 132 public static GuiPackage getInstance( 133 JMeterTreeListener listener, 134 JMeterTreeModel treeModel) 135 { 136 if (guiPack == null) 137 { 138 guiPack = new GuiPackage(); 139 guiPack.setTreeListener(listener); 140 guiPack.setTreeModel(treeModel); 141 } 142 return guiPack; 143 } 144 145 159 public JMeterGUIComponent getGui(TestElement node) 160 { 161 String testClassName= node.getPropertyAsString(TestElement.TEST_CLASS); 162 String guiClassName= node.getPropertyAsString(TestElement.GUI_CLASS); 163 try 164 { 165 Class testClass; 166 if (testClassName.equals("")) 167 { 168 testClass= node.getClass(); 169 } 170 else 171 { 172 testClass= Class.forName(testClassName); 173 } 174 Class guiClass= null; 175 if (! guiClassName.equals("")) 176 { 177 guiClass= Class.forName(guiClassName); 178 } 179 return getGui(node, guiClass, testClass); 180 } 181 catch (ClassNotFoundException e) 182 { 183 log.error("Could not get GUI for " + node, e); 184 return null; 185 } 186 } 187 188 204 public JMeterGUIComponent getGui( 205 TestElement node, Class guiClass, Class testClass) 206 { 207 try 208 { 209 JMeterGUIComponent comp = (JMeterGUIComponent) nodesToGui.get(node); 210 if (comp == null) 211 { 212 comp = getGuiFromCache(guiClass, testClass); 213 nodesToGui.put(node, comp); 214 } 215 log.debug("Gui retrieved = " + comp); 216 return comp; 217 } 218 catch (Exception e) 219 { 220 log.error("Problem retrieving gui", e); 221 return null; 222 } 223 } 224 225 231 public void removeNode(TestElement node) 232 { 233 nodesToGui.remove(node); 234 } 235 236 241 public JMeterGUIComponent getCurrentGui() 242 { 243 try 244 { 245 TestElement currentNode = 246 treeListener.getCurrentNode().getTestElement(); 247 JMeterGUIComponent comp = getGui(currentNode); 248 if(!(comp instanceof AbstractVisualizer)) { 250 comp.clear(); 251 } 252 comp.configure(currentNode); 253 return comp; 254 } 255 catch (Exception e) 256 { 257 log.error("Problem retrieving gui", e); 258 return null; 259 } 260 } 261 262 267 public JMeterTreeNode getNodeOf(TestElement userObject) 268 { 269 return treeModel.getNodeOf(userObject); 270 } 271 272 281 public TestElement createTestElement(Class guiClass, Class testClass) 282 { 283 try 284 { 285 JMeterGUIComponent comp = getGuiFromCache(guiClass, testClass); 286 comp.clear(); 287 TestElement node = comp.createTestElement(); 288 nodesToGui.put(node, comp); 289 return node; 290 } 291 catch (Exception e) 292 { 293 log.error("Problem retrieving gui", e); 294 return null; 295 } 296 } 297 298 308 public TestElement createTestElement(String objClass) 309 { 310 JMeterGUIComponent comp; 311 Class c; 312 try 313 { 314 c= Class.forName(objClass); 315 if (TestBean.class.isAssignableFrom(c)) 316 { 317 comp= getGuiFromCache(TestBeanGUI.class, c); 318 } 319 else 320 { 321 comp= getGuiFromCache(c, null); 322 } 323 comp.clear(); 324 TestElement node = comp.createTestElement(); 325 nodesToGui.put(node, comp); 326 return node; 327 } 328 catch (ClassNotFoundException e) 329 { 330 log.error("Problem retrieving gui for "+objClass, e); 331 throw new Error (e.toString()); } catch (InstantiationException e) 333 { 334 log.error("Problem retrieving gui for "+objClass, e); 335 throw new Error (e.toString()); } catch (IllegalAccessException e) 337 { 338 log.error("Problem retrieving gui for "+objClass, e); 339 throw new Error (e.toString()); } 341 } 342 363 private JMeterGUIComponent getGuiFromCache(Class guiClass, Class testClass) 364 throws InstantiationException , 365 IllegalAccessException , 366 ClassNotFoundException 367 { 368 JMeterGUIComponent comp ; 369 if (guiClass == TestBeanGUI.class) 370 { 371 comp= (TestBeanGUI) testBeanGUIs.get(testClass); 372 if (comp == null) 373 { 374 comp= new TestBeanGUI(testClass); 375 testBeanGUIs.put(testClass, comp); 376 } 377 } 378 else 379 { 380 comp= (JMeterGUIComponent) guis.get(guiClass); 381 if (comp == null) 382 { 383 comp = (JMeterGUIComponent) guiClass.newInstance(); 384 if (!(comp instanceof UnsharedComponent)) 385 { 386 guis.put(guiClass, comp); 387 } 388 } 389 } 390 return comp; 391 } 392 393 398 public void updateCurrentGui() 399 { 400 currentNode= treeListener.getCurrentNode(); 401 TestElement element = currentNode.getTestElement(); 402 JMeterGUIComponent comp = getGui(element); 403 comp.configure(element); 404 } 405 406 411 public void updateCurrentNode() 412 { 413 try 414 { 415 if (currentNode != null) 416 { 417 log.debug( 418 "Updating current node " + currentNode.getName()); 419 JMeterGUIComponent comp = 420 getGui(currentNode.getTestElement()); 421 TestElement el = currentNode.getTestElement(); 422 comp.modifyTestElement(el); 423 } 424 currentNode = treeListener.getCurrentNode(); 425 } 426 catch (Exception e) 427 { 428 log.error("Problem retrieving gui", e); 429 } 430 } 431 432 public JMeterTreeNode getCurrentNode() 433 { 434 return treeListener.getCurrentNode(); 435 } 436 437 public TestElement getCurrentElement() 438 { 439 return getCurrentNode().getTestElement(); 440 } 441 442 450 public void setDirty(boolean dirty) 451 { 452 this.dirty = dirty; 453 } 454 455 463 public boolean isDirty() 464 { 465 return dirty; 466 } 467 468 478 public HashTree addSubTree(HashTree subTree) 479 throws IllegalUserActionException 480 { 481 return treeModel.addSubTree(subTree, treeListener.getCurrentNode()); 482 } 483 484 489 public HashTree getCurrentSubTree() 490 { 491 return treeModel.getCurrentSubTree(treeListener.getCurrentNode()); 492 } 493 494 499 public JMeterTreeModel getTreeModel() 500 { 501 return treeModel; 502 } 503 504 509 public void setTreeModel(JMeterTreeModel newTreeModel) 510 { 511 treeModel = newTreeModel; 512 } 513 514 519 public ValueReplacer getReplacer() 520 { 521 return new ValueReplacer( 522 (TestPlan) ((JMeterTreeNode) getTreeModel() 523 .getTestPlan() 524 .getArray()[0]) 525 .getTestElement()); 526 } 527 528 533 public void setMainFrame(MainFrame newMainFrame) 534 { 535 mainFrame = newMainFrame; 536 } 537 538 543 public MainFrame getMainFrame() 544 { 545 return mainFrame; 546 } 547 548 553 public void setTreeListener(JMeterTreeListener newTreeListener) 554 { 555 treeListener = newTreeListener; 556 } 557 558 563 public JMeterTreeListener getTreeListener() 564 { 565 return treeListener; 566 } 567 568 575 public void displayPopUp(MouseEvent e, JPopupMenu popup) 576 { 577 displayPopUp((Component ) e.getSource(), e, popup); 578 } 579 580 588 public void displayPopUp(Component invoker, MouseEvent e, JPopupMenu popup) 589 { 590 if (popup != null) 591 { 592 log.debug( 593 "Showing pop up for " + invoker 594 + " at x,y = " + e.getX() + "," + e.getY()); 595 596 popup.pack(); 597 popup.show(invoker, e.getX(), e.getY()); 598 popup.setVisible(true); 599 popup.requestFocus(); 600 } 601 } 602 603 606 public void localeChanged(LocaleChangeEvent event) 607 { 608 updateCurrentNode(); 611 612 guis= new HashMap (); 614 nodesToGui= new HashMap (); 615 testBeanGUIs= new HashMap (); 616 617 Introspector.flushCaches(); 619 620 MainFrame mf = getMainFrame(); if (mf == null) { 627 log.warn("Mainframe is null"); 628 } 629 else 630 { 631 mf.setMainPanel((javax.swing.JComponent ) getCurrentGui()); 632 mf.setEditMenu(getTreeListener().getCurrentNode().createPopupMenu()); 633 } 634 } 635 } | Popular Tags |