001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.command.support;
017    
018    import java.beans.PropertyChangeListener;
019    import java.util.Map;
020    
021    import org.springframework.binding.value.ValueModel;
022    import org.springframework.binding.value.support.ValueHolder;
023    import org.springframework.richclient.command.GuardedActionCommandExecutor;
024    import org.springframework.richclient.command.ParameterizableActionCommandExecutor;
025    import org.springframework.richclient.core.SecurityControllable;
026    
027    /**
028     * @author Keith Donald
029     */
030    public class AbstractActionCommandExecutor implements ParameterizableActionCommandExecutor,
031            GuardedActionCommandExecutor, SecurityControllable {
032    
033        private ValueModel enabled = new ValueHolder(Boolean.FALSE);
034    
035        private boolean authorized = true;
036        private boolean maskedEnabledState = false;
037        private String securityControllerId = null;
038    
039        /**
040         * Set the Id of the security controller that should manage this object.
041         * @param controllerId Id (bean name) of the security controller
042         */
043        public void setSecurityControllerId(String controllerId) {
044            this.securityControllerId = controllerId;
045        }
046    
047        /**
048         * Get the id (bean name) of the security controller that should manage this object.
049         * @return controller id
050         */
051        public String getSecurityControllerId() {
052            return securityControllerId;
053        }
054    
055        /**
056         * Set the authorized state.  Setting authorized to false will override any call
057         * to {@link #setEnabled(boolean)}.  As long as this object is unauthorized,
058         * it can not be enabled.
059         * @param authorized Pass <code>true</code> if the object is to be authorized
060         */
061        public void setAuthorized( boolean authorized ) {
062    
063            if( isAuthorized() != authorized ) {
064                
065                this.authorized = authorized;
066    
067                // We need to apply a change to our enabled state depending on our
068                // new authorized state.
069                if( authorized ) {
070                    // install the last requested enabled state
071                    setEnabled(maskedEnabledState);
072                } else {
073                    // Record the current enabled state and then disable
074                    maskedEnabledState = isEnabled();
075                    internalSetEnabled(false);
076                }
077            }
078        }
079    
080        /**
081         * Get the authorized state.
082         * @return authorized
083         */
084        public boolean isAuthorized() {
085            return authorized;
086        }
087    
088        public boolean isEnabled() {
089            return ((Boolean)enabled.getValue()).booleanValue();
090        }
091    
092        /**
093         * Set the enabled state of this command.  Note that if we are currently not
094         * authorized, then the new value will just be recorded and no change in the
095         * current enabled state will be made.
096         * @param enabled state
097         */
098        public void setEnabled(boolean enabled) {
099            maskedEnabledState = enabled;
100            if( isAuthorized() ) {
101                internalSetEnabled( enabled );
102            }
103        }
104    
105        /**
106         * Internal method to set the enabled state.  This is needed so that calls
107         * made to the public setEnabled and this method can be handled differently.
108         * @param enabled state
109         * @see #setAuthorized(boolean)
110         */
111        protected void internalSetEnabled( boolean enabled ) {
112            this.enabled.setValue(Boolean.valueOf(enabled));
113        }
114    
115        public void addEnabledListener(PropertyChangeListener listener) {
116            enabled.addValueChangeListener(listener);
117        }
118    
119        public void removeEnabledListener(PropertyChangeListener listener) {
120            enabled.removeValueChangeListener(listener);
121        }
122    
123        public void execute(Map parameters) {
124            execute();
125        }
126    
127        public void execute() {
128        }
129    
130    }