source: src/main/java/negotiator/parties/EnterBidDialog.java@ 48

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

Initial import : Genius 9.0.0

File size: 7.2 KB
Line 
1package negotiator.parties;
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 lastOppBid
52 * last oppponent bid that can be accepted, or null if no such
53 * bid.
54 * @throws Exception
55 */
56 public EnterBidDialog(Agent agent, java.awt.Frame parent, boolean modal,
57 AdditiveUtilitySpace us, Bid lastOppBid) throws Exception {
58 super(parent, modal);
59 this.agent = agent;
60 negoinfo = new NegoInfo(null, null, us);
61 this.lastOppBid = lastOppBid;
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
96 buttonAccept.setEnabled(lastOppBid != null);
97 // createFrom south panel: the buttons:
98 buttonPanel.setLayout(new FlowLayout());
99 buttonPanel.add(buttonEnd);
100 buttonPanel.add(buttonAccept);
101 // buttonPanel.add(buttonSkip);
102 buttonPanel.add(buttonBid);
103 pane.add(buttonPanel, "South");
104 buttonBid.setSelected(true);
105
106 // set action listeners for the buttons
107 buttonBid.addActionListener(new java.awt.event.ActionListener() {
108 public void actionPerformed(java.awt.event.ActionEvent evt) {
109 buttonBidActionPerformed(evt);
110 }
111 });
112 // buttonSkip.addActionListener(new java.awt.event.ActionListener() {
113 // public void actionPerformed(java.awt.event.ActionEvent evt) {
114 // buttonSkipActionPerformed(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,
136 "There is a problem with your bid: " + e.getMessage());
137 }
138 return bid;
139 }
140
141 private void buttonBidActionPerformed(java.awt.event.ActionEvent evt) {
142
143 Bid bid = getBid();
144 if (bid != null) {
145 selectedAction = new Offer(agent.getAgentID(), bid);
146 setVisible(false);
147 }
148 }
149
150 private void buttonAcceptActionPerformed(java.awt.event.ActionEvent evt) {
151 Bid bid = getBid();
152 if (bid != null) {
153 System.out.println("Accept performed");
154 selectedAction = new Accept(agent.getAgentID(), lastOppBid);
155 setVisible(false);
156 }
157 }
158
159 private void buttonEndActionPerformed(java.awt.event.ActionEvent evt) {
160 System.out.println("End Negotiation performed");
161 selectedAction = new EndNegotiation(agent.getAgentID());
162 setVisible(false);
163 }
164
165 /**
166 * This is called by UIAgent repeatedly, to ask for next action.
167 *
168 * @param opponentAction
169 * is action done by opponent
170 * @param myPreviousBid
171 * @return our next negotionat action.
172 */
173 public genius.core.actions.Action askUserForAction(
174 genius.core.actions.Action opponentAction, Bid myPreviousBid) {
175 negoinfo.lastAccepted = null;
176 if (opponentAction == null) {
177 negotiationMessages.setText("Opponent did not send any action.");
178 }
179 if (opponentAction instanceof Accept) {
180 negotiationMessages.setText("Opponent accepted your last bid!");
181 negoinfo.lastAccepted = myPreviousBid;
182 }
183 if (opponentAction instanceof EndNegotiation) {
184 negotiationMessages.setText("Opponent cancels the negotiation.");
185 }
186 if (opponentAction instanceof Offer) {
187 negotiationMessages.setText("Opponent proposes the following bid:");
188 negoinfo.lastAccepted = ((Offer) opponentAction).getBid();
189 }
190 try {
191 negoinfo.setOurBid(myPreviousBid);
192 } catch (Exception e) {
193 new Warning("error in askUserForAction:", e, true, 2);
194 }
195
196 BidTable.setDefaultRenderer(BidTable.getColumnClass(0),
197 new MyCellRenderer1(negoinfo));
198 BidTable.setDefaultEditor(BidTable.getColumnClass(0), new MyCellEditor(
199 negoinfo));
200
201 pack();
202 setVisible(true); // this returns only after the panel closes.
203 // Wouter: this WILL return normally if Thread is killed, and the
204 // ThreadDeath exception will disappear.
205 return selectedAction;
206 }
207}
208
209/********************************************************************/
210
211class MyCellRenderer1 implements TableCellRenderer {
212 NegoInfo negoinfo;
213
214 public MyCellRenderer1(NegoInfo n) {
215 negoinfo = n;
216 }
217
218 // the default converts everything to string...
219 public Component getTableCellRendererComponent(JTable table, Object value,
220 boolean isSelected, boolean hasFocus, int row, int column) {
221 return negoinfo.getValueAt(row, column);
222 }
223}
224
225/********************************************************/
226
Note: See TracBrowser for help on using the repository browser.