1 30 31 package com.jgoodies.looks.plastic; 32 33 import java.awt.Component ; 34 import java.awt.Insets ; 35 import java.awt.LayoutManager ; 36 37 import javax.swing.JComponent ; 38 import javax.swing.JSpinner ; 39 import javax.swing.SwingConstants ; 40 import javax.swing.UIManager ; 41 import javax.swing.border.Border ; 42 import javax.swing.plaf.BorderUIResource ; 43 import javax.swing.plaf.ComponentUI ; 44 import javax.swing.plaf.basic.BasicBorders ; 45 import javax.swing.plaf.basic.BasicSpinnerUI ; 46 47 import com.jgoodies.looks.common.ExtBasicArrowButtonHandler; 48 import com.jgoodies.looks.common.ExtBasicSpinnerLayout; 49 50 51 59 public class PlasticSpinnerUI extends BasicSpinnerUI { 60 61 private static final Border MARGIN_BORDER = 62 new BorderUIResource (new BasicBorders.MarginBorder ()); 63 64 65 public static ComponentUI createUI(JComponent b) { 66 return new PlasticSpinnerUI(); 67 } 68 69 70 78 private static final ExtBasicArrowButtonHandler nextButtonHandler 79 = new ExtBasicArrowButtonHandler("increment", true); 80 private static final ExtBasicArrowButtonHandler previousButtonHandler 81 = new ExtBasicArrowButtonHandler("decrement", false); 82 83 84 97 protected Component createPreviousButton() { 98 return new SpinnerArrowButton(SwingConstants.SOUTH, previousButtonHandler); 99 } 100 101 102 115 protected Component createNextButton() { 116 return new SpinnerArrowButton(SwingConstants.NORTH, nextButtonHandler); 117 } 118 119 120 133 protected LayoutManager createLayout() { 134 return new ExtBasicSpinnerLayout(); 135 } 136 137 138 161 protected JComponent createEditor() { 162 JComponent editor = spinner.getEditor(); 163 configureEditor(editor); 164 return editor; 165 } 166 167 182 protected void replaceEditor(JComponent oldEditor, JComponent newEditor) { 183 spinner.remove(oldEditor); 184 configureEditor(newEditor); 185 spinner.add(newEditor, "Editor"); 186 } 187 188 189 192 private void configureEditor(JComponent editor) { 193 if ((editor instanceof JSpinner.DefaultEditor )) { 194 JSpinner.DefaultEditor defaultEditor = (JSpinner.DefaultEditor ) editor; 195 defaultEditor.getTextField().getUI(); 196 defaultEditor.getTextField().setBorder(MARGIN_BORDER); 197 Insets insets = UIManager.getInsets("Spinner.defaultEditorInsets"); 198 defaultEditor.getTextField().setMargin(insets); 199 } 200 } 201 202 206 private static class SpinnerArrowButton extends PlasticArrowButton { 207 private SpinnerArrowButton(int direction, 208 ExtBasicArrowButtonHandler handler) { 209 super(direction, UIManager.getInt("ScrollBar.width"), true); 210 addActionListener(handler); 211 addMouseListener(handler); 212 } 213 214 protected int calculateArrowHeight(int height, int width) { 215 int arrowHeight = Math.min((height - 4) / 3, (width - 4) / 3); 216 return Math.max(arrowHeight, 3); 217 } 218 219 protected int calculateArrowOffset() { 220 return 1; 221 } 222 223 protected boolean isPaintingNorthBottom() { 224 return true; 225 } 226 227 } 228 229 } | Popular Tags |