Save This Page
Home » apache-harmony-6.0-src-r917296-snapshot » javax » swing » [javadoc | source]
    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   
   18   package javax.swing;
   19   
   20   import java.awt.event.ActionEvent;
   21   import java.awt.event.ActionListener;
   22   import java.awt.event.ItemEvent;
   23   import java.awt.event.ItemListener;
   24   import java.io.Serializable;
   25   import java.util.EventListener;
   26   import javax.swing.event.ChangeEvent;
   27   import javax.swing.event.ChangeListener;
   28   import javax.swing.event.EventListenerList;
   29   
   30   /**
   31    * <p>
   32    * <i>DefaultButtonModel</i>
   33    * </p>
   34    * <h3>Implementation Notes:</h3>
   35    * <ul>
   36    * <li>The <code>serialVersionUID</code> fields are explicitly declared as a performance
   37    * optimization, not as a guarantee of serialization compatibility.</li>
   38    * </ul>
   39    */
   40   public class DefaultButtonModel implements ButtonModel, Serializable {
   41       private static final long serialVersionUID = -8004185980087291435L;
   42   
   43       public static final int ARMED = 1;
   44   
   45       public static final int SELECTED = 2;
   46   
   47       public static final int PRESSED = 4;
   48   
   49       public static final int ENABLED = 8;
   50   
   51       public static final int ROLLOVER = 16;
   52   
   53       protected int stateMask = ENABLED;
   54   
   55       protected String actionCommand;
   56   
   57       protected ButtonGroup group;
   58   
   59       protected int mnemonic;
   60   
   61       protected transient ChangeEvent changeEvent;
   62   
   63       protected EventListenerList listenerList = new EventListenerList();
   64   
   65       public <T extends EventListener> T[] getListeners(Class<T> listenersClass) {
   66           return listenerList.getListeners(listenersClass);
   67       }
   68   
   69       public void addChangeListener(ChangeListener listener) {
   70           listenerList.add(ChangeListener.class, listener);
   71       }
   72   
   73       public void removeChangeListener(ChangeListener listener) {
   74           listenerList.remove(ChangeListener.class, listener);
   75       }
   76   
   77       public ChangeListener[] getChangeListeners() {
   78           return listenerList.getListeners(ChangeListener.class);
   79       }
   80   
   81       public void addItemListener(ItemListener listener) {
   82           listenerList.add(ItemListener.class, listener);
   83       }
   84   
   85       public void removeItemListener(ItemListener listener) {
   86           listenerList.remove(ItemListener.class, listener);
   87       }
   88   
   89       public ItemListener[] getItemListeners() {
   90           return listenerList.getListeners(ItemListener.class);
   91       }
   92   
   93       public void addActionListener(ActionListener listener) {
   94           listenerList.add(ActionListener.class, listener);
   95       }
   96   
   97       public void removeActionListener(ActionListener listener) {
   98           listenerList.remove(ActionListener.class, listener);
   99       }
  100   
  101       public ActionListener[] getActionListeners() {
  102           return listenerList.getListeners(ActionListener.class);
  103       }
  104   
  105       public void setGroup(ButtonGroup group) {
  106           this.group = group;
  107       }
  108   
  109       public ButtonGroup getGroup() {
  110           return group;
  111       }
  112   
  113       public void setActionCommand(String command) {
  114           actionCommand = command;
  115       }
  116   
  117       public String getActionCommand() {
  118           return actionCommand;
  119       }
  120   
  121       public Object[] getSelectedObjects() {
  122           return null;
  123       }
  124   
  125       public void setSelected(boolean selected) {
  126           if (isSelected() != selected) {
  127               toggleState(SELECTED);
  128               int state = selected ? ItemEvent.SELECTED : ItemEvent.DESELECTED;
  129               ItemEvent event = new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, this, state);
  130               fireItemStateChanged(event);
  131           }
  132       }
  133   
  134       public boolean isSelected() {
  135           return isStateSet(SELECTED);
  136       }
  137   
  138       public void setRollover(boolean rollover) {
  139           if (isEnabled() && isRollover() != rollover) {
  140               toggleState(ROLLOVER);
  141           }
  142       }
  143   
  144       public boolean isRollover() {
  145           return isStateSet(ROLLOVER);
  146       }
  147   
  148       public void setPressed(boolean pressed) {
  149           if (isEnabled() && isPressed() != pressed) {
  150               toggleState(PRESSED);
  151               if (!pressed && isArmed()) {
  152                   fireActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
  153                           actionCommand, System.currentTimeMillis(), 0));
  154               }
  155           }
  156       }
  157   
  158       public boolean isPressed() {
  159           return isStateSet(PRESSED);
  160       }
  161   
  162       public void setEnabled(boolean enabled) {
  163           if (isEnabled() != enabled) {
  164               stateMask = isSelected() ? SELECTED : 0;
  165               if (enabled) {
  166                   stateMask |= ENABLED;
  167               }
  168               fireStateChanged();
  169           }
  170       }
  171   
  172       public boolean isEnabled() {
  173           return isStateSet(ENABLED);
  174       }
  175   
  176       public void setArmed(boolean armed) {
  177           if (isEnabled() && isArmed() != armed) {
  178               toggleState(ARMED);
  179           }
  180       }
  181   
  182       public boolean isArmed() {
  183           return isStateSet(ARMED);
  184       }
  185   
  186       public void setMnemonic(int mnemonic) {
  187           if (this.mnemonic != mnemonic) {
  188               this.mnemonic = mnemonic;
  189               fireStateChanged();
  190           }
  191       }
  192   
  193       public int getMnemonic() {
  194           return mnemonic;
  195       }
  196   
  197       protected void fireStateChanged() {
  198           ChangeListener[] listeners = getChangeListeners();
  199           if (listeners.length == 0) {
  200               return;
  201           }
  202           if (changeEvent == null) {
  203               changeEvent = new ChangeEvent(this);
  204           }
  205           for (int i = 0; i < listeners.length; i++) {
  206               listeners[i].stateChanged(changeEvent);
  207           }
  208       }
  209   
  210       protected void fireItemStateChanged(ItemEvent event) {
  211           ItemListener[] listeners = getItemListeners();
  212           for (int i = 0; i < listeners.length; i++) {
  213               listeners[i].itemStateChanged(event);
  214           }
  215       }
  216   
  217       protected void fireActionPerformed(ActionEvent event) {
  218           ActionListener[] listeners = getActionListeners();
  219           for (int i = 0; i < listeners.length; i++) {
  220               listeners[i].actionPerformed(event);
  221           }
  222       }
  223       
  224       void toggleState(int stateFlag) {
  225           // visibility is changed from private to default because according to
  226           // HARMONY-4658 patch the method is needed by ToggleButtonModel
  227           stateMask ^= stateFlag;
  228           fireStateChanged();
  229       }
  230   
  231       private boolean isStateSet(int stateFlag) {
  232           return (stateMask & stateFlag) != 0;
  233       }
  234   }

Save This Page
Home » apache-harmony-6.0-src-r917296-snapshot » javax » swing » [javadoc | source]