source: src/main/java/genius/gui/agentrepository/AgentRepositoryUI.java@ 256

Last change on this file since 256 was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 7.2 KB
Line 
1package genius.gui.agentrepository;
2
3import java.awt.Component;
4import java.awt.event.KeyAdapter;
5import java.awt.event.KeyEvent;
6import java.awt.event.MouseAdapter;
7import java.awt.event.MouseEvent;
8import java.io.File;
9
10import javax.swing.JFileChooser;
11import javax.swing.JMenuItem;
12import javax.swing.JPopupMenu;
13import javax.swing.JScrollPane;
14import javax.swing.JTable;
15import javax.swing.filechooser.FileFilter;
16import javax.swing.table.AbstractTableModel;
17
18import genius.core.Global;
19import genius.core.repository.AgentRepItem;
20import genius.core.repository.Repository;
21import genius.core.repository.RepositoryFactory;
22import genius.gui.panels.GenericFileFilter;
23
24/**
25 * A user interface to the agent repository
26 *
27 * @author Wouter Pasman, Mark Hendrikx
28 */
29public class AgentRepositoryUI {
30 private static final String ADD_AN_AGENT = "Add an agent";
31 private Repository<AgentRepItem> agentrepository;
32 private AbstractTableModel dataModel;
33 private final JTable table;
34
35 /**
36 * appends the UI to the given scrollpane. Kind of hack, NegoGUIView has
37 * already created the scrollpane. #858 we need the scrollpane because we
38 * want to attach mouse listener to the scrollpane
39 *
40 * @param jScrollPane1
41 */
42 public AgentRepositoryUI(JScrollPane jScrollPane1) {
43 this.table = new JTable();
44 jScrollPane1.setViewportView(table);
45
46 agentrepository = RepositoryFactory.get_agent_repository();
47
48 initTable();
49 addPopupMenu(jScrollPane1);
50 addPopupMenu(table);
51
52 }
53
54 /**
55 * User clicked in our panel. Show popup menu
56 *
57 * if there is a row selected in the table, we also enable the Remove
58 * option.
59 *
60 * @return popupmenu
61 */
62 private JPopupMenu createPopupMenu() {
63 JPopupMenu popup = new JPopupMenu();
64
65 JMenuItem addAgent = new JMenuItem("Add new agent");
66 addAgent.addActionListener(new java.awt.event.ActionListener() {
67 public void actionPerformed(java.awt.event.ActionEvent evt) {
68 addAction();
69 }
70 });
71 popup.add(addAgent);
72
73 if (table.getSelectedRow() > 0) {
74
75 JMenuItem removeAgent = new JMenuItem("Remove agent");
76 removeAgent.addActionListener(new java.awt.event.ActionListener() {
77 public void actionPerformed(java.awt.event.ActionEvent evt) {
78 removeAction();
79 }
80 });
81 popup.add(removeAgent);
82 }
83
84 return popup;
85 }
86
87 /**
88 * Add a popup menu to a component. We need to attach this to multiple
89 * components: the scrollpane and the table. This is because we want the
90 * menu also to appear if users click outside the table #858
91 *
92 * @param component
93 * the component to add the menu to.
94 *
95 */
96 private void addPopupMenu(Component component) {
97 component.addMouseListener(new MouseAdapter() {
98
99 // if Windows
100 @Override
101 public void mouseReleased(MouseEvent e) {
102 mouseCode(e);
103 }
104
105 // if Linux
106 public void mousePressed(MouseEvent e) {
107 mouseCode(e);
108 }
109
110 private void mouseCode(MouseEvent e) {
111 int r = table.rowAtPoint(e.getPoint());
112 if (r >= 0 && r < table.getRowCount()) {
113 table.setRowSelectionInterval(r, r);
114 } else {
115 table.clearSelection();
116 }
117
118 if (e.isPopupTrigger()) {// && e.getComponent() instanceof
119 // JTable) {
120 // if rowindex>0, we actually selected a row.
121 JPopupMenu popup = createPopupMenu();
122 popup.show(e.getComponent(), e.getX(), e.getY());
123 }
124 }
125 });
126 }
127
128 private void initTable() {
129 dataModel = new AbstractTableModel() {
130 private static final long serialVersionUID = -4985008096999143587L;
131 final String columnnames[] = { "Agent Name", "Description" };
132
133 public int getColumnCount() {
134 return columnnames.length;
135 }
136
137 public int getRowCount() {
138 return agentrepository.getItems().size();
139 }
140
141 public Object getValueAt(int row, int col) {
142 AgentRepItem agt = (AgentRepItem) agentrepository.getItems().get(row);
143 switch (col) {
144 case 0:
145 String error = "";
146 if (agt.getVersion().equals("ERR") && !agt.getName().equals(ADD_AN_AGENT)) {
147 error = " (LOADING FAILED)";
148 }
149 return agt.getName() + error;
150 case 1:
151 return agt.getDescription();
152 }
153 return col;
154 }
155
156 public String getColumnName(int column) {
157 return columnnames[column];
158 }
159 };
160
161 if (agentrepository.getItems().size() == 0) {
162 addTemporaryAgent();
163 agentrepository.save();
164 }
165
166 table.setModel(dataModel);
167 table.setShowVerticalLines(false);
168
169 table.addKeyListener(new KeyAdapter() {
170 public void keyReleased(KeyEvent ke) {
171 if (ke.getKeyCode() == KeyEvent.VK_DELETE) {
172 removeAction();
173 }
174 }
175 });
176 }
177
178 /**
179 * Add new agent to repository. The agent is expected to be a .class file
180 */
181 public void addAction() {
182
183 // Restrict file picker to root and subdirectories.
184 // Ok, you can escape if you put in a path as directory. We catch this
185 // later on.
186 //
187 // FileSystemView fsv = new DirectoryRestrictedFileSystemView(new
188 // File(root));
189 // JFileChooser fc = new JFileChooser(fsv.getHomeDirectory(), fsv);
190 // Lifted the restriction. #856
191 JFileChooser fc = new JFileChooser(System.getProperty("user.dir"));
192
193 // Filter such that only directories and .class files are shown.
194 FileFilter filter = new GenericFileFilter("class", "Java class files (.class)");
195 fc.setFileFilter(filter);
196
197 // Open the file picker
198 int returnVal = fc.showOpenDialog(null);
199
200 // If file selected
201 if (returnVal == JFileChooser.APPROVE_OPTION) {
202 try {
203 addToRepo(new AgentRepItem(fc.getSelectedFile()));
204 } catch (Throwable e) {
205 Global.showLoadError(fc.getSelectedFile(), e);
206 }
207 }
208
209 }
210
211 /**
212 *
213 * @param file
214 * absolute file path, eg
215 * /Volumes/documents/NegoWorkspace3/NegotiatorGUI
216 * /bin/agents/BayesianAgent.class TODO I think this should be
217 * path to the root directory for the class path, eg
218 * '/Volumes/documents/NegoWorkspace3/NegotiatorGUI/bin'
219 * @param className
220 * the class path name, eg "agents.BayesianAgent"
221 */
222 private void addToRepo(File file, String className) {
223 // Remove "Add agents" if there were no agents first
224 int row = table.getSelectedRow();
225 if (agentrepository.getItems().get(row).getName().equals(ADD_AN_AGENT)) {
226 agentrepository.getItems().remove(row);
227 }
228
229 // Load the agent and save it in the XML. -6 strips the '.class'.
230 AgentRepItem rep = new AgentRepItem(file.getName().substring(0, file.getName().length() - 6), className, "");
231 agentrepository.getItems().add(rep);
232 agentrepository.save();
233 dataModel.fireTableDataChanged();
234
235 }
236
237 /**
238 * Add a Agent reference to the repo.
239 *
240 * @param agentref
241 */
242 private void addToRepo(AgentRepItem agentref) {
243 // Remove "Add agents" if there were no agents first
244 if (agentrepository.getItems().get(0).getName().equals(ADD_AN_AGENT)) {
245 agentrepository.getItems().remove(0);
246 }
247
248 agentrepository.getItems().add(agentref);
249 agentrepository.save();
250 dataModel.fireTableDataChanged();
251
252 }
253
254 public void removeAction() {
255 for (int i = 0; i < table.getSelectedRows().length; i++) {
256 agentrepository.getItems().remove(table.getSelectedRows()[i]);
257 }
258 if (dataModel.getRowCount() == 0) {
259 addTemporaryAgent();
260 }
261 dataModel.fireTableDataChanged();
262 agentrepository.save();
263 }
264
265 private void addTemporaryAgent() {
266 if (dataModel.getRowCount() == 0) {
267 agentrepository.getItems().add(new AgentRepItem(ADD_AN_AGENT, "", ""));
268 }
269 }
270}
Note: See TracBrowser for help on using the repository browser.