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