1 23 24 package swingwtx.swing; 25 26 import java.util.Iterator ; 27 import java.util.Vector ; 28 29 import swingwt.awt.BorderLayout; 30 import swingwt.awt.Container; 31 import swingwt.awt.Dimension; 32 import swingwt.awt.event.AdjustmentEvent; 33 import swingwt.awt.event.AdjustmentListener; 34 import swingwt.awt.event.FocusListener; 35 import swingwt.awt.event.KeyAdapter; 36 import swingwt.awt.event.KeyEvent; 37 import swingwt.awt.event.KeyListener; 38 import swingwtx.swing.event.ChangeEvent; 39 import swingwtx.swing.event.ChangeListener; 40 41 48 public class JSpinner extends JPanel implements ChangeListener { 49 50 51 protected Vector changeListeners = new Vector (); 52 53 protected SpinnerModel model = null; 54 55 protected JTextField text = new JTextField(); 56 protected JScrollBar spin = new JScrollBar(JScrollBar.VERTICAL); 57 58 protected int lastValue = 300000; 59 60 public JSpinner() { this(new SpinnerNumberModel()); } 61 62 public JSpinner(SpinnerModel model) { 63 64 super(); 65 66 setPreferredSize(new Dimension(100, 30)); 67 setModel(model); 68 setLayout(new BorderLayout()); 69 70 text.addKeyListener(new KeyAdapter() { 72 public void keyPressed(KeyEvent e) { 73 if (e.getKeyCode() == 16777218) { 74 nextItem(); e.consume(); 76 } 77 else if (e.getKeyCode() == 16777217) { 78 previousItem(); e.consume(); 80 } 81 } 82 }); 83 84 spin.setMinimum(0); 86 spin.setMaximum(650000); 87 spin.setValue(30000); 88 spin.addAdjustmentListener(new AdjustmentListener() { 89 public void adjustmentValueChanged(AdjustmentEvent e) { 90 if (e.getValue() > lastValue) 91 previousItem(); 92 else 93 nextItem(); 94 lastValue = e.getValue(); 95 } 96 }); 97 98 add(text, BorderLayout.CENTER); 100 add(spin, BorderLayout.EAST); 101 } 102 103 protected final static int CANCELED = 0; 104 protected final static int INVISIBLE = 1; 105 protected final static int VISIBLE = 2; 106 107 public void setModel(SpinnerModel model) { 108 this.model = model; 109 model.addChangeListener(this); 110 } 111 112 public SpinnerModel getModel() { 113 return model; 114 } 115 116 public void addFocusListener(FocusListener l) { 117 text.addFocusListener(l); 118 } 119 public void removeFocusListener(FocusListener l) { 120 text.removeFocusListener(l); 121 } 122 public void addKeyListener(KeyListener l) { 123 text.addKeyListener(l); 124 } 125 public void removeKeyListener(KeyListener l) { 126 text.removeKeyListener(l); 127 } 128 129 public void addChangeListener(ChangeListener l) { 130 changeListeners.add(l); 131 } 132 133 public void removeItemListener(ChangeListener l) { 134 changeListeners.remove(l); 135 } 136 137 public void processChangeEvent(ChangeEvent e) { 138 Iterator i = changeListeners.iterator(); 139 while (i.hasNext()) { 140 ChangeListener l = (ChangeListener) i.next(); 141 l.stateChanged(e); 142 } 143 } 144 145 public void setValue(Object o) { 146 model.setValue(o); 147 } 148 149 public Object getValue() { 150 return model.getValue(); 151 } 152 153 154 public void setEnabled(boolean b) { 155 text.setEnabled(b); 156 spin.setEnabled(b); 157 } 158 159 public boolean isEnabled() { 160 return spin.isEnabled(); 161 } 162 163 public String getToolTipText() { 164 return text.getToolTipText(); 165 } 166 167 public void setToolTipText(String tip) { 168 text.setToolTipText(tip); 169 } 170 171 protected void previousItem() { 172 model.setValue( model.getPreviousValue() ); 173 } 174 protected void nextItem() { 175 model.setValue( model.getNextValue() ); 176 } 177 178 public void requestFocus() { 179 text.requestFocus(); 180 } 181 182 public void grabFocus() { 183 text.grabFocus(); 184 } 185 186 public JTextField getJTextField() { 187 return text; 188 } 189 190 public void stateChanged(ChangeEvent e) { 191 this.text.setText( model.getValue().toString()); 192 } 193 194 198 public void setBounds(int x, int y, int width, int height) { 199 if (height > 30) height = 30; 200 super.setBounds(x, y, width, height); 201 } 202 203 206 protected swingwt.awt.Dimension calculatePreferredSize() { 207 swingwt.awt.Dimension size = new swingwt.awt.Dimension(100, 30); 209 setSize(size); 210 return size; 211 } 212 213 public void setSwingWTParent(Container parent) throws Exception { 214 super.setSwingWTParent(parent); 215 stateChanged(new ChangeEvent(model)); 216 } 217 218 } 219 | Popular Tags |