1 19 24 package org.openide.explorer.propertysheet; 25 26 import org.openide.util.Utilities; 27 28 import java.awt.*; 29 import java.awt.event.*; 30 31 import javax.swing.*; 32 import javax.swing.plaf.basic.*; 33 import javax.swing.plaf.basic.BasicComboBoxUI.ComboBoxLayoutManager; 34 import javax.swing.plaf.metal.MetalComboBoxIcon ; 35 36 37 43 class CleanComboUI extends BasicComboBoxUI { 44 private JButton button = null; 45 private boolean tableUI; 46 private ComboPopup popup = null; 47 48 public CleanComboUI(boolean tableUI) { 49 this.tableUI = tableUI; 50 } 51 52 protected void installDefaults() { 53 LookAndFeel.installColorsAndFont(comboBox, "ComboBox.background", "ComboBox.foreground", "ComboBox.font"); 55 if (tableUI) { 56 comboBox.setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 0)); 57 } else { 58 comboBox.setBorder( 59 BorderFactory.createCompoundBorder( 60 BorderFactory.createLineBorder(PropUtils.getShadowColor()), 61 62 BorderFactory.createEmptyBorder(0, 2, 0, 0) 65 ) 66 ); 67 } 68 69 installComboDefaults(comboBox); 70 } 71 72 protected ComboPopup createPopup() { 73 popup = new CleanComboPopup(comboBox); 74 75 return popup; 76 } 77 78 protected void installKeyboardActions() { 79 super.installKeyboardActions(); 80 81 if (!tableUI) { 84 comboBox.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "showPopup"); comboBox.getActionMap().put( 86 "showPopup", 87 new AbstractAction() { 88 public void actionPerformed(ActionEvent ae) { 89 if (!comboBox.isPopupVisible()) { 90 comboBox.showPopup(); 91 } 92 } 93 } 94 ); } 96 } 97 98 protected JButton createArrowButton() { 99 Icon i = UIManager.getIcon("ComboBox.icon"); 101 if (i == null) { 102 i = new MetalComboBoxIcon (); 103 } 104 105 button = new JButton(i); 106 button.setFocusable(false); 107 button.setContentAreaFilled(false); 108 button.setBorderPainted(false); 109 button.setBorder(null); 110 111 return button; 112 } 113 114 protected Insets getInsets() { 115 java.awt.Insets i = super.getInsets(); 116 i.right += 2; 117 118 return i; 119 } 120 121 public void paint(Graphics g, JComponent c) { 122 super.paint(g, c); 123 124 if (c.hasFocus() && !tableUI) { 125 Color prev = g.getColor(); 126 127 try { 128 g.setColor(PropUtils.getShadowColor()); 129 g.drawRect(2, 2, c.getWidth() - 5, c.getHeight() - 5); 130 } finally { 131 g.setColor(prev); 132 } 133 } 134 } 135 136 145 protected FocusListener createFocusListener() { 146 return super.createFocusListener(); 147 148 224 } 225 226 public void paintCurrentValue(Graphics g, Rectangle bounds, boolean hasFocus) { 227 ListCellRenderer renderer = comboBox.getRenderer(); 228 229 if ((listBox == null) || (renderer == null)) { 236 return; 237 } 238 239 Component c; 240 c = renderer.getListCellRendererComponent(listBox, comboBox.getSelectedItem(), -1, false, false); 241 c.setFont(comboBox.getFont()); 242 c.setForeground(comboBox.isEnabled() ? comboBox.getForeground() : PropUtils.getDisabledForeground()); 243 244 c.setBackground(comboBox.getBackground()); 245 246 boolean shouldValidate = false; 247 248 if (c instanceof JPanel) { 249 shouldValidate = true; 250 } 251 252 currentValuePane.paintComponent( 253 g, c, comboBox, bounds.x, bounds.y, bounds.width, bounds.height, shouldValidate 254 ); 255 } 256 257 protected Rectangle rectangleForCurrentValue() { 258 Rectangle r = super.rectangleForCurrentValue(); 259 260 if (editor != null) { 261 r.x += 1; 262 r.y += 1; 263 r.width -= 1; 264 r.height -= 1; 265 } 266 267 return r; 268 } 269 270 protected ComboBoxEditor createEditor() { 271 return new CleanComboBoxEditor(); 272 } 273 274 private static final void installComboDefaults(JComponent jc) { 275 Color c = UIManager.getColor("ComboBox.background"); 277 if (c == null) { 278 c = UIManager.getColor("text"); } 280 281 if (c != null) { 282 jc.setBackground(c); 283 } 284 285 c = UIManager.getColor("ComboBox.foreground"); 287 if (c == null) { 288 c = UIManager.getColor("textText"); } 290 291 if (c != null) { 292 jc.setForeground(c); 293 } 294 295 Font f = UIManager.getFont("ComboBox.font"); 297 if (f != null) { 298 jc.setFont(f); 299 } 300 } 301 302 private class CleanComboLayout extends ComboBoxLayoutManager { 303 public void layoutContainer(Container parent) { 304 super.layoutContainer(parent); 305 306 if (editor != null) { 307 java.awt.Rectangle r = rectangleForCurrentValue(); 308 r.x = 0; 309 r.y = 0; 310 r.height = comboBox.getHeight(); 311 editor.setBounds(r); 312 } 313 } 314 } 315 316 private static class CleanComboPopup extends BasicComboPopup { 317 public CleanComboPopup(JComboBox box) { 318 super(box); 319 installComboDefaults(this); 320 } 321 322 protected Rectangle computePopupBounds(int px, int py, int pw, int ph) { 323 Dimension d = list.getPreferredSize(); 324 Rectangle r = Utilities.getUsableScreenBounds(); 325 326 if (pw < d.width) { 327 pw = Math.min(d.width, r.width - px); 328 } 329 330 if (ph < d.height) { 331 ph = Math.min(r.height - py, d.height); 332 } 333 334 if ((px + pw) > (r.width - px)) { 335 px -= (r.width - pw); 336 } 337 338 Rectangle result = new Rectangle(px, py, pw, ph); 339 340 return result; 341 } 342 } 343 344 static class CleanComboBoxEditor extends BasicComboBoxEditor { 345 public CleanComboBoxEditor() { 346 editor = new JTextField(); 347 348 Color c = UIManager.getColor("Table.selectionBackground"); 350 if (c == null) { 351 c = Color.BLACK; 352 } 353 354 editor.setBorder(BorderFactory.createLineBorder(c)); 355 356 } 358 } 359 } 360 | Popular Tags |