1 7 package javax.swing.plaf.synth; 8 9 import java.awt.*; 10 import java.awt.event.*; 11 import java.text.ParseException ; 12 13 import javax.swing.*; 14 import javax.swing.event.*; 15 import javax.swing.plaf.*; 16 import javax.swing.plaf.basic.BasicSpinnerUI ; 17 import javax.swing.text.*; 18 19 import java.beans.*; 20 import java.text.*; 21 import java.util.*; 22 import sun.swing.plaf.synth.SynthUI; 23 24 31 class SynthSpinnerUI extends BasicSpinnerUI implements PropertyChangeListener, 32 SynthUI { 33 private SynthStyle style; 34 35 36 43 public static ComponentUI createUI(JComponent c) { 44 return new SynthSpinnerUI (); 45 } 46 47 protected void installListeners() { 48 spinner.addPropertyChangeListener(this); 49 } 50 51 59 protected void uninstallListeners() { 60 spinner.removePropertyChangeListener(this); 61 } 62 63 76 protected void installDefaults() { 77 LayoutManager layout = spinner.getLayout(); 78 79 if (layout == null || layout instanceof UIResource) { 80 spinner.setLayout(createLayout()); 81 } 82 updateStyle(spinner); 83 } 84 85 86 private void updateStyle(JSpinner c) { 87 SynthContext context = getContext(c, ENABLED); 88 SynthStyle oldStyle = style; 89 style = SynthLookAndFeel.updateStyle(context, this); 90 if (style != oldStyle) { 91 if (oldStyle != null) { 92 installKeyboardActions(); 95 } 96 } 97 context.dispose(); 98 } 99 100 101 108 protected void uninstallDefaults() { 109 if (spinner.getLayout() instanceof UIResource) { 110 spinner.setLayout(null); 111 } 112 113 SynthContext context = getContext(spinner, ENABLED); 114 115 style.uninstallDefaults(context); 116 context.dispose(); 117 style = null; 118 } 119 120 121 protected LayoutManager createLayout() { 122 return new SpinnerLayout(); 123 } 124 125 126 protected PropertyChangeListener createPropertyChangeListener() { 128 return this; 129 } 130 131 132 145 protected Component createPreviousButton() { 146 JButton b = new SynthArrowButton (SwingConstants.SOUTH); 147 b.setName("Spinner.previousButton"); 148 installPreviousButtonListeners(b); 149 return b; 150 } 151 152 153 166 protected Component createNextButton() { 167 JButton b = new SynthArrowButton (SwingConstants.NORTH); 168 b.setName("Spinner.nextButton"); 169 installNextButtonListeners(b); 170 return b; 171 } 172 173 174 197 protected JComponent createEditor() { 198 JComponent editor = spinner.getEditor(); 199 editor.setName("Spinner.editor"); 200 return editor; 201 } 202 203 204 219 protected void replaceEditor(JComponent oldEditor, JComponent newEditor) { 220 spinner.remove(oldEditor); 221 spinner.add(newEditor, "Editor"); 222 } 223 224 225 229 private void updateEnabledState() { 230 updateEnabledState(spinner, spinner.isEnabled()); 231 } 232 233 234 238 private void updateEnabledState(Container c, boolean enabled) { 239 for (int counter = c.getComponentCount() - 1; counter >= 0;counter--) { 240 Component child = c.getComponent(counter); 241 242 child.setEnabled(enabled); 243 if (child instanceof Container) { 244 updateEnabledState((Container)child, enabled); 245 } 246 } 247 } 248 249 250 public SynthContext getContext(JComponent c) { 251 return getContext(c, getComponentState(c)); 252 } 253 254 private SynthContext getContext(JComponent c, int state) { 255 return SynthContext.getContext(SynthContext .class, c, 256 SynthLookAndFeel.getRegion(c), style, state); 257 } 258 259 260 private Region getRegion(JComponent c) { 261 return SynthLookAndFeel.getRegion(c); 262 } 263 264 265 private int getComponentState(JComponent c) { 266 return SynthLookAndFeel.getComponentState(c); 267 } 268 269 270 public void update(Graphics g, JComponent c) { 271 SynthContext context = getContext(c); 272 273 SynthLookAndFeel.update(context, g); 274 context.getPainter().paintSpinnerBackground(context, 275 g, 0, 0, c.getWidth(), c.getHeight()); 276 paint(context, g); 277 context.dispose(); 278 } 279 280 281 public void paint(Graphics g, JComponent c) { 282 SynthContext context = getContext(c); 283 284 paint(context, g); 285 context.dispose(); 286 } 287 288 289 protected void paint(SynthContext context, Graphics g) { 290 } 291 292 public void paintBorder(SynthContext context, Graphics g, int x, 293 int y, int w, int h) { 294 context.getPainter().paintSpinnerBorder(context, g, x, y, w, h); 295 } 296 297 302 private static class SpinnerLayout implements LayoutManager, UIResource 303 { 304 private Component nextButton = null; 305 private Component previousButton = null; 306 private Component editor = null; 307 308 public void addLayoutComponent(String name, Component c) { 309 if ("Next".equals(name)) { 310 nextButton = c; 311 } 312 else if ("Previous".equals(name)) { 313 previousButton = c; 314 } 315 else if ("Editor".equals(name)) { 316 editor = c; 317 } 318 } 319 320 public void removeLayoutComponent(Component c) { 321 if (c == nextButton) { 322 c = null; 323 } 324 else if (c == previousButton) { 325 previousButton = null; 326 } 327 else if (c == editor) { 328 editor = null; 329 } 330 } 331 332 private Dimension preferredSize(Component c) { 333 return (c == null) ? new Dimension(0, 0) : c.getPreferredSize(); 334 } 335 336 public Dimension preferredLayoutSize(Container parent) { 337 Dimension nextD = preferredSize(nextButton); 338 Dimension previousD = preferredSize(previousButton); 339 Dimension editorD = preferredSize(editor); 340 341 343 editorD.height = ((editorD.height + 1) / 2) * 2; 344 345 Dimension size = new Dimension(editorD.width, editorD.height); 346 size.width += Math.max(nextD.width, previousD.width); 347 Insets insets = parent.getInsets(); 348 size.width += insets.left + insets.right; 349 size.height += insets.top + insets.bottom; 350 return size; 351 } 352 353 public Dimension minimumLayoutSize(Container parent) { 354 return preferredLayoutSize(parent); 355 } 356 357 private void setBounds(Component c, int x, int y, int width, int height) { 358 if (c != null) { 359 c.setBounds(x, y, width, height); 360 } 361 } 362 363 public void layoutContainer(Container parent) { 364 Insets insets = parent.getInsets(); 365 int availWidth = parent.getWidth() - (insets.left + insets.right); 366 int availHeight = parent.getHeight() - (insets.top + insets.bottom); 367 Dimension nextD = preferredSize(nextButton); 368 Dimension previousD = preferredSize(previousButton); 369 int nextHeight = availHeight / 2; 370 int previousHeight = availHeight - nextHeight; 371 int buttonsWidth = Math.max(nextD.width, previousD.width); 372 int editorWidth = availWidth - buttonsWidth; 373 374 376 int editorX, buttonsX; 377 if (parent.getComponentOrientation().isLeftToRight()) { 378 editorX = insets.left; 379 buttonsX = editorX + editorWidth; 380 } 381 else { 382 buttonsX = insets.left; 383 editorX = buttonsX + buttonsWidth; 384 } 385 386 int previousY = insets.top + nextHeight; 387 setBounds(editor, editorX, insets.top, editorWidth, availHeight); 388 setBounds(nextButton, buttonsX, insets.top, buttonsWidth, nextHeight); 389 setBounds(previousButton, buttonsX, previousY, buttonsWidth, previousHeight); 390 } 391 } 392 393 394 public void propertyChange(PropertyChangeEvent e) { 395 String propertyName = e.getPropertyName(); 396 JSpinner spinner = (JSpinner)(e.getSource()); 397 SpinnerUI spinnerUI = spinner.getUI(); 398 399 if (spinnerUI instanceof SynthSpinnerUI ) { 400 SynthSpinnerUI ui = (SynthSpinnerUI )spinnerUI; 401 402 if (SynthLookAndFeel.shouldUpdateStyle(e)) { 403 ui.updateStyle(spinner); 404 } 405 if ("editor".equals(propertyName)) { 406 JComponent oldEditor = (JComponent)e.getOldValue(); 407 JComponent newEditor = (JComponent)e.getNewValue(); 408 ui.replaceEditor(oldEditor, newEditor); 409 ui.updateEnabledState(); 410 } 411 else if ("enabled".equals(propertyName)) { 412 ui.updateEnabledState(); 413 } 414 } 415 } 416 } 417 | Popular Tags |