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             * ID for serialization.
048             */
049            private static final long serialVersionUID = 6852773649712428747L;
050    
051            /**
052         * Is this capability granted?
053         *
054         * @serial
055         */
056        private boolean m_fGranted;
057    
058        /**
059         * The action guarded.
060         *
061         * @serial
062         */
063        private Action m_aToDo;
064    
065        /**
066         * A message to be presented on denial.
067         *
068         * @serial
069         */
070        private String m_sOnDeny;
071    
072        /**
073         * Create a granted capability.
074         *
075         * @param sName the name of the capability.
076         * @param aToDo the Action to be guarded.
077         */
078        public ActionCapability(String sName, Action aToDo) {
079            this(sName, null, aToDo, true);
080        }
081    
082        /**
083         * Create a new ActionCapability.
084         *
085         * @param sName the name of the capability.
086         * @param sOnDeny a message to be popped up on denial.
087         * @param aToDo the Action to be guarded.
088         * @param fGranted true if this capability is to grant rights.
089         */
090        public ActionCapability(String sName, String sOnDeny, Action aToDo, boolean fGranted) {
091            super(sName);
092    
093            m_sOnDeny = sOnDeny;
094            m_aToDo = aToDo;
095            m_fGranted = fGranted;
096        }
097    
098        // Capability interface methods
099    
100        /**
101         * Get the ActionCapability that is the inverse to this one. I.e. if this capability grants the right to
102         * execute a given action, the returned capability will deny this right and vice versa.
103         *
104         * @return the ActionCapability that is the inverse to this one.
105         *
106         * @override Never
107         */
108        public Capability getToggled() {
109            return new ActionCapability(getName(), m_sOnDeny, m_aToDo, !m_fGranted);
110        }
111    
112        /**
113         * Return true if this capability is granting rights.
114         *
115         * @return true if this capability is granting rights.
116         *
117         * @override Never
118         */
119        public boolean isGranted() {
120            return m_fGranted;
121        }
122    
123        // Action interface method
124    
125        /**
126         * If the capability is granting rights, perform the guarded action; otherwise,
127         * print a message to the user that he/she is not granted the right to
128         * perform this action.
129         *
130         * @param p the process in which to perform the action
131         * @param sp the SalesPoint on which to perform the action.
132         *
133         * @override Never
134         */
135        public void doAction(SaleProcess p, SalesPoint sp) throws Throwable {
136            // This is not the proper way, normally the two cases should be different subclasses
137            // of Capability, but for the sake of useability I put it like this.
138            if (m_fGranted) {
139                if (m_aToDo != null) {
140                    m_aToDo.doAction(p, sp);
141                }
142            } else {
143                if (m_sOnDeny != null) {
144    
145                    // use the JOptionPane to make it easy to pop up a message box.
146                    // Should actually be using a p.popupFormSheet (new MsgForm (...)) instead
147                    javax.swing.JOptionPane.showMessageDialog(null, m_sOnDeny, "Error",
148                            javax.swing.JOptionPane.ERROR_MESSAGE);
149                }
150            }
151        }
152    }