source: exampleparties/humangui/src/main/java/geniusweb/exampleparties/humangui/BiddingTableModel.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

File size: 3.0 KB
Line 
1package geniusweb.exampleparties.humangui;
2
3import java.util.HashMap;
4import java.util.LinkedList;
5import java.util.List;
6import java.util.Map;
7
8import javax.swing.event.TableModelEvent;
9import javax.swing.event.TableModelListener;
10import javax.swing.table.TableModel;
11
12import geniusweb.actions.Offer;
13import geniusweb.issuevalue.Bid;
14import geniusweb.issuevalue.Value;
15
16/**
17 * An adapter that moulds the BiddingInfo into a {@link TableModel}. Makes a
18 * 3-col table: left column the issues, middle col the last received offer, and
19 * right col the offer the user is preparing. The right column can be edited.
20 */
21public class BiddingTableModel implements TableModel {
22 private final static String[] cols = { "Issue", "Last received offer",
23 "Your next offer" };
24 private BiddingInfo info;
25 private final List<TableModelListener> listeners = new LinkedList<>();
26
27 public BiddingTableModel(BiddingInfo info) {
28 this.info = info;
29 info.addListener(data -> update(data));
30 }
31
32 @Override
33 public int getRowCount() {
34 return info.getIssues().size();
35 }
36
37 @Override
38 public int getColumnCount() {
39 return cols.length;
40 }
41
42 @Override
43 public String getColumnName(int columnIndex) {
44 return cols[columnIndex];
45 }
46
47 @Override
48 public Class<?> getColumnClass(int columnIndex) {
49 switch (columnIndex) {
50 case 0:
51 return String.class;
52 case 1:
53 case 2:
54 return Value.class;
55 }
56 return null;
57 }
58
59 @Override
60 public boolean isCellEditable(int rowIndex, int columnIndex) {
61 return columnIndex == 2;
62 }
63
64 @Override
65 public Object getValueAt(int rowIndex, int columnIndex) {
66 String issue = getIssue(rowIndex);
67 if (issue == null)
68 return "";
69 switch (columnIndex) {
70 case 0:
71 return issue;
72 case 1:
73 if (info.getReceivedOffer() == null)
74 return null;
75 return info.getReceivedOffer().getBid().getValue(issue);
76 case 2:
77 return info.getCurrentBid().getValue(issue);
78 }
79 return null;
80 }
81
82 @Override
83 public void setValueAt(Object val, int rowIndex, int columnIndex) {
84 if (!(val instanceof Value))
85 throw new IllegalArgumentException("New val must be a value");
86 String issue = getIssue(rowIndex);
87 if (issue == null)
88 return;
89
90 Map<String, Value> newvalues = new HashMap<>();
91 newvalues.putAll(info.getCurrentBid().getIssueValues());
92 newvalues.put(issue, (Value) val);
93 info.setCurrentBid(new Bid(newvalues));
94 }
95
96 @Override
97 public void addTableModelListener(TableModelListener l) {
98 listeners.add(l);
99 }
100
101 @Override
102 public void removeTableModelListener(TableModelListener l) {
103 listeners.remove(l);
104 }
105
106 /************************ PRIVATE ************************/
107 private void update(Object data) {
108 if (data instanceof Offer) {
109 notifyTableListeners();
110 }
111 }
112
113 /**
114 * @param rowIndex
115 * @return Get issue associated with given row, or null if no such row.
116 */
117 private String getIssue(int rowIndex) {
118 if (rowIndex < 0 || rowIndex >= info.getIssues().size())
119 return null;
120 return info.getIssues().get(rowIndex);
121
122 }
123
124 private void notifyTableListeners() {
125 for (TableModelListener l : listeners) {
126 l.tableChanged(new TableModelEvent(this));
127 }
128 }
129
130}
Note: See TracBrowser for help on using the repository browser.