001    package market;
002    
003    import java.awt.event.WindowAdapter;
004    import java.awt.event.WindowEvent;
005    import java.awt.event.WindowListener;
006    
007    import sale.FormSheet;
008    import sale.FormSheetContentCreator;
009    import sale.JDisplayDialog;
010    import sale.JDisplayFrame;
011    import sale.MenuSheet;
012    import sale.SaleProcess;
013    import sale.SalesPoint;
014    import sale.stdforms.MsgForm;
015    
016    /**
017     * Provides a {@link JDisplayDialog} that shows a simple {@link MsgForm}.
018     */
019    public class JDDShowMessage {
020    
021        /**
022         * Creates a JDisplayDialog that shows a short message.
023         *
024         * @param message the message to be shown.
025         * @param caption the caption of the MsgForm.
026         */
027        public static JDisplayDialog showMessageDialog(String message, String caption) {
028            final JDisplayDialog jdd = new JDisplayDialog(SMarket.getTheMarket().getShopFrame());
029            final FormSheet fs = new MsgForm(caption, message, false);
030            fs.addContentCreator(new FormSheetContentCreator(){
031                protected void createFormSheetContent(final FormSheet fs) {
032                    fs.getButton(FormSheet.BTNID_OK).setAction(new sale.Action(){
033                        public void doAction(SaleProcess p, SalesPoint sp) throws Throwable {
034                            fs.cancel();
035                        }
036                    });
037                }
038            });
039            try {
040                jdd.popUpFormSheet(fs);
041            } catch (InterruptedException e) {
042                System.err.println(e.getMessage());
043            }
044            return jdd;
045        }
046    
047        /**
048         * Creates a JDisplayDialog that shows a short message, the JDisplayDialog will pop up
049         * in the center of the given FormSheet.
050         * The SalesPoint of the given FormSheet will be suspended while the message is shown.
051         *
052         * @param parent FormSheet to which the position of the JDisplayDialog is set relative to.
053         * @param message the message to be shown.
054         * @param caption the caption of the MsgForm.
055         */
056        public static JDisplayDialog showMessageDialog(final FormSheet parent, String message, String caption){
057            final SalesPoint sp = parent.getSalesPoint();
058            final MenuSheet ms = sp.getDisplay().getMenuSheet();
059            final JDisplayFrame jdf = (sale.JDisplayFrame)parent.getSalesPoint().getDisplay();
060            final JDisplayDialog jdd = new JDisplayDialog(jdf);
061            final FormSheet fs = new MsgForm(caption, message, false);
062            //WindowListener that causes to give the dialog the focus whenever the appropriate
063            //FormSheet got it.
064            final WindowListener wl = new WindowAdapter() {
065                    public void windowActivated(WindowEvent e) {
066                        jdd.setLocationRelativeTo(parent.getComponent());
067                        jdd.toFront();
068                    }
069                };
070            //WindowListener that causes wl to be removed and the process to resume when jdd is closed.
071            jdd.addWindowListener(new WindowAdapter(){
072                public void windowClosed(WindowEvent e) {
073                    jdf.removeWindowListener(wl);
074                    parent.attach(sp);
075                    if (ms != null) {
076                        ms.attach(sp);
077                    }
078                    if (parent.getProcess() != null) {
079                        parent.getProcess().resume();
080                    }
081                }
082            });
083            fs.addContentCreator(new FormSheetContentCreator(){
084                protected void createFormSheetContent(final FormSheet fs) {
085                    fs.getButton(FormSheet.BTNID_OK).setAction(new sale.Action(){
086                        public void doAction(SaleProcess p, SalesPoint sp) throws Throwable {
087                            fs.cancel();
088                        }
089                    });
090                }
091            });
092            try {
093                jdd.popUpFormSheet(parent, fs);
094                jdf.addWindowListener(wl);
095                if (parent.getProcess() != null) {
096                    parent.getProcess().suspend();  //stop current SaleProcess
097                }
098                if (ms != null) {
099                    ms.detachSalesPoint();          //remove MenuSheet
100                }
101                parent.detachSalesPoint();          //remove FormSheet
102            } catch (InterruptedException e) {
103                System.err.println(e.getMessage());
104            }
105            return jdd;
106        }
107    }