1 /* 2 * $Id$ 3 * 4 * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved. 5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6 * 7 * This code is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License version 2 only, as 9 * published by the Free Software Foundation. Oracle designates this 10 * particular file as subject to the "Classpath" exception as provided 11 * by Oracle in the LICENSE file that accompanied this code. 12 * 13 * This code is distributed in the hope that it will be useful, but WITHOUT 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16 * version 2 for more details (a copy is included in the LICENSE file that 17 * accompanied this code). 18 * 19 * You should have received a copy of the GNU General Public License version 20 * 2 along with this work; if not, write to the Free Software Foundation, 21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 22 * 23 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 24 * or visit www.oracle.com if you need additional information or have any 25 * questions. 26 */ 27 package com.sun.javatest.exec; 28 29 import com.sun.interview.Interview; 30 import java.awt.event.ActionEvent; 31 import java.io.File; 32 import java.io.FileNotFoundException; 33 import java.io.IOException; 34 import java.util.ArrayList; 35 import java.util.Map; 36 import javax.swing.Action; 37 import javax.swing.JFileChooser; 38 import javax.swing.JMenuBar; 39 import javax.swing.JMenuItem; 40 import javax.swing.JOptionPane; 41 42 import com.sun.javatest.InterviewParameters; 43 //import com.sun.javatest.TemplateUtilities; 44 import com.sun.javatest.TestSuite; 45 import com.sun.javatest.WorkDirectory; 46 import com.sun.javatest.tool.Desktop; 47 import com.sun.javatest.tool.FileOpener; 48 import com.sun.javatest.tool.Preferences; 49 import com.sun.javatest.tool.PreferencesPane; 50 import com.sun.javatest.tool.UIFactory; 51 import com.sun.javatest.tool.TestSuiteChooser; 52 import com.sun.javatest.tool.Tool; 53 import com.sun.javatest.tool.ToolAction; 54 import com.sun.javatest.tool.ToolManager; 55 import com.sun.javatest.tool.WorkDirChooser; 56 import javax.swing.JMenu; 57 import javax.swing.KeyStroke; 58 59 60 /** 61 * The ToolManager for {@link ExecTool test manager} windows. 62 */ 63 public class ExecToolManager extends ToolManager implements QSW_Listener 64 { 65 /** 66 * Create an ExecManager to manage the test manager windows on a desktop. 67 * @param desktop the desktop for which this manager is responsible 68 */ 69 public ExecToolManager(Desktop desktop) { 70 super(desktop); 71 emptyTool = new EmptyTool(this, "empty"); 72 } 73 74 @Override 75 public FileOpener[] getFileOpeners() { 76 return fileOpeners; 77 } 78 79 @Override 80 public Action[] getFileMenuActions() { 81 if (QuickStartWizard.isQswDisabled()) { 82 return new Action[0]; 83 } else { 84 return new Action[] {openQuickStartAction}; 85 } 86 87 // action is performed. 88 // The openWorkDirAction is always enabled, but we stash private data 89 // on it in case a default test suite is required. 90 /* 91 boolean done = false; 92 Desktop d = getDesktop(); 93 94 // if not got a test suite yet, check if the user's directory is a test suite; 95 // if so, remember that filename (don't open the test suite because it might not 96 // be needed.) 97 if (!done) { 98 try { 99 if (TestSuite.isTestSuite(userDir)) { 100 openWorkDirAction.putValue("testSuitePath", userDir); 101 done = true; 102 } 103 } 104 catch (Exception e) { 105 } 106 } 107 108 // if not got a test suite yet, check if the user's directory is a work directory; 109 // if so, remember that filename (don't open the test suite because it might not 110 // be needed.) 111 if (!done) { 112 try { 113 if (WorkDirectory.isWorkDirectory(userDir)) { 114 openWorkDirAction.putValue("workDirPath", userDir); 115 done = true; 116 } 117 } 118 catch (Exception e) { 119 } 120 } 121 122 123 // if not got a test suite yet, check if the JT installation directory or its parent 124 // is a test suite; if so, remember that filename (don't open the test suite because 125 // it might not be needed.) 126 if (!done) { 127 try { 128 File classDir = Harness.getClassDir(); 129 File installDir = (classDir == null ? null : classDir.getParentFile()); 130 File installParentDir = (installDir == null ? null : installDir.getParentFile()); 131 if (installDir != null && TestSuite.isTestSuite(installDir)) { 132 openWorkDirAction.putValue("testSuitePath", installDir); 133 done = true; 134 } 135 else if (installParentDir != null && TestSuite.isTestSuite(installParentDir)) { 136 openWorkDirAction.putValue("testSuitePath", installParentDir); 137 done = true; 138 } 139 } 140 catch (Exception e) { 141 } 142 } 143 144 if (QuickStartWizard.isQswDisabled()) { 145 fileMenuActions = new Action[1]; 146 // fileMenuActions[0] = newWorkDirAction; 147 } 148 149 return fileMenuActions; 150 */ 151 } 152 153 @Override 154 public JMenuItem[] getFileMenuPrimaries() { 155 156 // Tricky code: 157 // OpenWD action will behave differently depending on the current tool. 158 // If ExecTool is active and doesn't have a WD set - openWD will 159 // open WD for that ExecTool (for selected TestSuite) 160 // In all other cases it will cause opening new tab. 161 // It's not allowed to open or recreate WD if it's already open. 162 Desktop d = getDesktop(); 163 Tool currentTool = d.getSelectedTool(); 164 Action createWD = null; 165 Action openWD = openWorkDirAction; 166 if (currentTool != null && currentTool instanceof ExecTool) { 167 ExecTool et = (ExecTool)currentTool; 168 createWD = et.getCreateWDAction(); 169 openWD = et.getOpenWDAction(); 170 // Normally, create/open WD actions should become unavailable 171 // when a testsuite is associated with a WD. 172 // To make those JT users who are addicted to some strange behavior 173 // happy, the previous functionality is preserved. 174 // I don't abandon a hope to illuminate it some beautiful day though... 175 if (createWD == null || !createWD.isEnabled()) { 176 createWD = createWorkDirAction; 177 } 178 if (openWD == null || !openWD.isEnabled()) { 179 openWD = openWorkDirAction; 180 } 181 } 182 // 183 184 JMenu openMenu = new JMenu(i18n.getString("tmgr.openMenu.menu")); 185 openMenu.setName("tmgr.openMenu"); 186 // this craziness usually done by UIFactory 187 String keyString = i18n.getString("tmgr.openMenu.mne"); 188 KeyStroke keyStroke = KeyStroke.getKeyStroke(keyString); 189 openMenu.setMnemonic(keyStroke.getKeyCode()); 190 openMenu.getAccessibleContext().setAccessibleDescription(i18n.getString("tmgr.openMenu.desc")); 191 openMenu.add(new JMenuItem(openWD)); 192 openMenu.add(new JMenuItem(openTestSuiteAction)); 193 //openMenu.add(new JMenuItem(openConfigAction)); 194 195 return createWD == null ? new JMenuItem[] {openMenu} : 196 new JMenuItem[] {new JMenuItem(createWD), openMenu} ; 197 /* 198 return new JMenuItem[] { 199 new JMenuItem(openTestSuiteAction), 200 new JMenuItem(openWorkDirAction), 201 new JMenuItem(openConfigAction) 202 203 }; 204 */ 205 206 } 207 208 @Override 209 public JMenuItem[] getHelpPrimaryMenus() { 210 Desktop d = getDesktop(); 211 Tool t = d.getSelectedTool(); 212 if (t != null && (t instanceof ExecTool)) { 213 ExecTool et = (ExecTool)t; 214 ContextManager context = et.getContextManager(); 215 if (context == null) 216 return null; 217 218 JavaTestMenuManager mm = context.getMenuManager(); 219 if (mm != null) 220 return mm.getMenuItems(JavaTestMenuManager.HELP_PRIMARY); 221 else 222 return null; 223 } 224 return null; 225 } 226 227 @Override 228 public JMenuItem[] getHelpTestSuiteMenus() { 229 Desktop d = getDesktop(); 230 Tool t = d.getSelectedTool(); 231 if (t != null && (t instanceof ExecTool)) { 232 ExecTool et = (ExecTool)t; 233 ContextManager context = et.getContextManager(); 234 if (context == null) 235 return null; 236 237 JavaTestMenuManager mm = context.getMenuManager(); 238 if (mm != null) 239 return mm.getMenuItems(JavaTestMenuManager.HELP_TESTSUITE); 240 else 241 return null; 242 243 } 244 return null; 245 } 246 247 @Override 248 public JMenuItem[] getHelpAboutMenus() { 249 Desktop d = getDesktop(); 250 Tool t = d.getSelectedTool(); 251 if (t != null && (t instanceof ExecTool)) { 252 ExecTool et = (ExecTool)t; 253 ContextManager context = et.getContextManager(); 254 if (context == null) 255 return null; 256 257 JavaTestMenuManager mm = context.getMenuManager(); 258 if (mm != null) 259 return mm.getMenuItems(JavaTestMenuManager.HELP_ABOUT); 260 else 261 return null; 262 } 263 return null; 264 } 265 266 @Override 267 public Action[] getTaskMenuActions() { 268 return null; 269 } 270 271 @Override 272 public Action[] getWindowOpenMenuActions() { 273 if (QuickStartWizard.isQswDisabled()) { 274 return new Action[0]; 275 } else { 276 return new Action[] {openTMQSWAction}; 277 } 278 } 279 @Override 280 public PreferencesPane getPrefsPane() { 281 if (prefsPane == null) 282 prefsPane = new PrefsPane(getDesktop().getHelpBroker()); 283 return prefsPane; 284 } 285 286 287 /** 288 * If ExecTool have SINGLE_TEST_MANAGER enabled then 289 * this method check SINGLE_TEST_MANAGER in all 290 * loaded tools and return false if such found. 291 * 292 * @param newTool new tool which is added to Desktop 293 * @param d Desktop to add 294 * @return true if there is no conflict with SINGLE_TEST_MANAGER 295 * false otherwise 296 */ 297 boolean checkOpenNewTool(ExecTool newTool, Desktop d) { 298 return checkOpenNewTool(d, newTool.getContextManager()); 299 } 300 301 public boolean checkOpenNewTool(Desktop d, ContextManager conManager) { 302 if (conManager != null && conManager.getFeatureManager().isEnabled( 303 FeatureManager.SINGLE_TEST_MANAGER)) { 304 Tool[] tools = d.getTools(); 305 ArrayList<Tool> list = new ArrayList<>(); 306 for (int i = 0; i < tools.length; i++) { 307 if (tools[i] instanceof ExecTool) { 308 ExecTool tool = (ExecTool) tools[i]; 309 ContextManager cm = tool.getContextManager(); 310 if (cm != null) { 311 FeatureManager fm = cm.getFeatureManager(); 312 if (fm.isEnabled(FeatureManager.SINGLE_TEST_MANAGER)) { 313 // only single test manager 314 list.add(tools[i]); 315 } 316 } 317 } 318 } 319 if (list.isEmpty()) { 320 return true; 321 } 322 if (list.size() == 1) { 323 if (showCloseQuestion() == JOptionPane.YES_OPTION) { 324 ExecTool old = (ExecTool) list.get(0); 325 old.getDesktop().removeTool(old); 326 old.dispose(); 327 return true; 328 } else { 329 return false; 330 } 331 } 332 showError("tse.single"); 333 return false; 334 } 335 return true; 336 } 337 338 339 class EmptyTool extends Tool { 340 EmptyTool(ToolManager m, String uiKey) { 341 super(m, uiKey); 342 } 343 344 @Override 345 public JMenuBar getMenuBar() { 346 return new JMenuBar(); 347 } 348 349 @Override 350 protected void save(Map<String, String> m) { 351 } 352 353 @Override 354 protected void restore(Map<String, String> m) { 355 } 356 357 } 358 359 /** 360 * Shows Quick Configuration Editor if not disabled 361 * 362 * @return null 363 */ 364 public Tool startTool() { 365 showQSW(); 366 return null; 367 /* 368 Desktop d = getDesktop(); 369 Tool[] tools = d.getTools(); 370 if (tools != null) { 371 // check to see if there is an empty tool; if so, note it 372 for (Tool tool : tools) { 373 if (tool instanceof EmptyTool) { 374 return tool; 375 } 376 } 377 } 378 Tool t = new EmptyTool(this, "empty"); 379 d.addTool(t); 380 return t; 381 */ 382 } 383 384 /** 385 * Start an ExecTool for a particular configuration. 386 * @param p the configuration defining the tests and test results to be 387 * displayed 388 * @return the tool created to show the tests and test results specified 389 * by the configuration 390 */ 391 public Tool startTool(InterviewParameters p) { 392 if (p != null) { 393 return addNewExecTool(p.getTestSuite(), p.getWorkDirectory(), p, "tmgr.errorOpenConfig"); 394 } else { 395 return null; 396 } 397 } 398 399 public Tool restoreTool(Map<String, String> m) throws Fault { 400 try { 401 // 402 String tsp = m.get("testSuite"); 403 TestSuite ts = TestSuite.open(new File(tsp)); 404 ExecTool et = new ExecTool(this, ts); 405 et.restore(m); 406 return et; 407 408 /* 409 InterviewParameters ip = getInterview(m); 410 if (ip == null) { 411 return null; 412 } 413 414 415 TestSuite ts = ip.getTestSuite(); 416 if (ts == null) { 417 return null; 418 } 419 420 ExecTool et = new ExecTool(this, ts); 421 // it's better to rely on config.restore(m) although... 422 // may in be in the next life... 423 et.update(ip.getWorkDirectory()); 424 et.update(ip); 425 et.restore(m); 426 return et; 427 */ 428 } catch (Exception e) { 429 throw new Fault(i18n, "mgr.restoreFaultWD", e.getMessage()); 430 } 431 } 432 433 private static InterviewParameters getInterview(Map m) throws Interview.Fault { 434 String tsp = (String) (m.get("testSuite")); 435 String wdp = (String) (m.get("workDir")); 436 String cfp = (String) (m.get("config")); 437 if (isEmpty(tsp) && isEmpty(wdp) && isEmpty(cfp)) 438 return null; 439 440 return InterviewParameters.open(tsp, wdp, cfp); 441 } 442 private static boolean isEmpty(String s) { 443 return (s == null || s.length() == 0); 444 } 445 446 //------------------------------------------------------------------------- 447 448 /** 449 * Create an ExecTool instance using the given test suite. 450 * @param ts the test suite to seed the new tool with 451 * @return tool instance now associated with the given test suite 452 * @throws Interview.Fault if there is a problem initializing 453 * the test suite interview parameters 454 * @throws TestSuite.Fault if there is a problem while accessing the test 455 * suite object 456 * @deprecated 457 */ 458 @Deprecated 459 public ExecTool showTestSuite(TestSuite ts) 460 throws InterviewParameters.Fault, TestSuite.Fault 461 { 462 return addNewExecTool(ts, null, null, "tmgr.errorOpenTestSuite"); 463 464 /* 465 // check to see if there is an empty tool; if so select it 466 Desktop d = getDesktop(); 467 468 Tool[] tools = d.getTools(); 469 if (tools != null) { 470 // check to see if there is an empty tool; if so, note it 471 for (Tool tool : tools) { 472 if (tool instanceof EmptyTool) { 473 d.removeTool(tool); 474 } 475 } 476 } 477 478 try { 479 ExecTool t = new ExecTool(this, ts); 480 if (!checkOpenNewTool(t, d)) { 481 t.dispose(); 482 return null; 483 } 484 d.addTool(t); 485 d.setSelectedTool(t); 486 //d.addToFileHistory(ts.getRoot(), testSuiteOpener); 487 return t; 488 } catch (Session.Fault f) { 489 f.printStackTrace(); 490 return null; 491 } 492 */ 493 } 494 495 private TestSuiteChooser getTestSuiteChooser() { 496 if (testSuiteChooser == null) 497 testSuiteChooser = new TestSuiteChooser(); 498 499 return testSuiteChooser; 500 } 501 502 void addToFileHistory(TestSuite ts) { 503 // for 4.0, we think adding test suites is not useful 504 //getDesktop().addToFileHistory(ts.getRoot(), testSuiteOpener); 505 } 506 507 //------------------------------------------------------------------------- 508 509 /** 510 * Create an ExecTool instance using the given work directory. 511 * @param wd the work directory to open 512 * @return tool instance now associated with the given work directory 513 * @throws Interview.Fault if there is a problem initializing 514 * the test suite interview parameters 515 * @throws TestSuite.Fault if there is a problem while accessing the test 516 * suite object 517 * @deprecated 518 */ 519 @Deprecated 520 public ExecTool showWorkDirectory(WorkDirectory wd) 521 throws InterviewParameters.Fault, TestSuite.Fault 522 { 523 return addNewExecTool(wd.getTestSuite(), wd, null, "tmgr.errorOpenWorkDir"); 524 } 525 526 void addToFileHistory(WorkDirectory wd) { 527 getDesktop().addToFileHistory(wd.getRoot(), workDirOpener); 528 } 529 530 void showError(String key) { 531 showError(key, (Object[]) null); 532 } 533 534 void showError(String key, Object arg) { 535 showError(key, new Object[] { arg }); 536 } 537 538 void showError(String key, Object[] args) { 539 getUIF().showError(key, args); 540 } 541 542 int showCloseQuestion() { 543 return getUIF().showYesNoDialog("tse.closeCurrent"); 544 } 545 546 547 UIFactory getUIF() { 548 if (uif == null) 549 uif = new UIFactory(getClass(), getDesktop().getDialogParent(), getDesktop().getHelpBroker()); 550 return uif; 551 } 552 553 protected ExecTool addNewExecTool(TestSuite ts, WorkDirectory wd, 554 InterviewParameters ip, String errorKey) { 555 Desktop d = getDesktop(); 556 ExecTool et; 557 try { 558 et = new ExecTool(ExecToolManager.this, ts); 559 if (!checkOpenNewTool(et, d)) { 560 return null; 561 } 562 this.addToFileHistory(ts); 563 if (wd != null) { 564 et.update(wd, (ip == null)); 565 addToFileHistory(wd); 566 } 567 if (wd != null && ip != null) { 568 et.update(ip); 569 } 570 } catch (Session.Fault ex) { 571 showError(errorKey, ex.getMessage()); 572 return null; 573 } 574 d.addTool(et); 575 d.setSelectedTool(et); 576 return et; 577 } 578 579 //------------------------------------------------------------------------- 580 /** 581 * QSW_Listener interface method 582 * 583 * @param ts 584 * @param wd 585 * @param ip 586 * @param showConfigEditorFlag 587 * @param runTestsFlag 588 */ 589 public void finishQSW(TestSuite ts, WorkDirectory wd, InterviewParameters ip, 590 boolean showConfigEditorFlag, boolean runTestsFlag) { 591 592 qsw = null; 593 ExecTool et = addNewExecTool(ts, wd, ip, "tmgr.errorOpenWorkDir"); 594 if (et == null) { 595 return; 596 } 597 if (showConfigEditorFlag) { 598 et.showConfigEditor(); 599 } else if (runTestsFlag) { 600 et.runTests(); 601 } 602 603 } 604 /** 605 * QSW_Listener interface method 606 */ 607 public void cancelQSW() { 608 qsw = null; 609 } 610 611 /** 612 * To be invoked when user wants to open Quick Start Wizard 613 */ 614 public void showQSW() { 615 if (qsw != null || QuickStartWizard.isQswDisabled()) { 616 // QSW already running or disabled 617 return; 618 } 619 qsw = new QuickStartWizard(emptyTool, getDesktop().getLogo(), ExecToolManager.this, getUIF()); 620 qsw.setVisible(true); 621 } 622 623 QuickStartWizard qsw = null; 624 private Action openQuickStartAction = new ToolAction(i18n, "mgr.openQuickStart") { 625 public void actionPerformed(ActionEvent e) { 626 showQSW(); 627 } 628 }; 629 630 private Action openTMQSWAction = new ToolAction(i18n, "mgr.openTMQSW") { 631 public void actionPerformed(ActionEvent e) { 632 showQSW(); 633 } 634 }; 635 636 //------------------------------------------------------------------------- 637 638 private Action openTestSuiteAction = new ToolAction(i18n, "mgr.openTestSuite") { 639 public void actionPerformed(ActionEvent e) { 640 //System.err.println("EM:openTestSuiteAction " + e); 641 TestSuiteChooser tsc = getTestSuiteChooser(); 642 int action = tsc.showDialog(getDesktop().getDialogParent()); 643 if (action != JFileChooser.APPROVE_OPTION) { 644 return; 645 } 646 addNewExecTool(tsc.getSelectedTestSuite(), null, null, "tmgr.errorOpenTestSuite"); 647 tsc.setSelectedTestSuite(null); 648 } 649 }; 650 651 //------------------------------------------------------------------------- 652 653 private Action openWorkDirAction = new ToolAction(i18n, "mgr.openWorkDir") { 654 public void actionPerformed(ActionEvent e) { 655 WorkDirectory wd = WorkDirChooseTool.chooseWD(emptyTool, null, null, WorkDirChooser.OPEN_FOR_ANY_TESTSUITE); 656 if (wd == null) { 657 return; 658 } 659 addNewExecTool(wd.getTestSuite(), wd, null, "tmgr.errorOpenWorkDir"); 660 } 661 }; 662 private Action createWorkDirAction = new ToolAction(i18n, "mgr.createWorkDir") { 663 public void actionPerformed(ActionEvent e) { 664 665 Desktop d = getDesktop(); 666 Tool currentTool = d.getSelectedTool(); 667 if (currentTool == null || !(currentTool instanceof ExecTool)) { 668 return; 669 } 670 TestSuite ts = ((ExecTool)currentTool).getTestSuite(); 671 672 ExecTool et = addNewExecTool(ts, null, null, "tmgr.errorOpenTestSuite"); 673 if (et != null) { 674 Action act = et.getCreateWDAction(); 675 if (act != null && act.isEnabled()) { 676 act.actionPerformed(e); 677 } 678 } 679 } 680 }; 681 //------------------------------------------------------------------------- 682 683 684 //------------------------------------------------------------------------- 685 686 private FileOpener testSuiteOpener = new FileOpener() { 687 public String getFileType() { 688 return "testSuite"; 689 } 690 691 public void open(File f) throws FileNotFoundException, Fault { 692 try { 693 TestSuite ts = TestSuite.open(f); 694 addNewExecTool(ts, null, null, "tmgr.errorOpenTestSuite"); 695 } catch (TestSuite.Fault e) { 696 throw new Fault(i18n, "mgr.errorOpeningTestSuite", new Object[] { f, e }); 697 } 698 } 699 }; 700 701 //------------------------------------------------------------------------- 702 703 private FileOpener workDirOpener = new FileOpener() { 704 public String getFileType() { 705 return "workDirectory"; 706 } 707 708 public void open(File f) throws FileNotFoundException, Fault { 709 try { 710 WorkDirectory wd = WorkDirectory.open(f); 711 addNewExecTool(wd.getTestSuite(), wd, null, "tmgr.errorOpenWorkDir"); 712 } catch (WorkDirectory.Fault e) { 713 throw new Fault(i18n, "mgr.errorOpeningWorkDirectory", new Object[] { f, e.getMessage() }); 714 } 715 716 Preferences prefs = Preferences.access(); 717 try { 718 prefs.setPreference(WorkDirChooseTool.DEFAULT_WD_PREF_NAME, 719 f.getParentFile().getCanonicalPath()); 720 } 721 catch (IOException e) {} 722 } 723 }; 724 725 public boolean isQuickStartWizardActive() { 726 return qsw != null; 727 } 728 729 //------------------------------------------------------------------------- 730 731 private TestSuiteChooser testSuiteChooser; 732 private PrefsPane prefsPane; 733 private boolean doneQuickStart, doneWDChoser; 734 private UIFactory uif; 735 736 private static final String EXEC = "exec"; 737 private static final File userDir = new File(System.getProperty("user.dir")); 738 739 private final Tool emptyTool; 740 741 private FileOpener[] fileOpeners = { 742 testSuiteOpener, 743 workDirOpener 744 }; 745 /* 746 private Action[] fileMenuActions = { 747 openQuickStartAction, 748 //openWorkDirAction, 749 //openTestSuiteAction, 750 }; 751 */ 752 }