001    /*
002     * SimuCS - Simulator to use with Classifier Systems 
003     * MSc project - Oxford University 
004     * by Benoit Isaac - Summer 2005
005     */
006    
007    package simuLCS.graphics;
008    import java.awt.BorderLayout;
009    import java.awt.GridLayout;
010    import java.awt.event.ActionEvent;
011    import java.awt.event.ActionListener;
012    import java.awt.event.WindowAdapter;
013    import java.awt.event.WindowEvent;
014    
015    import javax.swing.ButtonGroup;
016    import javax.swing.JFrame;
017    import javax.swing.JLabel;
018    import javax.swing.JMenu;
019    import javax.swing.JMenuBar;
020    import javax.swing.JMenuItem;
021    import javax.swing.JRadioButtonMenuItem;
022    import javax.swing.JSplitPane;
023    
024    import simuLCS.Config;
025    import simuLCS.Simulation;
026    import simuLCS.Utils;
027    
028    /**
029     * Main frame - creates the two major panels and the menu for logging.
030     * @author Benoit
031     * 
032     */
033    public class G_MainWindow extends JFrame {
034    
035            /* Utilities : record mouse coord,... */
036            private Utils utils;
037    
038            private static final String TITLE = "Simulator";
039            private static final String SB_LOADING = "Loading ...";
040            private static final String SB_READY = "Ready";
041    
042            // unique instance of the class
043            protected static G_MainWindow instance = null;
044            // Layout of the panel on the Left
045            protected BorderLayout layoutLeft = new BorderLayout();
046            // Split panels
047            protected JSplitPane splitPanel;
048            protected JSplitPane splitPanelLeft;
049    
050            // Left Panel for Customization
051            protected G_PanelCustom panCustom;
052            // Right Panel for the drawing
053            protected G_PanelDraw panDraw;
054    
055            // Status Bar
056            protected G_StatusBar statusBar = new G_StatusBar();
057            //Layout of the Status Bar
058            protected GridLayout layoutStatusBar = new GridLayout(1, 1);
059            //JLabel to print messages in the Status Bar
060            protected JLabel lbMsg = new JLabel(SB_LOADING);
061    
062            private Simulation pointerSimulation;
063    
064            /**
065             * The only way to obtain a unique instance (Singleton Pattern).
066             * @param s
067             * @param u
068             * @return
069             */
070            public static G_MainWindow getInstance(Simulation s, Utils u) {
071                    if (instance == null) {
072                            instance = new G_MainWindow(s, u);
073                    }
074                    return instance;
075            }
076    
077            /**
078             *  Protected Constructor to have only one instance (Singleton Pattern)
079             */
080            private G_MainWindow(Simulation s, Utils u) {
081                    // initialization of the variables
082                    utils = u;
083                    pointerSimulation = s;
084                    panCustom = new G_PanelCustom(this, s);
085    
086                    panDraw = new G_PanelDraw(this, statusBar);
087    
088                    splitPanelLeft =
089                            new JSplitPane(JSplitPane.VERTICAL_SPLIT, panCustom, statusBar);
090                    splitPanelLeft.setDividerLocation(600);
091                    splitPanelLeft.setTopComponent(panCustom);
092                    splitPanelLeft.setBottomComponent(statusBar);
093                    // construction du split panel total
094                    splitPanel =
095                            new JSplitPane(
096                                    JSplitPane.HORIZONTAL_SPLIT,
097                                    splitPanelLeft,
098                                    panDraw);
099                    splitPanel.setDividerLocation(350);
100                    //              System.out.println(panDraw);
101    
102            
103                    statusBar.setLayout(layoutStatusBar);
104                    statusBar.add(lbMsg);
105                    
106                    getContentPane().add(splitPanel, BorderLayout.CENTER);
107    
108                    initializeMenus();
109                    addWindowListener(new WindowAdapter() {
110                            public void windowClosing(WindowEvent e) {
111                                    closeApplication();
112                            }
113                    });
114                    pack();
115                    setTitle(TITLE);
116            }
117    
118            public G_PanelCustom getPanelCustom() {
119                    return panCustom;
120            }
121    
122            public G_PanelDraw getPanelDraw() {
123                    return panDraw;
124            }
125    
126            public G_StatusBar getStatusBar() {
127                    return statusBar;
128            }
129    
130            public Utils getUtils() {
131                    return utils;
132            }
133    
134            /**
135             * Link the window to the Data Model - the Simulation
136             * @param s
137             */
138            public void linkSimulation(Simulation s) {
139                    pointerSimulation = s;
140                    this.getPanelDraw().linkSimulation(s);
141                    G_Panel.linkSimulation(s);
142            }
143    
144            /**
145             * Creates the Log Menu
146             *
147             */
148            protected void initializeMenus() {
149                    JMenu menuFile = new JMenu("Log");
150    
151                    ButtonGroup log = new ButtonGroup();
152                    //              menu Start Logging
153                    JRadioButtonMenuItem startLog = new JRadioButtonMenuItem("High Logging");
154                    startLog.addActionListener(new ActionListener() {
155                            public void actionPerformed(ActionEvent eve) {
156                                    Config.setPRINT_MODE(10);
157                            }
158                    });
159                    menuFile.add(startLog);
160                    log.add(startLog);
161                    
162                    JRadioButtonMenuItem lowLog = new JRadioButtonMenuItem("Low Logging");
163                    lowLog.addActionListener(new ActionListener() {
164                            public void actionPerformed(ActionEvent eve) {
165                                    Config.setPRINT_MODE(5);
166                            }
167                    });
168                    menuFile.add(lowLog);
169                    log.add(lowLog);
170                    
171                    JRadioButtonMenuItem reportLog = new JRadioButtonMenuItem("Just reports");
172                    reportLog.addActionListener(new ActionListener() {
173                            public void actionPerformed(ActionEvent eve) {
174                                    Config.setPRINT_MODE(3);
175                            }
176                    });
177                    menuFile.add(reportLog);
178    //              reportLog.setSelected(true);
179                    log.add(reportLog);
180            
181                    
182                    JRadioButtonMenuItem stopLog = new JRadioButtonMenuItem("No Logging");
183                    stopLog.addActionListener(new ActionListener() {
184                            public void actionPerformed(ActionEvent eve) {
185                                    Config.setPRINT_MODE(0);
186                            }
187                    });
188                    menuFile.add(stopLog);
189                    stopLog.setSelected(true);
190                    log.add(stopLog);
191                    
192    
193    
194                    // menu quit
195                    JMenuItem quit = new JMenuItem("Quit");
196                    quit.addActionListener(new ActionListener() {
197                            public void actionPerformed(ActionEvent eve) {
198                                    closeApplication();
199                            }
200                    });
201    
202                    menuFile.addSeparator();
203                    menuFile.add(quit);
204                    JMenuBar mb = new JMenuBar();
205                    mb.add(menuFile);
206                    setJMenuBar(mb);
207            }
208            
209            /**
210             *  putting the Status Bar 
211             */
212            public void setStatusBar(String text) {
213                    lbMsg.setText(text);
214                    super.show();
215            }
216            
217            /**
218             * Showing the window
219             */
220            public void show() {
221                    lbMsg.setText(SB_READY);
222                    super.show();
223            }
224    
225            private void closeApplication() {
226                    System.out.println("Closing the simulator...");
227                    System.exit(0);
228            }
229    
230            /**
231             * Start the windows, linking all the windows.
232             * Can be used only after having created the windows.
233             */
234            public void launch() {
235                    // start the Graphics
236                    //Graphics2D g = (Graphics2D) getGraphics();
237                    getPanelDraw().launch();
238    
239                    getPanelCustom().linkPanelDraw(this.getPanelDraw());
240                    getPanelDraw().draw_simu();
241    
242            }
243    
244    }