1 | package genius.gui.boaframework;
|
---|
2 |
|
---|
3 | import java.awt.Frame;
|
---|
4 | import java.awt.event.ActionEvent;
|
---|
5 | import java.awt.event.ActionListener;
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.IOException;
|
---|
8 |
|
---|
9 | import javax.swing.JButton;
|
---|
10 | import javax.swing.JDialog;
|
---|
11 | import javax.swing.JFileChooser;
|
---|
12 | import javax.swing.JLabel;
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 | import javax.swing.JSeparator;
|
---|
15 | import javax.swing.JTextField;
|
---|
16 | import javax.swing.filechooser.FileFilter;
|
---|
17 |
|
---|
18 | import genius.core.Global;
|
---|
19 | import genius.core.boaframework.AcceptanceStrategy;
|
---|
20 | import genius.core.boaframework.BoaType;
|
---|
21 | import genius.core.boaframework.OMStrategy;
|
---|
22 | import genius.core.boaframework.OfferingStrategy;
|
---|
23 | import genius.core.boaframework.OpponentModel;
|
---|
24 | import genius.core.boaframework.repository.BOAagentRepository;
|
---|
25 | import genius.core.boaframework.repository.BOArepItem;
|
---|
26 | import genius.gui.panels.GenericFileFilter;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * GUI to (add or edit?) a BOA component to the BOA repository.
|
---|
30 | *
|
---|
31 | *
|
---|
32 | * @author Mark Hendrikx
|
---|
33 | * @author W.Pasman 24mar'14 #876
|
---|
34 | */
|
---|
35 | public class BOAComponentEditor extends JDialog {
|
---|
36 |
|
---|
37 | private static final long serialVersionUID = -7204112461104285605L;
|
---|
38 | private JLabel componentNameLabel;
|
---|
39 | private JTextField componentNameTextField;
|
---|
40 | private JLabel componentClassLabel;
|
---|
41 | private JTextField componentClassTextField;
|
---|
42 | private JSeparator lowerSeparator;
|
---|
43 | private JButton addComponent;
|
---|
44 | private JButton openButton;
|
---|
45 | private JSeparator upperSeparator;
|
---|
46 | private BOArepItem result = null;
|
---|
47 | private BoaType type;
|
---|
48 |
|
---|
49 | public BOAComponentEditor(Frame frame, String title) {
|
---|
50 | super(frame, title, true);
|
---|
51 | this.setLocation(frame.getLocation().x + frame.getWidth() / 2,
|
---|
52 | frame.getLocation().y + frame.getHeight() / 4);
|
---|
53 | this.setSize(frame.getSize().width / 3, frame.getSize().height / 2);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public BOArepItem getResult(BOArepItem item) {
|
---|
57 | componentNameLabel = new JLabel("Component name");
|
---|
58 | componentNameTextField = new JTextField();
|
---|
59 |
|
---|
60 | componentClassLabel = new JLabel("Component class");
|
---|
61 | componentClassTextField = new javax.swing.JTextField();
|
---|
62 | componentClassTextField.setEditable(false);
|
---|
63 |
|
---|
64 | addComponent = new JButton("Add component");
|
---|
65 | addComponent.addActionListener(new ActionListener() {
|
---|
66 | public void actionPerformed(ActionEvent e) {
|
---|
67 | addComponent();
|
---|
68 | }
|
---|
69 | });
|
---|
70 |
|
---|
71 | openButton = new javax.swing.JButton("Open");
|
---|
72 | openButton.addActionListener(new ActionListener() {
|
---|
73 | public void actionPerformed(ActionEvent e) {
|
---|
74 | openAction();
|
---|
75 | }
|
---|
76 | });
|
---|
77 |
|
---|
78 | upperSeparator = new javax.swing.JSeparator();
|
---|
79 | lowerSeparator = new javax.swing.JSeparator();
|
---|
80 | setResizable(false);
|
---|
81 |
|
---|
82 | javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
|
---|
83 | getContentPane());
|
---|
84 | getContentPane().setLayout(layout);
|
---|
85 | layout.setHorizontalGroup(layout
|
---|
86 | .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
---|
87 | .addComponent(upperSeparator)
|
---|
88 | .addGroup(
|
---|
89 | layout.createSequentialGroup()
|
---|
90 | .addGap(12, 12, 12)
|
---|
91 | .addGroup(
|
---|
92 | layout.createParallelGroup(
|
---|
93 | javax.swing.GroupLayout.Alignment.LEADING)
|
---|
94 | .addGroup(
|
---|
95 | layout.createSequentialGroup()
|
---|
96 | .addComponent(
|
---|
97 | componentNameLabel)
|
---|
98 | .addGap(14, 14,
|
---|
99 | 14)
|
---|
100 | .addComponent(
|
---|
101 | componentNameTextField))
|
---|
102 | .addGroup(
|
---|
103 | layout.createSequentialGroup()
|
---|
104 | .addComponent(
|
---|
105 | componentClassLabel)
|
---|
106 | .addGap(18, 18,
|
---|
107 | 18)
|
---|
108 | .addComponent(
|
---|
109 | componentClassTextField)
|
---|
110 | .addPreferredGap(
|
---|
111 | javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
---|
112 | .addComponent(
|
---|
113 | openButton))
|
---|
114 | .addGroup(
|
---|
115 | layout.createSequentialGroup()
|
---|
116 | .addGroup(
|
---|
117 | layout.createParallelGroup(
|
---|
118 | javax.swing.GroupLayout.Alignment.LEADING,
|
---|
119 | false)
|
---|
120 |
|
---|
121 | .addGroup(
|
---|
122 | layout.createSequentialGroup()
|
---|
123 | .addComponent(
|
---|
124 | addComponent)
|
---|
125 | .addPreferredGap(
|
---|
126 | javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
|
---|
127 | .addGap(0,
|
---|
128 | 0,
|
---|
129 | Short.MAX_VALUE)))
|
---|
130 | .addContainerGap())
|
---|
131 | .addComponent(lowerSeparator));
|
---|
132 | layout.setVerticalGroup(layout
|
---|
133 | .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
---|
134 | .addGroup(
|
---|
135 | layout.createSequentialGroup()
|
---|
136 | .addContainerGap()
|
---|
137 | .addGroup(
|
---|
138 | layout.createParallelGroup(
|
---|
139 | javax.swing.GroupLayout.Alignment.BASELINE)
|
---|
140 | .addComponent(
|
---|
141 | componentNameLabel)
|
---|
142 | .addComponent(
|
---|
143 | componentNameTextField,
|
---|
144 | javax.swing.GroupLayout.PREFERRED_SIZE,
|
---|
145 | javax.swing.GroupLayout.DEFAULT_SIZE,
|
---|
146 | javax.swing.GroupLayout.PREFERRED_SIZE))
|
---|
147 | .addPreferredGap(
|
---|
148 | javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
---|
149 |
|
---|
150 | .addGroup(
|
---|
151 | layout.createParallelGroup(
|
---|
152 | javax.swing.GroupLayout.Alignment.BASELINE)
|
---|
153 | .addComponent(
|
---|
154 | componentClassLabel)
|
---|
155 | .addComponent(
|
---|
156 | componentClassTextField,
|
---|
157 | javax.swing.GroupLayout.PREFERRED_SIZE,
|
---|
158 | javax.swing.GroupLayout.DEFAULT_SIZE,
|
---|
159 | javax.swing.GroupLayout.PREFERRED_SIZE)
|
---|
160 | .addComponent(openButton))
|
---|
161 | .addPreferredGap(
|
---|
162 | javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
---|
163 | .addComponent(upperSeparator,
|
---|
164 | javax.swing.GroupLayout.PREFERRED_SIZE,
|
---|
165 | 10,
|
---|
166 | javax.swing.GroupLayout.PREFERRED_SIZE)
|
---|
167 | .addPreferredGap(
|
---|
168 | javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
---|
169 | .addComponent(lowerSeparator,
|
---|
170 | javax.swing.GroupLayout.PREFERRED_SIZE,
|
---|
171 | 10,
|
---|
172 | javax.swing.GroupLayout.PREFERRED_SIZE)
|
---|
173 | .addPreferredGap(
|
---|
174 | javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
---|
175 | .addGroup(
|
---|
176 | layout.createParallelGroup(
|
---|
177 | javax.swing.GroupLayout.Alignment.LEADING)
|
---|
178 | .addComponent(addComponent))
|
---|
179 | .addContainerGap(
|
---|
180 | javax.swing.GroupLayout.DEFAULT_SIZE,
|
---|
181 | Short.MAX_VALUE)));
|
---|
182 |
|
---|
183 | if (item != null) {
|
---|
184 | componentNameTextField.setText(item.getName());
|
---|
185 | componentClassTextField.setText(item.getClassPath());
|
---|
186 | type = item.getType();
|
---|
187 | // for (BOAparameter param : item.getParameters()) {
|
---|
188 | // parameterListModel.addElement(param);
|
---|
189 | // }
|
---|
190 | addComponent.setText("Save");
|
---|
191 | }
|
---|
192 |
|
---|
193 | pack();
|
---|
194 | setVisible(true);
|
---|
195 | return result;
|
---|
196 | }
|
---|
197 |
|
---|
198 | private void addComponent() {
|
---|
199 | boolean valid = true;
|
---|
200 | if (componentNameTextField.getText().length() == 0
|
---|
201 | || componentNameTextField.getText().length() > 35) {
|
---|
202 | valid = false;
|
---|
203 | JOptionPane
|
---|
204 | .showMessageDialog(
|
---|
205 | null,
|
---|
206 | "Component name should be non-empty and at most 35 characters.",
|
---|
207 | "Invalid parameter input", 0);
|
---|
208 | }
|
---|
209 | if (componentClassTextField.getText().length() == 0) {
|
---|
210 | valid = false;
|
---|
211 | JOptionPane.showMessageDialog(null, "Please specify a class.",
|
---|
212 | "Invalid parameter input", 0);
|
---|
213 | }
|
---|
214 | if (valid) {
|
---|
215 | String name = componentNameTextField.getText();
|
---|
216 | String classPath = componentClassTextField.getText();
|
---|
217 | BOArepItem newComponent = new BOArepItem(name, classPath, type);
|
---|
218 | // for (int i = 0; i < parameterListModel.getSize(); i++) {
|
---|
219 | // BOAparameter item = (BOAparameter) parameterListModel
|
---|
220 | // .getElementAt(i);
|
---|
221 | // newComponent.addParameter(item);
|
---|
222 | // }
|
---|
223 | BOAagentRepository.getInstance().addComponent(newComponent);
|
---|
224 | result = newComponent;
|
---|
225 | dispose();
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | private void openAction() {
|
---|
230 |
|
---|
231 | JFileChooser fc = new JFileChooser(System.getProperty("user.dir"));
|
---|
232 |
|
---|
233 | // Filter such that only directories and .class files are shown.
|
---|
234 | FileFilter filter = new GenericFileFilter("class",
|
---|
235 | "Java class files (.class)");
|
---|
236 | fc.setFileFilter(filter);
|
---|
237 |
|
---|
238 | // Open the file picker
|
---|
239 | int returnVal = fc.showOpenDialog(null);
|
---|
240 |
|
---|
241 | // If file selected
|
---|
242 | if (returnVal == JFileChooser.APPROVE_OPTION) {
|
---|
243 | File file = fc.getSelectedFile();
|
---|
244 |
|
---|
245 | try {
|
---|
246 | Object object = Global.loadClassFromFile(file);
|
---|
247 | if (object instanceof OfferingStrategy) {
|
---|
248 | type = BoaType.BIDDINGSTRATEGY;
|
---|
249 | } else if (object instanceof AcceptanceStrategy) {
|
---|
250 | type = BoaType.ACCEPTANCESTRATEGY;
|
---|
251 | } else if (object instanceof OpponentModel) {
|
---|
252 | type = BoaType.OPPONENTMODEL;
|
---|
253 | } else if (object instanceof OMStrategy) {
|
---|
254 | type = BoaType.OMSTRATEGY;
|
---|
255 | } else {
|
---|
256 | throw new IllegalArgumentException(
|
---|
257 | "File "
|
---|
258 | + file
|
---|
259 | + " does not extend OfferingStrategy, AcceptanceStrategy, \n"
|
---|
260 | + "OpponentModel, or OMStrategy.");
|
---|
261 | }
|
---|
262 | } catch (Throwable e) {
|
---|
263 | Global.showLoadError(file, e);
|
---|
264 | return;
|
---|
265 | }
|
---|
266 |
|
---|
267 | try {
|
---|
268 | componentClassTextField.setText(file.getCanonicalPath());
|
---|
269 | if (componentNameTextField.getText().isEmpty()) {
|
---|
270 | componentNameTextField.setText(file.getName());
|
---|
271 | }
|
---|
272 | } catch (IOException e) {
|
---|
273 | // TODO Auto-generated catch block
|
---|
274 | e.printStackTrace();
|
---|
275 | }
|
---|
276 |
|
---|
277 | }
|
---|
278 | }
|
---|
279 | } |
---|