1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 2001, 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.Component;
  30 import java.awt.Font;
  31 import javax.swing.border.Border;
  32 import javax.swing.BorderFactory;
  33 import javax.swing.JLabel;
  34 import javax.swing.JList;
  35 import javax.swing.ListCellRenderer;
  36 
  37 import com.sun.javatest.TestFilter;
  38 import com.sun.javatest.TestResult;
  39 import com.sun.javatest.TestResultTable;
  40 import com.sun.javatest.tool.I18NUtils;
  41 import com.sun.javatest.util.I18NResourceBundle;
  42 
  43 class RenderingUtilities {
  44     static ListCellRenderer createTestListRenderer() {
  45         return new TestCellRenderer(i18n);
  46     }
  47 
  48     static ListCellRenderer createTRTNodeRenderer() {
  49         return new TestCellRenderer(i18n);
  50     }
  51 
  52     static ListCellRenderer<TestFilter> createFilterListRenderer() {
  53         return new FilterCellRenderer(i18n);
  54     }
  55 
  56     private static I18NResourceBundle i18n =
  57         I18NResourceBundle.getBundleForClass(RenderingUtilities.class);
  58     private static TestCellRenderer tlRend;
  59     private static FilterCellRenderer flRend;
  60 
  61     // --------- Inner classes -------
  62 
  63     /**
  64      * Render a list of tests (TestResult objects).
  65      */
  66     static class TestCellRenderer extends JLabel implements ListCellRenderer {
  67          public TestCellRenderer(I18NResourceBundle i18n) {
  68              setOpaque(false);
  69              this.i18n = i18n;
  70          }
  71 
  72         public Component getListCellRendererComponent(JList list,
  73             Object value, int index, boolean isSelected, boolean cellHasFocus) {
  74             if (value == null)  // very strange...
  75                 return this;
  76 
  77             if (value instanceof TestResult) {
  78                 TestResult tr = (TestResult)value;
  79                 setText(tr.getTestName());
  80                 setToolTipText(I18NUtils.getStatusMessage(tr.getStatus()));
  81                 setBasicAttribs(isSelected, list);
  82             }
  83             else if (value instanceof TestResultTable.TreeNode) {
  84                 TestResultTable.TreeNode tn = (TestResultTable.TreeNode)value;
  85                 if (tn.getName() != null)
  86                     setText(TestResultTable.getRootRelativePath(tn));
  87                 else
  88                     setText(i18n.getString("rendUtil.rootName"));
  89                 //setToolTipText(I18NUtils.getStatusMessage(tr.getStatus()));
  90                 setBasicAttribs(isSelected, list);
  91             }
  92             else {          // this code really should never run
  93                 setText(value.toString());
  94                 if (isSelected) {
  95                     setOpaque(true);
  96                     setBackground(list.getSelectionBackground());
  97                     setForeground(list.getSelectionForeground());
  98                 }
  99                 else {
 100                     setForeground(list.getForeground());
 101                     setOpaque(false);
 102                 }
 103             }
 104 
 105             setFont(getFont().deriveFont(Font.PLAIN));
 106 
 107             return this;
 108         }
 109 
 110         private void setBasicAttribs(boolean isSelected, JList list) {
 111             // Hopefully safe to share...will help with saving space
 112             // This border is to provide space between the text and the
 113             // side of the widget, helping readability.
 114             setBorder(spacerBorder);
 115 
 116             if (isSelected) {
 117                 setOpaque(true);
 118                 setBackground(list.getSelectionBackground());
 119                 setForeground(list.getSelectionForeground());
 120             }
 121             else {
 122                 setForeground(list.getForeground());
 123                 setOpaque(false);
 124             }
 125         }
 126 
 127         private I18NResourceBundle i18n;
 128         // border to pad left and right
 129         private Border spacerBorder = BorderFactory.createEmptyBorder(0,3,0,3);
 130     }
 131 
 132     /**
 133      * Render a list of test filters with their descriptive name.
 134      * @see com.sun.javatest.TestFilter#getName()
 135      */
 136     static class FilterCellRenderer extends JLabel implements ListCellRenderer<TestFilter> {
 137         public FilterCellRenderer(I18NResourceBundle i18n) {
 138             setOpaque(false);
 139              this.i18n = i18n;                                                               
 140         }
 141 
 142         public Component getListCellRendererComponent(JList<? extends TestFilter> list,
 143                                                       TestFilter value, int index, boolean isSelected, boolean cellHasFocus) {
 144 
 145             String name = null;
 146 
 147             TestFilter filter = value;
 148             name = filter.getName();
 149 
 150             //setToolTipText(filter.getDescription());
 151             if (name != null && name.length() > 0)
 152                 setText(name);
 153             else
 154                 setText(i18n.getString("rendUtil.noFilterName"));
 155 
 156             setColors(isSelected, list);
 157             setFont(false);
 158 
 159             return this;
 160         }
 161 
 162         private void setColors(boolean isSelected, JList list) {
 163             if (isSelected) {
 164                 setOpaque(true);
 165                 setForeground(list.getSelectionForeground());
 166                 setBackground(list.getSelectionBackground());
 167             }
 168             else {
 169                 //setForeground(MetalLookAndFeel.getPrimaryControlDarkShadow());
 170                 setForeground(list.getForeground());
 171                 setOpaque(false);
 172             }
 173         }
 174 
 175         private void setFont(boolean isActive) {
 176             if (isActive)
 177                 setFont(getFont().deriveFont(Font.BOLD));
 178             else
 179                 setFont(getFont().deriveFont(Font.PLAIN));
 180         }
 181 
 182         private I18NResourceBundle i18n;
 183     }
 184     // end inner classes
 185 }