1 18 19 package org.apache.jmeter.gui; 20 import java.awt.BorderLayout ; 21 import java.awt.Component ; 22 import java.awt.Dimension ; 23 import java.awt.Font ; 24 import java.awt.Insets ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.awt.event.WindowAdapter ; 28 import java.awt.event.WindowEvent ; 29 import java.util.HashSet ; 30 import java.util.Set ; 31 32 import javax.swing.BorderFactory ; 33 import javax.swing.Box ; 34 import javax.swing.BoxLayout ; 35 import javax.swing.ImageIcon ; 36 import javax.swing.JButton ; 37 import javax.swing.JComponent ; 38 import javax.swing.JDialog ; 39 import javax.swing.JFrame ; 40 import javax.swing.JLabel ; 41 import javax.swing.JMenu ; 42 import javax.swing.JPanel ; 43 import javax.swing.JPopupMenu ; 44 import javax.swing.JScrollPane ; 45 import javax.swing.JSplitPane ; 46 import javax.swing.JTree ; 47 import javax.swing.MenuElement ; 48 import javax.swing.SwingUtilities ; 49 import javax.swing.tree.DefaultTreeCellRenderer ; 50 import javax.swing.tree.TreeCellRenderer ; 51 import javax.swing.tree.TreeModel ; 52 53 import org.apache.jmeter.engine.event.LoopIterationEvent; 54 import org.apache.jmeter.gui.action.ActionRouter; 55 import org.apache.jmeter.gui.action.GlobalMouseListener; 56 import org.apache.jmeter.gui.tree.JMeterCellRenderer; 57 import org.apache.jmeter.gui.tree.JMeterTreeListener; 58 import org.apache.jmeter.gui.util.JMeterMenuBar; 59 import org.apache.jmeter.samplers.Remoteable; 60 import org.apache.jmeter.testelement.TestListener; 61 import org.apache.jmeter.util.JMeterUtils; 62 import org.apache.jorphan.gui.ComponentUtil; 63 64 71 public class MainFrame extends JFrame implements TestListener, Remoteable 72 { 73 74 75 private JMeterMenuBar menuBar; 76 77 78 private JScrollPane mainPanel; 79 80 81 private JScrollPane treePanel; 82 83 84 private JTree tree; 85 86 87 88 private ImageIcon runningIcon = JMeterUtils.getImage("thread.enabled.gif"); 89 90 91 private ImageIcon stoppedIcon = JMeterUtils.getImage("thread.disabled.gif"); 92 93 94 private JButton runningIndicator; 95 96 97 98 private int previousDragXLocation = 0; 99 100 101 private int previousDragYLocation = 0; 102 103 104 private Set hosts = new HashSet (); 105 106 107 private JDialog stoppingMessage; 108 109 110 117 public MainFrame( 118 ActionListener actionHandler, 119 TreeModel treeModel, 120 JMeterTreeListener treeListener) 121 { 122 125 runningIndicator = new JButton (stoppedIcon); 127 runningIndicator.setMargin(new Insets (0, 0, 0, 0)); 128 runningIndicator.setBorder(BorderFactory.createEmptyBorder()); 129 130 tree = makeTree(treeModel, treeListener); 131 132 GuiPackage.getInstance().setMainFrame(this); 133 init(); 134 135 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 136 } 137 138 142 public MainFrame() 143 { 144 } 147 148 149 153 158 public void setFileLoadEnabled(boolean enabled) 159 { 160 menuBar.setFileLoadEnabled(enabled); 161 } 162 163 168 public void setFileSaveEnabled(boolean enabled) 169 { 170 menuBar.setFileSaveEnabled(enabled); 171 } 172 173 178 public void setEditMenu(JPopupMenu menu) 179 { 180 menuBar.setEditMenu(menu); 181 } 182 183 188 public void setEditEnabled(boolean enabled) 189 { 190 menuBar.setEditEnabled(enabled); 191 } 192 193 198 public void setEditAddMenu(JMenu menu) 199 { 200 menuBar.setEditAddMenu(menu); 201 } 202 203 208 public void setEditAddEnabled(boolean enabled) 209 { 210 menuBar.setEditAddEnabled(enabled); 211 } 212 213 218 public void setEditRemoveEnabled(boolean enabled) 219 { 220 menuBar.setEditRemoveEnabled(enabled); 221 } 222 223 226 public void closeMenu() 227 { 228 if (menuBar.isSelected()) 229 { 230 MenuElement [] menuElement = menuBar.getSubElements(); 231 if (menuElement != null) 232 { 233 for (int i = 0; i < menuElement.length; i++) 234 { 235 JMenu menu = (JMenu ) menuElement[i]; 236 if (menu.isSelected()) 237 { 238 menu.setPopupMenuVisible(false); 239 menu.setSelected(false); 240 break; 241 } 242 } 243 } 244 } 245 } 246 247 253 public void showStoppingMessage(String host) 254 { 255 stoppingMessage = 256 new JDialog ( 257 this, 258 JMeterUtils.getResString("stopping_test_title"), 259 true); 260 JLabel stopLabel = 261 new JLabel (JMeterUtils.getResString("stopping_test") + ": " + host); 262 stopLabel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); 263 stoppingMessage.getContentPane().add(stopLabel); 264 stoppingMessage.pack(); 265 ComponentUtil.centerComponentInComponent(this, stoppingMessage); 266 SwingUtilities.invokeLater(new Runnable () 267 { 268 public void run() 269 { 270 if (stoppingMessage != null) 271 { 272 stoppingMessage.show(); 273 } 274 } 275 }); 276 } 277 278 283 public void setMainPanel(JComponent comp) 284 { 285 mainPanel.setViewportView(comp); 286 } 287 288 293 public JTree getTree() 294 { 295 return tree; 296 } 297 298 299 300 302 307 public void testStarted() 308 { 309 testStarted("local"); 310 menuBar.setEnabled(true); 311 } 312 313 320 public void testStarted(String host) 321 { 322 hosts.add(host); 323 runningIndicator.setIcon(runningIcon); 324 menuBar.setRunning(true, host); 325 } 326 327 332 public void testEnded() 333 { 334 testEnded("local"); 335 menuBar.setEnabled(false); 336 } 337 338 344 public void testEnded(String host) 345 { 346 hosts.remove(host); 347 if (hosts.size() == 0) 348 { 349 runningIndicator.setIcon(stoppedIcon); 350 } 351 menuBar.setRunning(false, host); 352 if (stoppingMessage != null) 353 { 354 stoppingMessage.dispose(); 355 stoppingMessage = null; 356 } 357 } 358 359 360 public void testIterationStart(LoopIterationEvent event) 361 { 362 } 363 364 365 366 369 private void init() 370 { 371 menuBar = new JMeterMenuBar(); 372 setJMenuBar(menuBar); 373 374 375 JPanel all = new JPanel (new BorderLayout ()); 376 all.add(createToolBar(), BorderLayout.NORTH); 377 378 JSplitPane treeAndMain = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT); 379 380 treePanel = createTreePanel(); 381 treeAndMain.setLeftComponent(treePanel); 382 383 mainPanel = createMainPanel(); 384 treeAndMain.setRightComponent(mainPanel); 385 386 387 treeAndMain.setResizeWeight(.2); 388 treeAndMain.setContinuousLayout(true); 389 all.add(treeAndMain, BorderLayout.CENTER); 390 391 getContentPane().add(all); 392 393 tree.setSelectionRow(1); 394 addWindowListener(new WindowHappenings()); 395 addMouseListener(new GlobalMouseListener()); 396 } 397 398 403 private Component createToolBar() 404 { 405 Box toolPanel = new Box (BoxLayout.X_AXIS); 406 toolPanel.add(Box.createRigidArea(new Dimension (10, 15))); 407 toolPanel.add(Box.createGlue()); 408 toolPanel.add(runningIndicator); 409 return toolPanel; 410 } 411 412 419 private JScrollPane createTreePanel() 420 { 421 JScrollPane treePanel = new JScrollPane (tree); 422 treePanel.setMinimumSize(new Dimension (100, 0)); 423 return treePanel; 424 } 425 426 431 private JScrollPane createMainPanel() 432 { 433 return new JScrollPane (); 434 } 435 436 444 private JTree makeTree(TreeModel treeModel, JMeterTreeListener treeListener) 445 { 446 JTree tree = new JTree (treeModel); 447 tree.setCellRenderer(getCellRenderer()); 448 tree.setRootVisible(false); 449 tree.setShowsRootHandles(true); 450 451 treeListener.setJTree(tree); 452 tree.addTreeSelectionListener(treeListener); 453 tree.addMouseListener(treeListener); 454 tree.addMouseMotionListener(treeListener); 455 tree.addKeyListener(treeListener); 456 457 return tree; 458 } 459 460 465 private TreeCellRenderer getCellRenderer() 466 { 467 DefaultTreeCellRenderer rend = new JMeterCellRenderer(); 468 rend.setFont(new Font ("Dialog", Font.PLAIN, 11)); 469 return rend; 470 } 471 472 480 public void drawDraggedComponent(Component dragIcon, int x, int y) 481 { 482 Dimension size = dragIcon.getPreferredSize(); 483 treePanel.paintImmediately( 484 previousDragXLocation, 485 previousDragYLocation, 486 size.width, 487 size.height); 488 this.getLayeredPane().setLayer(dragIcon, 400); 489 SwingUtilities.paintComponent( 490 treePanel.getGraphics(), 491 dragIcon, 492 treePanel, 493 x, 494 y, 495 size.width, 496 size.height); 497 previousDragXLocation = x; 498 previousDragYLocation = y; 499 } 500 501 502 506 private class WindowHappenings extends WindowAdapter 507 { 508 514 public void windowClosing(WindowEvent event) 515 { 516 ActionRouter.getInstance().actionPerformed( 517 new ActionEvent (this, event.getID(), "exit")); 518 } 519 } 520 } 521 | Popular Tags |