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