1 7 package javax.swing; 8 9 import java.awt.*; 10 import java.awt.event.*; 11 import java.awt.image.*; 12 import java.io.Serializable ; 13 import java.util.EventListener ; 14 import javax.swing.event.*; 15 16 31 public class DefaultButtonModel implements ButtonModel , Serializable { 32 33 protected int stateMask = 0; 34 protected String actionCommand = null; 35 protected ButtonGroup group = null; 36 37 protected int mnemonic = 0; 38 39 45 protected transient ChangeEvent changeEvent = null; 46 protected EventListenerList listenerList = new EventListenerList(); 47 48 49 53 public DefaultButtonModel() { 54 stateMask = 0; 55 setEnabled(true); 56 } 57 58 62 public final static int ARMED = 1 << 0; 63 64 68 public final static int SELECTED = 1 << 1; 69 70 74 public final static int PRESSED = 1 << 2; 75 76 80 public final static int ENABLED = 1 << 3; 81 82 85 public final static int ROLLOVER = 1 << 4; 86 87 94 public void setActionCommand(String actionCommand) { 95 this.actionCommand = actionCommand; 96 } 97 98 104 public String getActionCommand() { 105 return actionCommand; 106 } 107 108 115 public boolean isArmed() { 116 return (stateMask & ARMED) != 0; 117 } 118 119 125 public boolean isSelected() { 126 return (stateMask & SELECTED) != 0; 127 } 128 129 137 public boolean isEnabled() { 138 return (stateMask & ENABLED) != 0; 139 } 140 141 146 public boolean isPressed() { 147 return (stateMask & PRESSED) != 0; 148 } 149 150 155 public boolean isRollover() { 156 return (stateMask & ROLLOVER) != 0; 157 } 158 159 167 public void setArmed(boolean b) { 168 if((isArmed() == b) || !isEnabled()) { 169 return; 170 } 171 172 if (b) { 173 stateMask |= ARMED; 174 } else { 175 stateMask &= ~ARMED; 176 } 177 178 fireStateChanged(); 179 } 180 181 187 public void setEnabled(boolean b) { 188 if(isEnabled() == b) { 189 return; 190 } 191 192 if (b) { 193 stateMask |= ENABLED; 194 } else { 195 stateMask &= ~ENABLED; 196 stateMask &= ~ARMED; 198 stateMask &= ~PRESSED; 199 } 200 201 202 fireStateChanged(); 203 } 204 205 211 public void setSelected(boolean b) { 212 if (this.isSelected() == b) { 213 return; 214 } 215 216 if (b) { 217 stateMask |= SELECTED; 218 } else { 219 stateMask &= ~SELECTED; 220 } 221 222 fireItemStateChanged( 223 new ItemEvent(this, 224 ItemEvent.ITEM_STATE_CHANGED, 225 this, 226 b ? ItemEvent.SELECTED : ItemEvent.DESELECTED)); 227 228 fireStateChanged(); 229 230 } 231 232 233 239 public void setPressed(boolean b) { 240 if((isPressed() == b) || !isEnabled()) { 241 return; 242 } 243 244 if (b) { 245 stateMask |= PRESSED; 246 } else { 247 stateMask &= ~PRESSED; 248 } 249 250 if(!isPressed() && isArmed()) { 251 int modifiers = 0; 252 AWTEvent currentEvent = EventQueue.getCurrentEvent(); 253 if (currentEvent instanceof InputEvent) { 254 modifiers = ((InputEvent)currentEvent).getModifiers(); 255 } else if (currentEvent instanceof ActionEvent) { 256 modifiers = ((ActionEvent)currentEvent).getModifiers(); 257 } 258 fireActionPerformed( 259 new ActionEvent(this, ActionEvent.ACTION_PERFORMED, 260 getActionCommand(), 261 EventQueue.getMostRecentEventTime(), 262 modifiers)); 263 } 264 265 fireStateChanged(); 266 } 267 268 274 public void setRollover(boolean b) { 275 if((isRollover() == b) || !isEnabled()) { 276 return; 277 } 278 279 if (b) { 280 stateMask |= ROLLOVER; 281 } else { 282 stateMask &= ~ROLLOVER; 283 } 284 285 fireStateChanged(); 286 } 287 288 294 public void setMnemonic(int key) { 295 mnemonic = key; 296 fireStateChanged(); 297 } 298 299 305 public int getMnemonic() { 306 return mnemonic; 307 } 308 309 314 public void addChangeListener(ChangeListener l) { 315 listenerList.add(ChangeListener.class, l); 316 } 317 318 323 public void removeChangeListener(ChangeListener l) { 324 listenerList.remove(ChangeListener.class, l); 325 } 326 327 340 public ChangeListener[] getChangeListeners() { 341 return (ChangeListener[])listenerList.getListeners( 342 ChangeListener.class); 343 } 344 345 352 protected void fireStateChanged() { 353 Object [] listeners = listenerList.getListenerList(); 355 for (int i = listeners.length-2; i>=0; i-=2) { 358 if (listeners[i]==ChangeListener.class) { 359 if (changeEvent == null) 361 changeEvent = new ChangeEvent(this); 362 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent); 363 } 364 } 365 } 366 367 372 public void addActionListener(ActionListener l) { 373 listenerList.add(ActionListener.class, l); 374 } 375 376 381 public void removeActionListener(ActionListener l) { 382 listenerList.remove(ActionListener.class, l); 383 } 384 385 398 public ActionListener[] getActionListeners() { 399 return (ActionListener[])listenerList.getListeners( 400 ActionListener.class); 401 } 402 403 410 protected void fireActionPerformed(ActionEvent e) { 411 Object [] listeners = listenerList.getListenerList(); 413 for (int i = listeners.length-2; i>=0; i-=2) { 416 if (listeners[i]==ActionListener.class) { 417 ((ActionListener)listeners[i+1]).actionPerformed(e); 421 } 422 } 423 } 424 425 430 public void addItemListener(ItemListener l) { 431 listenerList.add(ItemListener.class, l); 432 } 433 434 439 public void removeItemListener(ItemListener l) { 440 listenerList.remove(ItemListener.class, l); 441 } 442 443 456 public ItemListener[] getItemListeners() { 457 return (ItemListener[])listenerList.getListeners(ItemListener.class); 458 } 459 460 467 protected void fireItemStateChanged(ItemEvent e) { 468 Object [] listeners = listenerList.getListenerList(); 470 for (int i = listeners.length-2; i>=0; i-=2) { 473 if (listeners[i]==ItemListener.class) { 474 ((ItemListener)listeners[i+1]).itemStateChanged(e); 478 } 479 } 480 } 481 482 519 public <T extends EventListener > T[] getListeners(Class <T> listenerType) { 520 return listenerList.getListeners(listenerType); 521 } 522 523 524 public Object [] getSelectedObjects() { 525 return null; 526 } 527 528 535 public void setGroup(ButtonGroup group) { 536 this.group = group; 537 } 538 539 548 public ButtonGroup getGroup() { 549 return group; 550 } 551 552 } 553 | Popular Tags |