001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package org.jvnet.substance;
031:
032: import java.awt.*;
033:
034: import javax.swing.*;
035: import javax.swing.border.EmptyBorder;
036: import javax.swing.plaf.ListUI;
037:
038: import org.jvnet.lafwidget.animation.FadeKind;
039: import org.jvnet.substance.theme.SubstanceTheme;
040: import org.jvnet.substance.utils.*;
041:
042: /**
043: * Default renderer for list cells.
044: *
045: * @author Kirill Grouchnikov
046: */
047: public class SubstanceDefaultListCellRenderer extends
048: DefaultListCellRenderer {
049: /**
050: * Default border to provide consistent offsets on left and right sides.
051: */
052: // protected static final Border noFocusBorder = new EmptyBorder(1, 4, 1,
053: // 4);
054: /**
055: * Constructs a default renderer object for an item in a list.
056: */
057: public SubstanceDefaultListCellRenderer() {
058: super ();
059: }
060:
061: /*
062: * (non-Javadoc)
063: *
064: * @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing.JList,
065: * java.lang.Object, int, boolean, boolean)
066: */
067: @Override
068: public Component getListCellRendererComponent(JList list,
069: Object value, int index, boolean isSelected,
070: boolean cellHasFocus) {
071: this .setComponentOrientation(list.getComponentOrientation());
072:
073: ListUI listUI = list.getUI();
074: if (listUI instanceof SubstanceListUI) {
075: SubstanceListUI ui = (SubstanceListUI) listUI;
076: ComponentState state = ui.getCellState(index);
077: ComponentState prevState = ui.getPrevCellState(index);
078:
079: // // special case for the combobox. The selected value is
080: // // painted using the renderer of the list, and the index
081: // // is -1.
082: // if (index == -1) {
083: // if (isSelected && list.isEnabled()) {
084: // state = ComponentState.SELECTED;
085: // prevState = ComponentState.SELECTED;
086: // }
087: // // check if the originating combo is enabled
088: // Component popup = SwingUtilities.getAncestorOfClass(
089: // SubstanceComboPopup.class, list);
090: // if (popup != null) {
091: // boolean isEnabled = ((SubstanceComboPopup) popup)
092: // .isComboEnabled();
093: // if (!isEnabled) {
094: // state = ComponentState.DISABLED_SELECTED;
095: // prevState = ComponentState.DISABLED_SELECTED;
096: // }
097: // }
098: // }
099: SubstanceTheme theme = SubstanceThemeUtilities.getTheme(
100: list, state);
101: Color color = SubstanceCoreUtilities
102: .getInterpolatedForegroundColor(list, index, theme,
103: state, prevState, FadeKind.SELECTION,
104: FadeKind.ROLLOVER);
105:
106: // System.out.println("[row " + index + "] - " + prevState.name() +
107: // ":"
108: // + state.name() + ":" + color + " from "
109: // + theme.getDisplayName());
110:
111: super .setForeground(color);
112: } else {
113: if (isSelected) {
114: this .setForeground(list.getSelectionForeground());
115: } else {
116: this .setForeground(list.getForeground());
117: }
118: }
119:
120: if (isSelected) {
121: // setBackground(list.getSelectionBackground());
122: // this.setForeground(list.getSelectionForeground());
123: } else {
124: SubstanceCoreUtilities.applyStripedBackground(list, index,
125: this );
126: // this.setForeground(list.getForeground());
127: }
128:
129: if (value instanceof Icon) {
130: this .setIcon((Icon) value);
131: this .setText("");
132: } else {
133: this .setIcon(null);
134: this .setText((value == null) ? "" : value.toString());
135: }
136:
137: this .setEnabled(list.isEnabled());
138: this .setFont(list.getFont());
139:
140: Insets ins = SubstanceSizeUtils
141: .getListCellRendererInsets(SubstanceSizeUtils
142: .getComponentFontSize(list));
143: this .setBorder(new EmptyBorder(ins.top, ins.left, ins.bottom,
144: ins.right));
145:
146: this .setOpaque(true);
147: return this ;
148: }
149:
150: /**
151: * UI resource for renderer (does nothing yet).
152: *
153: * @author Kirill Grouchnikov
154: */
155: public static class SubstanceUIResource extends
156: SubstanceDefaultListCellRenderer implements
157: javax.swing.plaf.UIResource {
158: /*
159: * (non-Javadoc)
160: *
161: * @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing.JList,
162: * java.lang.Object, int, boolean, boolean)
163: */
164: @Override
165: public Component getListCellRendererComponent(JList list,
166: Object value, int index, boolean isSelected,
167: boolean cellHasFocus) {
168: return super.getListCellRendererComponent(list, value,
169: index, isSelected, cellHasFocus);
170: }
171: }
172:
173: }
|