1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 2001, 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.Color;
  30 import javax.swing.JPanel;
  31 
  32 import com.sun.javatest.TestResult;
  33 import com.sun.javatest.TestResultTable;
  34 import com.sun.javatest.tool.UIFactory;
  35 import com.sun.javatest.util.I18NResourceBundle;
  36 import java.util.ArrayList;
  37 
  38 /**
  39  * Base class for the individual displays of the BranchPanel.
  40  */
  41 abstract class BP_BranchSubpanel extends JPanel {
  42 
  43     BP_BranchSubpanel(String name, UIFactory uif, BP_Model model, TestTreeModel ttm,
  44             String uiKey) {
  45         setName(name);
  46         setBackground(Color.white);
  47         uif.setAccessibleInfo(this, uiKey);
  48 
  49         this.uif = uif;
  50         this.model = model;
  51         this.ttm = ttm;
  52     }
  53 
  54     void setTreeModel(TestTreeModel ttm) {
  55         this.ttm = ttm;
  56     }
  57 
  58     boolean isUpdateRequired(TT_BasicNode currNode) {
  59         return (subpanelNode != currNode);
  60     }
  61 
  62     protected void updateSubpanel(TT_BasicNode currNode) {
  63         subpanelNode = currNode;
  64     }
  65 
  66     /**
  67      * The test filters have either completely changed or they have
  68      * changed state.  In either case, subpanels should update
  69      * anything which depends on the filters.
  70      */
  71     protected void invalidateFilters() {
  72         filtersInvalidated = true;
  73     }
  74 
  75     protected void showMessage(String msg) {
  76         // this not on the event thread yet, it gets switched in
  77         // BranchModel
  78         lastMsg = msg;
  79 
  80         if (isVisible()) {
  81             model.showMessage(lastMsg);
  82         }
  83     }
  84 
  85     protected void showMesasge(I18NResourceBundle i18n, String key) {
  86         showMessage(i18n.getString(key));
  87     }
  88 
  89     // this method is to allow the GUI to restore the message if the
  90     // active panel is changed
  91     protected String getLastMessage() {
  92         return lastMsg;
  93     }
  94 
  95     protected void showTest(final TestResult tr) {
  96         TT_BasicNode root = (TT_BasicNode) (ttm.getRoot());
  97         if (root == null) {
  98             return;        // construct the path required by the model
  99         }
 100         TestResultTable.TreeNode p = tr.getParent();
 101         TestResultTable.TreeNode[] path = TestResultTable.getObjectPath(tr);
 102 
 103         if (path == null || path.length == 0) {
 104             // special root case - a test at the root
 105             TT_TreeNode node = root.findByName(tr.getTestName());
 106             if (node == null) {
 107                 return;
 108             }
 109         }
 110 
 111         TT_BasicNode spot = root;
 112         ArrayList<TT_TreeNode> list = new ArrayList(path.length + 1);
 113         list.add(root);
 114         for (int i = 1; i < path.length; i++) {
 115             try {
 116                 spot = (TT_BasicNode) spot.findByName(path[i].getName());
 117                 if (spot == null)
 118                     return;     // not available in tree
 119 
 120                 list.add(spot);
 121             } catch (ClassCastException e) {
 122                 // for some reason the path isn't valid
 123                 // assume that spot was a TT_TestNode for some reason
 124                 return;
 125             }
 126         }
 127 
 128         TT_TreeNode tn = spot.findByName(tr);
 129         if (!(tn instanceof TT_TestNode))
 130             return;     // path does not refer to a test apparently
 131 
 132         Object[] fp = new Object[list.size() + 1];
 133         list.toArray(fp);
 134         fp[fp.length-1] = tn;       // test node is the last in the path
 135 
 136         model.showTest(tr, fp);
 137     }
 138     protected TT_BasicNode subpanelNode;
 139     protected UIFactory uif;
 140     protected String lastMsg;
 141     protected BP_Model model;
 142     protected TestTreeModel ttm;
 143     protected boolean filtersInvalidated;
 144 }
 145