001    package data.stdforms.singletableformsheet;
002    
003    import sale.*;
004    import sale.stdforms.*;
005    
006    import data.*;
007    import data.stdforms.*;
008    
009    import users.*;
010    
011    /**
012     * Strategy that can be associated to the "Remove" button of a SingleTableFormSheet that displays
013     * the contents of a Catalog.
014     *
015     * @author Steffen Zschaler
016     * @version 2.0 20/08/1999
017     * @since v2.0
018     */
019    public class DefaultRemoveCatalogItemStrategy extends EditButtonStrategy {
020    
021        /**
022             * ID for serialization.
023             */
024            private static final long serialVersionUID = -8582482476800352301L;
025            
026            /**
027         * The Catalog that is being edited.
028         *
029         * @serial
030         */
031        protected Catalog m_cCatalog;
032    
033        /**
034         * Create a new DefaultRemoveCatalogItemStrategy.
035         *
036         * @param c the Catalog to be edited. Must be the same that is displayed by the {@link SingleTableFormSheet}.
037         */
038        public DefaultRemoveCatalogItemStrategy(Catalog c) {
039            super();
040    
041            m_cCatalog = c;
042        }
043    
044        /**
045         * @override Never
046         */
047        public Transition getEditProcess(SingleTableFormSheet stfs, SaleProcess p, SalesPoint sp) {
048            return new GateChangeTransition(getCheckGate(stfs));
049        }
050    
051        /**
052         * Get the Gate that checks whether the removal is allowed. If this is the case, the Gate must be left
053         * through {@link #getRemoveTransition}, otherwise, a transition to the
054         * {@link SingleTableFormSheet#getGate SingleTableFormSheet's gate} should be triggered.
055         *
056         * @override Sometimes The default implementation puts up a MsgForm asking for the user to confirm the
057         * removal. Caption and text of the message are obtained via {@link #getConfirmationCaption} and
058         * {@link #getConfirmationText}, resp.
059         *
060         * @param stfs the FormSheet that triggered the operation.
061         */
062        protected Gate getCheckGate(final SingleTableFormSheet stfs) {
063            CatalogItem ci = (CatalogItem)stfs.getSelectedRecord();
064            FormSheet fs = new MsgForm(getConfirmationCaption(stfs, ci), getConfirmationText(stfs, ci));
065    
066            final UIGate uigCheckGate = new UIGate(fs, null);
067    
068            fs.addContentCreator(new FormSheetContentCreator() {
069                            private static final long serialVersionUID = 5857843585464874928L;
070    
071                            protected void createFormSheetContent(FormSheet fs) {
072                    fs.removeAllButtons();
073    
074                    fs.addButton("Yes", FormSheet.BTNID_OK, new Action() {
075                                            private static final long serialVersionUID = 971213679980397563L;
076    
077                                            public void doAction(SaleProcess p, SalesPoint sp) {
078                            uigCheckGate.setNextTransition(getRemoveTransition(stfs));
079                        }
080                    });
081    
082                    fs.addButton("No", FormSheet.BTNID_CANCEL, new Action() {
083                                            private static final long serialVersionUID = -2650797323621014955L;
084    
085                                            public void doAction(SaleProcess p, SalesPoint sp) {
086                            uigCheckGate.setNextTransition(new GateChangeTransition(stfs.getGate()));
087                        }
088                    });
089                }
090            });
091    
092            return uigCheckGate;
093        }
094    
095        /**
096         * Get the caption for the default confirmation {@link MsgForm}.
097         *
098         * @param stfs the FormSheet that triggered the operation.
099         * @param ci the CatalogItem that is going to be removed.
100         *
101         * @override Sometimes The default implementation returns
102         * <code>(stfs.getCaption() + " - Confirmation")</code>.
103         */
104        protected String getConfirmationCaption(SingleTableFormSheet stfs, CatalogItem ci) {
105            return (stfs.getCaption() + " - Confirmation");
106        }
107    
108        /**
109         * Get the text for the default confirmation {@link MsgForm}.
110         *
111         * @param stfs the FormSheet that triggered the operation.
112         * @param ci the CatalogItem that is going to be removed.
113         *
114         * @override Sometimes The default implementation returns
115         * <code>("Are you sure, you want to remove \"" + ci.getName() + "\"?")</code>.
116         */
117        protected String getConfirmationText(SingleTableFormSheet stfs, CatalogItem ci) {
118            return ("Are you sure, you want to remove \"" + ci.getName() + "\"?");
119        }
120    
121        /**
122         * Get the transition that performs the actual removal. This basically calls {@link #doRemove}.
123         *
124         * @param stfs the FormSheet that triggered the operation.
125         *
126         * @override Never Instead, override {@link #doRemove}.
127         */
128        protected Transition getRemoveTransition(final SingleTableFormSheet stfs) {
129            return new Transition() {
130                            private static final long serialVersionUID = -6301707173621193648L;
131    
132                            public Gate perform(SaleProcess p, User u) {
133                    doRemove(p, (CatalogItem)stfs.getSelectedRecord(), p.getBasket());
134    
135                    return stfs.getGate();
136                }
137            };
138        }
139    
140        /**
141         * Perform the actual removal.
142         *
143         * <p>Any error condition should be passed on to {@link FormSheetStrategy#error} in
144         * {@link FormSheetStrategy}.</p>
145         *
146         * @override Sometimes
147         *
148         * @param p the process in which this sub-process will be implanted.
149         * @param ci the CatalogItem to be removed. Can be <code>null</code>.
150         * @param db the DataBasket relative to which to perform the operation.
151         */
152        protected void doRemove(SaleProcess p, CatalogItem ci, DataBasket db) {
153            if (ci != null) {
154                try {
155                    m_cCatalog.remove(ci, db);
156                }
157                catch (data.events.VetoException ve) {
158                    error(p, REMOVE_VETO_EXCEPTION);
159                }
160            }
161        }
162    }