1 7 8 package javax.swing.plaf.metal; 9 10 import java.awt.*; 11 import java.awt.event.*; 12 import javax.swing.*; 13 import javax.swing.plaf.*; 14 import javax.swing.border.*; 15 import javax.swing.plaf.basic.*; 16 import java.io.Serializable ; 17 import java.beans.*; 18 19 20 37 public class MetalComboBoxUI extends BasicComboBoxUI { 38 39 public static ComponentUI createUI(JComponent c) { 40 return new MetalComboBoxUI (); 41 } 42 43 public void paint(Graphics g, JComponent c) { 44 if (MetalLookAndFeel.usingOcean()) { 45 super.paint(g, c); 46 } 47 } 48 49 58 public void paintCurrentValue(Graphics g, Rectangle bounds, 59 boolean hasFocus) { 60 if (MetalLookAndFeel.usingOcean()) { 62 bounds.x += 2; 63 bounds.y += 2; 64 bounds.width -= 3; 65 bounds.height -= 4; 66 super.paintCurrentValue(g, bounds, hasFocus); 67 } 68 else if (g == null || bounds == null) { 69 throw new NullPointerException ( 70 "Must supply a non-null Graphics and Rectangle"); 71 } 72 } 73 74 83 public void paintCurrentValueBackground(Graphics g, Rectangle bounds, 84 boolean hasFocus) { 85 if (MetalLookAndFeel.usingOcean()) { 87 g.setColor(MetalLookAndFeel.getControlDarkShadow()); 88 g.drawRect(bounds.x, bounds.y, bounds.width, bounds.height - 1); 89 g.setColor(MetalLookAndFeel.getControlShadow()); 90 g.drawRect(bounds.x + 1, bounds.y + 1, bounds.width - 2, 91 bounds.height - 3); 92 } 93 else if (g == null || bounds == null) { 94 throw new NullPointerException ( 95 "Must supply a non-null Graphics and Rectangle"); 96 } 97 } 98 99 protected ComboBoxEditor createEditor() { 100 return new MetalComboBoxEditor.UIResource (); 101 } 102 103 protected ComboPopup createPopup() { 104 return super.createPopup(); 105 } 106 107 protected JButton createArrowButton() { 108 boolean iconOnly = (comboBox.isEditable() || 109 MetalLookAndFeel.usingOcean()); 110 JButton button = new MetalComboBoxButton ( comboBox, 111 new MetalComboBoxIcon (), 112 iconOnly, 113 currentValuePane, 114 listBox ); 115 button.setMargin( new Insets( 0, 1, 1, 3 ) ); 116 if (MetalLookAndFeel.usingOcean()) { 117 button.putClientProperty(MetalBorders.NO_BUTTON_ROLLOVER, 119 Boolean.TRUE); 120 } 121 updateButtonForOcean(button); 122 return button; 123 } 124 125 128 private void updateButtonForOcean(JButton button) { 129 if (MetalLookAndFeel.usingOcean()) { 130 button.setFocusPainted(comboBox.isEditable()); 133 } 134 } 135 136 public PropertyChangeListener createPropertyChangeListener() { 137 return new MetalPropertyChangeListener(); 138 } 139 140 145 public class MetalPropertyChangeListener extends BasicComboBoxUI.PropertyChangeHandler { 146 public void propertyChange(PropertyChangeEvent e) { 147 super.propertyChange( e ); 148 String propertyName = e.getPropertyName(); 149 150 if ( propertyName == "editable" ) { 151 MetalComboBoxButton button = (MetalComboBoxButton )arrowButton; 152 button.setIconOnly( comboBox.isEditable() || 153 MetalLookAndFeel.usingOcean() ); 154 comboBox.repaint(); 155 updateButtonForOcean(button); 156 } else if ( propertyName == "background" ) { 157 Color color = (Color)e.getNewValue(); 158 arrowButton.setBackground(color); 159 listBox.setBackground(color); 160 161 } else if ( propertyName == "foreground" ) { 162 Color color = (Color)e.getNewValue(); 163 arrowButton.setForeground(color); 164 listBox.setForeground(color); 165 } 166 } 167 } 168 169 176 @Deprecated 177 protected void editablePropertyChanged( PropertyChangeEvent e ) { } 178 179 protected LayoutManager createLayoutManager() { 180 return new MetalComboBoxLayoutManager(); 181 } 182 183 188 public class MetalComboBoxLayoutManager extends BasicComboBoxUI.ComboBoxLayoutManager { 189 public void layoutContainer( Container parent ) { 190 layoutComboBox( parent, this ); 191 } 192 public void superLayout( Container parent ) { 193 super.layoutContainer( parent ); 194 } 195 } 196 197 public void layoutComboBox( Container parent, MetalComboBoxLayoutManager manager ) { 201 if (comboBox.isEditable() && !MetalLookAndFeel.usingOcean()) { 202 manager.superLayout( parent ); 203 return; 204 } 205 206 if (arrowButton != null) { 207 if (MetalLookAndFeel.usingOcean() && 208 (arrowButton instanceof MetalComboBoxButton )) { 209 Icon icon = ((MetalComboBoxButton )arrowButton).getComboIcon(); 210 Insets buttonInsets = arrowButton.getInsets(); 211 Insets insets = comboBox.getInsets(); 212 int buttonWidth = icon.getIconWidth() + buttonInsets.left + 213 buttonInsets.right; 214 arrowButton.setBounds(MetalUtils.isLeftToRight(comboBox) 215 ? (comboBox.getWidth() - insets.right - buttonWidth) 216 : insets.left, 217 insets.top, buttonWidth, 218 comboBox.getHeight() - insets.top - insets.bottom); 219 } 220 else { 221 Insets insets = comboBox.getInsets(); 222 int width = comboBox.getWidth(); 223 int height = comboBox.getHeight(); 224 arrowButton.setBounds( insets.left, insets.top, 225 width - (insets.left + insets.right), 226 height - (insets.top + insets.bottom) ); 227 } 228 } 229 230 if (editor != null && MetalLookAndFeel.usingOcean()) { 231 Rectangle cvb = rectangleForCurrentValue(); 232 editor.setBounds(cvb); 233 } 234 } 235 236 242 @Deprecated 243 protected void removeListeners() { 244 if ( propertyChangeListener != null ) { 245 comboBox.removePropertyChangeListener( propertyChangeListener ); 246 } 247 } 248 249 254 public void configureEditor() { 255 super.configureEditor(); 256 } 257 258 public void unconfigureEditor() { 259 super.unconfigureEditor(); 260 } 261 262 public Dimension getMinimumSize( JComponent c ) { 263 if ( !isMinimumSizeDirty ) { 264 return new Dimension( cachedMinimumSize ); 265 } 266 267 Dimension size = null; 268 269 if ( !comboBox.isEditable() && 270 arrowButton != null && 271 arrowButton instanceof MetalComboBoxButton ) { 272 273 MetalComboBoxButton button = (MetalComboBoxButton )arrowButton; 274 Insets buttonInsets = button.getInsets(); 275 Insets insets = comboBox.getInsets(); 276 277 size = getDisplaySize(); 278 size.width += insets.left + insets.right; 279 size.width += buttonInsets.left + buttonInsets.right; 280 size.width += buttonInsets.right + button.getComboIcon().getIconWidth(); 281 size.height += insets.top + insets.bottom; 282 size.height += buttonInsets.top + buttonInsets.bottom; 283 } 284 else if ( comboBox.isEditable() && 285 arrowButton != null && 286 editor != null ) { 287 size = super.getMinimumSize( c ); 288 Insets margin = arrowButton.getMargin(); 289 size.height += margin.top + margin.bottom; 290 size.width += margin.left + margin.right; 291 } 292 else { 293 size = super.getMinimumSize( c ); 294 } 295 296 cachedMinimumSize.setSize( size.width, size.height ); 297 isMinimumSizeDirty = false; 298 299 return new Dimension( cachedMinimumSize ); 300 } 301 302 313 @Deprecated 314 public class MetalComboPopup extends BasicComboPopup { 315 316 public MetalComboPopup( JComboBox cBox) { 317 super( cBox ); 318 } 319 320 325 public void delegateFocus(MouseEvent e) { 326 super.delegateFocus(e); 327 } 328 } 329 } 330 331 332 | Popular Tags |