1 | package genius.gui.boaframework;
|
---|
2 |
|
---|
3 | import java.awt.Dialog;
|
---|
4 | import java.awt.Dimension;
|
---|
5 | import java.awt.Font;
|
---|
6 | import java.awt.Frame;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.ActionListener;
|
---|
9 | import java.awt.event.KeyAdapter;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.awt.event.WindowEvent;
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.Iterator;
|
---|
14 | import java.util.Set;
|
---|
15 |
|
---|
16 | import javax.swing.DefaultListModel;
|
---|
17 | import javax.swing.JButton;
|
---|
18 | import javax.swing.JDialog;
|
---|
19 | import javax.swing.JLabel;
|
---|
20 | import javax.swing.JList;
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 | import javax.swing.JScrollPane;
|
---|
23 | import javax.swing.WindowConstants;
|
---|
24 |
|
---|
25 | import org.netbeans.lib.awtextra.AbsoluteConstraints;
|
---|
26 | import org.netbeans.lib.awtextra.AbsoluteLayout;
|
---|
27 |
|
---|
28 | import genius.core.boaframework.BOAagentInfo;
|
---|
29 | import genius.core.boaframework.BOAcomponent;
|
---|
30 | import genius.core.boaframework.BoaType;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * @author Mark Hendrikx
|
---|
34 | * @author W.Pasman refactored and modified 13feb2014 #883
|
---|
35 | */
|
---|
36 | public class BOAagentsFrame extends JDialog {
|
---|
37 |
|
---|
38 | private static final long serialVersionUID = -8031426652298029936L;
|
---|
39 |
|
---|
40 | // AGENTS LIST
|
---|
41 | private JLabel boaAgentsLabel;
|
---|
42 | private JScrollPane agentsListSP;
|
---|
43 | private JList agentsList;
|
---|
44 | private DefaultListModel agentsModel;
|
---|
45 |
|
---|
46 | // BUTTONS
|
---|
47 | private JButton addAgentButton;
|
---|
48 | private JButton editAgentButton;
|
---|
49 | private JButton deleteAgentButton;
|
---|
50 | private JButton saveButton;
|
---|
51 |
|
---|
52 | private ArrayList<BOAagentInfo> result;
|
---|
53 |
|
---|
54 | public BOAagentsFrame(Frame frame) {
|
---|
55 | super(frame, "Select BOA agents", true);
|
---|
56 | this.setLocation(frame.getLocation().x + frame.getWidth() / 4,
|
---|
57 | frame.getLocation().y + frame.getHeight() / 4);
|
---|
58 | }
|
---|
59 |
|
---|
60 | public ArrayList<BOAagentInfo> getResult(
|
---|
61 | ArrayList<BOAagentInfo> BOAagentList) {
|
---|
62 | initFrameUI();
|
---|
63 | initAgentsListUI();
|
---|
64 | initButtons();
|
---|
65 | initControls();
|
---|
66 |
|
---|
67 | for (BOAagentInfo agent : BOAagentList) {
|
---|
68 | agentsModel.addElement(agent);
|
---|
69 | }
|
---|
70 |
|
---|
71 | pack();
|
---|
72 | setVisible(true);
|
---|
73 | return result;
|
---|
74 | }
|
---|
75 |
|
---|
76 | private void initFrameUI() {
|
---|
77 | setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
|
---|
78 | // allow deeper dialogs to appear on top of this dialog.
|
---|
79 | setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
|
---|
80 | setMaximumSize(new Dimension(1010, 440));
|
---|
81 | setMinimumSize(new Dimension(1010, 440));
|
---|
82 | setPreferredSize(new Dimension(1010, 440));
|
---|
83 | setResizable(false);
|
---|
84 | setTitle("Select BOA agents");
|
---|
85 | getContentPane().setLayout(new AbsoluteLayout());
|
---|
86 | }
|
---|
87 |
|
---|
88 | private void initAgentsListUI() {
|
---|
89 | boaAgentsLabel = new JLabel();
|
---|
90 | boaAgentsLabel.setText("BOA Agents");
|
---|
91 | boaAgentsLabel.setFont(new Font("Tahoma", 1, 13));
|
---|
92 | getContentPane().add(boaAgentsLabel,
|
---|
93 | new AbsoluteConstraints(10, 50, -1, -1));
|
---|
94 |
|
---|
95 | agentsList = new JList();
|
---|
96 | agentsModel = new DefaultListModel();
|
---|
97 | agentsList.setModel(agentsModel);
|
---|
98 | agentsListSP = new JScrollPane();
|
---|
99 | agentsListSP.setViewportView(agentsList);
|
---|
100 | agentsList.addKeyListener(new KeyAdapter() {
|
---|
101 | public void keyReleased(KeyEvent ke) {
|
---|
102 | if (ke.getKeyCode() == KeyEvent.VK_DELETE) {
|
---|
103 | Object[] values = agentsList.getSelectedValues();
|
---|
104 | for (int i = 0; i < values.length; i++) {
|
---|
105 | agentsModel.removeElement(values[i]);
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 | });
|
---|
110 |
|
---|
111 | getContentPane().add(agentsListSP,
|
---|
112 | new AbsoluteConstraints(10, 75, 980, 290));
|
---|
113 | }
|
---|
114 |
|
---|
115 | private void initButtons() {
|
---|
116 | addAgentButton = new JButton();
|
---|
117 | addAgentButton.setText("Add agent(s)");
|
---|
118 | getContentPane().add(addAgentButton,
|
---|
119 | new AbsoluteConstraints(10, 10, 107, -1));
|
---|
120 |
|
---|
121 | editAgentButton = new JButton();
|
---|
122 | editAgentButton.setText("Edit agent");
|
---|
123 | getContentPane().add(editAgentButton,
|
---|
124 | new AbsoluteConstraints(120, 10, 107, -1));
|
---|
125 |
|
---|
126 | deleteAgentButton = new JButton();
|
---|
127 | deleteAgentButton.setText("Delete agent");
|
---|
128 | getContentPane().add(deleteAgentButton,
|
---|
129 | new AbsoluteConstraints(230, 10, 107, -1));
|
---|
130 |
|
---|
131 | saveButton = new JButton();
|
---|
132 | saveButton.setText("Save agents");
|
---|
133 | getContentPane().add(saveButton,
|
---|
134 | new AbsoluteConstraints(10, 375, 105, -1));
|
---|
135 | }
|
---|
136 |
|
---|
137 | private void initControls() {
|
---|
138 | addAgentButton.addActionListener(new ActionListener() {
|
---|
139 | public void actionPerformed(ActionEvent e) {
|
---|
140 | Set<Set<BOAcomponent>> res = new BOAAgentUI(null).getResult();
|
---|
141 | if (res != null) {
|
---|
142 | insertNewAgents(res);
|
---|
143 | }
|
---|
144 | }
|
---|
145 | });
|
---|
146 |
|
---|
147 | saveButton.addActionListener(new ActionListener() {
|
---|
148 | public void actionPerformed(ActionEvent e) {
|
---|
149 | ArrayList<BOAagentInfo> agents = new ArrayList<BOAagentInfo>();
|
---|
150 | for (int i = 0; i < agentsModel.getSize(); i++) {
|
---|
151 | agents.add((BOAagentInfo) agentsModel.getElementAt(i));
|
---|
152 | }
|
---|
153 | result = agents;
|
---|
154 | dispose();
|
---|
155 | }
|
---|
156 | });
|
---|
157 |
|
---|
158 | editAgentButton.addActionListener(new ActionListener() {
|
---|
159 | public void actionPerformed(ActionEvent e) {
|
---|
160 | BOAagentInfo s = (BOAagentInfo) agentsList.getSelectedValue();
|
---|
161 | if (s == null) {
|
---|
162 | JOptionPane.showMessageDialog(null,
|
---|
163 | "Please select an agent to edit.",
|
---|
164 | "Edit notification", 1);
|
---|
165 | } else {
|
---|
166 | Set<Set<BOAcomponent>> res = new BOAAgentUI(s).getResult();
|
---|
167 | if (res != null) {
|
---|
168 | agentsModel.removeElement(s);
|
---|
169 | insertNewAgents(res);
|
---|
170 | }
|
---|
171 |
|
---|
172 | }
|
---|
173 | }
|
---|
174 | });
|
---|
175 |
|
---|
176 | deleteAgentButton.addActionListener(new ActionListener() {
|
---|
177 | public void actionPerformed(ActionEvent e) {
|
---|
178 | BOAagentInfo s = (BOAagentInfo) agentsList.getSelectedValue();
|
---|
179 | if (s == null) {
|
---|
180 | JOptionPane.showMessageDialog(null,
|
---|
181 | "Please select an agent to delete.",
|
---|
182 | "Edit notification", 1);
|
---|
183 | } else {
|
---|
184 | agentsModel.removeElement(s);
|
---|
185 | }
|
---|
186 | }
|
---|
187 | });
|
---|
188 | }
|
---|
189 |
|
---|
190 | private void insertNewAgents(Set<Set<BOAcomponent>> result) {
|
---|
191 |
|
---|
192 | Iterator<Set<BOAcomponent>> strategyIterator = result.iterator();
|
---|
193 | while (strategyIterator.hasNext()) {
|
---|
194 | Set<BOAcomponent> fullStrat = strategyIterator.next();
|
---|
195 | Iterator<BOAcomponent> strat = fullStrat.iterator();
|
---|
196 | BOAcomponent os = null, as = null, om = null, oms = null;
|
---|
197 | while (strat.hasNext()) {
|
---|
198 | BOAcomponent strategy = strat.next();
|
---|
199 | if (strategy.getType() == BoaType.BIDDINGSTRATEGY) {
|
---|
200 | os = strategy;
|
---|
201 | } else if (strategy.getType() == BoaType.ACCEPTANCESTRATEGY) {
|
---|
202 | as = strategy;
|
---|
203 | } else if (strategy.getType() == BoaType.OPPONENTMODEL) {
|
---|
204 | om = strategy;
|
---|
205 | } else if (strategy.getType() == BoaType.OMSTRATEGY) {
|
---|
206 | oms = strategy;
|
---|
207 | }
|
---|
208 | }
|
---|
209 | BOAagentInfo agent = new BOAagentInfo(os, as, om, oms);
|
---|
210 | agentsModel.addElement(agent);
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | @Override
|
---|
215 | public void processWindowEvent(WindowEvent evt) {
|
---|
216 | // Why is the getNewState not working as expected?
|
---|
217 | if (evt.paramString().contains("CLOSING")) {
|
---|
218 |
|
---|
219 | int n = JOptionPane.showConfirmDialog(this,
|
---|
220 | "Discard your changes?", "Confirm discard",
|
---|
221 | JOptionPane.YES_NO_OPTION);
|
---|
222 | if (n == JOptionPane.YES_OPTION) {
|
---|
223 | dispose();
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | }
|
---|
228 | } |
---|