1 7 8 package javax.swing.plaf.synth; 9 10 import java.awt.*; 11 import java.awt.event.*; 12 import java.lang.reflect.*; 13 import javax.swing.*; 14 import javax.accessibility.*; 15 import javax.swing.FocusManager ; 16 import javax.swing.plaf.*; 17 import javax.swing.border.*; 18 import javax.swing.text.*; 19 import javax.swing.event.*; 20 import javax.swing.plaf.basic.*; 21 import java.beans.PropertyChangeListener ; 22 import java.beans.PropertyChangeEvent ; 23 import sun.awt.AppContext; 24 import sun.swing.plaf.synth.SynthUI; 25 26 32 class SynthComboBoxUI extends BasicComboBoxUI implements 33 PropertyChangeListener , SynthUI { 34 private SynthStyle style; 35 private boolean useListColors; 36 37 public static ComponentUI createUI(JComponent c) { 38 return new SynthComboBoxUI (); 39 } 40 41 protected void installDefaults() { 42 updateStyle(comboBox); 43 } 44 45 private void updateStyle(JComboBox comboBox) { 46 SynthStyle oldStyle = style; 47 SynthContext context = getContext(comboBox, ENABLED); 48 49 style = SynthLookAndFeel.updateStyle(context, this); 50 if (style != oldStyle) { 51 useListColors = style.getBoolean(context, 52 "ComboBox.rendererUseListColors", true); 53 if (oldStyle != null) { 54 uninstallKeyboardActions(); 55 installKeyboardActions(); 56 } 57 } 58 context.dispose(); 59 } 60 61 protected void installListeners() { 62 comboBox.addPropertyChangeListener(this); 63 super.installListeners(); 64 } 65 66 protected void uninstallDefaults() { 67 SynthContext context = getContext(comboBox, ENABLED); 68 69 style.uninstallDefaults(context); 70 context.dispose(); 71 style = null; 72 } 73 74 protected void uninstallListeners() { 75 comboBox.removePropertyChangeListener(this); 76 super.uninstallListeners(); 77 } 78 79 public SynthContext getContext(JComponent c) { 80 return getContext(c, getComponentState(c)); 81 } 82 83 private SynthContext getContext(JComponent c, int state) { 84 return SynthContext.getContext(SynthContext .class, c, 85 SynthLookAndFeel.getRegion(c), style, state); 86 } 87 88 private Region getRegion(JComponent c) { 89 return SynthLookAndFeel.getRegion(c); 90 } 91 92 private int getComponentState(JComponent c) { 93 return SynthLookAndFeel.getComponentState(c); 94 } 95 96 protected ComboPopup createPopup() { 97 SynthComboPopup popup = new SynthComboPopup ( comboBox ); 98 return popup; 99 } 100 101 protected ListCellRenderer createRenderer() { 102 return new SynthComboBoxRenderer(); 103 } 104 105 protected ComboBoxEditor createEditor() { 106 return new SynthComboBoxEditor(); 107 } 108 109 113 114 public void propertyChange(PropertyChangeEvent e) { 115 if (SynthLookAndFeel.shouldUpdateStyle(e)) { 116 updateStyle(comboBox); 117 } 118 } 119 120 protected JButton createArrowButton() { 121 SynthArrowButton button = new SynthArrowButton (SwingConstants.SOUTH); 122 button.setName("ComboBox.arrowButton"); 123 return button; 124 } 125 126 129 public void update(Graphics g, JComponent c) { 130 SynthContext context = getContext(c); 131 132 SynthLookAndFeel.update(context, g); 133 context.getPainter().paintComboBoxBackground(context, g, 0, 0, 134 c.getWidth(), c.getHeight()); 135 paint(context, g); 136 context.dispose(); 137 } 138 139 public void paint(Graphics g, JComponent c) { 140 SynthContext context = getContext(c); 141 142 paint(context, g); 143 context.dispose(); 144 } 145 146 protected void paint(SynthContext context, Graphics g) { 147 hasFocus = comboBox.hasFocus(); 148 if ( !comboBox.isEditable() ) { 149 Rectangle r = rectangleForCurrentValue(); 150 paintCurrentValue(g,r,hasFocus); 151 } 152 } 153 154 public void paintBorder(SynthContext context, Graphics g, int x, 155 int y, int w, int h) { 156 context.getPainter().paintComboBoxBorder(context, g, x, y, w, h); 157 } 158 159 160 163 public void paintCurrentValue(Graphics g,Rectangle bounds,boolean hasFocus) { 164 ListCellRenderer renderer = comboBox.getRenderer(); 165 Component c; 166 167 if ( hasFocus && !isPopupVisible(comboBox) ) { 168 c = renderer.getListCellRendererComponent( listBox, 169 comboBox.getSelectedItem(), 170 -1, 171 false, 172 false ); 173 } 174 else { 175 c = renderer.getListCellRendererComponent( listBox, 176 comboBox.getSelectedItem(), 177 -1, 178 false, 179 false ); 180 } 181 boolean shouldValidate = false; 183 if (c instanceof JPanel) { 184 shouldValidate = true; 185 } 186 187 if (c instanceof UIResource) { 188 c.setName("ComboBox.renderer"); 189 currentValuePane.paintComponent(g,c,comboBox,bounds.x,bounds.y, 190 bounds.width,bounds.height, shouldValidate); 191 } 192 else { 193 currentValuePane.paintComponent(g,c,comboBox,bounds.x,bounds.y, 194 bounds.width,bounds.height, shouldValidate); 195 } 196 } 197 198 201 private class SynthComboBoxRenderer extends JLabel implements ListCellRenderer, UIResource { 202 public SynthComboBoxRenderer() { 203 super(); 204 setText(" "); 205 } 206 207 public String getName() { 208 String name = super.getName(); 213 if (name == null) { 214 return "ComboBox.renderer"; 215 } 216 return name; 217 } 218 219 public Component getListCellRendererComponent(JList list, Object value, 220 int index, boolean isSelected, boolean cellHasFocus) { 221 setName("ComboBox.listRenderer"); 222 SynthLookAndFeel.resetSelectedUI(); 223 if (isSelected) { 224 setBackground(list.getSelectionBackground()); 225 setForeground(list.getSelectionForeground()); 226 if (!useListColors) { 227 SynthLookAndFeel.setSelectedUI( 228 (SynthLabelUI )SynthLookAndFeel.getUIOfType(getUI(), 229 SynthLabelUI .class), isSelected, cellHasFocus, 230 list.isEnabled()); 231 } 232 } 233 else { 234 setBackground(list.getBackground()); 235 setForeground(list.getForeground()); 236 } 237 238 setFont(list.getFont()); 239 240 if (value instanceof Icon) { 241 setIcon((Icon)value); 242 setText(""); 243 } 244 else { 245 String text = (value == null) ? " " : value.toString(); 246 247 if ("".equals(text)) { 248 text = " "; 249 } 250 setText(text); 251 } 252 return this; 253 } 254 255 public void paint(Graphics g) { 256 super.paint(g); 257 SynthLookAndFeel.resetSelectedUI(); 258 } 259 } 260 261 262 265 private static class SynthComboBoxEditor implements 266 ComboBoxEditor, UIResource { 267 protected JTextField editor; 268 private Object oldValue; 269 270 public SynthComboBoxEditor() { 271 editor = new JTextField("",9); 272 editor.setName("ComboBox.textField"); 273 } 274 275 public Component getEditorComponent() { 276 return editor; 277 } 278 279 284 public void setItem(Object anObject) { 285 String text; 286 287 if ( anObject != null ) { 288 text = anObject.toString(); 289 oldValue = anObject; 290 } else { 291 text = ""; 292 } 293 if (!text.equals(editor.getText())) { 295 editor.setText(text); 296 } 297 } 298 299 public Object getItem() { 300 Object newValue = editor.getText(); 301 302 if (oldValue != null && !(oldValue instanceof String )) { 303 if (newValue.equals(oldValue.toString())) { 306 return oldValue; 307 } else { 308 Class cls = oldValue.getClass(); 310 try { 311 Method method = cls.getMethod("valueOf", new Class []{String .class}); 312 newValue = method.invoke(oldValue, new Object [] { editor.getText()}); 313 } catch (Exception ex) { 314 } 316 } 317 } 318 return newValue; 319 } 320 321 public void selectAll() { 322 editor.selectAll(); 323 editor.requestFocus(); 324 } 325 326 public void addActionListener(ActionListener l) { 327 editor.addActionListener(l); 328 } 329 330 public void removeActionListener(ActionListener l) { 331 editor.removeActionListener(l); 332 } 333 } 334 } 335 | Popular Tags |