1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 2002, 2012, 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.javatest.Harness;
  30 import com.sun.javatest.Parameters;
  31 import com.sun.javatest.TestResult;
  32 import java.awt.event.ActionEvent;
  33 import java.awt.event.ActionListener;
  34 import java.io.File;
  35 import java.util.List;
  36 import java.util.Map;
  37 import java.util.HashMap;
  38 import javax.swing.Action;
  39 import javax.swing.JComponent;
  40 import javax.swing.JFileChooser;
  41 import javax.swing.JMenu;
  42 import javax.swing.JMenuItem;
  43 
  44 import com.sun.javatest.WorkDirectory;
  45 //import com.sun.javatest.report.Report;
  46 import com.sun.javatest.report.ReportDirChooser;
  47 import com.sun.javatest.report.Report;
  48 import com.sun.javatest.tool.FileChooser;
  49 import com.sun.javatest.tool.FileHistory;
  50 import com.sun.javatest.tool.ToolAction;
  51 import com.sun.javatest.tool.UIFactory;
  52 import com.sun.javatest.util.PrefixMap;
  53 
  54 /**
  55  * Class to handle report create/open operations in exec tool.
  56  */
  57 class ReportHandler implements ET_ReportControl, HarnessAware {
  58     ReportHandler(JComponent parent, ExecModel model, UIFactory uif) {
  59         this.parent = parent;
  60         this.model = model;
  61         this.uif = uif;
  62         initActions();
  63     }
  64 
  65     ReportHandler(JComponent parent, ExecModel model, Harness harness, UIFactory uif) {
  66         this(parent, model, uif);
  67         setHarness(harness);
  68     }
  69 
  70     public JMenu getMenu() {
  71         Action[] reportActions = {
  72             newReportAction,
  73             openReportAction,
  74             null
  75         };
  76 
  77         JMenu menu = uif.createMenu("rpth", reportActions);
  78         menu.addMenuListener(reportHistoryListener);
  79 
  80         return menu;
  81     }
  82 
  83     void showReportDialog(int mode) {
  84         // refactored for 4.0 to use standard chooser for opening a report
  85 
  86         if (mode == ReportDirChooser.NEW) {
  87             ReportDirChooser rdc = getReportDirChooser();
  88             rdc.setMode(mode);
  89             int option = rdc.showDialog(parent);
  90             if (option != JFileChooser.APPROVE_OPTION)
  91                 return;
  92 
  93             // set parameters?
  94         }
  95         else if (mode == ReportDirChooser.OPEN) {
  96             FileChooser fc = new FileChooser();
  97             fc.setApproveButtonToolTipText(uif.getI18NString("rh.open.tip"));
  98             fc.setApproveButtonMnemonic(uif.getI18NMnemonic("rh.open.mne"));
  99             fc.setDialogTitle(uif.getI18NString("rh.open.title"));
 100 
 101             if (lastOpenPath != null) {
 102                 fc.setCurrentDirectory(lastOpenPath);
 103             }
 104 
 105             int res = fc.showOpenDialog(parent);
 106             //File f = rdc.getSelectedReportDirectory();
 107             File f = fc.getSelectedFile();
 108             if (res == JFileChooser.APPROVE_OPTION) {
 109                 showReportBrowser(f);
 110                 history.add(f);
 111                 lastOpenPath = f.getParentFile();
 112             }
 113         }
 114     }
 115 
 116     void showNewReportDialog() {
 117         if (newReportD == null) {
 118             newReportD = new NewReportDialog(parent, uif, model.getFilterConfig(),
 119                             getReportBrowser(), model);
 120 
 121         newReportD.addObserver(new NewReportDialog.Observer() {
 122                 public void update(Map<String, String> l) {
 123                     lastState = l;
 124                     String lastReportDir =
 125                         lastState.get(NewReportDialog.REPORT_DIR);
 126 
 127                    if (lastReportDir != null)
 128                        history.add(new File(lastReportDir));
 129                 }
 130 
 131                 public void writingReport() {
 132                     newReportAction.setEnabled(false);
 133                 }
 134 
 135                 public void wroteReport() {
 136                     newReportAction.setEnabled(true);
 137                 }
 138 
 139                 public void errorWriting(String problem) {
 140                     newReportAction.setEnabled(true);
 141                 }
 142             });
 143         }
 144 
 145         newReportD.setInterviewParameters(model.getInterviewParameters());
 146         if (lastState != null)
 147             newReportD.setLastState(lastState);
 148 
 149         newReportD.setVisible(true);
 150     }
 151 
 152     void showReportBrowser(File reportDir) {
 153         // if if is a dir, try to find a particular file to show
 154         // since there may be multiple choices, use the one with the
 155         // most recent date
 156         File target = reportDir;
 157         if (reportDir.isDirectory()) {
 158             String[] names = Report.getHtmlReportFilenames();
 159             long newestTime = 0;
 160 
 161             for (int i = 0; i < names.length; i++) {
 162                 File f = new File(reportDir, names[i]);
 163                 if (f.exists()  && f.lastModified() > newestTime) {
 164                     target = f;
 165                     newestTime = f.lastModified();
 166                 }
 167             }   // for
 168         }
 169         else {
 170             // target is a file
 171         }
 172 
 173         getReportBrowser().show(target);
 174     }
 175 
 176     ReportBrowser getReportBrowser() {
 177         if (reportBrowser == null) {
 178             reportBrowser = new ReportBrowser(parent, model, uif, this);
 179         }
 180         return reportBrowser;
 181     }
 182 
 183     ReportDirChooser getReportDirChooser() {
 184         if (reportDirChooser == null)
 185             reportDirChooser = new ReportDirChooser();
 186         return reportDirChooser;
 187     }
 188 
 189     Action getNewReportAction() {
 190         return newReportAction;
 191     }
 192 
 193     Action getOpenReportAction() {
 194         return openReportAction;
 195     }
 196 
 197     // should really be observing ExecModel
 198     public void updateGUI() {
 199 
 200         workDir = model.getWorkDirectory();
 201 
 202         boolean workDirSet = (workDir != null);
 203         newReportAction.setEnabled(workDirSet);
 204         openReportAction.setEnabled(workDirSet);
 205 
 206         if (!workDirSet) return;
 207 
 208         if (history == null) {
 209             history = FileHistory.getFileHistory(workDir, "reportDirHistory.jtl");
 210             reportHistoryListener.setFileHistory(history);
 211         }
 212     }
 213 
 214     public void save(Map<String, String> parentMap) {
 215         if (lastState != null && lastState.size() > 0)  {
 216            PrefixMap<String> pm = new PrefixMap<>(parentMap, REPORT_PREFIX);
 217            pm.putAll(lastState);
 218         }
 219     }
 220 
 221     public void restore(Map<String, String> parentMap) {
 222         if (parentMap == null)
 223             return;
 224 
 225         try {
 226             PrefixMap<String> pm = new PrefixMap<>(parentMap, REPORT_PREFIX);
 227             if (pm == null) return;
 228 
 229             Object[] keys = pm.keySet().toArray();
 230             if (lastState == null)
 231                 lastState = new HashMap<>();
 232 
 233             for (int i = 0; i < keys.length; i++) {
 234                 String key = (String) keys[i];
 235                 String value = (String) pm.get(keys[i]);
 236 
 237                 if (value != null)
 238                     lastState.put(key, value);
 239             }
 240         } catch (Exception e) {
 241             e.printStackTrace();
 242         }
 243     }
 244 
 245     public void dispose() {
 246         if (newReportD != null) {
 247             newReportD.dispose();
 248         }
 249 
 250         if (reportBrowser != null) {
 251             reportBrowser.dispose();
 252         }
 253 
 254         if (uif != null) {
 255             uif.dispose();
 256         }
 257 
 258         workDir = null;
 259         model = null;
 260         lastState = null;
 261     }
 262 
 263     public void setHarness(Harness h) {
 264         h.addObserver(new Harness.Observer() {
 265 
 266             public void startingTestRun(Parameters params) {
 267                 newReportAction.setEnabled(false);
 268             }
 269 
 270             public void startingTest(TestResult tr) {}
 271             public void finishedTest(TestResult tr) {}
 272             public void stoppingTestRun() {}
 273             public void finishedTesting() {}
 274 
 275             public void finishedTestRun(boolean allOK) {
 276                 newReportAction.setEnabled(true);
 277             }
 278 
 279             public void error(String msg) {}
 280         });
 281     }
 282 
 283     /**
 284      * ET_Control interface method
 285      * @return null
 286      */
 287     public List<Action> getToolBarActionList() {
 288         return null;
 289     }
 290 
 291     //----------------------------------------------------------------------------
 292     // internal methods
 293 
 294     // cannot use instance initialization for the actions because they depend
 295     // on uif being initialized in the constructor
 296     private void initActions() {
 297         newReportAction = new ToolAction(uif, "rpth.new") {
 298             public void actionPerformed(ActionEvent e) {
 299                 showNewReportDialog();
 300             }
 301         };
 302 
 303         openReportAction = new ToolAction(uif, "rpth.open") {
 304             public void actionPerformed(ActionEvent e) {
 305                 showReportDialog(ReportDirChooser.OPEN);
 306             }
 307         };
 308 
 309         reportHistoryListener = new FileHistory.Listener(new ActionListener() {
 310             public void actionPerformed(ActionEvent e) {
 311                 JMenuItem mi = (JMenuItem) (e.getSource());
 312                 File f = (File) (mi.getClientProperty(FileHistory.FILE));
 313                 if (f != null)
 314                     showReportBrowser(f);
 315             }
 316         });
 317 
 318     }
 319 
 320     private Action newReportAction;
 321     private Action openReportAction;
 322 
 323     private static final String REPORT_PREFIX = "report";
 324 
 325     private JComponent parent;
 326     private UIFactory uif;
 327 
 328     private FileHistory.Listener reportHistoryListener;
 329     private FileHistory history;
 330     private File lastOpenPath;
 331     private Map<String, String> lastState;
 332     private ExecModel model;
 333     private NewReportDialog newReportD;
 334     private ReportBrowser reportBrowser;
 335     private ReportDirChooser reportDirChooser;
 336     private WorkDirectory workDir;
 337 }