| 1 |
package com.jidesoft.plaf.basic; |
| 2 |
|
| 3 |
import com.jidesoft.plaf.UIDefaultsLookup; |
| 4 |
import com.jidesoft.swing.JideSwingUtilities; |
| 5 |
|
| 6 |
import javax.swing.*; |
| 7 |
import javax.swing.border.AbstractBorder; |
| 8 |
import javax.swing.event.PopupMenuEvent; |
| 9 |
import javax.swing.event.PopupMenuListener; |
| 10 |
import javax.swing.plaf.UIResource; |
| 11 |
import javax.swing.plaf.metal.MetalComboBoxUI; |
| 12 |
import java.awt.*; |
| 13 |
import java.awt.event.FocusEvent; |
| 14 |
import java.awt.event.FocusListener; |
| 15 |
import java.awt.event.MouseAdapter; |
| 16 |
import java.awt.event.MouseEvent; |
| 17 |
|
| 18 |
public class BasicJideComboBoxUI extends MetalComboBoxUI { |
| 19 |
|
| 20 |
@SuppressWarnings({"UnusedDeclaration"}) |
| 21 |
public static BasicJideComboBoxUI createUI(JComponent c) { |
| 22 |
return new BasicJideComboBoxUI(); |
| 23 |
} |
| 24 |
|
| 25 |
@Override |
| 26 |
protected void installDefaults() { |
| 27 |
super.installDefaults(); |
| 28 |
JideSwingUtilities.installBorder(comboBox, createComboBoxBorder()); |
| 29 |
} |
| 30 |
|
| 31 |
protected BasicJideComboBoxBorder createComboBoxBorder() { |
| 32 |
return new BasicJideComboBoxBorder(); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
@Override |
| 37 |
protected void uninstallDefaults() { |
| 38 |
super.uninstallDefaults(); |
| 39 |
LookAndFeel.uninstallBorder(comboBox); |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
protected void installListeners() { |
| 44 |
super.installListeners(); |
| 45 |
if (_rolloverListener == null) { |
| 46 |
_rolloverListener = createRolloverListener(); |
| 47 |
} |
| 48 |
comboBox.addMouseListener(_rolloverListener); |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
protected void uninstallListeners() { |
| 53 |
super.uninstallListeners(); |
| 54 |
comboBox.removeMouseListener(_rolloverListener); |
| 55 |
_rolloverListener = null; |
| 56 |
} |
| 57 |
|
| 58 |
protected RolloverListener createRolloverListener() { |
| 59 |
return new RolloverListener(); |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
protected JButton createArrowButton() { |
| 64 |
JButton button = new BasicJideComboBoxButton( |
| 65 |
comboBox, new BasicJideComboBoxIcon(), |
| 66 |
comboBox.isEditable(), |
| 67 |
currentValuePane, listBox); |
| 68 |
button.setMargin(new Insets(1, 3, 0, 4)); |
| 69 |
button.setFocusPainted(comboBox.isEditable()); |
| 70 |
button.addMouseListener(_rolloverListener); |
| 71 |
return button; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public void unconfigureArrowButton() { |
| 76 |
super.unconfigureArrowButton(); |
| 77 |
arrowButton.removeMouseListener(_rolloverListener); |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public void unconfigureEditor() { |
| 82 |
super.unconfigureEditor(); |
| 83 |
editor.removeMouseListener(_rolloverListener); |
| 84 |
editor.removeFocusListener(_rolloverListener); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public void configureEditor() { |
| 89 |
if (editor instanceof JComponent) { |
| 90 |
((JComponent) editor).setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 0)); |
| 91 |
} |
| 92 |
editor.addMouseListener(_rolloverListener); |
| 93 |
editor.addFocusListener(_rolloverListener); |
| 94 |
} |
| 95 |
|
| 96 |
// This is here because of a bug in the compiler. |
| 97 |
// When a protected-inner-class-savvy compiler comes out we |
| 98 |
// should move this into MetalComboBoxLayoutManager. |
| 99 |
@Override |
| 100 |
public void layoutComboBox(Container parent, MetalComboBoxLayoutManager manager) { |
| 101 |
if (arrowButton != null) { |
| 102 |
if (arrowButton instanceof BasicJideComboBoxButton) { |
| 103 |
Icon icon = ((BasicJideComboBoxButton) arrowButton).getComboIcon(); |
| 104 |
Insets buttonInsets = arrowButton.getInsets(); |
| 105 |
Insets insets = comboBox.getInsets(); |
| 106 |
int buttonWidth = icon.getIconWidth() + buttonInsets.left + |
| 107 |
buttonInsets.right; |
| 108 |
arrowButton.setBounds( |
| 109 |
comboBox.getComponentOrientation().isLeftToRight() |
| 110 |
? (comboBox.getWidth() - insets.right - buttonWidth) |
| 111 |
: insets.left, |
| 112 |
insets.top, buttonWidth, |
| 113 |
comboBox.getHeight() - insets.top - insets.bottom); |
| 114 |
} |
| 115 |
else { |
| 116 |
Insets insets = comboBox.getInsets(); |
| 117 |
int width = comboBox.getWidth(); |
| 118 |
int height = comboBox.getHeight(); |
| 119 |
arrowButton.setBounds( |
| 120 |
insets.left, insets.top, |
| 121 |
width - (insets.left + insets.right), |
| 122 |
height - (insets.top + insets.bottom)); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
if (editor != null) { |
| 127 |
Rectangle cvb = rectangleForCurrentValue(); |
| 128 |
editor.setBounds(cvb); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
public boolean isRollOver() { |
| 133 |
return _rollOver || (editor != null ? editor.hasFocus() : hasFocus); |
| 134 |
} |
| 135 |
|
| 136 |
public void setRollOver(boolean rollOver) { |
| 137 |
_rollOver = rollOver; |
| 138 |
} |
| 139 |
|
| 140 |
protected class BasicJideComboBoxBorder extends AbstractBorder implements UIResource { |
| 141 |
private static final long serialVersionUID = 4633179647696691207L; |
| 142 |
|
| 143 |
@Override |
| 144 |
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { |
| 145 |
Color old = g.getColor(); |
| 146 |
JComboBox box = (JComboBox) c; |
| 147 |
if (box.isPopupVisible() || isRollOver()) { |
| 148 |
g.setColor(UIDefaultsLookup.getColor("JideButton.borderColor")); |
| 149 |
} |
| 150 |
else { |
| 151 |
g.setColor(UIDefaultsLookup.getColor("TextField.background")); |
| 152 |
} |
| 153 |
// draw default border |
| 154 |
g.drawRect(x, y, width - 1, height - 1); |
| 155 |
|
| 156 |
g.setColor(old); |
| 157 |
} |
| 158 |
|
| 159 |
@Override |
| 160 |
public Insets getBorderInsets(Component c) { |
| 161 |
return new Insets(1, 1, 1, 1); |
| 162 |
} |
| 163 |
|
| 164 |
@Override |
| 165 |
public Insets getBorderInsets(Component c, Insets insets) { |
| 166 |
insets.left = insets.right = insets.top = insets.bottom = 1; |
| 167 |
return insets; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
private boolean _rollOver = false; |
| 172 |
protected RolloverListener _rolloverListener; |
| 173 |
|
| 174 |
protected class RolloverListener extends MouseAdapter implements FocusListener { |
| 175 |
@Override |
| 176 |
public void mouseEntered(MouseEvent e) { |
| 177 |
setRollOver(true); |
| 178 |
comboBox.repaint(); |
| 179 |
} |
| 180 |
|
| 181 |
@Override |
| 182 |
public void mouseExited(MouseEvent e) { |
| 183 |
if (comboBox.isPopupVisible()) { |
| 184 |
// this might be the ugliest hack I've ever written |
| 185 |
// so here is an explanation: |
| 186 |
// when a popup is visible, we need to show the white background until the |
| 187 |
// popup is closed if, however, the mouse re-enters, we do not want to reset the background color |
| 188 |
// In order to get around these problems we'll change the background to white, but a different instance |
| 189 |
// than HOVER_COLOR and perform an identity check when the popup closes. |
| 190 |
// If the background color still is not HOVER_COLOR (which it'd be if the mouse had re-entered meanwhile) |
| 191 |
// then we can override it with DEFAULT_COLOR. |
| 192 |
final PopupMenuListener l = new PopupMenuListener() { |
| 193 |
public void popupMenuWillBecomeVisible(PopupMenuEvent e) { |
| 194 |
// it already is visible |
| 195 |
} |
| 196 |
|
| 197 |
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { |
| 198 |
setRollOver(false); |
| 199 |
((JPopupMenu) popup).removePopupMenuListener(this); |
| 200 |
} |
| 201 |
|
| 202 |
public void popupMenuCanceled(PopupMenuEvent e) { |
| 203 |
} |
| 204 |
}; |
| 205 |
((JPopupMenu) popup).addPopupMenuListener(l); |
| 206 |
setRollOver(true); |
| 207 |
} |
| 208 |
else { |
| 209 |
setRollOver(false); |
| 210 |
} |
| 211 |
comboBox.repaint(); |
| 212 |
} |
| 213 |
|
| 214 |
public void focusGained(FocusEvent e) { |
| 215 |
comboBox.repaint(); |
| 216 |
} |
| 217 |
|
| 218 |
public void focusLost(FocusEvent e) { |
| 219 |
comboBox.repaint(); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
protected class BasicJideComboBoxIcon implements Icon { |
| 224 |
public void paintIcon(Component c, Graphics g, int x, int y) { |
| 225 |
JideSwingUtilities.paintArrow(g, c.getForeground(), x, y, 5, SwingConstants.HORIZONTAL); |
| 226 |
} |
| 227 |
|
| 228 |
public int getIconWidth() { |
| 229 |
return 5; |
| 230 |
} |
| 231 |
|
| 232 |
public int getIconHeight() { |
| 233 |
return 3; |
| 234 |
} |
| 235 |
} |
| 236 |
} |