001: /*
002: * @(#)StyledListCellRenderer.java 8/11/2005
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006:
007: package com.jidesoft.list;
008:
009: import com.jidesoft.plaf.UIDefaultsLookup;
010: import com.jidesoft.swing.StyledLabel;
011:
012: import javax.swing.*;
013: import javax.swing.border.Border;
014: import javax.swing.border.EmptyBorder;
015: import java.awt.*;
016: import java.io.Serializable;
017:
018: /**
019: * A list cell renderer based on StyledLabel.
020: * To use it, you should make your cell renderer extending this one and override
021: * {@link #customizeStyledLabel(javax.swing.JList,Object,int,boolean,boolean)} method.
022: * If your overridden method, you can call setStyleRange() or setStyleRanges() based on
023: * the item value, if it is leaf etc information.
024: */
025: public class StyledListCellRenderer extends StyledLabel implements
026: ListCellRenderer, Serializable {
027:
028: protected static Border noFocusBorder;
029:
030: /**
031: * Constructs a default renderer object for an item
032: * in a list.
033: */
034: public StyledListCellRenderer() {
035: super ();
036: if (noFocusBorder == null) {
037: noFocusBorder = new EmptyBorder(1, 1, 1, 1);
038: }
039: setOpaque(true);
040: setBorder(noFocusBorder);
041: }
042:
043: public Component getListCellRendererComponent(JList list,
044: Object value, int index, boolean isSelected,
045: boolean cellHasFocus) {
046: setComponentOrientation(list.getComponentOrientation());
047: if (isSelected) {
048: setBackground(list.getSelectionBackground());
049: setForeground(list.getSelectionForeground());
050: } else {
051: setBackground(list.getBackground());
052: setForeground(list.getForeground());
053: }
054:
055: setIgnoreColorSettings(isSelected);
056: customizeStyledLabel(list, value, index, isSelected,
057: cellHasFocus);
058:
059: setEnabled(list.isEnabled());
060: setFont(list.getFont());
061:
062: Border border = null;
063: if (cellHasFocus) {
064: if (isSelected) {
065: border = UIDefaultsLookup
066: .getBorder("List.focusSelectedCellHighlightBorder");
067: }
068: if (border == null) {
069: border = UIDefaultsLookup
070: .getBorder("List.focusCellHighlightBorder");
071: }
072: } else {
073: border = noFocusBorder;
074: }
075: setBorder(border);
076:
077: return this ;
078: }
079:
080: /**
081: * Overrides this method to customize the styled label.
082: *
083: * @param list
084: * @param value
085: * @param index
086: * @param isSelected
087: * @param cellHasFocus
088: */
089: protected void customizeStyledLabel(JList list, Object value,
090: int index, boolean isSelected, boolean cellHasFocus) {
091: clearStyleRanges();
092: if (value instanceof Icon) {
093: setIcon((Icon) value);
094: setText("");
095: } else {
096: setIcon(null);
097: setText((value == null) ? "" : value.toString());
098: }
099: }
100:
101: /**
102: * Overridden for performance reasons.
103: * See the <a href="#override">Implementation Note</a>
104: * for more information.
105: *
106: * @return <code>true</code> if the background is completely opaque
107: * and differs from the JList's background;
108: * <code>false</code> otherwise
109: * @since 1.5
110: */
111: @Override
112: public boolean isOpaque() {
113: Color back = getBackground();
114: Component p = getParent();
115: if (p != null) {
116: p = p.getParent();
117: }
118: // p should now be the JList.
119: boolean colorMatch = (back != null) && (p != null)
120: && back.equals(p.getBackground()) && p.isOpaque();
121: return !colorMatch && super .isOpaque();
122: }
123:
124: /**
125: * Overridden for performance reasons.
126: * See the <a href="#override">Implementation Note</a>
127: * for more information.
128: */
129: @Override
130: public void validate() {
131: }
132:
133: /**
134: * Overridden for performance reasons.
135: * See the <a href="#override">Implementation Note</a>
136: * for more information.
137: *
138: * @since 1.5
139: */
140: @Override
141: public void invalidate() {
142: }
143:
144: /**
145: * Overridden for performance reasons.
146: * See the <a href="#override">Implementation Note</a>
147: * for more information.
148: *
149: * @since 1.5
150: */
151: @Override
152: public void repaint() {
153: }
154:
155: /**
156: * Overridden for performance reasons.
157: * See the <a href="#override">Implementation Note</a>
158: * for more information.
159: */
160: @Override
161: public void revalidate() {
162: }
163:
164: /**
165: * Overridden for performance reasons.
166: * See the <a href="#override">Implementation Note</a>
167: * for more information.
168: */
169: @Override
170: public void repaint(long tm, int x, int y, int width, int height) {
171: }
172:
173: /**
174: * Overridden for performance reasons.
175: * See the <a href="#override">Implementation Note</a>
176: * for more information.
177: */
178: @Override
179: public void repaint(Rectangle r) {
180: }
181:
182: /**
183: * Overridden for performance reasons.
184: * See the <a href="#override">Implementation Note</a>
185: * for more information.
186: */
187: @Override
188: protected void firePropertyChange(String propertyName,
189: Object oldValue, Object newValue) {
190: // Strings get interned...
191: if (propertyName.equals("text"))
192: super .firePropertyChange(propertyName, oldValue, newValue);
193: }
194:
195: /**
196: * Overridden for performance reasons.
197: * See the <a href="#override">Implementation Note</a>
198: * for more information.
199: */
200: @Override
201: public void firePropertyChange(String propertyName, byte oldValue,
202: byte newValue) {
203: }
204:
205: /**
206: * Overridden for performance reasons.
207: * See the <a href="#override">Implementation Note</a>
208: * for more information.
209: */
210: @Override
211: public void firePropertyChange(String propertyName, char oldValue,
212: char newValue) {
213: }
214:
215: /**
216: * Overridden for performance reasons.
217: * See the <a href="#override">Implementation Note</a>
218: * for more information.
219: */
220: @Override
221: public void firePropertyChange(String propertyName, short oldValue,
222: short newValue) {
223: }
224:
225: /**
226: * Overridden for performance reasons.
227: * See the <a href="#override">Implementation Note</a>
228: * for more information.
229: */
230: @Override
231: public void firePropertyChange(String propertyName, int oldValue,
232: int newValue) {
233: }
234:
235: /**
236: * Overridden for performance reasons.
237: * See the <a href="#override">Implementation Note</a>
238: * for more information.
239: */
240: @Override
241: public void firePropertyChange(String propertyName, long oldValue,
242: long newValue) {
243: }
244:
245: /**
246: * Overridden for performance reasons.
247: * See the <a href="#override">Implementation Note</a>
248: * for more information.
249: */
250: @Override
251: public void firePropertyChange(String propertyName, float oldValue,
252: float newValue) {
253: }
254:
255: /**
256: * Overridden for performance reasons.
257: * See the <a href="#override">Implementation Note</a>
258: * for more information.
259: */
260: @Override
261: public void firePropertyChange(String propertyName,
262: double oldValue, double newValue) {
263: }
264:
265: /**
266: * Overridden for performance reasons.
267: * See the <a href="#override">Implementation Note</a>
268: * for more information.
269: */
270: @Override
271: public void firePropertyChange(String propertyName,
272: boolean oldValue, boolean newValue) {
273: }
274:
275: /**
276: * A subclass of DefaultListCellRenderer that implements UIResource.
277: * DefaultListCellRenderer doesn't implement UIResource
278: * directly so that applications can safely override the
279: * cellRenderer property with DefaultListCellRenderer subclasses.
280: * <p/>
281: * <strong>Warning:</strong>
282: * Serialized objects of this class will not be compatible with
283: * future Swing releases. The current serialization support is
284: * appropriate for short term storage or RMI between applications running
285: * the same version of Swing. As of 1.4, support for long term storage
286: * of all JavaBeans<sup><font size="-2">TM</font></sup>
287: * has been added to the <code>java.beans</code> package.
288: * Please see {@link java.beans.XMLEncoder}.
289: */
290: public static class UIResource extends StyledListCellRenderer
291: implements javax.swing.plaf.UIResource {
292: }
293:
294: }
|