source: src/main/java/genius/gui/session/ParticipantsPanel.java@ 346

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

Initial import : Genius 9.0.0

File size: 5.0 KB
Line 
1package genius.gui.session;
2
3import java.awt.BorderLayout;
4import java.awt.Dimension;
5import java.awt.event.ActionEvent;
6import java.awt.event.ActionListener;
7
8import javax.swing.DefaultListModel;
9import javax.swing.JButton;
10import javax.swing.JPanel;
11import javax.swing.JScrollPane;
12import javax.swing.JTable;
13import javax.swing.event.ListDataEvent;
14import javax.swing.event.ListDataListener;
15import javax.swing.event.TableModelListener;
16import javax.swing.table.TableModel;
17
18import genius.core.repository.RepItem;
19import genius.core.session.Participant;
20import genius.gui.panels.VflowPanelWithBorder;
21import genius.gui.renderer.RepItemTableCellRenderer;
22
23/**
24 * An editor for a list of participants.
25 *
26 */
27@SuppressWarnings("serial")
28public class ParticipantsPanel extends VflowPanelWithBorder {
29
30 public ParticipantsPanel(final ParticipantModel participantModel,
31 final DefaultListModel<Participant> participantsModel) {
32 super("Participants");
33
34 add(new ParticipantPanel(participantModel));
35
36 JPanel buttonsPanel = new JPanel(new BorderLayout());
37 JButton addButton = new JButton("Add Party");
38 JButton removeButton = new JButton("Remove Party");
39 buttonsPanel.add(addButton, BorderLayout.WEST);
40 buttonsPanel.add(removeButton, BorderLayout.EAST);
41 buttonsPanel.setMaximumSize(new Dimension(99999999, 30));
42
43 add(buttonsPanel);
44
45 final JTable table = new JTable(new ParticipantTableAdapter(participantsModel));
46 table.setDefaultRenderer(RepItem.class, new RepItemTableCellRenderer());
47 add(new JScrollPane(table));
48
49 addButton.addActionListener(new ActionListener() {
50 @Override
51 public void actionPerformed(ActionEvent e) {
52 participantsModel.addElement(participantModel.getParticipant());
53 participantModel.increment();
54 }
55 });
56
57 removeButton.addActionListener(new ActionListener() {
58
59 @Override
60 public void actionPerformed(ActionEvent e) {
61 if (table.getSelectedRow() != -1) {
62 Participant participant = participantsModel.getElementAt(table.getSelectedRow());
63 participantsModel.removeElement(participant);
64 }
65 }
66 });
67 }
68
69}
70
71/******************************************************************************************
72 ************************************ INTERNAL ADAPTERS ***********************************
73 ******************************************************************************************/
74/**
75 * Adapts the DefaultListModel<Participant> to show it in a 3-column table.
76 */
77class ParticipantTableAdapter implements TableModel {
78
79 private DefaultListModel<Participant> model;
80
81 public ParticipantTableAdapter(DefaultListModel<Participant> participantsModel) {
82 this.model = participantsModel;
83 }
84
85 @Override
86 public int getRowCount() {
87 return model.getSize();
88 }
89
90 @Override
91 public int getColumnCount() {
92 return 3;
93 }
94
95 private static final String[] columns = { "Party ID", "Strategy", "Preference" };
96
97 @Override
98 public String getColumnName(int columnIndex) {
99 return columns[columnIndex];
100 }
101
102 @Override
103 public Class<?> getColumnClass(int columnIndex) {
104 if (columnIndex == 0)
105 return String.class;
106 return RepItem.class;
107 }
108
109 @Override
110 public boolean isCellEditable(int rowIndex, int columnIndex) {
111 return false;
112 }
113
114 @Override
115 public Object getValueAt(int rowIndex, int columnIndex) {
116 Participant party = model.get(rowIndex);
117 switch (columnIndex) {
118 case 0:
119 return party.getId();
120 case 1:
121 return party.getStrategy();
122 case 2:
123 return party.getProfile();
124 }
125 return "??";
126 }
127
128 @Override
129 public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
130 }
131
132 @Override
133 public void addTableModelListener(TableModelListener l) {
134 model.addListDataListener(new ListToTableListenerAdapter(l));
135 }
136
137 @Override
138 public void removeTableModelListener(TableModelListener l) {
139 model.removeListDataListener(new ListToTableListenerAdapter(l));
140 }
141
142}
143
144/**
145 * Bridges from ListDataListener to TableModelListeners so that a table can
146 * listen to changes in a ListDataModel.
147 *
148 */
149class ListToTableListenerAdapter implements ListDataListener {
150
151 private TableModelListener tableListener;
152
153 public ListToTableListenerAdapter(TableModelListener l) {
154 tableListener = l;
155 }
156
157 @Override
158 public void intervalAdded(ListDataEvent e) {
159 tableListener.tableChanged(null);
160 }
161
162 @Override
163 public void intervalRemoved(ListDataEvent e) {
164 tableListener.tableChanged(null);
165 }
166
167 @Override
168 public void contentsChanged(ListDataEvent e) {
169 tableListener.tableChanged(null);
170 }
171
172 @Override
173 public int hashCode() {
174 final int prime = 31;
175 int result = 1;
176 result = prime * result + ((tableListener == null) ? 0 : tableListener.hashCode());
177 return result;
178 }
179
180 @Override
181 public boolean equals(Object obj) {
182 if (this == obj)
183 return true;
184 if (obj == null)
185 return false;
186 if (getClass() != obj.getClass())
187 return false;
188 ListToTableListenerAdapter other = (ListToTableListenerAdapter) obj;
189 if (tableListener == null) {
190 if (other.tableListener != null)
191 return false;
192 } else if (!tableListener.equals(other.tableListener))
193 return false;
194 return true;
195 }
196
197}
Note: See TracBrowser for help on using the repository browser.