source: src/main/java/agents/EnterBidDialogAcceptance.java@ 209

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