source: exampleparties/humangui/src/main/java/geniusweb/exampleparties/humangui/MyGUI.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.1 KB
Line 
1package geniusweb.exampleparties.humangui;
2
3import java.awt.BorderLayout;
4import java.util.LinkedList;
5
6import javax.swing.Box;
7import javax.swing.BoxLayout;
8import javax.swing.DefaultCellEditor;
9import javax.swing.JButton;
10import javax.swing.JComboBox;
11import javax.swing.JOptionPane;
12import javax.swing.JPanel;
13import javax.swing.JScrollPane;
14import javax.swing.JTable;
15import javax.swing.SwingUtilities;
16import javax.swing.table.TableCellEditor;
17
18import geniusweb.actions.Accept;
19import geniusweb.actions.EndNegotiation;
20import geniusweb.actions.Offer;
21import geniusweb.issuevalue.Value;
22import geniusweb.profile.Profile;
23
24/**
25 * the human GUI panel
26 *
27 */
28@SuppressWarnings("serial")
29public 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}
Note: See TracBrowser for help on using the repository browser.