1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 2002, 2018, 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.audit;
  28 
  29 import java.awt.Component;
  30 import javax.swing.AbstractListModel;
  31 import javax.swing.BorderFactory;
  32 import javax.swing.DefaultListCellRenderer;
  33 import javax.swing.JList;
  34 import javax.swing.JScrollPane;
  35 
  36 import com.sun.javatest.TestDescription;
  37 import com.sun.javatest.TestResult;
  38 import com.sun.javatest.tool.UIFactory;
  39 
  40 abstract class ListPane extends AuditPane {
  41     ListPane(String uiKey, UIFactory uif) {
  42         super(uiKey, uif);
  43 
  44         model = new ListModel();
  45         list = uif.createList(uiKey + ".lst", model);
  46         list.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  47         list.setCellRenderer(new Renderer());
  48 
  49         JScrollPane sp = uif.createScrollPane(list,
  50                                          JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  51                                          JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  52         setBody(sp);
  53     }
  54 
  55     void setData(Object[] data) {
  56         if (data == null || data.length == 0)
  57             show(uif.getI18NString("list.noEntries"));
  58         else {
  59             model.setData(data);
  60             showBody();
  61         }
  62     }
  63 
  64     private class ListModel extends AbstractListModel<Object> {
  65         public Object getElementAt(int index) {
  66             return data[index];
  67         }
  68 
  69         public int getSize() {
  70             return (data == null ? 0 : data.length);
  71         }
  72 
  73         void setData(Object[] data) {
  74             this.data = data;
  75             fireContentsChanged(this, 0, data.length - 1);
  76         }
  77 
  78         private Object[] data;
  79     }
  80 
  81     private class Renderer extends DefaultListCellRenderer {
  82         public Component getListCellRendererComponent(JList<?> list, Object o, int index, boolean isSelected, boolean cellHasFocus) {
  83             String name;
  84             if (o instanceof TestResult) {
  85                 TestResult tr = (TestResult) o;
  86                 name = tr.getTestName();
  87             }
  88             else if (o instanceof TestDescription) {
  89                 TestDescription td = (TestDescription) o;
  90                 name = td.getRootRelativeURL();
  91             }
  92             else
  93                 name = String.valueOf(o);
  94             return super.getListCellRendererComponent(list, name, index, isSelected, cellHasFocus);
  95         }
  96     }
  97 
  98     protected JList<Object> list;
  99     private ListModel model;
 100 }