source: src/main/java/genius/gui/boaframework/BOARepositoryUI.java

Last change on this file was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

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