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.Color;
010 import java.awt.FlowLayout;
011 import java.awt.Graphics;
012 import java.awt.GridLayout;
013 import java.awt.event.ActionEvent;
014 import java.awt.event.ActionListener;
015
016 import javax.swing.AbstractAction;
017 import javax.swing.BoxLayout;
018 import javax.swing.JButton;
019 import javax.swing.JCheckBox;
020 import javax.swing.JColorChooser;
021 import javax.swing.JLabel;
022 import javax.swing.JPanel;
023 import javax.swing.SwingConstants;
024
025 import simuLCS.Agent;
026 import simuLCS.AgentClassifierLearning;
027 import simuLCS.Simulation;
028
029 /**
030 * Handles the tab "Learning"
031 * @author Benoit
032 *
033 */
034 public class G_PanCustTabLearning extends G_Panel {
035
036 protected G_ListButtons panList;
037
038 protected G_ListClassifiers listRealBehaviour;
039 protected G_ListClassifiers listExpectedBehaviour;
040
041 private JButton btUpdate;
042 private JCheckBox cbLearning;
043 private JCheckBox cbPaintGhost;
044 private JCheckBox cbVisibilityCircle;
045
046 private boolean previousIsLearning = true;
047 private boolean previousIsGhostPainted = true;
048 private boolean previousIsWithVisibCircle = true;
049
050 public G_PanCustTabLearning() {
051 super();
052 this.setLayout(new BorderLayout());
053
054 // panList = new G_Panel();
055 // createListEntities(panList);
056 // panList.setLayout(new FlowLayout());
057
058 panList = new G_ListButtons(true, currentEntityToWatch, true);
059 panList.createListEntities();
060 panList.setLayout(new FlowLayout());
061
062
063
064 // JSplitPane splitPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT,panList,panAgentCustom);
065 // splitPanel.setDividerLocation(150);
066 // splitPanel.setDividerSize(1);
067 // this.add(splitPanel);
068
069 this.add(panList, BorderLayout.CENTER);
070
071 G_Panel panAgentWatch = createAgentWatch();
072 G_Panel p = new G_Panel();
073 p.setLayout(new BorderLayout());
074 p.add(createButtonsLearning(),BorderLayout.NORTH);
075 p.add(panAgentWatch,BorderLayout.CENTER);
076
077 this.add(p, BorderLayout.SOUTH);
078
079 }
080
081 private G_Panel createAgentWatch() {
082 G_Panel pan = new G_Panel();
083 // pan.setMinimumSize(new Dimension(200,300));
084 pan.setLayout(new BoxLayout(pan, BoxLayout.Y_AXIS));
085 // pan.setLayout(new FlowLayout());
086
087 JButton btColor = new JButton("Color circle");
088
089 btColor.addActionListener(new ActionListener() {
090 public void actionPerformed(ActionEvent eve) {
091 JColorChooser pColorChooser = new JColorChooser();
092
093 Color colorChosen =
094 JColorChooser.showDialog(
095 pColorChooser,
096 "Chooose the color of the circle around the agent:",
097 Color.BLUE);
098
099 Agent[] ag = new Agent[Simulation.getMaxNbEntities()];
100 currentEntityToWatch.toArray(ag);
101 if (ag.length != 0) {
102 for (int i = 0; i < currentEntityToWatch.size(); i++) {
103 ag[i].setColor_ext(colorChosen);
104 }
105 }
106 }
107 });
108
109 btUpdate = new JButton("Update Behaviour");
110
111 btUpdate.addActionListener(new ActionListener() {
112 public void actionPerformed(ActionEvent eve) {
113
114 Agent[] ag = new Agent[Simulation.getMaxNbEntities()];
115 currentEntityToWatch.toArray(ag);
116 System.out.println(
117 "Click update:" + currentEntityToWatch + " ag[0]" + ag[0]);
118 if (ag.length != 0
119 && ag[0].getBehaviour(
120 AgentClassifierLearning.EXPECTED_BEHAVIOUR)
121 != null) {
122 System.out.println("Thread will be launched");
123 class UpdateThread implements Runnable {
124 AgentClassifierLearning agent;
125 protected UpdateThread(AgentClassifierLearning a) {
126 agent = a;
127 }
128 public void run() {
129 System.out.println("[Thread Update] " + agent);
130 agent.requestUpdateBehaviourToShow(
131 getSimu().isSimuRunning(),
132 btUpdate);
133 //now the update can be done again
134
135 // btUpdate.setText("Update Behaviour");
136 // btUpdate.setEnabled(true);
137 }
138 }
139 Thread update =
140 new Thread(
141 new UpdateThread((AgentClassifierLearning) ag[0]));
142 // UpdateThread update = new UpdateThread((AgentClassifierLearning) ag[0]);
143 // forbids to launch several updates
144 btUpdate.setText("Updating...Wait");
145 btUpdate.setEnabled(false);
146 update.start();
147 // update.run();
148 }
149
150 }
151 });
152
153
154
155 listExpectedBehaviour =
156 new G_ListClassifiers(
157 currentEntityToWatch,
158 10,
159 AgentClassifierLearning.EXPECTED_BEHAVIOUR_TO_SHOW,
160 false,
161 null,
162 true);
163
164 listRealBehaviour =
165 new G_ListClassifiers(
166 currentEntityToWatch,
167 5,
168 AgentClassifierLearning.REAL_BEHAVIOUR,
169 true,
170 listExpectedBehaviour,
171 false);
172
173 JLabel j = new JLabel("Real Behaviour:", SwingConstants.LEFT);
174
175 JLabel j2 = new JLabel("Expected Behaviour:", SwingConstants.LEFT);
176 JPanel jUpdate = new JPanel(new FlowLayout());
177 jUpdate.add(j2);
178 jUpdate.add(btUpdate);
179
180 pan.add(j);
181 pan.add(listRealBehaviour);
182 // pan.add(scrollpanClassifier);
183 // pan.add(c2.getUICondition(a));
184 pan.add(jUpdate);
185 pan.add(listExpectedBehaviour);
186 pan.add(btColor);
187 return pan;
188
189 }
190
191 private G_Panel createButtonsLearning() {
192 G_Panel p = new G_Panel();
193
194 class ActCheckbox extends AbstractAction {
195 public void actionPerformed(ActionEvent evt) {
196 setFlags(evt);
197 }
198 }
199 cbLearning = new JCheckBox(new ActCheckbox());
200 cbLearning.setText("Learning is active ? (if no, no ghost)");
201 cbPaintGhost = new JCheckBox(new ActCheckbox());
202 cbPaintGhost.setText("Ghost is painted?");
203 cbVisibilityCircle = new JCheckBox(new ActCheckbox());
204 cbVisibilityCircle.setText("With a Visibility Circle?");
205
206
207 updateCheckbox();
208
209 p.setLayout(new GridLayout(3,1));
210 p.add(cbLearning);
211 p.add(cbPaintGhost);
212 p.add(cbVisibilityCircle);
213
214
215 return p;
216 }
217
218 /**
219 * Update the checkboxes from the properties of the currently watched
220 * AgentClassifierLearning
221 *
222 */
223 private void updateCheckbox() {
224 if (getCurrentEntityToWatch() instanceof AgentClassifierLearning) {
225 AgentClassifierLearning acl =
226 (AgentClassifierLearning) getCurrentEntityToWatch();
227 cbLearning.setSelected(acl.isLearning());
228 cbPaintGhost.setSelected(acl.isGhostPainted());
229 cbVisibilityCircle.setSelected(acl.isDetectionLimited());
230 previousIsLearning = acl.isLearning();
231 previousIsGhostPainted = acl.isGhostPainted();
232 previousIsWithVisibCircle = acl.isDetectionLimited();
233 }
234
235 }
236
237 /**
238 * Updates several properties of the ACL, depending on the clicks
239 * of the user on the checkboxes
240 * @param e
241 */
242 private void setFlags(ActionEvent e) {
243 boolean hasChanged = false;
244 if (getCurrentEntityToWatch() instanceof AgentClassifierLearning) {
245 AgentClassifierLearning acl =
246 (AgentClassifierLearning) getCurrentEntityToWatch();
247 if (cbLearning.isSelected() != previousIsLearning) {
248 acl.setLearning(cbLearning.isSelected());
249 previousIsLearning = cbLearning.isSelected();
250 hasChanged = true;
251 }
252 if (cbPaintGhost.isSelected() != previousIsGhostPainted) {
253 acl.setGhostPainted(cbPaintGhost.isSelected());
254 previousIsGhostPainted = cbPaintGhost.isSelected();
255 hasChanged = true;
256 }
257 if(cbVisibilityCircle.isSelected() != previousIsWithVisibCircle){
258 acl.setDetectionLimited(cbVisibilityCircle.isSelected());
259 previousIsWithVisibCircle = cbVisibilityCircle.isSelected();
260 hasChanged = true;
261 }
262
263 }
264 if (hasChanged)
265 {
266 System.out.println("Flags changed : Learning["+cbLearning.isSelected()
267 +"] PaintGhost["+cbPaintGhost.isSelected()+"] Visibility Circle["
268 +cbVisibilityCircle+"] ");
269 repaintAll();
270 }
271
272 }
273
274 public void paintComponent(Graphics g) {
275 panList.createListEntities();
276 updateCheckbox();
277 listRealBehaviour.updateValues();
278 listExpectedBehaviour.updateValues();
279 }
280
281 }