1 | package genius.gui.boaframework;
|
---|
2 |
|
---|
3 | import javax.swing.JMenuItem;
|
---|
4 | import javax.swing.JPopupMenu;
|
---|
5 | import javax.swing.JTable;
|
---|
6 | import javax.swing.table.AbstractTableModel;
|
---|
7 |
|
---|
8 | import genius.core.boaframework.BoaType;
|
---|
9 | import genius.core.boaframework.repository.BOAagentRepository;
|
---|
10 | import genius.core.boaframework.repository.BOArepItem;
|
---|
11 | import genius.gui.NegoGUIApp;
|
---|
12 |
|
---|
13 | import java.awt.event.KeyAdapter;
|
---|
14 | import java.awt.event.KeyEvent;
|
---|
15 | import java.awt.event.MouseAdapter;
|
---|
16 | import java.awt.event.MouseEvent;
|
---|
17 | import java.sql.Savepoint;
|
---|
18 | import java.util.ArrayList;
|
---|
19 | import java.util.Collections;
|
---|
20 | import java.util.Map.Entry;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * A user interface to the agent repository
|
---|
24 | * @author Wouter Pasman, Mark Hendrikx
|
---|
25 | */
|
---|
26 | public class BOARepositoryUI {
|
---|
27 |
|
---|
28 | private static final String ADD_A_COMPONENT = "Add a component";
|
---|
29 | private BOAagentRepository boaRepository;
|
---|
30 | private AbstractTableModel dataModel;
|
---|
31 | private final JTable table;
|
---|
32 | private ArrayList<BOArepItem> items;
|
---|
33 |
|
---|
34 | public BOARepositoryUI(JTable pTable) {
|
---|
35 | this.table = pTable;
|
---|
36 | boaRepository = BOAagentRepository.getInstance();
|
---|
37 | items = new ArrayList<BOArepItem>();
|
---|
38 | referenceComponents();
|
---|
39 | initTable();
|
---|
40 | }
|
---|
41 |
|
---|
42 | private JPopupMenu createPopupMenu() {
|
---|
43 | JPopupMenu popup = new JPopupMenu();
|
---|
44 |
|
---|
45 | JMenuItem addComponent = new JMenuItem("Add new component");
|
---|
46 | addComponent.addActionListener(new java.awt.event.ActionListener() {
|
---|
47 | public void actionPerformed(java.awt.event.ActionEvent evt) {
|
---|
48 | addAction();
|
---|
49 | }
|
---|
50 | });
|
---|
51 |
|
---|
52 | JMenuItem editComponent = new JMenuItem("Edit component");
|
---|
53 | editComponent.addActionListener(new java.awt.event.ActionListener() {
|
---|
54 | public void actionPerformed(java.awt.event.ActionEvent evt) {
|
---|
55 | editAction();
|
---|
56 | }
|
---|
57 | });
|
---|
58 |
|
---|
59 | JMenuItem removeComponent = new JMenuItem("Remove component");
|
---|
60 | removeComponent.addActionListener(new java.awt.event.ActionListener() {
|
---|
61 | public void actionPerformed(java.awt.event.ActionEvent evt) {
|
---|
62 | removeAction();
|
---|
63 | }
|
---|
64 | });
|
---|
65 |
|
---|
66 | popup.add(addComponent);
|
---|
67 | popup.add(editComponent);
|
---|
68 | popup.add(removeComponent);
|
---|
69 |
|
---|
70 | return popup;
|
---|
71 | }
|
---|
72 |
|
---|
73 | private void referenceComponents() {
|
---|
74 | items.clear();
|
---|
75 | for (Entry<String, BOArepItem> entry : boaRepository.getOfferingStrategiesRepItems().entrySet()) {
|
---|
76 | items.add(entry.getValue());
|
---|
77 | }
|
---|
78 |
|
---|
79 | for (Entry<String, BOArepItem> entry : boaRepository.getAcceptanceStrategiesRepItems().entrySet()) {
|
---|
80 | items.add(entry.getValue());
|
---|
81 | }
|
---|
82 |
|
---|
83 | for (Entry<String, BOArepItem> entry : boaRepository.getOpponentModelsRepItems().entrySet()) {
|
---|
84 | items.add(entry.getValue());
|
---|
85 | }
|
---|
86 |
|
---|
87 | for (Entry<String, BOArepItem> entry : boaRepository.getOMStrategiesRepItems().entrySet()) {
|
---|
88 | items.add(entry.getValue());
|
---|
89 | }
|
---|
90 |
|
---|
91 | Collections.sort(items);
|
---|
92 |
|
---|
93 | if (items.size() == 0) {
|
---|
94 | addTemporaryComponent();
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | private void initTable() {
|
---|
99 | dataModel = new AbstractTableModel() {
|
---|
100 | private static final long serialVersionUID = -4985008096999143587L;
|
---|
101 | final String columnnames[] = {"Type","Name"};
|
---|
102 |
|
---|
103 | public int getColumnCount() {
|
---|
104 | return columnnames.length;
|
---|
105 | }
|
---|
106 |
|
---|
107 | public int getRowCount() {
|
---|
108 | return items.size();
|
---|
109 | }
|
---|
110 |
|
---|
111 | public Object getValueAt(int row, int col) {
|
---|
112 | BOArepItem boaComponent = (BOArepItem) items.get(row);
|
---|
113 | switch(col) {
|
---|
114 | case 0:
|
---|
115 | return boaComponent.getTypeString();
|
---|
116 | case 1:
|
---|
117 | return boaComponent.getName();
|
---|
118 | }
|
---|
119 | return col;
|
---|
120 | }
|
---|
121 | public String getColumnName(int column) {
|
---|
122 | return columnnames[column];
|
---|
123 | }
|
---|
124 | };
|
---|
125 |
|
---|
126 | table.setModel(dataModel);
|
---|
127 | table.setShowVerticalLines(false);
|
---|
128 | table.addMouseListener(new MouseAdapter() {
|
---|
129 |
|
---|
130 | // if Windows
|
---|
131 | @Override
|
---|
132 | public void mouseReleased(MouseEvent e) {
|
---|
133 | mouseCode(e);
|
---|
134 | }
|
---|
135 |
|
---|
136 | // if Linux
|
---|
137 | public void mousePressed(MouseEvent e) {
|
---|
138 | mouseCode(e);
|
---|
139 | }
|
---|
140 |
|
---|
141 | private void mouseCode(MouseEvent e) {
|
---|
142 | int r = table.rowAtPoint(e.getPoint());
|
---|
143 | if (r >= 0 && r < table.getRowCount()) {
|
---|
144 | table.setRowSelectionInterval(r, r);
|
---|
145 | } else {
|
---|
146 | table.clearSelection();
|
---|
147 | }
|
---|
148 |
|
---|
149 | int rowindex = table.getSelectedRow();
|
---|
150 | if (rowindex < 0)
|
---|
151 | return;
|
---|
152 | if (e.isPopupTrigger() && e.getComponent() instanceof JTable ) {
|
---|
153 | JPopupMenu popup = createPopupMenu();
|
---|
154 | popup.show(e.getComponent(), e.getX(), e.getY());
|
---|
155 | }
|
---|
156 | }
|
---|
157 | });
|
---|
158 |
|
---|
159 | table.addKeyListener(new KeyAdapter() {
|
---|
160 | public void keyReleased(KeyEvent ke) {
|
---|
161 | if (ke.getKeyCode() == KeyEvent.VK_DELETE) {
|
---|
162 | removeAction();
|
---|
163 | }
|
---|
164 | }
|
---|
165 | });
|
---|
166 | }
|
---|
167 |
|
---|
168 | public void addAction() {
|
---|
169 | // shoud return boolean if added an item.
|
---|
170 | // if so, sort items and display again
|
---|
171 | BOAComponentEditor loader = new BOAComponentEditor(NegoGUIApp.negoGUIView.getFrame(), "Add BOA component");
|
---|
172 | BOArepItem item = loader.getResult(null);
|
---|
173 | if (item != null) {
|
---|
174 | items.add(item);
|
---|
175 | Collections.sort(items);
|
---|
176 | table.updateUI();
|
---|
177 | }
|
---|
178 | }
|
---|
179 | public void editAction() {
|
---|
180 | BOArepItem item = items.get(table.getSelectedRow());
|
---|
181 | BOAComponentEditor loader = new BOAComponentEditor(NegoGUIApp.negoGUIView.getFrame(), "Edit BOA component");
|
---|
182 | BOArepItem result = loader.getResult(item);
|
---|
183 | if (result != null) {
|
---|
184 | items.remove(item);
|
---|
185 | items.add(result);
|
---|
186 | Collections.sort(items);
|
---|
187 | table.updateUI();
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | public void removeAction() {
|
---|
192 | if (table.getSelectedRow() != -1) {
|
---|
193 | BOArepItem removed = items.remove(table.getSelectedRow());
|
---|
194 | if (dataModel.getRowCount() == 0) {
|
---|
195 | addTemporaryComponent();
|
---|
196 | }
|
---|
197 | dataModel.fireTableDataChanged();
|
---|
198 | if (removed.getType() != BoaType.UNKNOWN) {
|
---|
199 | boaRepository.removeComponent(removed);
|
---|
200 | }
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | private void addTemporaryComponent() {
|
---|
205 | if (items.size() == 0) {
|
---|
206 | items.add(new BOArepItem(ADD_A_COMPONENT, "", BoaType.UNKNOWN));
|
---|
207 | }
|
---|
208 | }
|
---|
209 | } |
---|