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.Dimension;
011 import java.awt.FlowLayout;
012 import java.awt.Graphics;
013 import java.awt.event.ActionEvent;
014 import java.awt.event.ActionListener;
015 import java.util.Iterator;
016
017 import javax.swing.AbstractAction;
018 import javax.swing.BoxLayout;
019 import javax.swing.JButton;
020 import javax.swing.JCheckBox;
021 import javax.swing.JColorChooser;
022 import javax.swing.JLabel;
023 import javax.swing.JOptionPane;
024 import javax.swing.JPanel;
025 import javax.swing.SwingConstants;
026
027 import simuLCS.ClassifierComponent;
028 import simuLCS.Entity;
029
030 /**
031 * Handles the Tab Agent
032 * @author Benoit
033 *
034 */
035 public class G_PanCustTabAgents extends G_Panel {
036
037 // protected G_Panel panList;
038
039 protected G_ListButtons panList;
040
041 public G_PanCustTabAgents() {
042 super();
043 this.setLayout(new BorderLayout());
044
045 // panList = new G_Panel();
046 // createListEntities(panList);
047 // panList.setLayout(new FlowLayout());
048
049 panList = new G_ListButtons(false, selectedEntities);
050 panList.createListEntities();
051 panList.setLayout(new FlowLayout());
052
053 G_Panel panAgentCustom = createAgentCustomButtons();
054 panAgentCustom.setPreferredSize(new Dimension(200, 400));
055 panAgentCustom.setMinimumSize(new Dimension(200, 400));
056
057 // JSplitPane splitPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT,panList,panAgentCustom);
058 // splitPanel.setDividerLocation(150);
059 // splitPanel.setDividerSize(1);
060 // this.add(splitPanel);
061 this.add(panList, BorderLayout.CENTER);
062 this.add(panAgentCustom, BorderLayout.SOUTH);
063
064 }
065
066 protected JButton getBtAdd() {
067 JButton btAddAgent = new JButton("Add Agent");
068 btAddAgent.addActionListener(new ActionListener() {
069 public void actionPerformed(ActionEvent eve) {
070 getSimu().addWithChoice();
071 }
072 });
073 return btAddAgent;
074 }
075
076 protected JButton getBtDel() {
077 JButton btDelAgent = new JButton("Delete agent(s)");
078 btDelAgent.addActionListener(new ActionListener() {
079 public void actionPerformed(ActionEvent eve) {
080 Object[] options = { "Yes", "No, cancel this action." };
081 int n =
082 JOptionPane.showOptionDialog(
083 pointerMainWindow,
084 "Are you sure you want to delete all the selected entities ?"
085 + "This action cannot be undone.",
086 "Confirmation",
087 JOptionPane.YES_NO_OPTION,
088 JOptionPane.WARNING_MESSAGE,
089 null,
090 options,
091 options[1]);
092 if (n == JOptionPane.YES_OPTION) {
093 Iterator iter = getSetSelectedEntities().iterator();
094 while (iter.hasNext()) {
095 getSimu().remove((Entity) iter.next());
096 }
097 selectedEntities.clear();
098 repaintAll();
099 }
100 }
101 });
102 return btDelAgent;
103 }
104
105 /**
106 * Creates the Buttons to Customize the selected Entities
107 * @return
108 */
109 public G_Panel createAgentCustomButtons() {
110 // Box pan = new Box(BoxLayout.Y_AXIS);
111 // Box pan = Box.createVerticalBox();
112 G_Panel pan = new G_Panel();
113
114 // pan.setMinimumSize(new Dimension(200,300));
115 // pan.setLayout(new BoxLayout(pan, BoxLayout.Y_AXIS));
116
117 // GridBagLayout g = new GridBagLayout();
118 // GridBagConstraints ct = new GridBagConstraints();
119 // pan.setLayout(g);
120 // ct.fill = GridBagConstraints.BOTH;
121 // ct.weightx = 1.0;
122
123 pan.setLayout(new BoxLayout(pan, BoxLayout.Y_AXIS));
124
125 JButton btColor = new JButton("Change their Color");
126
127 btColor.addActionListener(new ActionListener() {
128 public void actionPerformed(ActionEvent eve) {
129 JColorChooser pColorChooser = new JColorChooser();
130
131 Color colorChosen =
132 JColorChooser.showDialog(
133 pColorChooser,
134 "Chooose the color of the agent(s):",
135 Color.BLUE);
136
137 Entity[] en = getArraySelectedEntities();
138 if (en.length != 0) {
139 for (int i = 0; i < en.length; i++) {
140 en[i].setColor_int(colorChosen);
141 }
142 }
143 }
144 });
145
146 ClassifierComponent c =
147 new ClassifierComponent("Paint their Name ?", true);
148
149 class ActPaintName extends AbstractAction {
150
151 public void actionPerformed(ActionEvent evt) {
152 // Perform action
153 JCheckBox cb = (JCheckBox) evt.getSource();
154
155 // Determine status
156 boolean isSel = cb.isSelected();
157 if (isSel) {
158 // The checkbox is selected
159 Entity[] en = getArraySelectedEntities();
160 if (en.length != 0) {
161 for (int i = 0; i < en.length; i++) {
162 en[i].setPaintName(true);
163 }
164 }
165 //System.out.println(selectedEntities.toString());
166 } else {
167 // The checkbox is now deselected
168 Entity[] en = getArraySelectedEntities();
169 if (en.length != 0) {
170 for (int i = 0; i < en.length; i++) {
171 en[i].setPaintName(false);
172 }
173 }
174 }
175 }
176
177 }
178 ActPaintName a = new ActPaintName();
179
180 JLabel j =
181 new JLabel(
182 "Changing the properties of the selected agents:",
183 SwingConstants.LEFT);
184
185 G_Panel panButtons = new G_Panel();
186 panButtons.setLayout(new FlowLayout());
187 panButtons.add(getBtAdd());
188 panButtons.add(getBtDel());
189
190 JPanel emptyPan = new JPanel();
191 emptyPan.setMinimumSize(new Dimension(panButtons.getWidth(), 50));
192 emptyPan.setPreferredSize(new Dimension(panButtons.getWidth(), 50));
193 // ct.gridwidth = GridBagConstraints.REMAINDER;
194 // g.setConstraints(panButtons,ct);
195 pan.add(panButtons);
196 // g.setConstraints(j,ct);
197 pan.add(emptyPan);
198 pan.add(j);
199 G_Panel btPaintName = c.getUICondition(a);
200 // g.setConstraints(btPaintName,ct);
201 pan.add(btPaintName);
202 // g.setConstraints(btColor,ct);
203 pan.add(btColor);
204
205 // ct.gridheight = 15; //GridBagConstraints.REMAINDER;
206 // g.setConstraints(emptyPan,ct);
207
208 G_Panel panTotal = new G_Panel();
209 panTotal.setLayout(new BorderLayout());
210 panTotal.add(pan, BorderLayout.NORTH);
211
212 return panTotal;
213
214 }
215
216 /**
217 * @return
218 */
219 public G_Panel getPanList() {
220 return panList;
221 }
222
223 public void paintComponent(Graphics g) {
224 panList.createListEntities();
225 }
226
227 }