001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Anton Avtamonov
020: * @version $Revision$
021: */package javax.swing.plaf.metal;
022:
023: import java.awt.Component;
024: import java.awt.Graphics;
025: import java.awt.Insets;
026: import java.awt.Rectangle;
027:
028: import javax.swing.CellRendererPane;
029: import javax.swing.DefaultButtonModel;
030: import javax.swing.Icon;
031: import javax.swing.JButton;
032: import javax.swing.JComboBox;
033: import javax.swing.JComponent;
034: import javax.swing.JList;
035: import javax.swing.UIManager;
036:
037: import org.apache.harmony.x.swing.ButtonCommons;
038:
039: public class MetalComboBoxButton extends JButton {
040: protected JComboBox comboBox;
041: protected Icon comboIcon;
042: protected boolean iconOnly;
043: protected JList listBox;
044: protected CellRendererPane rendererPane;
045:
046: private Insets cachedInsets = new Insets(0, 0, 0, 0);
047:
048: private class MetalComboBoxButtonModel extends DefaultButtonModel {
049: public void setArmed(final boolean isArmed) {
050: super .setArmed(isArmed || isPressed());
051: }
052: }
053:
054: public MetalComboBoxButton(final JComboBox comboBox,
055: final Icon icon, final CellRendererPane rendererPane,
056: final JList list) {
057: this (comboBox, icon, false, rendererPane, list);
058: }
059:
060: public MetalComboBoxButton(final JComboBox comboBox,
061: final Icon icon, final boolean iconOnly,
062: final CellRendererPane rendererPane, final JList list) {
063: setComboBox(comboBox);
064: setComboIcon(icon);
065: setIconOnly(iconOnly);
066: this .rendererPane = rendererPane;
067: this .listBox = list;
068: setEnabled(comboBox.isEnabled());
069: setModel(new MetalComboBoxButtonModel());
070: }
071:
072: public final JComboBox getComboBox() {
073: return comboBox;
074: }
075:
076: public final void setComboBox(final JComboBox comboBox) {
077: this .comboBox = comboBox;
078: }
079:
080: public final Icon getComboIcon() {
081: return comboIcon;
082: }
083:
084: public final void setComboIcon(final Icon icon) {
085: this .comboIcon = icon;
086: }
087:
088: public final boolean isIconOnly() {
089: return iconOnly;
090: }
091:
092: public final void setIconOnly(final boolean isIconOnly) {
093: this .iconOnly = isIconOnly;
094: }
095:
096: public boolean isFocusTraversable() {
097: return false;
098: }
099:
100: public void setEnabled(final boolean enabled) {
101: super .setEnabled(enabled);
102: if (enabled) {
103: setForeground(comboBox.getForeground());
104: setBackground(comboBox.getBackground());
105: } else {
106: setForeground(UIManager
107: .getColor("ComboBox.disabledForeground"));
108: setBackground(UIManager
109: .getColor("ComboBox.disabledBackground"));
110: }
111: }
112:
113: public void paintComponent(final Graphics g) {
114: super .paintComponent(g);
115: Insets insets = getInsets(cachedInsets);
116: int viewX = insets.left;
117: int viewY = insets.top;
118: int viewWidth = getWidth() - insets.left - insets.right;
119: int viewHeight = getHeight() - insets.top - insets.bottom;
120:
121: if (iconOnly) {
122: if (comboIcon != null) {
123: int iconX = viewX
124: + (viewWidth - comboIcon.getIconWidth()) / 2
125: + 1;
126: int iconY = viewY
127: + (viewHeight - comboIcon.getIconHeight()) / 2
128: + 1;
129: paintIcon(g, iconX, iconY);
130: }
131: } else {
132: if (comboIcon != null) {
133: if (comboBox.getComponentOrientation().isLeftToRight()) {
134: paintItem(g, viewX, viewY, viewWidth
135: - comboIcon.getIconWidth() - 1, viewHeight);
136:
137: int iconX = viewX + viewWidth
138: - comboIcon.getIconWidth() - 1;
139: int iconY = viewY
140: + (viewHeight - comboIcon.getIconHeight())
141: / 2;
142: paintIcon(g, iconX, iconY);
143: } else {
144: int iconX = viewX + 1;
145: int iconY = viewY
146: + (viewHeight - comboIcon.getIconHeight())
147: / 2;
148: paintIcon(g, iconX, iconY);
149:
150: paintItem(g, iconX + comboIcon.getIconWidth() + 1,
151: viewY, viewWidth - comboIcon.getIconWidth()
152: - 1, viewHeight);
153: }
154: } else {
155: paintItem(g, viewX, viewY, viewWidth, viewHeight);
156: }
157: }
158:
159: if (comboBox.isFocusOwner() && !comboBox.isEditable()) {
160: ButtonCommons.paintFocus(g, new Rectangle(viewX, viewY,
161: viewWidth + 2, viewHeight),
162: ((MetalButtonUI) getUI()).getFocusColor());
163: }
164: }
165:
166: private void paintItem(final Graphics g, final int x, final int y,
167: final int w, final int h) {
168: if (listBox == null) {
169: return;
170: }
171: Component renderer = comboBox.getRenderer()
172: .getListCellRendererComponent(listBox,
173: comboBox.getSelectedItem(), -1, false, false);
174: if (renderer instanceof JComponent) {
175: ((JComponent) renderer).setOpaque(false);
176: if (comboBox.isEnabled()) {
177: renderer.setForeground(comboBox.getForeground());
178: } else {
179: renderer.setForeground(UIManager
180: .getColor("ComboBox.disabledForeground"));
181: }
182: }
183:
184: rendererPane.paintComponent(g, renderer, this , x, y, w, h);
185: }
186:
187: private void paintIcon(final Graphics g, final int x, final int y) {
188: comboIcon.paintIcon(this, g, x, y);
189: }
190: }
|