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.event.ActionEvent;
009 import java.util.Iterator;
010 import java.util.Set;
011
012 import javax.swing.AbstractAction;
013 import javax.swing.ButtonGroup;
014 import javax.swing.JCheckBox;
015 import javax.swing.JRadioButton;
016 import javax.swing.JToggleButton;
017
018 import simuLCS.AgentClassifierLearning;
019 import simuLCS.Entity;
020
021 /**
022 * Handles the list of checkboxes or radiobuttons used to select the entities
023 * @author Benoit
024 *
025 */
026 public class G_ListButtons extends G_Panel {
027
028 /**
029 * true if RadioButtons should be used (ie. only one entity can be selected)
030 */
031 private boolean isRadioButtons;
032
033 /**
034 * The Set to update when the user clicks on the buttons,
035 * and to listen when drawing the buttons (to know which ones are selected)
036 */
037 private Set linkedWith;
038
039 /**
040 * true if we want to have buttons only for the AgentClassifierLearning
041 */
042 private boolean isShowingOnlyAgentLearning = false ;
043
044 /**
045 * used to record the last action of the user
046 */
047 private Entity lastClicked = null;
048
049 public G_ListButtons(boolean isRadio, Set toLinkWith) {
050 super();
051 isRadioButtons = isRadio;
052 linkedWith = toLinkWith;
053
054 // this.setPreferredSize(new Dimension(200,100));
055 // this.setMinimumSize(new Dimension(400,300));
056 // createListEntities();
057 // this.setLayout(new FlowLayout());
058
059 }
060
061 public G_ListButtons(boolean isRadio, Set toLinkWith, boolean onlyLearning)
062 {
063 this(isRadio,toLinkWith);
064 isShowingOnlyAgentLearning = onlyLearning;
065 }
066
067 /**
068 * Generates the list of buttions
069 *
070 */
071 public void createListEntities() {
072 removeAll();
073
074 Entity curEntity;
075 ButtonGroup bg = new ButtonGroup();
076
077 //System.out.println(pointerSimulation);
078 Iterator iterator = getSimu().getEntities();
079 while(iterator.hasNext()) {
080 curEntity = (Entity) iterator.next();
081 //System.out.println(curAg.getName());
082
083 if (!isShowingOnlyAgentLearning ||
084 (isShowingOnlyAgentLearning &&
085 curEntity instanceof AgentClassifierLearning)) {
086 // TODO : create a class for this ?
087 class ActSelect extends AbstractAction {
088 private Entity en;
089 public ActSelect(Entity e) {
090 super(e.getName());
091 en = e;
092 }
093
094 public void actionPerformed(ActionEvent evt) {
095 // Perform action
096 JToggleButton cb;
097 if (isRadioButtons) {
098 cb = (JRadioButton) evt.getSource();
099 } else {
100 cb = (JCheckBox) evt.getSource();
101 }
102
103 // Determine status
104 boolean isSel = cb.isSelected();
105 System.out.println(currentEntityToWatch);
106 if (isSel) {
107 // The checkbox is selected
108 // System.out.println("Click ! L:"+lastClicked+" N:"+ag);
109 if (isRadioButtons) // only one at a time
110 linkedWith.clear();
111
112 linkedWith.add(en);
113 lastClicked = en;
114 // System.out.println("Clicked ! I call repaint");
115 repaintCustom();
116
117
118 //System.out.println(selectedEntities.toString());
119 } else {
120 // The checkbox is now deselected
121 // System.out.println("DeClick ! L:"+lastClicked+" N:"+ag);
122 linkedWith.remove(en);
123 lastClicked = en;
124 // System.out.println("UN clicked ! I call repaint");
125 repaintCustom();
126
127 }
128 }
129
130 }
131
132 ActSelect action = new ActSelect(curEntity);
133
134 JToggleButton j;
135 if (isRadioButtons) {
136 j = new JRadioButton(action);
137 } else {
138 j = new JCheckBox(action);
139 }
140
141 if (linkedWith.contains(curEntity))
142 j.setSelected(true);
143 if (isRadioButtons) {
144 bg.add(j);
145 }
146
147 this.add(j);
148 }
149
150 }
151 // if(isRadioButtons)
152 // this.add(bg);
153 validate();
154
155 }
156
157
158
159 }