| 1 |
package com.jidesoft.plaf.basic; |
| 2 |
|
| 3 |
import com.jidesoft.plaf.UIDefaultsLookup; |
| 4 |
|
| 5 |
import javax.swing.*; |
| 6 |
import java.awt.*; |
| 7 |
|
| 8 |
public class BasicJideComboBoxButton extends JButton { |
| 9 |
protected JComboBox _comboBox; |
| 10 |
protected JList _listBox; |
| 11 |
protected CellRendererPane _rendererPane; |
| 12 |
protected Icon _comboIcon; |
| 13 |
protected boolean _iconOnly = false; |
| 14 |
|
| 15 |
public final JComboBox getComboBox() { |
| 16 |
return _comboBox; |
| 17 |
} |
| 18 |
|
| 19 |
public final void setComboBox(JComboBox cb) { |
| 20 |
_comboBox = cb; |
| 21 |
} |
| 22 |
|
| 23 |
public final Icon getComboIcon() { |
| 24 |
return _comboIcon; |
| 25 |
} |
| 26 |
|
| 27 |
public final void setComboIcon(Icon i) { |
| 28 |
_comboIcon = i; |
| 29 |
} |
| 30 |
|
| 31 |
public final boolean isIconOnly() { |
| 32 |
return _iconOnly; |
| 33 |
} |
| 34 |
|
| 35 |
public final void setIconOnly(boolean isIconOnly) { |
| 36 |
_iconOnly = isIconOnly; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
public BasicJideComboBoxButton() { |
| 41 |
super(""); |
| 42 |
addMouseListener(new BasicJideButtonListener(this)); |
| 43 |
// this is required so that the rollover state is always updated correctly. |
| 44 |
// remove it and the button will not be notified of a rollover-event in most cases. |
| 45 |
DefaultButtonModel model = new DefaultButtonModel() { |
| 46 |
private static final long serialVersionUID = -5866286842846125926L; |
| 47 |
|
| 48 |
@Override |
| 49 |
public void setArmed(boolean armed) { |
| 50 |
super.setArmed(isPressed() || armed); |
| 51 |
} |
| 52 |
}; |
| 53 |
setModel(model); |
| 54 |
customizeButton(); |
| 55 |
} |
| 56 |
|
| 57 |
public BasicJideComboBoxButton( |
| 58 |
JComboBox cb, Icon i, |
| 59 |
CellRendererPane pane, JList list) { |
| 60 |
this(); |
| 61 |
_comboBox = cb; |
| 62 |
_comboIcon = i; |
| 63 |
_rendererPane = pane; |
| 64 |
_listBox = list; |
| 65 |
setEnabled(_comboBox.isEnabled()); |
| 66 |
} |
| 67 |
|
| 68 |
protected void customizeButton() { |
| 69 |
setFocusable(false); |
| 70 |
setBorderPainted(false); |
| 71 |
setRequestFocusEnabled(false); |
| 72 |
} |
| 73 |
|
| 74 |
public BasicJideComboBoxButton(JComboBox comboBox, Icon icon, boolean editable, |
| 75 |
CellRendererPane currentValuePane, JList listBox) { |
| 76 |
this(comboBox, icon, currentValuePane, listBox); |
| 77 |
_iconOnly = editable; |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public boolean isFocusTraversable() { |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
protected void paintComponent(Graphics g) { |
| 87 |
Color old = g.getColor(); |
| 88 |
Object painterObject = UIDefaultsLookup.get("Theme.painter"); |
| 89 |
if (!(painterObject instanceof ThemePainter)) { |
| 90 |
return; |
| 91 |
} |
| 92 |
ThemePainter painter = (ThemePainter) painterObject; |
| 93 |
if (getModel().isSelected() || getModel().isPressed() || _comboBox.isPopupVisible()) { |
| 94 |
// paint pressed style |
| 95 |
painter.paintButtonBackground(this, g, new Rectangle(0, 0, getWidth(), getHeight()), 0, ThemePainter.STATE_PRESSED, false); |
| 96 |
} |
| 97 |
else if (getModel().isRollover() || ((BasicJideComboBoxUI) _comboBox.getUI()).isRollOver()) { |
| 98 |
// paint rollover |
| 99 |
painter.paintButtonBackground(this, g, new Rectangle(0, 0, getWidth(), getHeight()), 0, ThemePainter.STATE_ROLLOVER, false); |
| 100 |
} |
| 101 |
else { |
| 102 |
// paint default |
| 103 |
painter.paintButtonBackground(this, g, new Rectangle(0, 0, getWidth(), getHeight()), 0, ThemePainter.STATE_DEFAULT, false); |
| 104 |
} |
| 105 |
if (((BasicJideComboBoxUI) _comboBox.getUI()).isRollOver() || _comboBox.isPopupVisible()) { |
| 106 |
// draw left border |
| 107 |
g.setColor(painter.getMenuItemBorderColor()); |
| 108 |
g.drawLine(0, 0, 0, getHeight()); |
| 109 |
} |
| 110 |
|
| 111 |
paintIcon(g); |
| 112 |
g.setColor(old); |
| 113 |
} |
| 114 |
|
| 115 |
protected void paintIcon(Graphics g) { |
| 116 |
Insets insets = getInsets(); |
| 117 |
|
| 118 |
int width = getWidth() - (insets.left + insets.right); |
| 119 |
int height = getHeight() - (insets.top + insets.bottom); |
| 120 |
|
| 121 |
if (height <= 0 || width <= 0) { |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
int left = insets.left; |
| 126 |
int top = insets.top; |
| 127 |
int bottom = top + (height - 1); |
| 128 |
|
| 129 |
int iconWidth; |
| 130 |
int iconLeft; |
| 131 |
|
| 132 |
// Paint the icon |
| 133 |
if (_comboIcon != null) { |
| 134 |
iconWidth = _comboIcon.getIconWidth(); |
| 135 |
int iconHeight = _comboIcon.getIconHeight(); |
| 136 |
int iconTop; |
| 137 |
|
| 138 |
if (_iconOnly) { |
| 139 |
iconLeft = (getWidth() / 2) - (iconWidth / 2); |
| 140 |
iconTop = (getHeight() / 2) - (iconHeight / 2) - 1; |
| 141 |
} |
| 142 |
else { |
| 143 |
iconLeft = left; |
| 144 |
iconTop = (top + ((bottom - top) / 2)) - (iconHeight / 2); |
| 145 |
} |
| 146 |
_comboIcon.paintIcon(this, g, iconLeft, iconTop); |
| 147 |
} |
| 148 |
} |
| 149 |
} |