001    package users;
002    
003    import sale.*;
004    
005    /**
006     * A special capability guarding an action object.
007     *
008     * <p>The ActionCapability is itself an Action again, so that you can write code like
009     * this:</p>
010     *
011     * <pre>
012     *   public class S extends SalesPoint {
013     *
014     *   ...
015     *
016     *     public MenuSheet getDefaultMenuSheet() {
017     *
018     *       MenuSheet ms = new MenuSheet();
019     *
020     * <b>      ms.addItem ("Capability Test",
021             *         (Action) {@link SalesPoint#getUser getUser}().{@link User#getCapability getCapability}("CapTest"));
022     * </b>
023     *       return ms;
024     *     }
025     *
026     *   ...
027     *
028     *   }
029     *
030     * </pre>
031     *
032     * <p>Assuming that the CapTest capability is represented by an ActionCapability wrapping
033     * some other Action.</p>
034     *
035     * @see SalesPoint
036     * @see MenuSheet
037     * @see UserManager
038     * @see User
039     *
040     * @author Steffen Zschaler
041     * @version 2.0 05/05/1999
042     * @since v2.0
043     */
044    public class ActionCapability extends AbstractCapability implements Action {
045    
046        /**
047         * Is this capability granted?
048         *
049         * @serial
050         */
051        private boolean m_fGranted;
052    
053        /**
054         * The action guarded.
055         *
056         * @serial
057         */
058        private Action m_aToDo;
059    
060        /**
061         * A message to be presented on denial.
062         *
063         * @serial
064         */
065        private String m_sOnDeny;
066    
067        /**
068         * Create a granted capability.
069         *
070         * @param sName the name of the capability.
071         * @param aToDo the Action to be guarded.
072         */
073        public ActionCapability(String sName, Action aToDo) {
074            this(sName, null, aToDo, true);
075        }
076    
077        /**
078         * Create a new ActionCapability.
079         *
080         * @param sName the name of the capability.
081         * @param sOnDeny a message to be popped up on denial.
082         * @param aToDo the Action to be guarded.
083         * @param fGranted true if this capability is to grant rights.
084         */
085        public ActionCapability(String sName, String sOnDeny, Action aToDo, boolean fGranted) {
086            super(sName);
087    
088            m_sOnDeny = sOnDeny;
089            m_aToDo = aToDo;
090            m_fGranted = fGranted;
091        }
092    
093        // Capability interface methods
094    
095        /**
096         * Get the ActionCapability that is the inverse to this one. I.e. if this capability grants the right to
097         * execute a given action, the returned capability will deny this right and vice versa.
098         *
099         * @return the ActionCapability that is the inverse to this one.
100         *
101         * @override Never
102         */
103        public Capability getToggled() {
104            return new ActionCapability(getName(), m_sOnDeny, m_aToDo, !m_fGranted);
105        }
106    
107        /**
108         * Return true if this capability is granting rights.
109         *
110         * @return true if this capability is granting rights.
111         *
112         * @override Never
113         */
114        public boolean isGranted() {
115            return m_fGranted;
116        }
117    
118        // Action interface method
119    
120        /**
121         * If the capability is granting rights, perform the guarded action; otherwise,
122         * print a message to the user that he/she is not granted the right to
123         * perform this action.
124         *
125         * @param p the process in which to perform the action
126         * @param sp the SalesPoint on which to perform the action.
127         *
128         * @override Never
129         */
130        public void doAction(SaleProcess p, SalesPoint sp) throws Throwable {
131            // This is not the proper way, normally the two cases should be different subclasses
132            // of Capability, but for the sake of useability I put it like this.
133            if (m_fGranted) {
134                if (m_aToDo != null) {
135                    m_aToDo.doAction(p, sp);
136                }
137            } else {
138                if (m_sOnDeny != null) {
139    
140                    // use the JOptionPane to make it easy to pop up a message box.
141                    // Should actually be using a p.popupFormSheet (new MsgForm (...)) instead
142                    javax.swing.JOptionPane.showMessageDialog(null, m_sOnDeny, "Error",
143                            javax.swing.JOptionPane.ERROR_MESSAGE);
144                }
145            }
146        }
147    }