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: * Renderer for combo boxes.
044: *
045: * @author Kirill Grouchnikov
046: */
047: public class SubstanceDefaultComboBoxRenderer extends
048: SubstanceDefaultListCellRenderer {
049: /**
050: * The associated combo box.
051: */
052: private JComboBox combo;
053:
054: /**
055: * Simple constructor.
056: *
057: * @param combo
058: * The associated combo box.
059: */
060: public SubstanceDefaultComboBoxRenderer(JComboBox combo) {
061: super ();
062: this .combo = combo;
063: // this.setOpaque(true);
064:
065: Insets ins = SubstanceSizeUtils
066: .getListCellRendererInsets(SubstanceSizeUtils
067: .getComponentFontSize(combo));
068: this .setBorder(new EmptyBorder(ins.top, ins.left, ins.bottom,
069: ins.right));
070: //
071: // Insets i = b.getBorderInsets(combo);
072: // System.out.println("Combo inner - " + combo.getFont().getSize() + " :
073: // "
074: // + i.top + ", " + i.left + ", " + i.bottom + ", " + i.right);
075: }
076:
077: /*
078: * (non-Javadoc)
079: *
080: * @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing.JList,
081: * java.lang.Object, int, boolean, boolean)
082: */
083: @Override
084: public Component getListCellRendererComponent(JList list,
085: Object value, int index, boolean isSelected,
086: boolean cellHasFocus) {
087:
088: JComponent result = (JComponent) super
089: .getListCellRendererComponent(list, value, index,
090: isSelected, cellHasFocus);
091:
092: ListUI listUI = list.getUI();
093: if (listUI instanceof SubstanceListUI) {
094: SubstanceListUI ui = (SubstanceListUI) listUI;
095: ComponentState state = ui.getCellState(index);
096: ComponentState prevState = ui.getPrevCellState(index);
097:
098: // special case for the combobox. The selected value is
099: // painted using the renderer of the list, and the index
100: // is -1.
101: if (index == -1) {
102: boolean isEnabled = this .combo.isEnabled();
103: if (isSelected && isEnabled) {
104: state = ComponentState.SELECTED;
105: prevState = ComponentState.SELECTED;
106: }
107: if (!isEnabled) {
108: state = ComponentState.DISABLED_UNSELECTED;
109: prevState = ComponentState.DISABLED_UNSELECTED;
110: }
111: }
112: SubstanceTheme theme = SubstanceThemeUtilities.getTheme(
113: list, state);
114: Color color = SubstanceCoreUtilities
115: .getInterpolatedForegroundColor(list, index, theme,
116: state, prevState, FadeKind.SELECTION,
117: FadeKind.ROLLOVER);
118:
119: // System.out.println("[" + row + "," + column + "] - "
120: // + prevState.name() + ":" + state.name() + ":" + color);
121:
122: result.setForeground(color);
123:
124: SubstanceCoreUtilities.applyStripedBackground(combo, index,
125: this );
126: }
127: result.setOpaque(!isSelected && (index >= 0));
128: return result;
129: }
130:
131: /*
132: * (non-Javadoc)
133: *
134: * @see javax.swing.JComponent#getPreferredSize()
135: */
136: @Override
137: public Dimension getPreferredSize() {
138: Dimension size;
139:
140: if ((this .getText() == null) || (this .getText().equals(""))) {
141: this .setText(" ");
142: size = super .getPreferredSize();
143: this .setText("");
144: } else {
145: size = super .getPreferredSize();
146: }
147:
148: return size;
149: }
150:
151: /**
152: * UI resource for renderer (does nothing yet).
153: *
154: * @author Kirill Grouchnikov
155: */
156: public static class SubstanceUIResource extends
157: SubstanceDefaultComboBoxRenderer implements
158: javax.swing.plaf.UIResource {
159: /**
160: * Creates a new renderer resource.
161: *
162: * @param combo
163: * Combobox.
164: */
165: public SubstanceUIResource(JComboBox combo) {
166: super(combo);
167: }
168: }
169: }
|