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.interview.Help;
  30 import java.awt.BorderLayout;
  31 import java.awt.Component;
  32 import java.awt.Dimension;
  33 import java.awt.FontMetrics;
  34 import java.awt.GridBagConstraints;
  35 import java.awt.Graphics;
  36 import java.awt.GridBagLayout;
  37 import java.awt.Insets;
  38 import java.awt.event.ActionListener;
  39 import javax.swing.BorderFactory;
  40 import javax.swing.JButton;
  41 import javax.swing.JPanel;
  42 import javax.swing.JTabbedPane;
  43 import javax.swing.JTextField;
  44 import javax.swing.event.AncestorEvent;
  45 import javax.swing.event.AncestorListener;
  46 import javax.swing.event.ChangeEvent;
  47 import javax.swing.event.ChangeListener;
  48 
  49 import com.sun.javatest.InterviewParameters;
  50 import com.sun.javatest.tool.ToolDialog;
  51 import com.sun.javatest.tool.UIFactory;
  52 import com.sun.javatest.tool.jthelp.HelpID;
  53 import com.sun.javatest.tool.jthelp.HelpSet;
  54 import com.sun.javatest.tool.jthelp.JHelpContentViewer;
  55 import com.sun.javatest.tool.jthelp.ContextHelpManager;
  56 
  57 class CE_StdView extends CE_View
  58 {
  59     CE_StdView(InterviewParameters config,
  60                JHelpContentViewer infoPanel, UIFactory uif, ActionListener l) {
  61         super(config, infoPanel, uif, l);
  62         initGUI();
  63     }
  64 
  65     public Dimension getPreferredSize() {
  66         Insets tabInsets = tabs.getInsets();
  67         int w = (tabInsets == null ? 0 : tabInsets.left + tabInsets.right);
  68         Graphics g = tabs.getGraphics();
  69         if (g != null) {
  70             FontMetrics fm = g.getFontMetrics();
  71             for (int i = 0; i < tabs.getTabCount(); i++)
  72                 w += 15 + fm.stringWidth(tabs.getTitleAt(i));
  73         }
  74         Dimension d = super.getPreferredSize();
  75         d.width = Math.max(d.width, w);
  76         return d;
  77     }
  78 
  79     public void setVisible(boolean b) {
  80         if (b)
  81             load();
  82         super.setVisible(b);
  83     }
  84 
  85 
  86     public void setParentToolDialog(ToolDialog d) {
  87         super.setParentToolDialog(d);
  88         for (CE_StdPane pane : panes) {
  89             pane.setParentToolDialog(d);
  90         }
  91     }
  92 
  93 
  94     void showTab(int id) {
  95         Class c;
  96         switch (id) {
  97         case TESTS_PANE:
  98             c = CE_TestsPane.class;
  99             break;
 100         case EXCLUDE_LIST_PANE:
 101             c = CE_ExcludeListPane.class;
 102             break;
 103         case KFL_PANE:
 104             c = CE_KFLPane.class;
 105             break;
 106         case KEYWORDS_PANE:
 107             c = CE_KeywordsPane.class;
 108             break;
 109         case PRIOR_STATUS_PANE:
 110             c = CE_PriorStatusPane.class;
 111             break;
 112         case ENVIRONMENT_PANE:
 113             c = CE_EnvironmentPane.class;
 114             break;
 115         case EXECUTION_PANE:
 116         case CONCURRENCY_PANE:
 117         case TIMEOUT_FACTOR_PANE:
 118             c = CE_ExecutionPane.class;
 119             break;
 120         default:
 121             throw new IllegalArgumentException();
 122         }
 123 
 124         for (int i = 0; i < tabs.getTabCount(); i++) {
 125             Component tab = tabs.getComponentAt(i);
 126             if (c.isAssignableFrom(tab.getClass())) {
 127                 tabs.setSelectedIndex(i);
 128                 return;
 129             }
 130         }
 131     }
 132 
 133     static final int TESTS_PANE = 0;
 134     static final int EXCLUDE_LIST_PANE = 1;
 135     static final int KEYWORDS_PANE = 2;
 136     static final int PRIOR_STATUS_PANE = 3;
 137     static final int ENVIRONMENT_PANE = 4;
 138     static final int EXECUTION_PANE = 5;
 139     static final int CONCURRENCY_PANE = 6;  // note displayed in EXECUTION_PANE
 140     static final int TIMEOUT_FACTOR_PANE = 7; // note displayed in EXECUTION_PANE
 141     static final int KFL_PANE = 8;
 142 
 143     boolean isOKToClose() {
 144         CE_StdPane currPane = (CE_StdPane) (tabs.getSelectedComponent());
 145         if (currPane == null)
 146             return true;
 147         return currPane.isOKToClose();
 148     }
 149 
 150     void load() {
 151         for (int i = 0; i < panes.length; i++)
 152             panes[i].load();
 153     }
 154 
 155     void save() {
 156         for (int i = 0; i < panes.length; i++)
 157             panes[i].save();
 158     }
 159 
 160     void setCheckExcludeListListener(ActionListener l) {
 161         for (int i = 0; i < panes.length; i++) {
 162             CE_StdPane pane = panes[i];
 163             if (pane instanceof CE_ExcludeListPane)
 164                 ((CE_ExcludeListPane) pane).setCheckExcludeListListener(l);
 165         }
 166     }
 167 
 168     private void initGUI() {
 169         setName(STD);
 170         ContextHelpManager.setHelpIDString(this, "confEdit.stdView.csh");
 171 
 172         setLayout(new BorderLayout());
 173         initBody();
 174         initButtons();
 175     }
 176 
 177     private void initBody() {
 178         CE_KeywordsPane kp = new CE_KeywordsPane(uif, config);
 179         kp.setParentToolDialog(toolDialog);
 180         panes = new CE_StdPane[] {
 181             new CE_TestsPane(uif, config),
 182             new CE_ExcludeListPane(uif, config),
 183             new CE_KFLPane(uif, config),
 184             kp,
 185             new CE_PriorStatusPane(uif, config),
 186             new CE_EnvironmentPane(uif, config),
 187             new CE_ExecutionPane(uif, config)
 188         };
 189 
 190         tabs = new JTabbedPane() {
 191                 public void setSelectedIndex(int index) {
 192                     if (index == getSelectedIndex())
 193                         return;
 194 
 195                     CE_StdPane p = (CE_StdPane) (getSelectedComponent());
 196                     if (p != null && !p.isOKToClose())
 197                         return;
 198 
 199                     super.setSelectedIndex(index);
 200                 }
 201             };
 202         tabs.setName("tabs");
 203         uif.setAccessibleName(tabs, "ce.tabs");
 204         uif.setToolTip(tabs, "ce.tabs");
 205 
 206         // contentious design issue here:
 207         // should we disable the tabs for disabled panes, or completely hide them,
 208         // or choose on a pane by pane basis
 209         for (int i = 0; i < panes.length; i++) {
 210             if (panes[i].isEnabled())
 211                 uif.addTab(tabs, "ce." + panes[i].getName(), panes[i]);
 212         }
 213 
 214         tabs.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
 215         tabs.addChangeListener(localListener);
 216 
 217         add(tabs, BorderLayout.CENTER);
 218 
 219         addAncestorListener(localListener);
 220     }
 221 
 222     private void initButtons() {
 223         JPanel btnPanel = uif.createPanel("ce.std.btns",
 224                                     new GridBagLayout(),
 225                                     false);
 226         GridBagConstraints c = new GridBagConstraints();
 227         c.insets.top = 5;
 228         c.insets.bottom = 11;  // value from JL&F Guidelines
 229         c.insets.right = 5;    // value from JL&F Guidelines
 230         c.insets.left = 11;
 231 
 232         // Message Area, grow to fit
 233         c.fill = GridBagConstraints.HORIZONTAL;
 234         c.weightx = 1;
 235         msgField = uif.createOutputField("ce.msgs");
 236         msgField.setBorder(null);
 237         msgField.setEnabled(false);
 238         //completeMsg = uif.getI18NString("ce.msgs.complete");
 239         //incompleteMsg = uif.getI18NString("ce.msgs.incomplete");
 240         btnPanel.add(msgField, c);
 241 
 242         c.weightx = 0;
 243         c.insets.left = 0;
 244         c.insets.right = 11;    // value from JL&F Guidelines
 245         JButton doneBtn = uif.createButton("ce.done", listener, DONE);
 246         btnPanel.add(doneBtn, c);
 247 
 248         add(btnPanel, BorderLayout.SOUTH);
 249     }
 250 
 251     private void showInfoForTab(CE_StdPane p) {
 252         HelpID helpId = (HelpID) (p.getClientProperty(this));
 253         if (helpId == null) {
 254             String s = "ConfigEditor.stdValues." + p.getName();
 255             HelpSet configHelpSet = Help.getHelpSet(config);
 256             if (configHelpSet != null) {
 257                 helpId = HelpID.create(s, configHelpSet);
 258                 p.putClientProperty(this, helpId);
 259             }
 260         }
 261         if (helpId == null)
 262             System.err.println("CESV: no help for " + p);
 263         else
 264             showInfo(helpId);
 265     }
 266 
 267     private JTabbedPane tabs;
 268     private CE_StdPane[] panes;
 269     private JTextField msgField;
 270 
 271     private Listener localListener = new Listener();
 272 
 273     private class Listener
 274         implements AncestorListener, ChangeListener
 275     {
 276         // ---------- from AncestorListener -----------
 277 
 278         public void ancestorAdded(AncestorEvent e) {
 279             updateCSHAndInfo();
 280         }
 281 
 282         public void ancestorMoved(AncestorEvent e) { }
 283 
 284         public void ancestorRemoved(AncestorEvent e) {
 285         }
 286 
 287         // ---------- from ChangeListener -----------
 288 
 289         public void stateChanged(ChangeEvent e) {
 290             updateCSHAndInfo();
 291         }
 292 
 293         // --------------------------------------------
 294 
 295         private void updateCSHAndInfo() {
 296             CE_StdPane p = (CE_StdPane) (tabs.getSelectedComponent());
 297             ContextHelpManager.setHelpIDString(tabs, ContextHelpManager.getHelpIDString(p));
 298             if (isInfoVisible())
 299                 showInfoForTab(p);
 300         }
 301     };
 302 }