1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 2010, 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 
  28 
  29 package com.sun.javatest.exec;
  30 
  31 import com.sun.javatest.TestSuite;
  32 import com.sun.javatest.services.ServiceManager;
  33 import com.sun.javatest.tool.ToolAction;
  34 import com.sun.javatest.tool.UIFactory;
  35 import com.sun.javatest.util.Debug;
  36 import java.awt.event.ActionEvent;
  37 import java.util.LinkedList;
  38 import java.util.List;
  39 import java.util.Map;
  40 import javax.swing.Action;
  41 import javax.swing.JComponent;
  42 import javax.swing.JMenu;
  43 import javax.swing.JMenuItem;
  44 
  45 /**
  46  *
  47  * @author Dmitry Fazunenko
  48  */
  49 public class ET_DefaultViewControl implements ET_ViewControl {
  50 
  51     SessionExt config = null;
  52     UIFactory uif = null;
  53     JComponent parent = null;
  54     TestSuite testSuite;
  55     List<Action> actions = null;
  56     ExecModel execModel = null;
  57     EnvironmentBrowser environmentBrowser = null;
  58     ExcludeListBrowser excludeListBrowser = null;
  59     QuestionLogBrowser questionLogBrowser = null;
  60     ChecklistBrowser checkListBrowser = null;
  61     ET_FilterControl filterControl = null;
  62     private static int debug = Debug.getInt(BasicSessionControl.class);
  63 
  64     public ET_DefaultViewControl(JComponent parent, TestSuite ts,
  65             ExecModel execModel, UIFactory uif, ET_FilterControl filterControl) {
  66         this.parent = parent;
  67         this.uif = uif;
  68         this.testSuite = ts;
  69         this.execModel = execModel;
  70         this.actions = createActions();
  71         this.filterControl = filterControl;
  72     }
  73 
  74     public void setConfig(Session cfg) {
  75         if (cfg instanceof SessionExt) {
  76             config = (SessionExt)cfg;
  77         } else {
  78             throw new Error(uif.getI18NString("bcc.notSessionExtInstance.err", cfg.getClass()));
  79         }
  80     }
  81 
  82     public Session getConfig() {
  83         return config;
  84     }
  85 
  86     public void updateGUI() {
  87         boolean isWD  = (config != null && config.getWorkDirectory() != null);
  88 
  89         // we always allow View/Properties action
  90         // even if config and/or wd is not set
  91         propertiesAction.setEnabled(true);
  92 
  93         logViewerAction.setEnabled(isWD);
  94         if (serviceViewerAction != null) {
  95             serviceViewerAction.setEnabled(isWD);
  96         }
  97         if (showChecklistAction != null) {
  98             showChecklistAction.setEnabled(config.getInterviewParameters() != null
  99                     && !config.getInterviewParameters().isChecklistEmpty());
 100         }
 101     }
 102 
 103     public void save(Map m) {
 104         // nothing to save
 105     }
 106 
 107     public void restore(Map m) {
 108         // nothing to restore
 109     }
 110 
 111     public JMenu getMenu() {
 112         JMenu viewMenu = uif.createMenu("exec.view");
 113 
 114         List<Action> cfgActs = createConfigActions();
 115         Action[] viewConfigActions = new Action[cfgActs.size()];
 116         cfgActs.toArray(viewConfigActions);
 117         JMenu viewConfigMenu = uif.createMenu("exec.view.cfg", viewConfigActions);
 118         viewMenu.add(viewConfigMenu);
 119 
 120         if (filterControl != null) {
 121             JMenu filters = filterControl.getFilterMenu();
 122             if (filters != null) {
 123                  viewMenu.add(filters);
 124             }
 125         }
 126         viewMenu.addSeparator();
 127         for (Action action: createActions()) {
 128             viewMenu.add(action);
 129         }
 130 
 131         ContextManager cm = execModel.getContextManager();
 132         JavaTestMenuManager mm = null;
 133         if (cm != null) {
 134             mm = cm.getMenuManager();
 135             if (mm != null) {
 136                 JMenuItem[] items = mm.getMenuItems(JavaTestMenuManager.CONFIG_VIEW);
 137                 if (items != null) {
 138                     for (int i = 0; i < items.length; i++) {
 139                         viewConfigMenu.add(items[i]);
 140                     }
 141                 }
 142             }
 143         }
 144         return viewMenu;
 145 
 146     }
 147 
 148     public List<Action> getToolBarActionList() {
 149         return null;
 150     }
 151 
 152     protected List<Action> createActions() {
 153         if (actions != null) {
 154             return actions;
 155         }
 156         actions = new LinkedList<Action>();
 157         propertiesAction = createPropertyAction();
 158         actions.add(propertiesAction);
 159         logViewerAction = createLogViewerAction();
 160         actions.add(logViewerAction);
 161         if (testSuite != null && testSuite.needServices()) {
 162             serviceViewerAction = createServiceViewerAction();
 163             actions.add(serviceViewerAction);
 164         }
 165         testSuiteErrorsAction = createTestSuiteErrorsAction();
 166         actions.add(testSuiteErrorsAction);
 167         return actions;
 168     }
 169 
 170     public void dispose() {
 171         // nothing to dispose
 172     }
 173 
 174     private Action propertiesAction;
 175     private Action createPropertyAction() {
 176         return new ToolAction(uif, "exec.view.props") {
 177             public void actionPerformed(ActionEvent e) {
 178                 if (propertiesBrowser == null) {
 179                     propertiesBrowser = new PropertiesBrowser(parent, uif);
 180                 }
 181                 propertiesBrowser.showDialog(testSuite,
 182                         config.getWorkDirectory(), config.getInterviewParameters());
 183             }
 184 
 185             private PropertiesBrowser propertiesBrowser;
 186         };
 187     };
 188 
 189     TestSuiteErrorsDialog testSuiteErrorsDialog = null;
 190     private Action testSuiteErrorsAction;
 191     private Action createTestSuiteErrorsAction() {
 192         return new ToolAction(uif, "exec.view.testSuiteErrors") {
 193             public void actionPerformed(ActionEvent e) {
 194                 if (testSuiteErrorsDialog == null)
 195                     testSuiteErrorsDialog = new TestSuiteErrorsDialog(parent, uif);
 196                 testSuiteErrorsDialog.show(testSuite);
 197             }
 198         };
 199     }
 200 
 201     private Action logViewerAction;
 202     private Action createLogViewerAction() {
 203         return new ToolAction(uif, "exec.view.logviewer") {
 204             public void actionPerformed(ActionEvent e) {
 205 
 206                 if (config != null && config.getWorkDirectory() != null)
 207                     openLogViewer();
 208                 else
 209                     // should not happen: menu item is disabled in this case
 210                     testSuite.getNotificationLog(null).info(uif.getI18NString("exec.view.logviewer.noworkdir"));
 211             }
 212         };
 213     }
 214 
 215     private void openLogViewer() {
 216         new LogViewer(config.getWorkDirectory(), uif, parent);
 217     }
 218 
 219     private ServiceViewer serviceViewer;
 220 
 221     private Action serviceViewerAction;
 222     private Action createServiceViewerAction() {
 223         return new ToolAction(uif, "exec.view.serviceviewer") {
 224             public void actionPerformed(ActionEvent e) {
 225 
 226                 if (config != null && config.getWorkDirectory() != null)
 227                     openServiceViewer();
 228                 else
 229                     // should not happen: menu item is disabled in this case
 230                     testSuite.getNotificationLog(null).info(uif.getI18NString("exec.view.serviceviewer.noworkdir"));
 231             }
 232         };
 233     }
 234 
 235     private void openServiceViewer() {
 236         if (serviceViewer == null) {
 237             ServiceManager mgr = testSuite.getServiceManager();
 238             serviceViewer = new ServiceViewer(mgr, uif, parent);
 239             updateServiceViewer();
 240         }
 241 
 242         serviceViewer.setVisible(true);
 243     }
 244 
 245     private void updateServiceViewer() {
 246         if (serviceViewer != null) {
 247             ServiceManager mgr = serviceViewer.getServiceManager();
 248             if (config != null && config.getInterviewParameters() != null) {
 249                 mgr.setParameters(config.getInterviewParameters());
 250                 if (!config.getInterviewParameters().containsObserver(serviceViewer)) {
 251                     config.getInterviewParameters().addObserver(serviceViewer);
 252                 }
 253             }
 254         }
 255     }
 256 
 257     Action showEnvironmentAction;
 258     Action showExcludeListAction;
 259     Action showChecklistAction;
 260     Action showQuestionLogAction;
 261     List<Action> createConfigActions() {
 262         List<Action> acts = new LinkedList<Action>();
 263 
 264         showEnvironmentAction = new ToolAction(uif, "ch.env") {
 265             public void actionPerformed(ActionEvent e) {
 266                 showEnvironment();
 267             }
 268         };
 269 
 270         showExcludeListAction = new ToolAction(uif, "ch.excl") {
 271             public void actionPerformed(ActionEvent e) {
 272                 showExcludeList();
 273             }
 274         };
 275 
 276         showChecklistAction = new ToolAction(uif, "ch.checkList") {
 277             public void actionPerformed(ActionEvent e) {
 278                 showChecklist();
 279             }
 280         };
 281 
 282         showQuestionLogAction = new ToolAction(uif, "ch.quLog") {
 283             public void actionPerformed(ActionEvent e) {
 284                 showQuestionLog();
 285             }
 286         };
 287         acts.add(showEnvironmentAction);
 288         acts.add(showExcludeListAction);
 289         acts.add(showChecklistAction);
 290         acts.add(showQuestionLogAction);
 291         return acts;
 292     }
 293 
 294     void ensureInterviewUpToDate() {
 295         try {
 296             config.reloadInterview();
 297         } catch (Exception ex) {
 298             if (debug > 0) {
 299                 ex.printStackTrace(Debug.getWriter());
 300             }
 301             uif.showError("exec.loadInterview", ex.toString());
 302         }
 303     }
 304 
 305     boolean isConfigEdited() {
 306         return false;
 307     }
 308     boolean isOKToContinue() {
 309         return true;
 310     }
 311 
 312     void showEnvironment() {
 313         ensureInterviewUpToDate();
 314 
 315         if (isConfigEdited() && !isOKToContinue())
 316             return;
 317 
 318         if (environmentBrowser == null)
 319             environmentBrowser = new EnvironmentBrowser(parent, uif);
 320 
 321         environmentBrowser.show(config.getInterviewParameters());
 322     }
 323 
 324     void showExcludeList() {
 325         ensureInterviewUpToDate();
 326 
 327         if (isConfigEdited() && !isOKToContinue())
 328             return;
 329 
 330         if (excludeListBrowser == null)
 331             excludeListBrowser = new ExcludeListBrowser(parent, uif);
 332 
 333         excludeListBrowser.show(config.getInterviewParameters());
 334     }
 335 
 336     void showChecklist() {
 337         ensureInterviewUpToDate();
 338 
 339         if (isConfigEdited() && !isOKToContinue())
 340             return;
 341 
 342         if (checkListBrowser == null)
 343             checkListBrowser = new ChecklistBrowser(parent, execModel, uif);
 344 
 345         checkListBrowser.setVisible(true);
 346     }
 347 
 348     void showQuestionLog() {
 349         ensureInterviewUpToDate();
 350 
 351         if (isConfigEdited() && !isOKToContinue())
 352             return;
 353 
 354         if (questionLogBrowser == null)
 355             questionLogBrowser = new QuestionLogBrowser(parent, execModel, uif);
 356 
 357         questionLogBrowser.setVisible(true);
 358     }
 359 
 360 }