source: src/main/java/negotiator/parties/EnterBidDialog2.java@ 256

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

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 6.7 KB
Line 
1package negotiator.parties;
2
3import java.awt.BorderLayout;
4import java.awt.Color;
5import java.awt.Container;
6import java.awt.FlowLayout;
7import java.awt.Frame;
8
9import javax.swing.JButton;
10import javax.swing.JDialog;
11import javax.swing.JOptionPane;
12import javax.swing.JPanel;
13import javax.swing.JTable;
14import javax.swing.JTextArea;
15import javax.swing.WindowConstants;
16
17import genius.core.AgentID;
18import genius.core.Bid;
19import genius.core.actions.Accept;
20import genius.core.actions.EndNegotiation;
21import genius.core.actions.Offer;
22import genius.core.exceptions.Warning;
23import genius.core.parties.NegotiationParty;
24import genius.core.utility.AdditiveUtilitySpace;
25
26/**
27 * Ask user to enter bid details. Modal dialog. only works with
28 * {@link AdditiveUtilitySpace}
29 *
30 * @author W.Pasman
31 */
32public class EnterBidDialog2 extends JDialog implements EnterBidDialogInterface {
33
34 // we have some whitespace in the buttons,
35 // that makes nicer buttons and also artificially increases the window size.
36 public static final String DO_BID = " Do Bid ";
37 public static final String END_NEGOTIATION = "End Negotiation";
38 public static final String ACCEPT = " Accept Opponent Bid ";
39
40 private static final long serialVersionUID = -8582527630534972700L;
41 private NegoInfo negoinfo; // the table model
42 private genius.core.actions.Action selectedAction;
43 private NegotiationParty party;
44 private JTextArea negotiationMessages = new JTextArea("NO MESSAGES YET");
45 private JButton buttonAccept = button(ACCEPT);
46 private JButton buttonEnd = button(END_NEGOTIATION);
47 private JButton buttonBid = button(DO_BID);
48 private JPanel buttonPanel = new JPanel();
49 private JTable BidTable;
50 private Bid lastOppBid;
51 private AgentID partyID;
52
53 /**
54 * Create dialog but do not yet show it. Call {@link #setVisible(boolean)}
55 * to show.
56 *
57 * @param party
58 * @param parent
59 * @param modal
60 * @param us
61 * @param lastOppBid
62 * last oppponent bid that can be accepted, or null if no such
63 * bid.
64 * @throws Exception
65 */
66 public EnterBidDialog2(NegotiationParty party, AgentID id, Frame parent, boolean modal, AdditiveUtilitySpace us,
67 Bid lastOppBid) throws Exception {
68 super(parent, modal);
69 this.party = party;
70 this.partyID = id;
71 this.lastOppBid = lastOppBid;
72 negoinfo = new NegoInfo(null, null, us);
73 initThePanel();
74 }
75
76 private JButton button(String name) {
77 JButton button = new JButton(name);
78 button.setName(name);
79 return button;
80 }
81
82 private void initThePanel() {
83 if (negoinfo == null)
84 throw new NullPointerException("negoinfo is null");
85 Container pane = getContentPane();
86 pane.setLayout(new BorderLayout());
87 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
88 setTitle("Choose action for party " + partyID.toString());
89
90 // createFrom north field: the message field
91 pane.add(negotiationMessages, "North");
92
93 // createFrom center panel: the bid table
94 BidTable = new JTable(negoinfo);
95 BidTable.setGridColor(Color.lightGray);
96 JPanel tablepane = new JPanel(new BorderLayout());
97 tablepane.add(BidTable.getTableHeader(), "North");
98 tablepane.add(BidTable, "Center");
99 pane.add(tablepane, "Center");
100 BidTable.setRowHeight(35);
101
102 // createFrom south panel: the buttons:
103 buttonPanel.setLayout(new FlowLayout());
104 buttonPanel.add(buttonEnd);
105 buttonAccept.setEnabled(lastOppBid != null);
106 buttonPanel.add(buttonAccept);
107 buttonPanel.add(buttonBid);
108 pane.add(buttonPanel, "South");
109 buttonBid.setSelected(true);
110
111 // set action listeners for the buttons
112 buttonBid.addActionListener(new java.awt.event.ActionListener() {
113 public void actionPerformed(java.awt.event.ActionEvent evt) {
114 buttonBidActionPerformed(evt);
115 }
116 });
117 buttonEnd.addActionListener(new java.awt.event.ActionListener() {
118 public void actionPerformed(java.awt.event.ActionEvent evt) {
119 buttonEndActionPerformed(evt);
120 }
121 });
122 buttonAccept.addActionListener(new java.awt.event.ActionListener() {
123 public void actionPerformed(java.awt.event.ActionEvent evt) {
124 buttonAcceptActionPerformed(evt);
125 }
126 });
127 pack(); // pack will do complete layout, getting all cells etc.
128 }
129
130 private Bid getBid() {
131 Bid bid = null;
132 try {
133 bid = negoinfo.getBid();
134 } catch (Exception e) {
135 JOptionPane.showMessageDialog(null, "There is a problem with your bid: " + e.getMessage());
136 }
137 return bid;
138 }
139
140 private void buttonBidActionPerformed(java.awt.event.ActionEvent evt) {
141
142 Bid bid = getBid();
143 if (bid != null) {
144 selectedAction = new Offer(partyID, bid);
145 setVisible(false);
146 }
147 }
148
149 private void buttonAcceptActionPerformed(java.awt.event.ActionEvent evt) {
150 Bid bid = getBid();
151 if (bid != null) {
152 System.out.println("Accept performed");
153 selectedAction = new Accept(partyID, lastOppBid);
154 setVisible(false);
155 }
156 }
157
158 private void buttonEndActionPerformed(java.awt.event.ActionEvent evt) {
159 System.out.println("End Negotiation performed");
160 selectedAction = new EndNegotiation(partyID);
161 setVisible(false);
162 }
163
164 public genius.core.actions.Action askUserForAction(genius.core.actions.Action opponentAction, Bid myPreviousBid,
165 Bid mostRecentOffer) {
166 negoinfo.lastAccepted = mostRecentOffer;
167 return askUserForAction(opponentAction, myPreviousBid);
168 }
169
170 /**
171 * This is called by UIAgent repeatedly, to ask for next action.
172 *
173 * @param opponentAction
174 * is action done by opponent
175 * @param myPreviousBid
176 * @return our next negotionat action.
177 */
178 public genius.core.actions.Action askUserForAction(genius.core.actions.Action opponentAction, Bid myPreviousBid) {
179
180 setTitle("Choose action for party " + partyID.toString());
181 if (opponentAction == null) {
182 negotiationMessages.setText("Opponent did not send any action.");
183 }
184 if (opponentAction instanceof Accept) {
185 negotiationMessages.setText("Opponent accepted your last bid!");
186 }
187 if (opponentAction instanceof EndNegotiation) {
188 negotiationMessages.setText("Opponent cancels the negotiation.");
189 }
190 if (opponentAction instanceof Offer) {
191 negotiationMessages.setText("Opponent proposes the following bid:");
192 }
193 try {
194 negoinfo.setOurBid(myPreviousBid);
195 } catch (Exception e) {
196 new Warning("error in askUserForAction:", e, true, 2);
197 }
198
199 BidTable.setDefaultRenderer(BidTable.getColumnClass(0), new MyCellRenderer1(negoinfo));
200 BidTable.setDefaultEditor(BidTable.getColumnClass(0), new MyCellEditor(negoinfo));
201
202 pack();
203 setVisible(true); // this returns only after the panel closes.
204 // Wouter: this WILL return normally if Thread is killed, and the
205 // ThreadDeath exception will disappear.
206 return selectedAction;
207 }
208}
209
210/********************************************************/
Note: See TracBrowser for help on using the repository browser.