001: /*
002: * @(#)CheckBoxtListCellRenderer.java 5/11/2005
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006:
007: package com.jidesoft.swing;
008:
009: import javax.swing.*;
010: import javax.swing.border.Border;
011: import javax.swing.border.EmptyBorder;
012: import java.awt.*;
013: import java.awt.event.MouseEvent;
014: import java.io.Serializable;
015:
016: /**
017: * Renders an item in a list using JCheckBox.
018: */
019: public class CheckBoxListCellRenderer extends JPanel implements
020: ListCellRenderer, Serializable {
021:
022: protected static Border noFocusBorder;
023:
024: /**
025: * The checkbox that is used to paint the check box in cell renderer
026: */
027: protected JCheckBox _checkBox = new NullCheckBox();
028: protected JLabel _label = new NullLabel();
029:
030: /**
031: * The label which appears after the check box.
032: */
033: protected ListCellRenderer _actualListRenderer;
034:
035: public CheckBoxListCellRenderer(ListCellRenderer renderer) {
036: if (noFocusBorder == null) {
037: noFocusBorder = new EmptyBorder(1, 1, 1, 1);
038: }
039: setOpaque(true);
040: setBorder(noFocusBorder);
041: setLayout(new BorderLayout(0, 0));
042: add(_checkBox, BorderLayout.BEFORE_LINE_BEGINS);
043: _actualListRenderer = renderer;
044: }
045:
046: /**
047: * Constructs a default renderer object for an item
048: * in a list.
049: */
050: public CheckBoxListCellRenderer() {
051: this (null);
052: }
053:
054: public ListCellRenderer getActualListRenderer() {
055: return _actualListRenderer;
056: }
057:
058: public void setActualListRenderer(
059: ListCellRenderer actualListRenderer) {
060: _actualListRenderer = actualListRenderer;
061: }
062:
063: @Override
064: public String getToolTipText(MouseEvent event) {
065: if (_actualListRenderer instanceof JComponent) {
066: Point p = event.getPoint();
067: p.translate(-_checkBox.getWidth(), 0);
068: MouseEvent newEvent = new MouseEvent(
069: ((JComponent) _actualListRenderer), event.getID(),
070: event.getWhen(), event.getModifiers(), p.x, p.y,
071: event.getClickCount(), event.isPopupTrigger());
072:
073: String tip = ((JComponent) _actualListRenderer)
074: .getToolTipText(newEvent);
075:
076: if (tip != null) {
077: return tip;
078: }
079: }
080: return super .getToolTipText(event);
081: }
082:
083: public Component getListCellRendererComponent(JList list,
084: Object value, int index, boolean isSelected,
085: boolean cellHasFocus) {
086:
087: _checkBox.setPreferredSize(new Dimension(_checkBox
088: .getPreferredSize().width, 0));
089: setComponentOrientation(list.getComponentOrientation());
090:
091: Object actualValue;
092: if (list instanceof CheckBoxList) {
093: CheckBoxListSelectionModel selectionModel = ((CheckBoxList) list)
094: .getCheckBoxListSelectionModel();
095: if (selectionModel != null) {
096: boolean enabled = list.isEnabled()
097: && ((CheckBoxList) list).isCheckBoxEnabled()
098: && ((CheckBoxList) list)
099: .isCheckBoxEnabled(index);
100: if (!enabled && !isSelected) {
101: setForeground(getBackground().darker());
102: }
103: _checkBox.setEnabled(enabled);
104: _checkBox.setSelected(selectionModel
105: .isSelectedIndex(index));
106: }
107: actualValue = value;
108: } else if (list instanceof CheckBoxListWithSelectable) {
109: if (value instanceof Selectable) {
110: _checkBox
111: .setSelected(((Selectable) value).isSelected());
112: boolean enabled = list.isEnabled()
113: && ((Selectable) value).isEnabled()
114: && ((CheckBoxListWithSelectable) list)
115: .isCheckBoxEnabled();
116: if (!enabled && !isSelected) {
117: setForeground(getBackground().darker());
118: }
119: _checkBox.setEnabled(enabled);
120: } else {
121: boolean enabled = list.isEnabled();
122: if (!enabled && !isSelected) {
123: setForeground(getBackground().darker());
124: }
125: _checkBox.setEnabled(enabled);
126: }
127:
128: if (value instanceof DefaultSelectable) {
129: actualValue = ((DefaultSelectable) value).getObject();
130: } else {
131: actualValue = value;
132: }
133: } else {
134: throw new IllegalArgumentException(
135: "CheckBoxListCellRenderer should only be used for CheckBoxList.");
136: }
137:
138: if (_actualListRenderer != null) {
139: JComponent listCellRendererComponent = (JComponent) _actualListRenderer
140: .getListCellRendererComponent(list, value, index,
141: isSelected, cellHasFocus);
142: if (list instanceof CheckBoxList) {
143: if (!((CheckBoxList) list).isCheckBoxVisible(index)) {
144: return listCellRendererComponent;
145: }
146: } else if (list instanceof CheckBoxListWithSelectable) {
147: if (!((CheckBoxListWithSelectable) list)
148: .isCheckBoxVisible(index)) {
149: return listCellRendererComponent;
150: }
151: }
152: Border border = listCellRendererComponent.getBorder();
153: setBorder(border);
154: listCellRendererComponent.setBorder(BorderFactory
155: .createEmptyBorder());
156: if (getComponentCount() == 2) {
157: remove(1);
158: }
159: add(listCellRendererComponent);
160: setBackground(listCellRendererComponent.getBackground());
161: setForeground(listCellRendererComponent.getForeground());
162: } else {
163: if (isSelected) {
164: setBackground(list.getSelectionBackground());
165: setForeground(list.getSelectionForeground());
166: } else {
167: setBackground(list.getBackground());
168: setForeground(list.getForeground());
169: }
170: if (getComponentCount() == 2) {
171: remove(1);
172: }
173: add(_label);
174: customizeDefaultCellRenderer(actualValue);
175: setFont(list.getFont());
176: }
177:
178: return this ;
179: }
180:
181: /**
182: * Customizes the cell renderer. By default, it will use toString to covert the object
183: * and use it as the text for the checkbox. You can subclass it to set an icon, change alignment etc.
184: * Since "this" is a JCheckBox, you can call all methods available on JCheckBox in the overridden method.
185: *
186: * @param value
187: */
188: protected void customizeDefaultCellRenderer(Object value) {
189: if (value instanceof Icon) {
190: _label.setIcon((Icon) value);
191: _label.setText("");
192: } else {
193: _label.setIcon(null);
194: _label.setText((value == null) ? "" : value.toString());
195: }
196: }
197:
198: /**
199: * A subclass of DefaultListCellRenderer that implements UIResource.
200: * DefaultListCellRenderer doesn't implement UIResource
201: * directly so that applications can safely override the
202: * cellRenderer property with DefaultListCellRenderer subclasses.
203: * <p/>
204: * <strong>Warning:</strong>
205: * Serialized objects of this class will not be compatible with
206: * future Swing releases. The current serialization support is
207: * appropriate for short term storage or RMI between applications running
208: * the same version of Swing. As of 1.4, support for long term storage
209: * of all JavaBeans<sup><font size="-2">TM</font></sup>
210: * has been added to the <code>java.beans</code> package.
211: * Please see {@link java.beans.XMLEncoder}.
212: */
213: public static class UIResource extends CheckBoxListCellRenderer
214: implements javax.swing.plaf.UIResource {
215: }
216:
217: }
|