source: src/main/java/agents/EnterBidDialogOffer.java@ 127

Last change on this file since 127 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.0 KB
Line 
1package agents;
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.Bid;
18import genius.core.actions.Accept;
19import genius.core.actions.EndNegotiation;
20import genius.core.actions.Offer;
21import genius.core.exceptions.Warning;
22import genius.core.parties.AbstractNegotiationParty;
23import genius.core.utility.AdditiveUtilitySpace;
24
25/**
26 *
27 * @author W.Pasman
28 */
29public class EnterBidDialogOffer extends JDialog implements
30 EnterBidDialogInterface {
31
32 private static final long serialVersionUID = -8582527630534972702L;
33 private NegoInfo negoinfo; // the table model
34 private genius.core.actions.Action selectedAction;
35 private AbstractNegotiationParty party;
36 private JTextArea negotiationMessages = new JTextArea("NO MESSAGES YET");
37 // Wouter: we have some whitespace in the buttons,
38 // that makes nicer buttons and also artificially increases the window size.
39 private JButton buttonEnd = new JButton("End Negotiation");
40 private JButton buttonBid = new JButton(" Propose ");
41 private JPanel buttonPanel = new JPanel();
42 private JTable BidTable;
43
44 public EnterBidDialogOffer(AbstractNegotiationParty party, Frame parent,
45 boolean modal, AdditiveUtilitySpace us) throws Exception {
46 super(parent, modal);
47 this.party = party;
48 negoinfo = new NegoInfo(null, null, us);
49 initThePanel();
50 }
51
52 // quick hack.. we can't refer to the Agent's utilitySpace because
53 // the field is protected and there is no getUtilitySpace function either.
54 // therefore the Agent has to inform us when utilspace changes.
55 public void setUtilitySpace(AdditiveUtilitySpace us) {
56 negoinfo.utilitySpace = us;
57 }
58
59 private void initThePanel() {
60 if (negoinfo == null)
61 throw new NullPointerException("negoinfo is null");
62 Container pane = getContentPane();
63 pane.setLayout(new BorderLayout());
64 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
65 setTitle("Choose action for party " + party.getPartyId().toString());
66 // setSize(new java.awt.Dimension(600, 400));
67 // setBounds(0,0,640,480);
68
69 // createFrom north field: the message field
70 pane.add(negotiationMessages, "North");
71
72 // createFrom center panel: the bid table
73 BidTable = new JTable(negoinfo);
74 // BidTable.setModel(negoinfo); // need a model for column size etc...
75 // Why doesn't this work???
76 BidTable.setGridColor(Color.lightGray);
77 JPanel tablepane = new JPanel(new BorderLayout());
78 tablepane.add(BidTable.getTableHeader(), "North");
79 tablepane.add(BidTable, "Center");
80 pane.add(tablepane, "Center");
81 BidTable.setRowHeight(35);
82 // createFrom south panel: the buttons:
83 buttonPanel.setLayout(new FlowLayout());
84 buttonPanel.add(buttonEnd);
85 // buttonPanel.add(buttonSkip);
86 buttonPanel.add(buttonBid);
87 pane.add(buttonPanel, "South");
88 buttonBid.setSelected(true);
89
90 // set action listeners for the buttons
91 buttonBid.addActionListener(new java.awt.event.ActionListener() {
92 public void actionPerformed(java.awt.event.ActionEvent evt) {
93 buttonBidActionPerformed(evt);
94 }
95 });
96 // buttonSkip.addActionListener(new java.awt.event.ActionListener() {
97 // public void actionPerformed(java.awt.event.ActionEvent evt) {
98 // buttonSkipActionPerformed(evt);
99 // }
100 // });
101 buttonEnd.addActionListener(new java.awt.event.ActionListener() {
102 public void actionPerformed(java.awt.event.ActionEvent evt) {
103 buttonEndActionPerformed(evt);
104 }
105 });
106 pack(); // pack will do complete layout, getting all cells etc.
107 }
108
109 private Bid getBid() {
110 Bid bid = null;
111 try {
112 bid = negoinfo.getBid();
113 } catch (Exception e) {
114 JOptionPane.showMessageDialog(null,
115 "There is a problem with your bid: " + e.getMessage());
116 }
117 return bid;
118 }
119
120 private void buttonBidActionPerformed(java.awt.event.ActionEvent evt) {
121
122 Bid bid = getBid();
123 if (bid != null) {
124 selectedAction = new Offer(party.getPartyId(), bid);
125 setVisible(false);
126 }
127 }
128
129 private void buttonEndActionPerformed(java.awt.event.ActionEvent evt) {
130 System.out.println("End Negotiation performed");
131 selectedAction = new EndNegotiation(party.getPartyId());
132 this.dispose();
133 }
134
135 /**
136 * This is called by UIAgent repeatedly, to ask for next action.
137 *
138 * @param opponentAction
139 * is action done by opponent
140 * @param myPreviousBid
141 * @return our next negotionat action.
142 */
143 public genius.core.actions.Action askUserForAction(
144 genius.core.actions.Action opponentAction, Bid myPreviousBid) {
145
146 setTitle("Choose action for party " + party.getPartyId().toString());
147 negoinfo.lastAccepted = null;
148 if (opponentAction == null) {
149 negotiationMessages.setText("Opponent did not send any action.");
150 }
151 if (opponentAction instanceof Accept) {
152 negotiationMessages.setText("Opponent accepted your last bid!");
153 negoinfo.lastAccepted = myPreviousBid;
154 }
155 if (opponentAction instanceof EndNegotiation) {
156 negotiationMessages.setText("Opponent cancels the negotiation.");
157 }
158 if (opponentAction instanceof Offer) {
159 negotiationMessages.setText("Opponent proposes the following bid:");
160 negoinfo.lastAccepted = ((Offer) opponentAction).getBid();
161 }
162 try {
163 negoinfo.setOurBid(myPreviousBid);
164 } catch (Exception e) {
165 new Warning("error in askUserForAction:", e, true, 2);
166 }
167
168 BidTable.setDefaultRenderer(BidTable.getColumnClass(0),
169 new MyCellRenderer1(negoinfo));
170 BidTable.setDefaultEditor(BidTable.getColumnClass(0), new MyCellEditor(
171 negoinfo));
172
173 pack();
174 setVisible(true); // this returns only after the panel closes.
175 // Wouter: this WILL return normally if Thread is killed, and the
176 // ThreadDeath exception will disappear.
177 return selectedAction;
178 }
179}
180
181/********************************************************/
182
Note: See TracBrowser for help on using the repository browser.