/*
 * Copyright 2002-2004 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.springframework.richclient.forms;

import java.awt.AWTEvent;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.ItemEvent;

import javax.swing.DefaultButtonModel;

import org.springframework.binding.value.ValueChangeListener;
import org.springframework.binding.value.ValueModel;

final class SelectableButtonValueModel extends DefaultButtonModel implements ValueChangeListener {
    private ValueModel valueModel;

    public SelectableButtonValueModel(ValueModel valueModel) {
        this.valueModel = valueModel;
        this.valueModel.addValueChangeListener(this);
    }

    public void valueChanged() {
        fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, this,
                this.isSelected() ? ItemEvent.SELECTED : ItemEvent.DESELECTED));
    }

    public void setPressed(boolean b) {
        if ((isPressed() == b) || !isEnabled()) {
            return;
        }

        if (b == false && isArmed()) {
            setSelected(!this.isSelected());
        }

        if (b) {
            stateMask |= PRESSED;
        }
        else {
            stateMask &= ~PRESSED;
        }

        fireStateChanged();

        if (!isPressed() && isArmed()) {
            int modifiers = 0;
            AWTEvent currentEvent = EventQueue.getCurrentEvent();
            if (currentEvent instanceof InputEvent) {
                modifiers = ((InputEvent)currentEvent).getModifiers();
            }
            else if (currentEvent instanceof ActionEvent) {
                modifiers = ((ActionEvent)currentEvent).getModifiers();
            }
            fireActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, getActionCommand(), EventQueue
                    .getMostRecentEventTime(), modifiers));
        }
    }

    public boolean isSelected() {
        Boolean selected = (Boolean)valueModel.getValue();
        if (selected == null) {
            return false;
        }
        return selected.booleanValue();
    }

    public void setSelected(boolean b) {
        if (isSelected() == b) {
            return;
        }

        if (b) {
            stateMask |= SELECTED;
        }
        else {
            stateMask &= ~SELECTED;
        }

        fireStateChanged();

        if (b) {
            valueModel.setValue(Boolean.TRUE);
        }
        else {
            valueModel.setValue(Boolean.FALSE);
        }

    }
}


@@ -663,165 +663,8 @@
 s;%0A%0A
-import java.awt.AWTEvent;%0Aimport java.awt.EventQueue;%0Aimport java.awt.event.ActionEvent;%0Aimport java.awt.event.InputEvent;%0Aimport java.awt.event.ItemEvent;%0A%0A
 impo
@@ -819,13 +819,14 @@
 l;%0A%0A
-final
+public
  cla
@@ -1106,16 +1106,40 @@
 (this);%0A
+        valueChanged();%0A
     %7D%0A%0A 
@@ -1182,101 +1182,73 @@
     
-fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, this,%0A  
+Boolean selected = (Boolean)valueModel.getValue();
       
+%0A
         
 this
@@ -1243,23 +1243,19 @@
         
-this.is
+set
 Selected
@@ -1259,53 +1259,57 @@
 ted(
-) ? ItemEvent.SELECTED : ItemEvent.DESELECTED
+selected == null ? false : selected.booleanValue(
 ));%0A
@@ -1342,33 +1342,39 @@
 Pressed(boolean 
-b
+pressed
 ) %7B%0A        if (
@@ -1389,17 +1389,23 @@
 ed() == 
-b
+pressed
 ) %7C%7C !is
@@ -1450,32 +1450,27 @@
    %7D
-%0A%0A        if (b == fal
+ else if (! pres
 se
+d
  && 
@@ -1541,794 +1541,86 @@
   %7D%0A
-%0A        if (b) %7B%0A            stateMask %7C= PRESSED;%0A        %7D%0A        else %7B%0A            stateMask &= ~PRESSED;%0A        %7D%0A%0A        fireStateChanged();%0A%0A        if (!isPressed() && isArmed()) %7B%0A            int modifiers = 0;%0A            AWTEvent currentEvent = EventQueue.getCurrentEvent();%0A            if (currentEvent instanceof InputEvent) %7B%0A                modifiers = ((InputEvent)currentEvent).getModifiers();%0A            %7D%0A            else if (currentEvent instanceof ActionEvent) %7B%0A                modifiers = ((ActionEvent)currentEvent).getModifiers();%0A            %7D%0A            fireActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, getActionCommand(), EventQueue%0A                    .getMostRecentEventTime(), modifiers));%0A        %7D%0A    %7D%0A%0A    public 
+        super.setPressed(pressed);%0A    %7D%0A%0A    public void setSelected(
 boolean 
 isSe
@@ -1615,27 +1615,24 @@
 boolean 
-isS
+s
 elected
-(
 ) %7B%0A    
@@ -1639,87 +1639,36 @@
     
-Boolean selected = (Boolean)valueModel.getValue();%0A        if (selected == null
+if (isSelected() == selected
 ) %7B%0A
@@ -1685,22 +1685,16 @@
   return
- false
 ;%0A      
@@ -1709,458 +1709,102 @@
     
-return selected.booleanValue();%0A    %7D%0A%0A    public void setSelected(boolean b) %7B%0A        if (isSelected() == b) %7B%0A            return;%0A        %7D%0A%0A        if (b) %7B%0A            stateMask %7C= SELECTED;%0A        %7D%0A        else %7B%0A            stateMask &= ~SELECTED;%0A        %7D%0A%0A        fireStateChanged();%0A%0A        if (b) %7B%0A            valueModel.setValue(Boolean.TRUE);%0A        %7D%0A        else %7B%0A            valueModel.setValue(Boolean.FALSE);%0A        %7D%0A
+super.setSelected(selected);%0A        valueModel.setValue(Boolean.valueOf(isSelected()));
 %0A    %7D%0A%7D
 %0A%0A
@@ -1801,10 +1801,8 @@
 %0A    %7D%0A%7D
-%0A%0A

fix for RCP-63