1 30 31 package com.jgoodies.looks.plastic; 32 33 import java.awt.*; 34 import java.beans.PropertyChangeEvent ; 35 import java.beans.PropertyChangeListener ; 36 37 import javax.swing.*; 38 import javax.swing.plaf.ComponentUI ; 39 import javax.swing.plaf.basic.BasicComboBoxUI ; 40 import javax.swing.plaf.basic.ComboPopup ; 41 import javax.swing.plaf.metal.MetalComboBoxUI ; 42 import javax.swing.plaf.metal.MetalScrollBarUI ; 43 44 45 52 53 public final class PlasticComboBoxUI extends MetalComboBoxUI { 54 55 public static ComponentUI createUI(JComponent b) { 56 return new PlasticComboBoxUI(); 57 } 58 59 64 protected ComboBoxEditor createEditor() { 65 return new PlasticComboBoxEditor.UIResource(); 66 } 67 68 69 protected ComboPopup createPopup() { 70 return new PlasticComboPopup(comboBox); 71 } 72 73 74 77 private Insets getEditorInsets() { 78 if (editor instanceof JComponent) { 79 return ((JComponent)editor).getInsets(); 80 } 81 return new Insets(0, 0, 0, 0); 82 } 83 84 92 private int getEditableButtonWidth() { 93 return UIManager.getInt("ScrollBar.width") - 1; 94 } 95 96 99 public Dimension getMinimumSize(JComponent c) { 100 if (!isMinimumSizeDirty) { 101 return new Dimension(cachedMinimumSize); 102 } 103 104 Dimension size = null; 105 106 if (!comboBox.isEditable() 107 && arrowButton != null 108 && arrowButton instanceof PlasticComboBoxButton) { 109 110 PlasticComboBoxButton button = 111 (PlasticComboBoxButton) arrowButton; 112 Insets buttonInsets = button.getInsets(); 113 Insets buttonMargin = button.getMargin(); 114 Insets insets = comboBox.getInsets(); 115 size = getDisplaySize(); 116 117 120 125 size.height += 2; 126 size.width += insets.left + insets.right; 127 size.width += buttonInsets.left + buttonInsets.right; 128 size.width += buttonMargin.left + buttonMargin.right; 129 size.width += button.getComboIcon().getIconWidth(); 130 size.height += insets.top + insets.bottom; 131 size.height += buttonInsets.top + buttonInsets.bottom; 132 } else if ( 133 comboBox.isEditable() && arrowButton != null && editor != null) { 134 135 size = getDisplaySize(); 137 Insets insets = comboBox.getInsets(); 138 Insets editorInsets = getEditorInsets(); 139 int buttonWidth = getEditableButtonWidth(); 140 141 size.width += insets.left + insets.right; 142 size.width += editorInsets.left + editorInsets.right -1; 143 size.width += buttonWidth; 144 size.height += insets.top + insets.bottom; 145 } else { 146 size = super.getMinimumSize(c); 147 } 148 149 cachedMinimumSize.setSize(size.width, size.height); 150 isMinimumSizeDirty = false; 151 152 return new Dimension(cachedMinimumSize); 153 } 154 155 160 protected JButton createArrowButton() { 161 return new PlasticComboBoxButton( 162 comboBox, 163 PlasticIconFactory.getComboBoxButtonIcon(), 164 comboBox.isEditable(), 165 currentValuePane, 166 listBox); 167 } 168 169 177 protected LayoutManager createLayoutManager() { 178 return new PlasticComboBoxLayoutManager(); 179 } 180 181 183 public void update(Graphics g, JComponent c) { 184 if (c.isOpaque()) { 185 g.setColor(c.getBackground()); 186 g.fillRect(0, 0, c.getWidth(), c.getHeight()); 187 if (isToolBarComboBox(c)) { 188 c.setOpaque(false); 189 } } 190 paint(g, c); 191 } 192 193 194 200 protected boolean isToolBarComboBox(JComponent c) { 201 Container parent = c.getParent(); 202 return parent != null 203 && (parent instanceof JToolBar 204 || parent.getParent() instanceof JToolBar); 205 } 206 207 208 210 217 private class PlasticComboBoxLayoutManager 218 extends MetalComboBoxUI.MetalComboBoxLayoutManager { 219 220 public void layoutContainer(Container parent) { 221 JComboBox cb = (JComboBox) parent; 222 223 if (!cb.isEditable()) { 225 super.layoutContainer(parent); 226 return; 227 } 228 229 int width = cb.getWidth(); 230 int height = cb.getHeight(); 231 232 Insets insets = getInsets(); 233 int buttonWidth = getEditableButtonWidth(); 234 int buttonHeight = height - (insets.top + insets.bottom); 235 236 if (arrowButton != null) { 237 if (cb.getComponentOrientation().isLeftToRight()) { 238 arrowButton.setBounds( 239 width - (insets.right + buttonWidth), 240 insets.top, 241 buttonWidth, 242 buttonHeight); 243 } else { 244 arrowButton.setBounds( 245 insets.left, 246 insets.top, 247 buttonWidth, 248 buttonHeight); 249 } 250 } 251 if (editor != null) { 252 editor.setBounds(rectangleForCurrentValue()); 253 } 254 } 255 } 256 257 public PropertyChangeListener createPropertyChangeListener() { 259 return new PlasticPropertyChangeListener(); 260 } 261 262 private class PlasticPropertyChangeListener 265 extends BasicComboBoxUI.PropertyChangeHandler { 266 267 public void propertyChange(PropertyChangeEvent e) { 268 super.propertyChange(e); 269 String propertyName = e.getPropertyName(); 270 271 if (propertyName.equals("editable")) { 272 PlasticComboBoxButton button = 273 (PlasticComboBoxButton) arrowButton; 274 button.setIconOnly(comboBox.isEditable()); 275 comboBox.repaint(); 276 } else if (propertyName.equals("background")) { 277 Color color = (Color) e.getNewValue(); 278 arrowButton.setBackground(color); 279 listBox.setBackground(color); 280 281 } else if (propertyName.equals("foreground")) { 282 Color color = (Color) e.getNewValue(); 283 arrowButton.setForeground(color); 284 listBox.setForeground(color); 285 } 286 } 287 } 288 289 private class PlasticComboPopup extends MetalComboPopup { 291 292 private PlasticComboPopup(JComboBox combo) { 293 super(combo); 294 } 295 296 299 protected void configureList() { 300 super.configureList(); 301 list.setForeground(UIManager.getColor("MenuItem.foreground")); 302 list.setBackground(UIManager.getColor("MenuItem.background")); 303 } 304 305 308 protected void configureScroller() { 309 super.configureScroller(); 310 scroller.getVerticalScrollBar().putClientProperty( 311 MetalScrollBarUI.FREE_STANDING_PROP, 312 Boolean.FALSE); 313 } 314 315 } 316 317 } | Popular Tags |