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                            private static final long serialVersionUID = 442251504819698301L;
032                            protected void createFormSheetContent(final FormSheet fs) {
033                    fs.getButton(FormSheet.BTNID_OK).setAction(new sale.Action(){
034                                            private static final long serialVersionUID = -8951634680505770210L;
035                                            public void doAction(SaleProcess p, SalesPoint sp) throws Throwable {
036                            fs.cancel();
037                        }
038                    });
039                }
040            });
041            try {
042                jdd.popUpFormSheet(fs);
043            } catch (InterruptedException e) {
044                System.err.println(e.getMessage());
045            }
046            return jdd;
047        }
048    
049        /**
050         * Creates a JDisplayDialog that shows a short message, the JDisplayDialog will pop up
051         * in the center of the given FormSheet.
052         * The SalesPoint of the given FormSheet will be suspended while the message is shown.
053         *
054         * @param parent FormSheet to which the position of the JDisplayDialog is set relative to.
055         * @param message the message to be shown.
056         * @param caption the caption of the MsgForm.
057         */
058        public static JDisplayDialog showMessageDialog(final FormSheet parent, String message, String caption){
059            final SalesPoint sp = parent.getSalesPoint();
060            final MenuSheet ms = sp.getDisplay().getMenuSheet();
061            final JDisplayFrame jdf = (sale.JDisplayFrame)parent.getSalesPoint().getDisplay();
062            final JDisplayDialog jdd = new JDisplayDialog(jdf);
063            final FormSheet fs = new MsgForm(caption, message, false);
064            //WindowListener that causes to give the dialog the focus whenever the appropriate
065            //FormSheet got it.
066            final WindowListener wl = new WindowAdapter() {
067                    public void windowActivated(WindowEvent e) {
068                        jdd.setLocationRelativeTo(parent.getComponent());
069                        jdd.toFront();
070                    }
071                };
072            //WindowListener that causes wl to be removed and the process to resume when jdd is closed.
073            jdd.addWindowListener(new WindowAdapter(){
074                public void windowClosed(WindowEvent e) {
075                    jdf.removeWindowListener(wl);
076                    parent.attach(sp);
077                    if (ms != null) {
078                        ms.attach(sp);
079                    }
080                    if (parent.getProcess() != null) {
081                        parent.getProcess().resume();
082                    }
083                }
084            });
085            fs.addContentCreator(new FormSheetContentCreator(){
086                            private static final long serialVersionUID = -2496346947105424566L;
087                            protected void createFormSheetContent(final FormSheet fs) {
088                    fs.getButton(FormSheet.BTNID_OK).setAction(new sale.Action(){
089                                            private static final long serialVersionUID = 5005873514686820350L;
090                                            public void doAction(SaleProcess p, SalesPoint sp) throws Throwable {
091                            fs.cancel();
092                        }
093                    });
094                }
095            });
096            try {
097                jdd.popUpFormSheet(parent, fs);
098                jdf.addWindowListener(wl);
099                if (parent.getProcess() != null) {
100                    parent.getProcess().suspend();  //stop current SaleProcess
101                }
102                if (ms != null) {
103                    ms.detachSalesPoint();          //remove MenuSheet
104                }
105                parent.detachSalesPoint();          //remove FormSheet
106            } catch (InterruptedException e) {
107                System.err.println(e.getMessage());
108            }
109            return jdd;
110        }
111    }