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.GridLayout;
009    import java.awt.event.ActionEvent;
010    import java.awt.event.ActionListener;
011    
012    import javax.swing.JButton;
013    import javax.swing.JOptionPane;
014    
015    import simuLCS.Config;
016    
017    /**
018     * Handles the Panels with the Control Buttons (start, stop, step,...)
019     * @author Benoit
020     * 
021     */
022    public class G_PanCustControls extends G_Panel {
023    
024            JButton btStart, btStop, btStep, btReset, btAddAgent;
025    
026            private String toAskForFile = "Please give the name (without extension) "
027            + "of the file used to save the data "
028            + "(for plotting):\n Let empty or click 'Cancel' if you don't want to save the " +
029                    "data.";
030            
031            public G_PanCustControls() {
032                    super();
033                    this.setLayout(new GridLayout(1, 5));
034                    btStart = new JButton();
035                    btStop = new JButton();
036                    btStop.setEnabled(false);
037                    btStep = new JButton();
038                    btReset = new JButton();
039                    btAddAgent = new JButton();
040                    //btReset.setEnabled(false);
041                    initButtons();
042    
043                    btStart.addActionListener(new ActionListener() {
044                            public void actionPerformed(ActionEvent eve) {
045    
046                                    String actionToDo = ((JButton) eve.getSource()).getText();
047    
048                                    if (actionToDo.equals("Start")) {
049                                            boolean willRun = true;
050                                            if (Config.getPRINT_MODE() > 5) {
051    
052                                                    Object[] options =
053                                                            {
054                                                                    "Yes, my computer is fast !",
055                                                                    "No, I will change the log parameter." };
056                                                    int n =
057                                                            JOptionPane.showOptionDialog(
058                                                                    pointerMainWindow,
059                                                                    "The log mode is too high and the program may crash.\n"
060                                                                            + "Are you sure you want to start the simulation ?",
061                                                                    "Program may crash !",
062                                                                    JOptionPane.YES_NO_OPTION,
063                                                                    JOptionPane.WARNING_MESSAGE,
064                                                                    null,
065                                                                    options,
066                                                                    options[1]);
067                                                    if (n != JOptionPane.YES_OPTION)
068                                                            willRun = false;
069    
070                                            }
071                                            if (willRun && getSimu().isNewRun())
072                                                    getSimu().setFileBaseForPlotting(
073                                                            JOptionPane.showInputDialog(
074                                                                    pointerMainWindow,
075                                                                    toAskForFile));
076                                            if (willRun) {
077                                                    btStart.setText("Pause");
078                                                    getSimu().start();
079                                                    btReset.setEnabled(false);
080                                                    btStep.setEnabled(false);
081                                                    btStop.setEnabled(true);
082                                            }
083                                    } else if (actionToDo.equals("Pause")) {
084                                            btStart.setText("Resume");
085                                            getSimu().stop();
086                                            btReset.setEnabled(true);
087                                            btStep.setEnabled(true);
088                                    } else if (actionToDo.equals("Resume")) {
089                                            btStart.setText("Pause");
090                                            boolean willRun = true;
091                                            if (Config.getPRINT_MODE() > 5) {
092    
093                                                    Object[] options =
094                                                            {
095                                                                    "Yes, my computer is fast !",
096                                                                    "No, I will change the log parameter." };
097                                                    int n =
098                                                            JOptionPane.showOptionDialog(
099                                                                    pointerMainWindow,
100                                                                    "The log mode is too high and the program may crash.\n"
101                                                                            + "Are you sure you want to start the simulation ?",
102                                                                    "Program may crash !",
103                                                                    JOptionPane.YES_NO_OPTION,
104                                                                    JOptionPane.WARNING_MESSAGE,
105                                                                    null,
106                                                                    options,
107                                                                    options[1]);
108                                                    if (n != JOptionPane.YES_OPTION)
109                                                            willRun = false;
110    
111                                            }
112    
113                                            if (willRun) {
114                                                    getSimu().start();
115                                                    btReset.setEnabled(false);
116                                                    btStep.setEnabled(false);
117                                                    btStop.setEnabled(true);
118                                            }
119                                    }
120    
121                            }
122                    });
123    
124                    btStop.addActionListener(new ActionListener() {
125                            public void actionPerformed(ActionEvent eve) {
126                                    getSimu().finish();
127                                    btReset.setEnabled(true);
128                                    btStep.setEnabled(false);
129                                    btStop.setEnabled(false);
130                                    btStart.setText("Start");
131                                    btStart.setEnabled(false);
132                            }
133                    });
134    
135                    btStep.addActionListener(new ActionListener() {
136                            public void actionPerformed(ActionEvent eve) {
137                                    if (getSimu().isNewRun())
138                                            getSimu().setFileBaseForPlotting(
139                                                    JOptionPane.showInputDialog(
140                                                            pointerMainWindow,
141                                                            toAskForFile));
142                                    getSimu().step();
143                            }
144                    });
145    
146                    btReset.addActionListener(new ActionListener() {
147                            public void actionPerformed(ActionEvent eve) {
148                                    initButtons();
149                                    getSimu().reset();
150                            }
151                    });
152    
153                    btAddAgent.addActionListener(new ActionListener() {
154                            public void actionPerformed(ActionEvent eve) {
155                                    getSimu().add();
156                            }
157                    });
158    
159                    this.add(btStart);
160                    this.add(btStop);
161                    this.add(btStep);
162                    this.add(btReset);
163                    //              this.add(btAddAgent);
164            }
165    
166            protected void initButtons() {
167                    btStart.setText("Start");
168                    btStart.setEnabled(true);
169                    btStop.setText("Finish");
170                    btStop.setEnabled(false);
171                    btStep.setText("Step");
172                    btStep.setEnabled(true);
173                    btReset.setText("Reset");
174                    btAddAgent.setText("+Ag.");
175            }
176    
177    }