1 /* 2 * $Id$ 3 * 4 * Copyright (c) 2002, 2011, 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 java.awt.GridBagConstraints; 30 import java.awt.GridBagLayout; 31 import java.awt.event.ActionEvent; 32 import java.awt.event.ActionListener; 33 import java.io.*; 34 import java.nio.charset.StandardCharsets; 35 import java.util.HashSet; 36 import java.util.Set; 37 import javax.swing.ButtonGroup; 38 import javax.swing.JButton; 39 import javax.swing.JFileChooser; 40 import javax.swing.JPanel; 41 import javax.swing.JRadioButton; 42 import javax.swing.event.ChangeEvent; 43 import javax.swing.event.ChangeListener; 44 45 import com.sun.javatest.InterviewParameters; 46 import com.sun.javatest.TestResultTable; 47 import com.sun.javatest.WorkDirectory; 48 import com.sun.javatest.Parameters.TestsParameters; 49 import com.sun.javatest.Parameters.MutableTestsParameters; 50 import com.sun.javatest.tool.FileChooser; 51 import com.sun.javatest.tool.TestTreeSelectionPane; 52 import com.sun.javatest.tool.UIFactory; 53 import com.sun.javatest.tool.jthelp.ContextHelpManager; 54 import java.util.ArrayList; 55 56 /** 57 * Standard values view, initial tests selection panel. 58 */ 59 class CE_TestsPane extends CE_StdPane 60 { 61 CE_TestsPane(UIFactory uif, InterviewParameters config) { 62 super(uif, config, "tests"); 63 64 updateConfig(); 65 initGUI(); 66 } 67 68 boolean isOKToClose() { 69 if (mutableTestsParameters == null) 70 return true; 71 72 if (selectTestsBtn.isSelected() && testsField.isSelectionEmpty()) { 73 uif.showError("ce.tests.noTests"); 74 return false; 75 } 76 77 return true; 78 } 79 80 void updateConfig() { 81 testsParameters = config.getTestsParameters(); 82 if (testsParameters instanceof MutableTestsParameters) 83 mutableTestsParameters = ((MutableTestsParameters) (testsParameters)); 84 else 85 mutableTestsParameters = null; 86 } 87 88 void load() { 89 updateConfig(); 90 91 if (mutableTestsParameters != null) { 92 int tm = mutableTestsParameters.getTestsMode(); 93 if (tm == MutableTestsParameters.ALL_TESTS) 94 allTestsBtn.setSelected(true); 95 else 96 selectTestsBtn.setSelected(true); 97 98 testsField.setSelection(mutableTestsParameters.getSpecifiedTests()); 99 testsField.setEnabled(selectTestsBtn.isSelected()); 100 } 101 else { 102 mutableTestsParameters = null; 103 104 String[] tests = testsParameters.getTests(); 105 if (tests == null || tests.length == 0) { 106 allTestsBtn.setSelected(true); 107 testsField.clear(); 108 } 109 else { 110 selectTestsBtn.setSelected(true); 111 testsField.setSelection(tests); 112 } 113 allTestsBtn.setEnabled(false); 114 selectTestsBtn.setEnabled(false); 115 testsField.setEnabled(false); 116 } 117 } 118 119 void save() { 120 if (mutableTestsParameters != null) { 121 if (allTestsBtn.isSelected()) 122 mutableTestsParameters.setTestsMode(MutableTestsParameters.ALL_TESTS); 123 else if (selectTestsBtn.isSelected()) 124 mutableTestsParameters.setTestsMode(MutableTestsParameters.SPECIFIED_TESTS); 125 mutableTestsParameters.setSpecifiedTests(testsField.getSelection()); 126 } 127 } 128 129 private void initGUI() { 130 ContextHelpManager.setHelpIDString(this, "confEdit.testsTab.csh"); 131 132 JPanel p = uif.createPanel("ce.tests", new GridBagLayout(), false); 133 134 GridBagConstraints c = new GridBagConstraints(); 135 c.gridwidth = GridBagConstraints.REMAINDER; 136 c.fill = GridBagConstraints.HORIZONTAL; 137 c.weightx = 1; 138 139 btnGrp = new ButtonGroup(); 140 allTestsBtn = uif.createRadioButton("ce.tests.all", btnGrp); 141 p.add(allTestsBtn, c); 142 143 144 selectTestsBtn = uif.createRadioButton("ce.tests.select", btnGrp); 145 selectTestsBtn.addChangeListener(new ChangeListener() { 146 public void stateChanged(ChangeEvent e) { 147 boolean s = selectTestsBtn.isSelected(); 148 testsField.setEnabled(s); 149 loadBtn.setEnabled(s); 150 } 151 }); 152 c.gridheight = 2; 153 c.gridwidth = 1; 154 c.weightx = 0; 155 p.add(selectTestsBtn, c); 156 157 testsField = new TestTreeSelectionPane(config.getWorkDirectory().getTestResultTable()); 158 testsField.setEnabled(selectTestsBtn.isSelected()); 159 c.fill = GridBagConstraints.BOTH; 160 c.gridheight = 1; 161 c.gridwidth = GridBagConstraints.REMAINDER; 162 c.weightx = 1; 163 c.weighty = 1; 164 p.add(testsField, c); 165 166 loadBtn = uif.createButton("ce.tests.load", new ActionListener() { 167 public void actionPerformed(ActionEvent e) { 168 selectFromFile(); 169 } 170 }); 171 loadBtn.setEnabled(selectTestsBtn.isSelected()); 172 c.anchor = GridBagConstraints.EAST; 173 c.fill = GridBagConstraints.NONE; 174 c.insets.top = 5; 175 c.weighty = 0; 176 p.add(loadBtn, c); 177 178 addBody(p); 179 } 180 181 private void selectFromFile() { 182 if (chooser == null) { 183 chooser = new FileChooser(); 184 } 185 186 int rc = chooser.showDialog(this, chooser.getApproveButtonText()); 187 if (rc != JFileChooser.APPROVE_OPTION) 188 return; 189 190 WorkDirectory wd = config.getWorkDirectory(); 191 TestResultTable trt = wd.getTestResultTable(); 192 193 File file = chooser.getSelectedFile(); 194 ArrayList<String> paths = new ArrayList<>(); 195 ArrayList<String> badPaths = new ArrayList<>(); 196 Set<String> seen = new HashSet<>(); 197 FileReader fr = null; 198 BufferedReader in = null; 199 try { 200 in = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)); 201 String line; 202 while ((line = in.readLine()) != null) { 203 line = line.trim(); 204 if (line.length() == 0 || line.startsWith("#")) 205 continue; 206 int sp = line.indexOf(' '); 207 String path = (sp == -1 ? line : line.substring(0, sp)); 208 if (!seen.contains(path)) { 209 if (trt.validatePath(path)) 210 paths.add(path); 211 else 212 badPaths.add(path); 213 seen.add(path); 214 } 215 } 216 in.close(); 217 } 218 catch (FileNotFoundException e) { 219 uif.showError("ce.tests.cantFindFile", file.toString()); 220 return; 221 } 222 catch (IOException e) { 223 uif.showError("ce.tests.cantReadFile", new Object[] { file, e.toString() }); 224 return; 225 } 226 finally { 227 // attempt to close buffered reader first 228 // followed by the underlying reader for leak prevention 229 if (in != null){ 230 try { in.close(); } catch (IOException e) { } 231 } 232 233 if (fr != null){ 234 try { fr.close(); } catch (IOException e) { } 235 } 236 } 237 238 final int MAX_BAD_PATHS = 10; 239 240 if (badPaths.size() > 0) { 241 if (badPaths.size() == 1) 242 uif.showError("ce.tests.badPath", badPaths.get(0)); 243 else { 244 StringBuffer sb = new StringBuffer(); 245 for (int i = 0; i < Math.min(badPaths.size(), MAX_BAD_PATHS); i++) { 246 if (sb.length() > 0) 247 sb.append('\n'); 248 sb.append(badPaths.get(i)); 249 } 250 boolean more = badPaths.size() > MAX_BAD_PATHS; 251 uif.showError("ce.tests.badPaths", 252 new Object[] { sb.toString(), new Integer(more ? 1 : 0) }); 253 } 254 } 255 256 testsField.setSelection((String[]) (paths.toArray(new String[paths.size()]))); 257 } 258 259 private TestsParameters testsParameters; 260 private MutableTestsParameters mutableTestsParameters; 261 private ButtonGroup btnGrp; 262 private JRadioButton allTestsBtn; 263 private JRadioButton selectTestsBtn; 264 private TestTreeSelectionPane testsField; 265 private JButton loadBtn; 266 private FileChooser chooser; 267 }