source: src/main/java/genius/gui/repository/PartyRepositoryUI.java

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

Initial import : Genius 9.0.0

File size: 5.5 KB
Line 
1package genius.gui.repository;
2
3import java.awt.BorderLayout;
4import java.awt.Component;
5import java.awt.event.KeyAdapter;
6import java.awt.event.KeyEvent;
7import java.awt.event.MouseAdapter;
8import java.awt.event.MouseEvent;
9
10import javax.swing.JFileChooser;
11import javax.swing.JMenuItem;
12import javax.swing.JPanel;
13import javax.swing.JPopupMenu;
14import javax.swing.JScrollPane;
15import javax.swing.JTable;
16import javax.swing.filechooser.FileFilter;
17import javax.swing.table.AbstractTableModel;
18
19import genius.core.Global;
20import genius.core.repository.PartyRepItem;
21import genius.core.repository.Repository;
22import genius.core.repository.RepositoryFactory;
23import genius.gui.panels.GenericFileFilter;
24
25/**
26 * A user interface to for RepItems, usable for AgentRepItem and PartyRepItem
27 * repository
28 *
29 * @author Wouter Pasman
30 */
31@SuppressWarnings("serial")
32public class PartyRepositoryUI extends JPanel {
33 private partyTableModel dataModel;
34 private final JTable table = new JTable();
35 private JScrollPane scrollpane = new JScrollPane();
36
37 public PartyRepositoryUI() {
38 setLayout(new BorderLayout());
39
40 scrollpane.setViewportView(table);
41 add(scrollpane, BorderLayout.CENTER);
42
43 dataModel = new partyTableModel();
44
45 table.setModel(dataModel);
46 table.setShowVerticalLines(false);
47
48 table.addKeyListener(new KeyAdapter() {
49 public void keyReleased(KeyEvent ke) {
50 if (ke.getKeyCode() == KeyEvent.VK_DELETE) {
51 removeAction();
52 }
53 }
54 });
55
56 addPopupMenu(scrollpane);
57 addPopupMenu(table);
58
59 }
60
61 /**
62 * User clicked in our panel. Show popup menu
63 *
64 * if there is a row selected in the table, we also enable the Remove
65 * option.
66 *
67 * @return popupmenu
68 */
69 private JPopupMenu createPopupMenu() {
70 JPopupMenu popup = new JPopupMenu();
71
72 JMenuItem addAgent = new JMenuItem("Add new party");
73 addAgent.addActionListener(new java.awt.event.ActionListener() {
74 public void actionPerformed(java.awt.event.ActionEvent evt) {
75 addAction();
76 }
77 });
78 popup.add(addAgent);
79
80 if (table.getSelectedRow() > 0) {
81 JMenuItem removeAgent = new JMenuItem("Remove party");
82 removeAgent.addActionListener(new java.awt.event.ActionListener() {
83 public void actionPerformed(java.awt.event.ActionEvent evt) {
84 removeAction();
85 }
86 });
87 popup.add(removeAgent);
88 }
89
90 return popup;
91 }
92
93 /**
94 * Add a popup menu to a component. We need to attach this to multiple
95 * components: the scrollpane and the table. This is because we want the
96 * menu also to appear if users click outside the table #858
97 *
98 * @param component
99 * the component to add the menu to.
100 *
101 */
102 private void addPopupMenu(Component component) {
103 component.addMouseListener(new MouseAdapter() {
104
105 // if Windows
106 @Override
107 public void mouseReleased(MouseEvent e) {
108 mouseCode(e);
109 }
110
111 // if Linux
112 public void mousePressed(MouseEvent e) {
113 mouseCode(e);
114 }
115
116 private void mouseCode(MouseEvent e) {
117 int r = table.rowAtPoint(e.getPoint());
118 if (r >= 0 && r < table.getRowCount()) {
119 table.setRowSelectionInterval(r, r);
120 } else {
121 table.clearSelection();
122 }
123
124 if (e.isPopupTrigger()) {// && e.getComponent() instanceof
125 // JTable) {
126 // if rowindex>0, we actually selected a row.
127 JPopupMenu popup = createPopupMenu();
128 popup.show(e.getComponent(), e.getX(), e.getY());
129 }
130 }
131 });
132 }
133
134 /**
135 * Add new agent to repository. The party is expected to be a .class file
136 */
137 public void addAction() {
138
139 JFileChooser fc = new JFileChooser(System.getProperty("user.dir"));
140
141 // Filter such that only directories and .class files are shown.
142 FileFilter filter = new GenericFileFilter("class", "Java class files (.class)");
143 fc.setFileFilter(filter);
144
145 // Open the file picker
146 int returnVal = fc.showOpenDialog(null);
147
148 // If file selected
149 if (returnVal == JFileChooser.APPROVE_OPTION) {
150 try {
151 dataModel.add(new PartyRepItem(fc.getSelectedFile().getPath()));
152 } catch (Throwable e) {
153 Global.showLoadError(fc.getSelectedFile(), e);
154 }
155 }
156
157 }
158
159 public void removeAction() {
160 // CHECK is this ok? After removing a row, all row numbers change?
161 for (int i = 0; i < table.getSelectedRows().length; i++) {
162 dataModel.remove(table.getSelectedRows()[i]);
163 }
164 }
165
166}
167
168/**
169 * Contains the data model for the Party Repository.
170 *
171 * @author W.Pasman 3aug15
172 *
173 */
174@SuppressWarnings("serial")
175class partyTableModel extends AbstractTableModel {
176
177 private Repository repository;
178 final String columnnames[] = { "Party Name", "Description", "Protocol" };
179
180 public partyTableModel() {
181 repository = RepositoryFactory.get_party_repository();
182
183 }
184
185 public void add(PartyRepItem agentref) {
186 repository.getItems().add(agentref);
187 repository.save();
188 fireTableDataChanged();
189 }
190
191 /**
192 * Remove row from the model.
193 *
194 * @param i
195 * the row to remove
196 */
197 public void remove(int i) {
198 repository.getItems().remove(i);
199 repository.save();
200 fireTableDataChanged();
201 }
202
203 public int getColumnCount() {
204 return columnnames.length;
205 }
206
207 public int getRowCount() {
208 return repository.getItems().size();
209 }
210
211 public Object getValueAt(int row, int col) {
212 try {
213 PartyRepItem party = (PartyRepItem) repository.getItems().get(row);
214 switch (col) {
215 case 0:
216 return party.getName();
217 case 1:
218 return party.getDescription();
219 case 2:
220 return Class.forName(party.getProtocolClassPath()).getSimpleName();
221 }
222 return col;
223 } catch (Exception e) {
224 return e.toString();
225 }
226 }
227
228 public String getColumnName(int column) {
229 return columnnames[column];
230 }
231
232}
Note: See TracBrowser for help on using the repository browser.