1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 2003, 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.exec;
  28 
  29 import java.awt.Container;
  30 import java.awt.Dimension;
  31 import java.awt.GridBagConstraints;
  32 import java.awt.GridBagLayout;
  33 import java.awt.event.ComponentAdapter;
  34 import java.awt.event.ComponentEvent;
  35 import java.io.File;
  36 import java.util.Comparator;
  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.JLabel;
  43 import javax.swing.JPanel;
  44 import javax.swing.JScrollPane;
  45 import javax.swing.JTable;
  46 import javax.swing.JTextField;
  47 import javax.swing.event.ListSelectionEvent;
  48 import javax.swing.event.ListSelectionListener;
  49 import javax.swing.event.TableModelListener;
  50 import javax.swing.table.TableModel;
  51 
  52 import com.sun.interview.Question;
  53 import com.sun.interview.Interview;
  54 import com.sun.javatest.ExcludeList;
  55 import com.sun.javatest.InterviewParameters;
  56 import com.sun.javatest.Parameters;
  57 import com.sun.javatest.tool.ToolDialog;
  58 import com.sun.javatest.tool.UIFactory;
  59 
  60 class ExcludeListBrowser extends ToolDialog
  61 {
  62     ExcludeListBrowser(Container parent, UIFactory uif) {
  63         super(parent, uif, "elb");
  64 
  65         listener = new Listener();
  66     }
  67 
  68     public void show(InterviewParameters params) {
  69         this.params = params;
  70         setVisible(true);
  71     }
  72 
  73     private void updateContent() {
  74         ExcludeList exclList = params.getExcludeList();
  75         File[] exclFiles;
  76         Parameters.ExcludeListParameters eParams =
  77             params.getExcludeListParameters();
  78         if (eParams instanceof Parameters.MutableExcludeListParameters)
  79             exclFiles = ((Parameters.MutableExcludeListParameters) eParams).getExcludeFiles();
  80         else
  81             exclFiles = null;
  82 
  83         // rely on interview caching to allow reference equality here
  84         if (list != exclList || files != exclFiles || model == null || list.size() != model.getRowCount()) {
  85             setTable(exclFiles, exclList);
  86     }
  87     }
  88 
  89     private void setTable(File[] files, ExcludeList list) {
  90 
  91         this.list = list;
  92         this.files = files;
  93 
  94         model = new ExcludeListTableModel(list);
  95         table.setModel(model);
  96 
  97     if (model.getRowCount() == 0) {
  98             setI18NTitle("elb.title0");
  99     } else {
 100             setI18NTitle("elb.title1", "" + model.getRowCount());
 101     }
 102 
 103     }
 104 
 105     protected void initGUI() {
 106         setHelp("exclList.window.csh");
 107 
 108         // fix
 109         // TO DO...
 110         //fileField = uif.createOutputField("elb.file", 30);
 111         //fileField.setBorder(null);
 112         //fileField.setHorizontalAlignment(JTextField.RIGHT);
 113         //setHeadExtras(fileField);
 114         JPanel body = uif.createPanel("elb.body", new GridBagLayout(), false);
 115         body.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
 116         int dpi = uif.getDotsPerInch();
 117         body.setPreferredSize(new Dimension(5 * dpi, 2 * dpi));
 118 
 119         GridBagConstraints c = new GridBagConstraints();
 120         c.fill = GridBagConstraints.BOTH;
 121         c.gridwidth = GridBagConstraints.REMAINDER;
 122         c.weighty = 1;
 123         c.insets.bottom = 5;
 124         /*
 125         list = uif.createList("elb.list");
 126         list.addListSelectionListener(new ListSelectionListener() {
 127             public void valueChanged(ListSelectionEvent e) {
 128                 selectEntry((ExcludeList.Entry)(list.getSelectedValue()));
 129             }
 130         });
 131 
 132         list.setCellRenderer(new DefaultListCellRenderer() {
 133             public Component getListCellRendererComponent(JList list, Object o, int index, boolean isSelected, boolean cellHasFocus) {
 134                 String name = entryToString((ExcludeList.Entry)o);
 135                 return super.getListCellRendererComponent(list, name, index, isSelected, cellHasFocus);
 136             }
 137         });
 138         list.setVisibleRowCount(3);
 139         body.add(uif.createScrollPane(list), c);
 140         */
 141         table = new JTable();
 142         table.setRowSelectionAllowed(true);
 143         table.setColumnSelectionAllowed(false);
 144         table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
 145             public void valueChanged(ListSelectionEvent e) {
 146                 showSelectedEntry();
 147             }
 148         });
 149 
 150         uif.setAccessibleInfo(table, "elb.tbl");
 151         uif.setToolTip(table, "elb.tbl");
 152 
 153         body.add(new JScrollPane(table), c);
 154 
 155         GridBagConstraints lc = new GridBagConstraints();
 156         lc.insets.top = 2;
 157         lc.insets.right = 5;
 158         lc.anchor = GridBagConstraints.EAST;
 159 
 160         GridBagConstraints fc = new GridBagConstraints();
 161         fc.gridwidth = GridBagConstraints.REMAINDER;
 162         fc.insets.top = 2;
 163         fc.weightx = 1;
 164         fc.fill = GridBagConstraints.HORIZONTAL;
 165 
 166         JLabel synopsisLabel = uif.createLabel("elb.synopsis", true);
 167         body.add(synopsisLabel, lc);
 168         synopsisField = uif.createOutputField("elb.synopsis", synopsisLabel);
 169         body.add(synopsisField, fc);
 170 
 171         JLabel kwLabel = uif.createLabel("elb.kws", true);
 172         body.add(kwLabel, lc);
 173         kwField = uif.createOutputField("elb.kws", kwLabel);
 174         body.add(kwField, fc);
 175 
 176         JLabel bugIdsLabel = uif.createLabel("elb.bugids", true);
 177         body.add(bugIdsLabel, lc);
 178         bugIdsField = uif.createOutputField("elb.bugids", bugIdsLabel);
 179         body.add(bugIdsField, fc);
 180 
 181         setBody(body);
 182 
 183         JButton helpBtn = uif.createHelpButton("elb.help", "exclList.window.csh");
 184         JButton closeBtn = uif.createCloseButton("elb.close");
 185         setButtons(new JButton[] { helpBtn, closeBtn }, closeBtn);
 186 
 187         setComponentListener(listener);
 188     }
 189 
 190     private void showSelectedEntry() {
 191         ExcludeList.Entry e = model.getEntry(table.getSelectedRow());
 192 
 193         if (e == null) {
 194             synopsisField.setText("");
 195             kwField.setText("");
 196             bugIdsField.setText("");
 197         }
 198         else {
 199             synopsisField.setText(e.getSynopsis());
 200             kwField.setText(getKeywords(e));
 201             bugIdsField.setText(getBugIds(e));
 202         }
 203     }
 204 
 205     private String entryToString(ExcludeList.Entry e) {
 206         String u = e.getRelativeURL();
 207         String tc = e.getTestCases();
 208         return (tc == null ? u : u + "[" + tc + "]");
 209     }
 210 
 211     private String getBugIds(ExcludeList.Entry e) {
 212         String[] bugIds = e.getBugIdStrings();
 213         StringBuffer sb = new StringBuffer();
 214         for (int i = 0; i < bugIds.length; i++) {
 215             if (i > 0)
 216                 sb.append(", ");
 217             sb.append(bugIds[i]);
 218         }
 219         return sb.toString();
 220     }
 221 
 222     private String getKeywords(ExcludeList.Entry e) {
 223         String[] keywords = e.getPlatforms();
 224         StringBuffer sb = new StringBuffer();
 225         for (int i = 0; i < keywords.length; i++) {
 226             if (i > 0)
 227                 sb.append(", ");
 228             sb.append(keywords[i]);
 229         }
 230         return sb.toString();
 231     }
 232 
 233 
 234     private InterviewParameters params;
 235     private File[] files;
 236     private ExcludeList list;
 237 
 238     private ExcludeListTableModel model;
 239     private JTable table;
 240     private JTextField synopsisField;
 241     private JTextField kwField;
 242     private JTextField bugIdsField;
 243 
 244     private Listener listener;
 245 
 246     private class Listener
 247         extends ComponentAdapter
 248         implements Interview.Observer
 249     {
 250         // ComponentListener
 251         public void componentShown(ComponentEvent e) {
 252             params.addObserver(this);
 253             updateContent();
 254         }
 255 
 256         public void componentHidden(ComponentEvent e) {
 257             params.removeObserver(this);
 258         }
 259 
 260         // Interview.Observer
 261         public void currentQuestionChanged(Question q) {
 262         }
 263 
 264         public void pathUpdated() {
 265             updateContent();
 266         }
 267     }
 268 
 269     private static final int TEST_NAME_COL = 0;
 270     private static final int TEST_CASE_COL = 1;
 271     private static final int BUG_COL = 2;
 272     private static final int KEYWORDS_COL = 3;
 273     private static final int SYNOPSIS_COL = 4;
 274     private static final int COLUMN_COUNT = 5;
 275 
 276     private String[] columnNames;
 277 
 278     private class ExcludeListTableModel implements TableModel
 279     {
 280         ExcludeListTableModel(ExcludeList list) {
 281             if (columnNames == null) {
 282                 columnNames = new String[COLUMN_COUNT];
 283                 columnNames[TEST_NAME_COL] = uif.getI18NString("elb.col.testName");
 284                 columnNames[TEST_CASE_COL] = uif.getI18NString("elb.col.testCase");
 285                 columnNames[BUG_COL] = uif.getI18NString("elb.col.bugId");
 286                 columnNames[KEYWORDS_COL] = uif.getI18NString("elb.col.keywords");
 287                 columnNames[SYNOPSIS_COL] = uif.getI18NString("elb.col.synopsis");
 288             }
 289 
 290             // The following operation is slow and should arguably be
 291             // done by a worker thread, perhaps using the nested List class as
 292             // a Runnable.
 293             SortedSet sortedEntries = new TreeSet(new Comparator() {
 294                     public int compare(Object o1, Object o2) {
 295                         String s1 = entryToString((ExcludeList.Entry)o1);
 296                         String s2 = entryToString((ExcludeList.Entry)o2);
 297                         return s1.compareTo(s2);
 298                     }
 299                 });
 300 
 301             if (list != null) {
 302                 for (Iterator iter = list.getIterator(false); iter.hasNext(); ) {
 303                     ExcludeList.Entry ee = (ExcludeList.Entry) (iter.next());
 304                     sortedEntries.add(ee);
 305                 }
 306             }
 307 
 308             entries = new ExcludeList.Entry[sortedEntries.size()];
 309             sortedEntries.toArray(entries);
 310         }
 311 
 312         ExcludeList.Entry getEntry(int index) {
 313             return (index < 0 || index >= entries.length ? null : entries[index]);
 314         }
 315 
 316         public void addTableModelListener(TableModelListener l) {
 317             // model never changes, so ignore listener
 318         }
 319 
 320         public Class getColumnClass(int columnIndex) {
 321             // for now, all are strings
 322             return String.class;
 323         }
 324 
 325         public int getColumnCount() {
 326             return COLUMN_COUNT;
 327         }
 328 
 329         public String getColumnName(int index) {
 330             return columnNames[index];
 331         }
 332 
 333         public int getRowCount() {
 334             return entries.length;
 335         }
 336 
 337         public Object getValueAt(int rowIndex, int colIndex) {
 338             ExcludeList.Entry e = entries[rowIndex];
 339             switch (colIndex) {
 340             case TEST_NAME_COL:
 341                 return e.getRelativeURL();
 342 
 343             case TEST_CASE_COL:
 344                 return e.getTestCases();
 345 
 346             case BUG_COL:
 347                 return getBugIds(e);
 348 
 349             case KEYWORDS_COL:
 350                 return getKeywords(e);
 351 
 352             case SYNOPSIS_COL:
 353                 return e.getSynopsis();
 354             }
 355 
 356             throw new IllegalArgumentException();
 357         }
 358 
 359         public boolean isCellEditable(int rowIndex, int colIndex) {
 360             return false;
 361         }
 362 
 363         public void removeTableModelListener(TableModelListener l) {
 364             // model never changes, so ignore listener
 365         }
 366 
 367         public void setValueAt(Object aValue, int rowIndex, int colIndex) {
 368             throw new UnsupportedOperationException();
 369         }
 370 
 371         private ExcludeList.Entry[] entries;
 372 
 373     }
 374 }