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         * The Catalog that is being edited.
023         *
024         * @serial
025         */
026        protected Catalog m_cCatalog;
027    
028        /**
029         * Create a new DefaultRemoveCatalogItemStrategy.
030         *
031         * @param c the Catalog to be edited. Must be the same that is displayed by the {@link SingleTableFormSheet}.
032         */
033        public DefaultRemoveCatalogItemStrategy(Catalog c) {
034            super();
035    
036            m_cCatalog = c;
037        }
038    
039        /**
040         * @override Never
041         */
042        public Transition getEditProcess(SingleTableFormSheet stfs, SaleProcess p, SalesPoint sp) {
043            return new GateChangeTransition(getCheckGate(stfs));
044        }
045    
046        /**
047         * Get the Gate that checks whether the removal is allowed. If this is the case, the Gate must be left
048         * through {@link #getRemoveTransition}, otherwise, a transition to the
049         * {@link SingleTableFormSheet#getGate SingleTableFormSheet's gate} should be triggered.
050         *
051         * @override Sometimes The default implementation puts up a MsgForm asking for the user to confirm the
052         * removal. Caption and text of the message are obtained via {@link #getConfirmationCaption} and
053         * {@link #getConfirmationText}, resp.
054         *
055         * @param stfs the FormSheet that triggered the operation.
056         */
057        protected Gate getCheckGate(final SingleTableFormSheet stfs) {
058            CatalogItem ci = (CatalogItem)stfs.getSelectedRecord();
059            FormSheet fs = new MsgForm(getConfirmationCaption(stfs, ci), getConfirmationText(stfs, ci));
060    
061            final UIGate uigCheckGate = new UIGate(fs, null);
062    
063            fs.addContentCreator(new FormSheetContentCreator() {
064                protected void createFormSheetContent(FormSheet fs) {
065                    fs.removeAllButtons();
066    
067                    fs.addButton("Yes", FormSheet.BTNID_OK, new Action() {
068                        public void doAction(SaleProcess p, SalesPoint sp) {
069                            uigCheckGate.setNextTransition(getRemoveTransition(stfs));
070                        }
071                    });
072    
073                    fs.addButton("No", FormSheet.BTNID_CANCEL, new Action() {
074                        public void doAction(SaleProcess p, SalesPoint sp) {
075                            uigCheckGate.setNextTransition(new GateChangeTransition(stfs.getGate()));
076                        }
077                    });
078                }
079            });
080    
081            return uigCheckGate;
082        }
083    
084        /**
085         * Get the caption for the default confirmation {@link MsgForm}.
086         *
087         * @param stfs the FormSheet that triggered the operation.
088         * @param ci the CatalogItem that is going to be removed.
089         *
090         * @override Sometimes The default implementation returns
091         * <code>(stfs.getCaption() + " - Confirmation")</code>.
092         */
093        protected String getConfirmationCaption(SingleTableFormSheet stfs, CatalogItem ci) {
094            return (stfs.getCaption() + " - Confirmation");
095        }
096    
097        /**
098         * Get the text for the default confirmation {@link MsgForm}.
099         *
100         * @param stfs the FormSheet that triggered the operation.
101         * @param ci the CatalogItem that is going to be removed.
102         *
103         * @override Sometimes The default implementation returns
104         * <code>("Are you sure, you want to remove \"" + ci.getName() + "\"?")</code>.
105         */
106        protected String getConfirmationText(SingleTableFormSheet stfs, CatalogItem ci) {
107            return ("Are you sure, you want to remove \"" + ci.getName() + "\"?");
108        }
109    
110        /**
111         * Get the transition that performs the actual removal. This basically calls {@link #doRemove}.
112         *
113         * @param stfs the FormSheet that triggered the operation.
114         *
115         * @override Never Instead, override {@link #doRemove}.
116         */
117        protected Transition getRemoveTransition(final SingleTableFormSheet stfs) {
118            return new Transition() {
119                public Gate perform(SaleProcess p, User u) {
120                    doRemove(p, (CatalogItem)stfs.getSelectedRecord(), p.getBasket());
121    
122                    return stfs.getGate();
123                }
124            };
125        }
126    
127        /**
128         * Perform the actual removal.
129         *
130         * <p>Any error condition should be passed on to {@link FormSheetStrategy#error} in
131         * {@link FormSheetStrategy}.</p>
132         *
133         * @override Sometimes
134         *
135         * @param p the process in which this sub-process will be implanted.
136         * @param ci the CatalogItem to be removed. Can be <code>null</code>.
137         * @param db the DataBasket relative to which to perform the operation.
138         */
139        protected void doRemove(SaleProcess p, CatalogItem ci, DataBasket db) {
140            if (ci != null) {
141                try {
142                    m_cCatalog.remove(ci, db);
143                }
144                catch (data.events.VetoException ve) {
145                    error(p, REMOVE_VETO_EXCEPTION);
146                }
147            }
148        }
149    }