       1 /*
       2  * $Id$
       3  *
       4  * Copyright (c) 2002, 2009, 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.audit;
      28
      29 import java.awt.Dimension;
      30 import java.awt.GridBagConstraints;
      31 import java.awt.GridBagLayout;
      32 import java.awt.event.ActionEvent;
      33 import java.awt.event.ActionListener;
      34 import java.awt.event.ComponentAdapter;
      35 import java.awt.event.ComponentEvent;
      36 import java.io.File;
      37 import java.util.Iterator;
      38 import java.util.SortedSet;
      39 import java.util.TreeSet;
      40 import javax.swing.BorderFactory;
      41 import javax.swing.JButton;
      42 import javax.swing.JComboBox;
      43 import javax.swing.JFileChooser;
      44 import javax.swing.JLabel;
      45 import javax.swing.JPanel;
      46 import javax.swing.border.Border;
      47 import javax.swing.filechooser.FileFilter;
      48
      49 import com.sun.javatest.InterviewParameters;
      50 import com.sun.javatest.TestSuite;
      51 import com.sun.javatest.WorkDirectory;
      52 import com.sun.javatest.tool.Desktop;
      53 import com.sun.javatest.tool.FileHistory;
      54 import com.sun.javatest.tool.TestSuiteChooser;
      55 import com.sun.javatest.tool.Tool;
      56 import com.sun.javatest.tool.ToolDialog;
      57 import com.sun.javatest.tool.UIFactory;
      58 import com.sun.javatest.tool.WorkDirChooser;
      59
      60 class OptionsDialog extends ToolDialog
      61 {
      62     OptionsDialog(Tool tool, ActionListener okListener, UIFactory uif) {
      63         super(tool, uif, "opts");
      64         this.tool = tool;
      65         this.okListener = okListener;
      66         setHelp("audit.options.csh");
      67     }
      68
      69     void setParameters(InterviewParameters params) {
      70         if (body == null)
      71             initGUI();
      72
      73         if (params != null) {
      74             TestSuite ts = params.getTestSuite();
      75             if (ts != null)
      76                 tsField.setSelectedItem(ts.getPath());
      77
      78             WorkDirectory wd = params.getWorkDirectory();
      79             if (wd != null)
      80                 wdField.setSelectedItem(wd.getPath());
      81
      82             File cf = params.getFile();
      83             if (cf != null)
      84                 cfField.setSelectedItem(cf.getPath());
      85         }
      86     }
      87
      88     String getTestSuitePath() {
      89         return (String) (tsField.getSelectedItem());
      90     }
      91
      92     String getWorkDirPath() {
      93         return (String) (wdField.getSelectedItem());
      94     }
      95
      96     String getConfigFilePath() {
      97         return (String) (cfField.getSelectedItem());
      98     }
      99
     100     protected void initGUI() {
     101         setI18NTitle("opts.title");
     102         listener = new Listener();
     103
     104         body = uif.createPanel("opts.fields", new GridBagLayout(), false);
     105         body.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
     106
     107         GridBagConstraints lc = new GridBagConstraints();
     108         lc.anchor = GridBagConstraints.EAST;
     109         lc.insets.right = 10;
     110
     111         GridBagConstraints fc = new GridBagConstraints();
     112         fc.fill = GridBagConstraints.HORIZONTAL;
     113         fc.weightx = 1;
     114
     115         GridBagConstraints bc = new GridBagConstraints();
     116         bc.gridwidth = GridBagConstraints.REMAINDER;
     117         bc.fill = GridBagConstraints.VERTICAL;
     118
     119         Border smallBorder = BorderFactory.createCompoundBorder(
     120             BorderFactory.createEtchedBorder(),
     121             BorderFactory.createEmptyBorder(0,3,0,3));
     122
     123         // test suite
     124         JLabel tsLabel = uif.createLabel("opts.ts", true);
     125         body.add(tsLabel, lc);
     126
     127         tsField = uif.createChoice("opts.ts", true, tsLabel);
     128         tsField.addItem("");
     129         tsField.addActionListener(listener);
     130         body.add(tsField, fc);
     131
     132         tsBtn = uif.createIconButton("opts.ts.browse", listener);
     133         tsBtn.setBorder(smallBorder);
     134         body.add(tsBtn, bc);
     135
     136         // results directory (work directory)
     137         JLabel wdLabel = uif.createLabel("opts.wd", true);
     138         body.add(wdLabel, lc);
     139
     140         wdField = uif.createChoice("opts.wd", true, wdLabel);
     141         wdField.addItem("");
     142         wdField.addActionListener(listener);
     143         body.add(wdField, fc);
     144
     145         wdBtn = uif.createIconButton("opts.wd.browse", listener);
     146         wdBtn.setBorder(smallBorder);
     147         body.add(wdBtn, bc);
     148
     149         // config file
     150         JLabel cfLabel = uif.createLabel("opts.config", true);
     151         body.add(cfLabel, lc);
     152
     153         cfField = uif.createChoice("opts.config", true, cfLabel);
     154         cfField.addItem("");
     155         cfField.setEditable(true);
     156         cfField.addActionListener(listener);
     157         body.add(cfField, fc);
     158
     159         cfBtn = uif.createIconButton("opts.config.browse", listener);
     160         cfBtn.setBorder(smallBorder);
     161         body.add(cfBtn, bc);
     162
     163         Dimension d = body.getPreferredSize();
     164         int dpi = uif.getDotsPerInch();
     165         body.setPreferredSize(new Dimension(Math.max(d.width, 5 * dpi), d.height));
     166         setBody(body);
     167
     168         JButton okBtn = uif.createButton("opts.ok", okListener, OK);
     169         JButton cancelBtn = uif.createCancelButton("opts.cancel");
     170         JButton helpBtn = uif.createHelpButton("opts.help", "audit.options.csh");
     171         setButtons(new JButton[] { okBtn, cancelBtn, helpBtn }, okBtn);
     172
     173         setComponentListener(listener);
     174     }
     175
     176     private void updateFieldChoices() {
     177         updateTestSuiteChoices();
     178         updateWorkDirectoryChoices();
     179         updateConfigFileChoices();
     180     }
     181
     182     private void chooseTestSuite() {
     183         if (testSuiteChooser == null)
     184             testSuiteChooser = new TestSuiteChooser();
     185         int action = testSuiteChooser.showDialog(tool);
     186         if (action != JFileChooser.APPROVE_OPTION)
     187             return;
     188         TestSuite ts = testSuiteChooser.getSelectedTestSuite();
     189         tsField.setSelectedItem(ts.getPath());
     190     }
     191
     192     private void updateTestSuiteChoices() {
     193         // get the paths of currently loaded test suites
     194         // could move this to TestSuite, and use the dirMap cache
     195         SortedSet s = new TreeSet();
     196         Desktop d = tool.getDesktop();
     197         Tool[] tools = d.getTools();
     198         if (tools != null) {
     199             for (int i = 0; i < tools.length; i++) {
     200                 Tool t = tools[i];
     201                 TestSuite[] tss = t.getLoadedTestSuites();
     202                 if (tss != null) {
     203                     for (int j = 0; j < tss.length; j++)
     204                         s.add(tss[j].getPath());
     205                 }
     206             }
     207         }
     208
     209         setItems(tsField, s);
     210         updateWorkDirectoryChoices();
     211     }
     212
     213     private void chooseWorkDirectory() {
     214         if (workDirChooser == null) {
     215             workDirChooser = new WorkDirChooser(true);
     216             workDirChooser.setMode(WorkDirChooser.OPEN_FOR_ANY_TESTSUITE);
     217         }
     218         int action = workDirChooser.showDialog(tool);
     219         if (action != JFileChooser.APPROVE_OPTION)
     220             return;
     221         WorkDirectory wd = workDirChooser.getSelectedWorkDirectory();
     222         wdField.setSelectedItem(wd.getPath());
     223     }
     224
     225     private void updateWorkDirectoryChoices() {
     226         String tsID = null;
     227         try {
     228             String tsp = (String) (tsField.getSelectedItem());
     229             if (tsp != null && tsp.length() > 0) {
     230                 TestSuite ts = TestSuite.open(new File(tsp));
     231                 tsID = ts.getID();
     232             }
     233         }
     234         catch (Exception e) {
     235             // ignore
     236         }
     237
     238         // get the paths of currently loaded work directories
     239         // could move this to WorkDirectory and use the dirMap cache
     240         SortedSet s = new TreeSet();
     241         Desktop d = tool.getDesktop();
     242         Tool[] tools = d.getTools();
     243         if (tools != null) {
     244             for (int i = 0; i < tools.length; i++) {
     245                 Tool t = tools[i];
     246                 WorkDirectory[] wds = t.getLoadedWorkDirectories();
     247                 if (wds != null) {
     248                     for (int j = 0; j < wds.length; j++) {
     249                         WorkDirectory wd = wds[j];
     250                         if (tsID == null || tsID.equals(wd.getTestSuite().getID()))
     251                             s.add(wd.getPath());
     252                     }
     253                 }
     254             }
     255         }
     256
     257         setItems(wdField, s);
     258     }
     259
     260     private void chooseConfigFile() {
     261         if (configFileChooser == null) {
     262             configFileChooser = new JFileChooser();
     263             String userDir = System.getProperty("user.dir");
     264             if (userDir != null)
     265                 configFileChooser.setCurrentDirectory(new File(userDir));
     266             configFileChooser.addChoosableFileFilter(new FileFilter() {
     267                     public boolean accept(File f) {
     268                         return (f.isDirectory() || f.getPath().endsWith(".jti"));
     269                     }
     270
     271                     public String getDescription() {
     272                         return uif.getI18NString("opts.jtiFiles");
     273                     }
     274                 });
     275         }
     276
     277         int action = configFileChooser.showOpenDialog(tool);
     278         if (action != JFileChooser.APPROVE_OPTION)
     279             return;
     280
     281         File cf = configFileChooser.getSelectedFile();
     282         String cfp;
     283         if (cf == null)
     284             cfp = "";
     285         else {
     286             cfp = cf.getPath();
     287             if (!cfp.endsWith(".jti"))
     288                 cfp += ".jti";
     289         }
     290         cfField.setSelectedItem(cfp);
     291     }
     292
     293     private void updateConfigFileChoices() {
     294         SortedSet s = new TreeSet();
     295         String wdp = (String) (wdField.getSelectedItem());
     296         try {
     297             WorkDirectory wd = WorkDirectory.open(new File(wdp));
     298             FileHistory history = FileHistory.getFileHistory(wd, "configHistory.jtl");
     299             File[] entries = history.getRecentEntries(10);
     300             for (int i = 0; i < entries.length; i++)
     301                 s.add(entries[i].getPath());
     302         }
     303         catch (Exception e) {
     304             // ignore
     305         }
     306         setItems(cfField, s);
     307     }
     308
     309     private void setItems(JComboBox field, SortedSet s) {
     310         // first, remove unwanted entries from field
     311         for (int i = field.getItemCount() - 1; i >= 0; i-- ) {
     312             String item = (String) (field.getItemAt(i));
     313             if (s.contains(item)) {
     314                 // this item is required, so remove it from the
     315                 // set to be added later
     316                 s.remove(item);
     317             }
     318             else if (item.length() > 0 && !item.equals(field.getSelectedItem()))
     319                 // this item is not required, remove it
     320                 field.removeItemAt(i);
     321         }
     322
     323         // those items remaining in s need to be added to the field
     324         for (Iterator iter = s.iterator(); iter.hasNext(); )
     325             field.addItem(iter.next());
     326     }
     327
     328     private Tool tool;
     329     private ActionListener okListener;
     330     private JPanel body;
     331
     332     private JComboBox tsField;
     333     private JButton tsBtn;
     334     private TestSuiteChooser testSuiteChooser;
     335
     336     private JComboBox wdField;
     337     private JButton wdBtn;
     338     private WorkDirChooser workDirChooser;
     339
     340     private JComboBox cfField;
     341     private JButton cfBtn;
     342     private JFileChooser configFileChooser;
     343
     344     private Listener listener;
     345
     346     static final String OK = "OK";
     347
     348     private class Listener
     349         extends ComponentAdapter
     350         implements ActionListener
     351     {
     352         // ComponentListener
     353         public void componentShown(ComponentEvent e) {
     354             updateFieldChoices();
     355         }
     356
     357         // ActionListener
     358         public void actionPerformed(ActionEvent e) {
     359             Object src = e.getSource();
     360             if (src == tsField)
     361                 updateWorkDirectoryChoices();
     362             else if (src == tsBtn)
     363                 chooseTestSuite();
     364             else if (src == wdField)
     365                 updateConfigFileChoices();
     366             else if (src == wdBtn)
     367                 chooseWorkDirectory();
     368             else if (src == cfBtn)
     369                 chooseConfigFile();
     370         }
     371     };
     372
     373 }