1 18 19 package org.apache.jmeter.gui.util; 20 import java.awt.Component ; 21 import java.awt.event.KeyEvent ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.util.LinkedList ; 25 26 import javax.swing.JMenu ; 27 import javax.swing.JMenuBar ; 28 import javax.swing.JMenuItem ; 29 import javax.swing.JPopupMenu ; 30 import javax.swing.KeyStroke ; 31 import javax.swing.MenuElement ; 32 import javax.swing.UIManager ; 33 34 import org.apache.jmeter.gui.action.ActionRouter; 35 import org.apache.jmeter.gui.action.ChangeLanguage; 36 import org.apache.jmeter.util.JMeterUtils; 37 import org.apache.jmeter.util.LocaleChangeEvent; 38 import org.apache.jmeter.util.LocaleChangeListener; 39 import org.apache.jmeter.util.SSLManager; 40 import org.apache.jorphan.util.JOrphanUtils; 41 import org.apache.jorphan.logging.LoggingManager; 42 import org.apache.log.Logger; 43 44 49 public class JMeterMenuBar extends JMenuBar implements LocaleChangeListener 50 { 51 transient private static Logger log = LoggingManager.getLoggerForClass(); 52 JMenu fileMenu; 53 JMenuItem file_save_as; 54 JMenuItem file_load; 55 JMenuItem file_merge; 56 JMenuItem file_exit; 57 JMenuItem file_close; 58 JMenu editMenu; 59 JMenu edit_add; 60 JMenuItem edit_remove; 62 JMenu runMenu; 63 JMenuItem run_start; 64 JMenu remote_start; 65 JMenuItem remote_start_all; 66 Collection remote_engine_start; 67 JMenuItem run_stop; 68 private JMenuItem run_shut; JMenu remote_stop; 70 JMenuItem remote_stop_all; 71 Collection remote_engine_stop; 72 JMenuItem run_clear; 73 JMenuItem run_clearAll; 74 JMenu optionsMenu; 77 JMenu lafMenu; 78 JMenuItem sslManager; 79 JMenu helpMenu; 80 JMenuItem help_about; 81 String [] remoteHosts; 82 83 private JMenu remote_exit; 84 private JMenuItem remote_exit_all; 85 private Collection remote_engine_exit; 86 87 public JMeterMenuBar() 88 { 89 remote_engine_start = new LinkedList (); 90 remote_engine_stop = new LinkedList (); 91 remote_engine_exit = new LinkedList (); 92 remoteHosts = 93 JOrphanUtils.split( 94 JMeterUtils.getPropDefault("remote_hosts", ""), 95 ","); 96 if (remoteHosts.length == 1 && remoteHosts[0].equals("")) 97 { 98 remoteHosts = new String [0]; 99 } 100 this.getRemoteItems(); 101 createMenuBar(); 102 } 103 104 public void setFileSaveEnabled(boolean enabled) 105 { 106 file_save_as.setEnabled(enabled); 107 } 108 109 public void setFileLoadEnabled(boolean enabled) 110 { 111 if (file_load != null) 112 { 113 file_load.setEnabled(enabled); 114 } 115 if (file_merge != null) 116 { 117 file_merge.setEnabled(enabled); 118 } 119 } 120 121 public void setEditEnabled(boolean enabled) 122 { 123 if (editMenu != null) 124 { 125 editMenu.setEnabled(enabled); 126 } 127 } 128 129 public void setEditAddMenu(JMenu menu) 130 { 131 if (edit_add != null) 133 { 134 editMenu.remove(edit_add); 135 } 136 edit_add = menu; 138 editMenu.insert(edit_add, 0); 139 } 140 141 public void setEditMenu(JPopupMenu menu) 142 { 143 if (menu != null) 144 { 145 editMenu.removeAll(); 146 Component [] comps = menu.getComponents(); 147 for (int i = 0; i < comps.length; i++) 148 { 149 editMenu.add(comps[i]); 150 } 151 editMenu.setEnabled(true); 152 } 153 else 154 { 155 editMenu.setEnabled(false); 156 } 157 } 158 159 public void setEditAddEnabled(boolean enabled) 160 { 161 if (edit_add != null) 163 { 164 edit_add.setEnabled(enabled); 165 } 166 if (enabled) 170 { 171 setEditEnabled(true); 172 } 173 else 174 { 175 if (!edit_remove.isEnabled()) 181 { 182 editMenu.setEnabled(false); 183 } 184 } 185 } 186 187 public void setEditRemoveEnabled(boolean enabled) 188 { 189 edit_remove.setEnabled(enabled); 190 if (enabled) 194 { 195 setEditEnabled(true); 196 } 197 else 198 { 199 if (!edit_add.isEnabled()) 205 { 206 editMenu.setEnabled(false); 207 } 208 } 209 } 210 211 215 public void createMenuBar() 216 { 217 makeFileMenu(); 218 makeEditMenu(); 219 makeRunMenu(); 220 makeOptionsMenu(); 221 makeHelpMenu(); 222 this.add(fileMenu); 223 this.add(editMenu); 224 this.add(runMenu); 225 this.add(optionsMenu); 226 this.add(helpMenu); 227 } 228 229 private void makeHelpMenu() 230 { 231 helpMenu = new JMenu (JMeterUtils.getResString("help")); 233 helpMenu.setMnemonic('H'); 234 JMenuItem contextHelp = 235 new JMenuItem (JMeterUtils.getResString("help"), 'H'); 236 contextHelp.setActionCommand("help"); 237 contextHelp.setAccelerator( 238 KeyStroke.getKeyStroke(KeyEvent.VK_H, KeyEvent.CTRL_MASK)); 239 contextHelp.addActionListener(ActionRouter.getInstance()); 240 help_about = new JMenuItem (JMeterUtils.getResString("about"), 'A'); 241 help_about.setActionCommand("about"); 242 help_about.addActionListener(ActionRouter.getInstance()); 243 helpMenu.add(contextHelp); 244 helpMenu.add(help_about); 245 } 246 247 private void makeOptionsMenu() 248 { 249 optionsMenu = new JMenu (JMeterUtils.getResString("option")); 251 JMenuItem functionHelper = 252 new JMenuItem ( 253 JMeterUtils.getResString("function_dialog_menu_item"), 254 'F'); 255 functionHelper.addActionListener(ActionRouter.getInstance()); 256 functionHelper.setActionCommand("functions"); 257 functionHelper.setAccelerator( 258 KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.CTRL_MASK)); 259 lafMenu = new JMenu (JMeterUtils.getResString("appearance")); 260 UIManager.LookAndFeelInfo lafs[] = UIManager.getInstalledLookAndFeels(); 261 for (int i = 0; i < lafs.length; ++i) 262 { 263 JMenuItem laf = new JMenuItem (lafs[i].getName()); 264 laf.addActionListener(ActionRouter.getInstance()); 265 laf.setActionCommand("laf:" + lafs[i].getClassName()); 266 lafMenu.setMnemonic('L'); 267 lafMenu.add(laf); 268 } 269 optionsMenu.setMnemonic('O'); 270 optionsMenu.add(functionHelper); 271 optionsMenu.add(lafMenu); 272 if (SSLManager.isSSLSupported()) 273 { 274 sslManager = new JMenuItem (JMeterUtils.getResString("sslManager")); 275 sslManager.addActionListener(ActionRouter.getInstance()); 276 sslManager.setActionCommand("sslManager"); 277 sslManager.setMnemonic('S'); 278 sslManager.setAccelerator( 279 KeyStroke.getKeyStroke(KeyEvent.VK_M, KeyEvent.CTRL_MASK)); 280 optionsMenu.add(sslManager); 281 } 282 optionsMenu.add(makeLanguageMenu()); 283 } 284 285 private JMenu makeLanguageMenu() 286 { 287 JMenu languageMenu = 288 new JMenu (JMeterUtils.getResString("choose_language")); 289 languageMenu.setMnemonic('C'); 290 JMenuItem english = new JMenuItem (JMeterUtils.getResString("en"), 'E'); 292 english.addActionListener(ActionRouter.getInstance()); 293 english.setActionCommand(ChangeLanguage.CHANGE_LANGUAGE); 294 english.setName("en"); 295 languageMenu.add(english); 296 JMenuItem japanese = new JMenuItem (JMeterUtils.getResString("jp"), 'J'); 298 japanese.addActionListener(ActionRouter.getInstance()); 299 japanese.setActionCommand(ChangeLanguage.CHANGE_LANGUAGE); 300 japanese.setName("ja"); 301 languageMenu.add(japanese); 302 JMenuItem norway = new JMenuItem (JMeterUtils.getResString("no"), 'N'); 304 norway.addActionListener(ActionRouter.getInstance()); 305 norway.setActionCommand(ChangeLanguage.CHANGE_LANGUAGE); 306 norway.setName("no"); 307 languageMenu.add(norway); 308 JMenuItem german = new JMenuItem (JMeterUtils.getResString("de"), 'G'); 310 german.addActionListener(ActionRouter.getInstance()); 311 german.setActionCommand(ChangeLanguage.CHANGE_LANGUAGE); 312 german.setName("de"); 313 languageMenu.add(german); 314 JMenuItem french = new JMenuItem (JMeterUtils.getResString("fr"), 'F'); 316 french.addActionListener(ActionRouter.getInstance()); 317 french.setActionCommand(ChangeLanguage.CHANGE_LANGUAGE); 318 french.setName("fr"); 319 languageMenu.add(french); 320 JMenuItem chinese = new JMenuItem (JMeterUtils.getResString("cn"), 'C'); 322 chinese.addActionListener(ActionRouter.getInstance()); 323 chinese.setActionCommand(ChangeLanguage.CHANGE_LANGUAGE); 324 chinese.setName("cn"); 325 languageMenu.add(chinese); 326 return languageMenu; 327 } 328 329 333 public static final String ACTION_SHUTDOWN = "shutdown"; 334 public static final String ACTION_STOP = "stop"; 335 public static final String ACTION_START = "start"; 336 337 338 private void makeRunMenu() 339 { 340 runMenu = new JMenu (JMeterUtils.getResString("run")); 342 runMenu.setMnemonic('R'); 343 run_start = new JMenuItem (JMeterUtils.getResString("start"), 'S'); 344 run_start.setAccelerator( 345 KeyStroke.getKeyStroke(KeyEvent.VK_R, KeyEvent.CTRL_MASK)); 346 run_start.addActionListener(ActionRouter.getInstance()); 347 run_start.setActionCommand(ACTION_START); 348 run_stop = new JMenuItem (JMeterUtils.getResString("stop"), 'T'); 349 run_stop.setAccelerator( 350 KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, KeyEvent.CTRL_MASK)); 351 run_stop.setEnabled(false); 352 run_stop.addActionListener(ActionRouter.getInstance()); 353 run_stop.setActionCommand(ACTION_STOP); 354 355 run_shut = new JMenuItem (JMeterUtils.getResString("shutdown"), 'Y'); 356 run_shut.setAccelerator( 357 KeyStroke.getKeyStroke(KeyEvent.VK_COMMA, KeyEvent.CTRL_MASK)); 358 run_shut.setEnabled(false); 359 run_shut.addActionListener(ActionRouter.getInstance()); 360 run_shut.setActionCommand(ACTION_SHUTDOWN); 361 362 run_clear = new JMenuItem (JMeterUtils.getResString("clear"), 'C'); 363 run_clear.addActionListener(ActionRouter.getInstance()); 364 run_clear.setActionCommand(org.apache.jmeter.gui.action.Clear.CLEAR); 365 run_clearAll = 366 new JMenuItem (JMeterUtils.getResString("clear_all"), 'a'); 367 run_clearAll.addActionListener(ActionRouter.getInstance()); 368 run_clearAll.setActionCommand( 369 org.apache.jmeter.gui.action.Clear.CLEAR_ALL); 370 run_clearAll.setAccelerator( 371 KeyStroke.getKeyStroke(KeyEvent.VK_E, KeyEvent.CTRL_MASK)); 372 runMenu.add(run_start); 373 if (remote_start != null) 374 { 375 runMenu.add(remote_start); 376 } 377 remote_start_all = 378 new JMenuItem (JMeterUtils.getResString("remote_start_all"), 'Z'); 379 remote_start_all.setName("remote_start_all"); 380 remote_start_all.setAccelerator( 381 KeyStroke.getKeyStroke(KeyEvent.VK_Z, KeyEvent.CTRL_MASK)); 382 remote_start_all.addActionListener(ActionRouter.getInstance()); 383 remote_start_all.setActionCommand("remote_start_all"); 384 runMenu.add(remote_start_all); 385 runMenu.add(run_stop); 386 runMenu.add(run_shut); 387 if (remote_stop != null) 388 { 389 runMenu.add(remote_stop); 390 } 391 remote_stop_all = 392 new JMenuItem (JMeterUtils.getResString("remote_stop_all"), 'X'); 393 remote_stop_all.setAccelerator( 394 KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.ALT_MASK)); 395 remote_stop_all.addActionListener(ActionRouter.getInstance()); 396 remote_stop_all.setActionCommand("remote_stop_all"); 397 runMenu.add(remote_stop_all); 398 399 if (remote_exit != null) 400 { 401 runMenu.add(remote_exit); 402 } 403 remote_exit_all = 404 new JMenuItem (JMeterUtils.getResString("remote_exit_all")); 405 remote_exit_all.addActionListener(ActionRouter.getInstance()); 406 remote_exit_all.setActionCommand("remote_exit_all"); 407 runMenu.add(remote_exit_all); 408 409 410 runMenu.addSeparator(); 411 runMenu.add(run_clear); 412 runMenu.add(run_clearAll); 413 } 414 415 private void makeEditMenu() 416 { 417 editMenu = new JMenu (JMeterUtils.getResString("edit")); 419 editMenu.setEnabled(false); 422 } 423 424 private void makeFileMenu() 425 { 426 fileMenu = new JMenu (JMeterUtils.getResString("file")); 428 fileMenu.setMnemonic('F'); 429 JMenuItem file_save = 430 new JMenuItem (JMeterUtils.getResString("save"), 'S'); 431 file_save.setAccelerator( 432 KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK)); 433 file_save.setActionCommand("save"); 434 file_save.addActionListener(ActionRouter.getInstance()); 435 file_save.setEnabled(true); 436 437 file_save_as = 438 new JMenuItem (JMeterUtils.getResString("save_all_as"), 'A'); 439 file_save_as.setAccelerator( 440 KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK)); 441 file_save_as.setActionCommand("save_all_as"); 442 file_save_as.addActionListener(ActionRouter.getInstance()); 443 file_save_as.setEnabled(true); 444 445 file_load = new JMenuItem (JMeterUtils.getResString("menu_open"), 'O'); 446 file_load.setAccelerator( 447 KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.CTRL_MASK)); 448 file_load.addActionListener(ActionRouter.getInstance()); 449 file_load.setEnabled(false); 452 file_load.setActionCommand("open"); 453 454 file_close = new JMenuItem (JMeterUtils.getResString("menu_close"), 'C'); 455 file_close.setAccelerator( 456 KeyStroke.getKeyStroke(KeyEvent.VK_L, KeyEvent.CTRL_MASK)); 457 file_close.setActionCommand("close"); 458 file_close.addActionListener(ActionRouter.getInstance()); 459 460 file_exit = new JMenuItem (JMeterUtils.getResString("exit"), 'X'); 461 file_exit.setAccelerator( 462 KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_MASK)); 463 file_exit.setActionCommand("exit"); 464 file_exit.addActionListener(ActionRouter.getInstance()); 465 466 file_merge = new JMenuItem (JMeterUtils.getResString("menu_merge"), 'M'); 467 file_merge.addActionListener(ActionRouter.getInstance()); 470 file_merge.setEnabled(false); 473 file_merge.setActionCommand("merge"); 474 475 fileMenu.add(file_close); 476 fileMenu.add(file_load); 477 fileMenu.add(file_merge); 478 fileMenu.add(file_save); 479 fileMenu.add(file_save_as); 480 fileMenu.addSeparator(); 481 fileMenu.add(file_exit); 482 } 483 484 public void setRunning(boolean running, String host) 485 { 486 log.info("setRunning("+ running +","+ host + ")"); 487 488 Iterator iter = remote_engine_start.iterator(); 489 Iterator iter2 = remote_engine_stop.iterator(); 490 Iterator iter3 = remote_engine_exit.iterator(); 491 while (iter.hasNext() && iter2.hasNext() && iter3.hasNext()) 492 { 493 JMenuItem start = (JMenuItem ) iter.next(); 494 JMenuItem stop = (JMenuItem ) iter2.next(); 495 JMenuItem exit = (JMenuItem ) iter3.next(); 496 if (start.getText().equals(host)) 497 { 498 log.info("Found start host: " + start.getText()); 499 start.setEnabled(!running); 500 } 501 if (stop.getText().equals(host)) 502 { 503 log.info("Found stop host: " + stop.getText()); 504 stop.setEnabled(running); 505 } 506 if (exit.getText().equals(host)) 507 { 508 log.info("Found exit host: " + exit.getText()); 509 exit.setEnabled(true); 510 } 511 } 512 } 513 514 public void setEnabled(boolean enable) 515 { 516 run_start.setEnabled(!enable); 517 run_stop.setEnabled(enable); 518 run_shut.setEnabled(enable); 519 } 520 521 private void getRemoteItems() 522 { 523 if (remoteHosts.length > 0) 524 { 525 remote_start = new JMenu (JMeterUtils.getResString("remote_start")); 526 remote_stop = new JMenu (JMeterUtils.getResString("remote_stop")); 527 remote_exit = new JMenu (JMeterUtils.getResString("remote_exit")); 528 529 for (int i = 0; i < remoteHosts.length; i++) 530 { 531 remoteHosts[i] = remoteHosts[i].trim(); 532 JMenuItem item = new JMenuItem (remoteHosts[i]); 533 item.setActionCommand("remote_start"); 534 item.setName(remoteHosts[i]); 535 item.addActionListener(ActionRouter.getInstance()); 536 remote_engine_start.add(item); 537 remote_start.add(item); 538 item = new JMenuItem (remoteHosts[i]); 539 item.setActionCommand("remote_stop"); 540 item.setName(remoteHosts[i]); 541 item.addActionListener(ActionRouter.getInstance()); 542 item.setEnabled(false); 543 remote_engine_stop.add(item); 544 remote_stop.add(item); 545 item = new JMenuItem (remoteHosts[i]); 546 item.setActionCommand("remote_exit"); 547 item.setName(remoteHosts[i]); 548 item.addActionListener(ActionRouter.getInstance()); 549 item.setEnabled(false); 550 remote_engine_exit.add(item); 551 remote_exit.add(item); 552 } 553 } 554 } 555 556 560 public void localeChanged(LocaleChangeEvent event) 561 { 562 updateMenuElement(fileMenu); 563 updateMenuElement(editMenu); 564 updateMenuElement(runMenu); 565 updateMenuElement(optionsMenu); 566 updateMenuElement(helpMenu); 567 } 568 569 572 private void updateMenuElement(MenuElement menu) 573 { 574 Component component = menu.getComponent(); 575 576 if (component.getName() != null) 577 { 578 if (component instanceof JMenu ) 579 { 580 ((JMenu ) component).setText( 581 JMeterUtils.getResString(component.getName())); 582 } 583 else 584 { 585 ((JMenuItem ) component).setText( 586 JMeterUtils.getResString(component.getName())); 587 } 588 } 589 590 MenuElement [] subelements = menu.getSubElements(); 591 592 for (int i = 0; i < subelements.length; i++) 593 { 594 updateMenuElement(subelements[i]); 595 } 596 } 597 } 598 | Popular Tags |