View Javadoc

1   package org.wcb.autohome.util;
2   /***
3    * Copyright (C) 1999  Walter Bogaardt
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
18   *
19   * Project:  Alice X10 Home Automation
20   */
21  
22  import javax.swing.JLabel;
23  import javax.swing.JList;
24  import javax.swing.border.Border;
25  import javax.swing.BorderFactory;
26  import javax.swing.ListCellRenderer;
27  import java.awt.Color;
28  import java.awt.Component;
29  /***
30   * Filename:    $Id: TriggerRenderer.java,v 1.3 2004/02/27 01:29:53 wbogaardt Exp $
31   *
32   * Abstract: This class handles rendering the various trigger items these being
33   *          Time launched triggers, or Module activated triggers.  This rendering
34   *          occures mostly in the Macro Panels.
35   */
36  public class TriggerRenderer extends JLabel implements ListCellRenderer {
37  
38      private Border redBorder = BorderFactory.createLineBorder(Color.green, 2),
39          emptyBorder = BorderFactory.createEmptyBorder(2, 2, 2, 2);
40  
41      /***
42       * This sets up the customized cell render. Return a component that has been configured to display the specified
43       * value. That component's paint method is then called to "render" the cell. If it is necessary to compute the
44       * dimensions of a list because the list cells do not have a fixed size, this method is called to
45       * generate a component on which getPreferredSize can be invoked.
46       *
47       * @param list The JList we're painting.
48       * @param value The value returned by list.getModel().getElementAt(index).
49       * @param index The cells index.
50       * @param isSelected True if the specified cell was selected.
51       * @param cellHasFocus True if the specified cell has the focus.
52       * @return A component whose paint() method will render the specified value.
53       */
54      public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
55                                                    boolean cellHasFocus) {
56          TriggerItem item = (TriggerItem) value;
57          setIcon(item.getIcon());
58          setText(item.toString());
59          if (isSelected)
60          {
61              setBorder(redBorder);
62          }
63          else
64          {
65              setBorder(emptyBorder);
66          }
67          return this;
68      }
69  }