001: /*
002: * Copyright 2005 Patrick Gotthardt
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.pagosoft.plaf;
017:
018: import javax.swing.*;
019: import java.awt.*;
020:
021: public class PgsComboBoxButtonUI extends JButton {
022: protected JComboBox comboBox;
023: protected JList listBox;
024: protected CellRendererPane rendererPane;
025: protected Icon comboIcon;
026: protected boolean iconOnly = false;
027:
028: public final JComboBox getComboBox() {
029: return comboBox;
030: }
031:
032: public final void setComboBox(JComboBox cb) {
033: comboBox = cb;
034: }
035:
036: public final Icon getComboIcon() {
037: return comboIcon;
038: }
039:
040: public final void setComboIcon(Icon i) {
041: comboIcon = i;
042: }
043:
044: public final boolean isIconOnly() {
045: return iconOnly;
046: }
047:
048: public final void setIconOnly(boolean isIconOnly) {
049: iconOnly = isIconOnly;
050: }
051:
052: public PgsComboBoxButtonUI() {
053: super ("");
054: DefaultButtonModel model = new DefaultButtonModel() {
055: public void setArmed(boolean armed) {
056: super .setArmed(isPressed() ? true : armed);
057: }
058: };
059: setModel(model);
060: }
061:
062: public PgsComboBoxButtonUI(JComboBox cb, Icon i,
063: CellRendererPane pane, JList list) {
064: this ();
065: comboBox = cb;
066: comboIcon = i;
067: rendererPane = pane;
068: listBox = list;
069: setEnabled(comboBox.isEnabled());
070: }
071:
072: public PgsComboBoxButtonUI(JComboBox cb, Icon i, boolean onlyIcon,
073: CellRendererPane pane, JList list) {
074: this (cb, i, pane, list);
075: iconOnly = onlyIcon;
076: }
077:
078: public boolean isFocusTraversable() {
079: return false;
080: }
081:
082: public void setEnabled(boolean enabled) {
083: super .setEnabled(enabled);
084:
085: // Set the background and foreground to the combobox colors.
086: if (enabled) {
087: setBackground(comboBox.getBackground());
088: setForeground(comboBox.getForeground());
089: } else {
090: setBackground(UIManager
091: .getColor("ComboBox.disabledBackground"));
092: setForeground(UIManager
093: .getColor("ComboBox.disabledForeground"));
094: }
095: }
096:
097: public void paintComponent(Graphics g) {
098: boolean leftToRight = PgsUtils.isLeftToRight(comboBox);
099:
100: // Paint the button as usual
101: super .paintComponent(g);
102:
103: Insets insets = getInsets();
104:
105: int width = getWidth() - (insets.left + insets.right);
106: int height = getHeight() - (insets.top + insets.bottom);
107:
108: if (height <= 0 || width <= 0) {
109: return;
110: }
111:
112: int left = insets.left;
113: int top = insets.top;
114: int right = left + (width - 1);
115: int bottom = top + (height - 1);
116:
117: int iconWidth = 0;
118: int iconLeft = (leftToRight) ? right : left;
119:
120: // Paint the icon
121: if (comboIcon != null) {
122: iconWidth = comboIcon.getIconWidth();
123: int iconHeight = comboIcon.getIconHeight();
124: int iconTop = 0;
125:
126: if (iconOnly) {
127: iconLeft = (getWidth() / 2) - (iconWidth / 2);
128: iconTop = (getHeight() / 2) - (iconHeight / 2);
129: } else {
130: if (leftToRight) {
131: //iconLeft = (left + (width - 1)) - iconWidth;
132: iconLeft = (getWidth() / 2) - (iconWidth / 2);
133: } else {
134: iconLeft = left;
135: }
136: iconTop = (top + ((bottom - top) / 2))
137: - (iconHeight / 2);
138: }
139:
140: comboIcon.paintIcon(this, g, iconLeft, iconTop);
141: }
142: }
143: }
|