1 | package geniusweb.exampleparties.humangui;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.util.LinkedList;
|
---|
5 |
|
---|
6 | import javax.swing.Box;
|
---|
7 | import javax.swing.BoxLayout;
|
---|
8 | import javax.swing.DefaultCellEditor;
|
---|
9 | import javax.swing.JButton;
|
---|
10 | import javax.swing.JComboBox;
|
---|
11 | import javax.swing.JOptionPane;
|
---|
12 | import javax.swing.JPanel;
|
---|
13 | import javax.swing.JScrollPane;
|
---|
14 | import javax.swing.JTable;
|
---|
15 | import javax.swing.SwingUtilities;
|
---|
16 | import javax.swing.table.TableCellEditor;
|
---|
17 |
|
---|
18 | import geniusweb.actions.Accept;
|
---|
19 | import geniusweb.actions.EndNegotiation;
|
---|
20 | import geniusweb.actions.Offer;
|
---|
21 | import geniusweb.issuevalue.Value;
|
---|
22 | import geniusweb.profile.Profile;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * the human GUI panel
|
---|
26 | *
|
---|
27 | */
|
---|
28 | @SuppressWarnings("serial")
|
---|
29 | public class MyGUI extends JPanel {
|
---|
30 |
|
---|
31 | // not static because buttons are mutable (have listeners)
|
---|
32 | private final JButton acceptButton = new JButton("Accept");
|
---|
33 | private final JButton offerButton = new JButton("Place Offer");
|
---|
34 | private final JButton stopButton = new JButton("Stop Negotiation");
|
---|
35 | private final BiddingInfo info;
|
---|
36 |
|
---|
37 | public MyGUI(BiddingInfo biddinginfo) {
|
---|
38 | if (biddinginfo == null)
|
---|
39 | throw new NullPointerException("biddinginfo=null");
|
---|
40 | this.info = biddinginfo;
|
---|
41 | setLayout(new BorderLayout());
|
---|
42 | JTable table = new JTable(new BiddingTableModel(info)) {
|
---|
43 | @Override
|
---|
44 | public TableCellEditor getCellEditor(int row, int col) {
|
---|
45 | if (row < 0 || row > info.getIssues().size())
|
---|
46 | return null;
|
---|
47 | String issue = info.getIssues().get(row);
|
---|
48 | LinkedList<Value> values = new LinkedList<Value>();
|
---|
49 | info.getProfile().getDomain().getValues(issue)
|
---|
50 | .forEach(v -> values.add(v));
|
---|
51 | return new DefaultCellEditor(new JComboBox<>(values.toArray()));
|
---|
52 | }
|
---|
53 | };
|
---|
54 | JScrollPane tablecontainer = new JScrollPane(table);
|
---|
55 | add(tablecontainer, BorderLayout.CENTER);
|
---|
56 |
|
---|
57 | JPanel buttonPane = new JPanel();
|
---|
58 | buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
|
---|
59 | buttonPane.add(Box.createHorizontalGlue());
|
---|
60 | buttonPane.add(offerButton);
|
---|
61 | buttonPane.add(acceptButton);
|
---|
62 | buttonPane.add(stopButton);
|
---|
63 | add(buttonPane, BorderLayout.SOUTH);
|
---|
64 |
|
---|
65 | biddinginfo.addListener(data -> updateGui(data));
|
---|
66 | acceptButton.addActionListener(e -> accept());
|
---|
67 | offerButton.addActionListener(e -> offer());
|
---|
68 | stopButton.addActionListener(e -> stop());
|
---|
69 | updateGui(false);
|
---|
70 | }
|
---|
71 |
|
---|
72 | private void offer() {
|
---|
73 | info.doAction(new Offer(info.getMyId(), info.getCurrentBid()));
|
---|
74 | }
|
---|
75 |
|
---|
76 | private void accept() {
|
---|
77 | info.doAction(
|
---|
78 | new Accept(info.getMyId(), info.getReceivedOffer().getBid()));
|
---|
79 | }
|
---|
80 |
|
---|
81 | private void stop() {
|
---|
82 | info.doAction(new EndNegotiation(info.getMyId()));
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Update visibility of the buttons when changes occur in model
|
---|
87 | */
|
---|
88 | private void updateGui(Object change) {
|
---|
89 | if (change instanceof Boolean) {
|
---|
90 | // boolean contains isMyTurn
|
---|
91 | Boolean isEnabled = (Boolean) change;
|
---|
92 | SwingUtilities.invokeLater(new Runnable() {
|
---|
93 | @Override
|
---|
94 | public void run() {
|
---|
95 | offerButton.setEnabled(isEnabled);
|
---|
96 | stopButton.setEnabled(isEnabled);
|
---|
97 | acceptButton.setEnabled(
|
---|
98 | isEnabled && info.getReceivedOffer() != null);
|
---|
99 | }
|
---|
100 | });
|
---|
101 | } else if (change instanceof Profile) {
|
---|
102 | JOptionPane.showMessageDialog(this,
|
---|
103 | "Notice, your preference profile has been changed!");
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | }
|
---|