source: src/main/java/agents/EnterBidDialogExtended.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: 27.2 KB
Line 
1/*
2 * EnterBidDialog.java
3 *
4 * Created on November 16, 2006, 10:18 AM
5 */
6
7package agents;
8
9import java.awt.BorderLayout;
10import java.awt.Color;
11import java.awt.Component;
12import java.awt.Container;
13import java.awt.Dimension;
14import java.awt.FlowLayout;
15import java.awt.GridBagConstraints;
16import java.awt.GridBagLayout;
17import java.awt.Insets;
18import java.awt.event.ActionEvent;
19import java.awt.event.ActionListener;
20import java.text.DecimalFormat;
21import java.util.ArrayList;
22import java.util.HashMap;
23import java.util.List;
24
25import javax.swing.BorderFactory;
26import javax.swing.BoxLayout;
27import javax.swing.DefaultCellEditor;
28import javax.swing.JButton;
29import javax.swing.JComboBox;
30import javax.swing.JDialog;
31import javax.swing.JLabel;
32import javax.swing.JOptionPane;
33import javax.swing.JPanel;
34import javax.swing.JProgressBar;
35import javax.swing.JTable;
36import javax.swing.JTextArea;
37import javax.swing.JTextField;
38import javax.swing.border.Border;
39import javax.swing.border.TitledBorder;
40import javax.swing.table.AbstractTableModel;
41import javax.swing.table.TableCellRenderer;
42
43import org.jfree.chart.ChartPanel;
44import org.jfree.chart.JFreeChart;
45
46import genius.core.Bid;
47import genius.core.actions.Accept;
48import genius.core.actions.EndNegotiation;
49import genius.core.actions.Offer;
50import genius.core.exceptions.Warning;
51import genius.core.issue.Issue;
52import genius.core.issue.IssueDiscrete;
53import genius.core.issue.Value;
54import genius.core.issue.ValueDiscrete;
55import genius.core.utility.AdditiveUtilitySpace;
56import genius.core.utility.Evaluator;
57import genius.core.utility.EvaluatorDiscrete;
58import genius.gui.chart.UtilityPlot;
59
60/**
61 * This dialog works based on the {@link Evaluator}s and therefore only can be
62 * used with {@link AdditiveUtilitySpace}. TODO can we lift this requirement?
63 *
64 * @author W.Pasman
65 */
66@SuppressWarnings("serial")
67public class EnterBidDialogExtended extends JDialog {
68
69 private NegoInfo negoinfo; // the table model
70 private genius.core.actions.Action selectedAction;
71 private UIAgentExtended agent;
72 private JTextArea negotiationMessages = new JTextArea("NO MESSAGES YET");
73 // Wouter: we have some whitespace in the buttons,
74 // that makes nicer buttons and also artificially increases the window size.
75 private JButton buttonAccept = new JButton(" Accept Opponent Bid ");
76 private JButton buttonEnd = new JButton("End Negotiation");
77 private JButton buttonBid = new JButton(" Do Bid ");
78 private JPanel buttonPanel = new JPanel();
79 private JTable BidTable;
80
81 // alinas variables
82 private JTable BidHistoryTable;
83 private HistoryInfo historyinfo; // the table model
84 private ChartPanel chartPanel;
85 private JPanel defaultChartPanel;
86 // private ScatterPlot plot;
87 private UtilityPlot plot;
88 private Bid lastOppBid;
89
90 /**
91 *
92 * @param agent
93 * @param parent
94 * @param modal
95 * @param us
96 * @param lastOpponentBid
97 * the last opponent bid, or null if no bid has been offered. If
98 * not null, the acceptbutton is disabled.
99 * @throws Exception
100 */
101 public EnterBidDialogExtended(UIAgentExtended agent, java.awt.Frame parent,
102 boolean modal, AdditiveUtilitySpace us, Bid lastOpponentBid)
103 throws Exception {
104 super(parent, modal);
105 this.agent = agent;
106 negoinfo = new NegoInfo(null, null, us);
107 historyinfo = new HistoryInfo(agent, null, null, us);
108 lastOppBid = lastOpponentBid;
109 initThePanel();
110 }
111
112 // quick hack.. we can't refer to the Agent's utilitySpace because
113 // the field is protected and there is no getUtilitySpace function either.
114 // therefore the Agent has to inform us when utilspace changes.
115 public void setUtilitySpace(AdditiveUtilitySpace us) {
116 negoinfo.utilitySpace = us;
117 historyinfo.utilitySpace = us;
118 }
119
120 private void initThePanel() {
121 if (negoinfo == null)
122 throw new NullPointerException("negoinfo is null");
123 Container pane = getContentPane();
124 // gridbag layout:
125 GridBagLayout gridbag = new GridBagLayout();
126 GridBagConstraints c = new GridBagConstraints();
127
128 pane.setLayout(gridbag);
129
130 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
131 setTitle("Choose action for agent " + agent.getName());
132 // setSize(new java.awt.Dimension(600, 400));
133 // setBounds(0,0,640,480);
134
135 c.gridx = 0;
136 c.gridy = 1;
137 c.gridwidth = 2;
138 c.fill = GridBagConstraints.HORIZONTAL;
139 c.insets = new Insets(0, 10, 0, 10);
140 // createFrom panel for history of bids
141 BidHistoryTable = new JTable(historyinfo);
142 BidHistoryTable.setGridColor(Color.lightGray);
143 // setting the columns that contain numbers to a small width:
144 BidHistoryTable.getColumnModel().getColumn(0).setMaxWidth(50);
145 BidHistoryTable.getColumnModel().getColumn(2).setMaxWidth(50);
146 BidHistoryTable.getColumnModel().getColumn(4).setMaxWidth(50);
147 JPanel tablepaneHistory = new JPanel(new BorderLayout());
148 tablepaneHistory.add(BidHistoryTable.getTableHeader(), "North");
149 tablepaneHistory.add(BidHistoryTable, "Center");
150 Border blackline = BorderFactory.createLineBorder(Color.black);
151 TitledBorder title = BorderFactory.createTitledBorder(blackline,
152 "History of Bids:");
153 // for having the title in the center
154 // title.setTitleJustification(TitledBorder.CENTER);
155 tablepaneHistory.setBorder(title);
156 pane.add(tablepaneHistory, c);
157
158 c.gridwidth = 1;
159 c.gridheight = 4;
160 c.gridx = 0;
161 c.gridy = 2;
162 c.insets = new Insets(10, 10, 10, 10);
163 // adding the chart
164 defaultChartPanel = new JPanel();
165 title = BorderFactory.createTitledBorder(blackline,
166 "Utilities of Bids per round:");
167
168 defaultChartPanel.setBorder(title);
169 pane.remove(defaultChartPanel);
170 pane.add(defaultChartPanel, c);
171
172 // user input:
173 JPanel userInputPanel = new JPanel();
174 userInputPanel
175 .setLayout(new BoxLayout(userInputPanel, BoxLayout.Y_AXIS));
176 title = BorderFactory.createTitledBorder(blackline,
177 "Please place your bid:");
178
179 userInputPanel.setBorder(title);
180 negotiationMessages.setBackground(Color.lightGray);
181 negotiationMessages.setEditable(false);
182 userInputPanel.add(negotiationMessages);
183
184 // createFrom center panel: the bid table
185 BidTable = new JTable(negoinfo);
186 // BidTable.setModel(negoinfo); // need a model for column size etc...
187 // Why doesn't this work???
188 BidTable.setGridColor(Color.lightGray);
189 BidTable.setRowHeight(18);
190 JPanel tablepane = new JPanel(new BorderLayout());
191 tablepane.add(BidTable.getTableHeader(), "North");
192 tablepane.add(BidTable, "Center");
193 userInputPanel.add(tablepane);
194
195 if (lastOppBid == null) {
196 buttonAccept.setEnabled(false);
197 }
198
199 // createFrom the buttons:
200 buttonPanel.setLayout(new FlowLayout());
201 buttonPanel.add(buttonEnd);
202 buttonPanel.add(buttonAccept);
203 buttonPanel.add(buttonBid);
204 userInputPanel.add(buttonPanel);
205
206 c.gridwidth = 1;
207 c.gridheight = 1;
208 c.gridx = 1;
209 c.gridy = 3;
210 c.weighty = 0;
211 c.fill = GridBagConstraints.HORIZONTAL;
212 c.insets = new Insets(10, 10, 10, 10);
213 pane.add(userInputPanel, c);
214 buttonBid.setSelected(true);
215
216 // set action listeners for the buttons
217 buttonBid.addActionListener(new java.awt.event.ActionListener() {
218 public void actionPerformed(java.awt.event.ActionEvent evt) {
219 buttonBidActionPerformed(evt);
220 }
221 });
222 // buttonSkip.addActionListener(new java.awt.event.ActionListener() {
223 // public void actionPerformed(java.awt.event.ActionEvent evt) {
224 // buttonSkipActionPerformed(evt);
225 // }
226 // });
227 buttonEnd.addActionListener(new java.awt.event.ActionListener() {
228 public void actionPerformed(java.awt.event.ActionEvent evt) {
229 buttonEndActionPerformed(evt);
230 }
231 });
232 buttonAccept.addActionListener(new java.awt.event.ActionListener() {
233 public void actionPerformed(java.awt.event.ActionEvent evt) {
234 buttonAcceptActionPerformed(evt);
235 }
236 });
237 pack(); // pack will do complete layout, getting all cells etc.
238 }
239
240 private Bid getBid() {
241 Bid bid = null;
242 try {
243 bid = negoinfo.getBid();
244 } catch (Exception e) {
245 JOptionPane.showMessageDialog(null,
246 "There is a problem with your bid: " + e.getMessage());
247 }
248 return bid;
249 }
250
251 private void buttonBidActionPerformed(java.awt.event.ActionEvent evt) {
252
253 Bid bid = getBid();
254 if (bid != null) {
255 selectedAction = new Offer(agent.getAgentID(), bid);
256 setVisible(false);
257 }
258 }
259
260 private void buttonAcceptActionPerformed(java.awt.event.ActionEvent evt) {
261 Bid bid = getBid();
262 if (bid != null) {
263 System.out.println("Accept performed");
264 selectedAction = new Accept(agent.getAgentID(), lastOppBid);
265 setVisible(false);
266 }
267 }
268
269 private void buttonEndActionPerformed(java.awt.event.ActionEvent evt) {
270 System.out.println("End Negotiation performed");
271 selectedAction = new EndNegotiation(agent.getAgentID());
272 setVisible(false);
273 }
274
275 /**
276 * This is called by UIAgent repeatedly, to ask for next action.
277 *
278 * @param opponentAction
279 * is action done by opponent
280 * @param myPreviousBid
281 * @return our next negotionat action.
282 */
283 public genius.core.actions.Action askUserForAction(
284 genius.core.actions.Action opponentAction, Bid myPreviousBid) {
285 historyinfo.nrOfBids = agent.historyOfBids.size();
286 negoinfo.lastAccepted = null;
287
288 if (opponentAction == null) {
289 negotiationMessages.setText("Opponent did not send any action.");
290 }
291 if (opponentAction instanceof Accept) {
292 negotiationMessages.setText("Opponent accepted your last bid!");
293 negoinfo.lastAccepted = myPreviousBid;
294
295 }
296 if (opponentAction instanceof EndNegotiation) {
297 negotiationMessages.setText("Opponent cancels the negotiation.");
298 }
299 if (opponentAction instanceof Offer) {
300 negotiationMessages.setText("Opponent proposes the following bid:");
301 negoinfo.lastAccepted = ((Offer) opponentAction).getBid();
302 }
303 try {
304 negoinfo.setOurBid(myPreviousBid);
305 } catch (Exception e) {
306 System.out.println("error in askUserForAction:" + e.getMessage());
307 e.printStackTrace();
308 }
309
310 BidTable.setDefaultRenderer(BidTable.getColumnClass(0),
311 new MyCellRenderer(negoinfo));
312 BidHistoryTable.setDefaultRenderer(BidHistoryTable.getColumnClass(0),
313 new MyHistoryCellRenderer(historyinfo));
314 BidHistoryTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // needs some
315 // fixing so
316 // that the
317 // bids are
318 // visible
319 // properly
320 BidTable.setDefaultEditor(BidTable.getColumnClass(0), new MyCellEditor(
321 negoinfo));
322
323 int round = agent.bidCounter;
324 System.out
325 .println("round# " + round + "/" + agent.historyOfBids.size());
326
327 // createFrom a new plot of the bid utilities for each round
328 double[][] myBidSeries = new double[2][round];
329 double[][] oppBidSeries = new double[2][round];
330
331 if (round > 0) {
332 System.out.println(agent.historyOfBids.get(0));
333 for (int i = 0; i < round; i++) {
334 try {
335 System.out.println("i " + i);
336 Bid oppBid = agent.historyOfBids.get(i).getOppentBid();
337 Bid ourBid = agent.historyOfBids.get(i).getOurBid();
338 double utilOpp = 0;
339 double ourUtil = 0;
340
341 if (agent.utilitySpace != null) {
342 if (oppBid != null)
343 utilOpp = agent.utilitySpace.getUtility(oppBid);
344 ourUtil = agent.utilitySpace.getUtility(ourBid);
345 } else {
346 System.out.println("agent.utilSpace=null");
347 }
348
349 myBidSeries[0][i] = i + 1;
350 myBidSeries[1][i] = ourUtil;
351
352 oppBidSeries[0][i] = i + 1;
353 oppBidSeries[1][i] = utilOpp;
354
355 } catch (Exception e) {
356 e.printStackTrace();
357 }
358 }
359 }
360
361 // if there is a chart already, remove and draw new one
362 if (defaultChartPanel.getComponents().length > 0)
363 defaultChartPanel.remove(chartPanel);
364 // plot = new ScatterPlot(myBidSeries, oppBidSeries);
365 plot = new UtilityPlot(myBidSeries, oppBidSeries);
366 JFreeChart chart = plot.getChart();
367 chartPanel = new ChartPanel(chart);
368 chartPanel.setPreferredSize(new Dimension(350, 350));
369 defaultChartPanel.add(chartPanel);
370
371 pack();
372 repaint();
373 setVisible(true); // this returns only after the panel closes.
374 negoinfo.comboBoxes.get(0).requestFocusInWindow();
375 return selectedAction;
376 }
377}
378
379/********************************************************/
380
381/**
382 * @author Alina HistoryInfo is the class that contains the former bids and
383 * fills the JTable for the history with it.
384 */
385@SuppressWarnings("serial")
386class HistoryInfo extends AbstractTableModel {
387 public Bid ourOldBid;
388 public Bid oppOldBid;
389 public int nrOfBids = 0;
390 private UIAgentExtended agent;
391 private String[] colNames = { "Round", "Bid of your Opponent", "u(opp)",
392 "Your Bid", "u(own)" };
393
394 public String getColumnName(int col) {
395 return colNames[col];
396 }
397
398 public AdditiveUtilitySpace utilitySpace;
399
400 HistoryInfo(UIAgentExtended agent, Bid our, Bid opponent,
401 AdditiveUtilitySpace us) throws Exception {
402 this.agent = agent;
403 utilitySpace = us;
404 }
405
406 public int getColumnCount() {
407 return 5;
408 }
409
410 public int getRowCount() {
411 return 10; // needs to be dynamically changed, right now we will have a
412 // problem when there are more than 10 rounds
413 }
414
415 public Component getValueAt(int row, int col) {
416
417 if (nrOfBids != 0 && row < nrOfBids) {
418 // get the bids for the row-th round:
419 Bid oppBid = agent.historyOfBids.get(row).getOppentBid();
420 Bid ourBid = agent.historyOfBids.get(row).getOurBid();
421
422 switch (col) {
423 case 0:
424 return new JLabel(Integer.toString(row + 1));// roundcount
425 case 1:
426 String str1 = "No Bid yet.";
427 if (oppBid != null) {
428 str1 = new String(oppBid.toString());
429 str1 = str1.substring(4, str1.length() - 3);
430 }
431 return new JTextArea(str1); // opponent bid as string
432 case 2:
433 try {
434 double utilOpp = 0.0;
435 if (oppBid != null)
436 utilOpp = utilitySpace.getUtility(oppBid);// utility of
437 // opponent
438 // bid
439
440 DecimalFormat df = new DecimalFormat("0.00");
441 return new JTextArea(df.format(utilOpp));
442 } catch (Exception e) {
443 }
444 ;
445 case 3:
446 String str2 = new String(ourBid.toString());
447 str2 = str2.substring(4, str2.length() - 3);
448 return new JTextArea(str2); // our bid as string
449 case 4:
450 try {
451 double utilOur = utilitySpace.getUtility(ourBid);// utility
452 // of
453 // our
454 // bid
455 DecimalFormat df = new DecimalFormat("0.00");
456 return new JTextArea(df.format(utilOur));
457 } catch (Exception e) {
458 }
459 ;
460 }
461 }
462
463 return null;
464 }
465
466}
467
468/********************************************************/
469
470/**
471 * NegoInfo is the class that contains all the negotiation data, and handles the
472 * GUI, updating the JTable. This is the main interface to the actual JTable.
473 * This is usually called XXXModel but I dont like the 'model' in the name. We
474 * implement actionlistener to hear the combo box events that require
475 * re-rendering of the total cost and utility field. We are pretty hard-wired
476 * for a 3-column table, with column 0 the labels, column 1 the opponent bid and
477 * col2 our own bid.
478 */
479@SuppressWarnings("serial")
480class NegoInfo extends AbstractTableModel implements ActionListener {
481 public Bid ourOldBid; // Bid is hashmap <issueID,Value>. Our current bid is
482 // only in the comboboxes,
483 // use getBid().
484 public Bid lastAccepted;
485 public AdditiveUtilitySpace utilitySpace; // WARNING: this may be null
486 public List<Issue> issues = new ArrayList<Issue>();
487 // the issues, in row order as in the GUI. Init to empty, to enable
488 // freshly initialized NegoInfo to give useful results to the GUI.
489 public ArrayList<Integer> IDs; // the IDs/numbers of the issues, ordered to
490 // row number
491 public ArrayList<JComboBox> comboBoxes; // the combo boxes for the second
492 // column, ordered to row number
493
494 NegoInfo(Bid our, Bid lastAccepted, AdditiveUtilitySpace us)
495 throws Exception {
496 // Wouter: just discovered that assert does nothing...........
497 // David@Wouter: Assert only works when assert compile flag is set to
498 // true
499 ourOldBid = our;
500 this.lastAccepted = lastAccepted;
501 utilitySpace = us;
502 issues = utilitySpace.getDomain().getIssues();
503 IDs = new ArrayList<Integer>();
504 for (int i = 0; i < issues.size(); i++)
505 IDs.add(issues.get(i).getNumber());
506 makeComboBoxes();
507 }
508
509 public int getColumnCount() {
510 return 3;
511 }
512
513 public int getRowCount() {
514 return issues.size() + 2;
515 }
516
517 public boolean isCellEditable(int row, int col) {
518 return (col == 2 && row < issues.size());
519 }
520
521 private String[] colNames = { "Issue", "Last Accepted Bid", "Your bid" };
522
523 public String getColumnName(int col) {
524 return colNames[col];
525 }
526
527 public void setOurBid(Bid bid) throws Exception {
528 ourOldBid = bid;
529 if (bid == null)
530 try {
531 ourOldBid = utilitySpace.getMaxUtilityBid();
532 } catch (Exception e) {
533 System.out.println("error getting max utility first bid:"
534 + e.getMessage());
535 e.printStackTrace();
536 }
537 makeComboBoxes(); // reset all
538 setComboBoxes(ourOldBid);
539 }
540
541 void makeComboBoxes() throws Exception {
542 comboBoxes = new ArrayList<JComboBox>();
543 for (Issue issue : issues) {
544 if (!(issue instanceof IssueDiscrete))
545 System.out.println("Problem: issue " + issue
546 + " is not IssueDiscrete. ");
547 List<ValueDiscrete> values = ((IssueDiscrete) issue).getValues();
548 JComboBox cbox = new JComboBox();
549 EvaluatorDiscrete eval = null;
550 if (utilitySpace != null)
551 eval = (EvaluatorDiscrete) utilitySpace.getEvaluator(issue
552 .getNumber());
553 for (ValueDiscrete val : values) {
554 String utilinfo = "";
555 if (eval != null)
556 try {
557 // utilinfo="("+eval.getEvaluation(val)+")";
558 utilinfo = "(" + eval.getValue(val) + ")";
559
560 } catch (Exception e) {
561 System.out.println("no evaluator for " + val + "???");
562 }
563
564 cbox.addItem(val + utilinfo);
565 }
566 comboBoxes.add(cbox);
567 for (JComboBox b : comboBoxes)
568 b.addActionListener(this);
569 }
570 }
571
572 /**
573 * set the initial combo box selections according to ourOldBid Note, we can
574 * only handle Discrete evaluators right now.
575 *
576 * @throws if
577 * there is a problem with the issues and evaluators.
578 */
579 void setComboBoxes(Bid bid) throws Exception {
580 for (int i = 0; i < issues.size(); i++) {
581 IssueDiscrete iss = (IssueDiscrete) issues.get(i);
582 ValueDiscrete val = (ValueDiscrete) bid.getValue(iss.getNumber());
583 comboBoxes.get(i).setSelectedIndex(
584 ((IssueDiscrete) iss).getValueIndex(val));
585 }
586 }
587
588 /**
589 * get the currently chosen evaluation value of given row in the table.
590 *
591 * @param bid
592 * : which bid (the column in the table are for ourBid and
593 * opponentBid)
594 * @return the evaluation of the given row in the bid. returns null if the
595 * bid has no value in that row.
596 * @throws probablly
597 * if rownr is out of range 0...issues.size()-1
598 */
599 Value getCurrentEval(Bid bid, int rownr) throws Exception {
600 if (bid == null)
601 return null;
602 Integer ID = IDs.get(rownr); // get ID of the issue in question.
603 return bid.getValue(ID); // get the current value for that issue in the
604 // bid
605 }
606
607 /**
608 * get a render component
609 *
610 * @return the Component that can be rendered to show this cell.
611 */
612 public Component getValueAt(int row, int col) {
613 if (row == issues.size()) {
614 if (col == 0)
615 return new JLabel("Utility:");
616 if (utilitySpace == null)
617 return new JLabel("No UtilSpace");
618 Bid bid;
619 if (col == 1)
620 bid = lastAccepted;
621 else
622 try {
623 bid = getBid();
624 } catch (Exception e) {
625 bid = null;
626 System.out.println("Internal err with getBid:"
627 + e.getMessage());
628 }
629 ;
630 JProgressBar bar = new JProgressBar(0, 100);
631 bar.setStringPainted(true);
632 try {
633 bar.setValue((int) (0.5 + 100.0 * utilitySpace.getUtility(bid)));
634 bar.setIndeterminate(false);
635 } catch (Exception e) {
636 new Warning("Exception during cost calculation:"
637 + e.getMessage(), false, 1);
638 bar.setIndeterminate(true);
639 }
640
641 return bar;
642 }
643 if (row == issues.size() + 1) {
644 return null;
645 }
646 switch (col) {
647 case 0:
648 return new JTextArea(issues.get(row).getName());
649 case 1:
650 Value value = null;
651 try {
652 value = getCurrentEval(lastAccepted, row);
653 } catch (Exception e) {
654 System.out.println("Err EnterBidDialog2.getValueAt: "
655 + e.getMessage());
656 }
657 if (value == null)
658 return new JTextArea("-");
659 return new JTextArea(value.toString());
660 case 2:
661 return comboBoxes.get(row);
662 }
663 return null;
664 }
665
666 /**
667 * @return the bid as currently set in the dialog.
668 * @throws Exception
669 */
670 Bid getBid() throws Exception {
671 HashMap<Integer, Value> values = new HashMap<Integer, Value>();
672
673 for (int i = 0; i < issues.size(); i++)
674 values.put(IDs.get(i), ((IssueDiscrete) issues.get(i))
675 .getValue(comboBoxes.get(i).getSelectedIndex()));
676 // values.put(IDs.get(i), (Value)comboBoxes.get(i).getSelectedItem());
677 return new Bid(utilitySpace.getDomain(), values);
678 }
679
680 public void actionPerformed(ActionEvent e) {
681 // System.out.println("event d!"+e);
682 // receiveMessage the cost and utility of our own bid.
683 fireTableCellUpdated(issues.size(), 2);
684 fireTableCellUpdated(issues.size() + 1, 2);
685 }
686
687}
688
689class NegoShowOffer extends NegoInfo {
690 private Bid topic;
691
692 public NegoShowOffer(Bid our, Bid opponent, AdditiveUtilitySpace us,
693 Bid topic) throws Exception {
694 super(our, opponent, us);
695 this.topic = topic;
696 }
697
698 private String[] colNames = { "Issue", "Current offer" };
699
700 @Override
701 public int getColumnCount() {
702 return 2;
703 }
704
705 @Override
706 public String getColumnName(int col) {
707 return colNames[col];
708 }
709
710 @Override
711 public Component getValueAt(int row, int col) {
712 if (row == issues.size()) {
713 if (col == 0)
714 return new JLabel("Utility:");
715 if (utilitySpace == null)
716 return new JLabel("No UtilSpace");
717 Bid bid;
718 if (col == 1)
719 bid = lastAccepted;
720 else
721 try {
722 bid = getBid();
723 } catch (Exception e) {
724 bid = null;
725 System.out.println("Internal err with getBid:"
726 + e.getMessage());
727 }
728 ;
729 JProgressBar bar = new JProgressBar(0, 100);
730 bar.setStringPainted(true);
731 try {
732 bar.setValue((int) (0.5 + 100.0 * utilitySpace.getUtility(bid)));
733 bar.setIndeterminate(false);
734 } catch (Exception e) {
735 new Warning("Exception during cost calculation:"
736 + e.getMessage(), false, 1);
737 bar.setIndeterminate(true);
738 }
739
740 return bar;
741 }
742 if (row == issues.size() + 1) {
743 return null;
744 }
745 switch (col) {
746 case 0:
747 return new JTextArea(issues.get(row).getName());
748 case 1:
749 Value value = null;
750 try {
751 value = getCurrentEval(topic, row);
752 } catch (Exception e) {
753 System.out.println("Err EnterBidDialog2.getValueAt: "
754 + e.getMessage());
755 }
756 if (value == null)
757 return new JTextArea("-");
758 return new JTextArea(value.toString());
759
760 }
761 return null;
762 }
763}
764
765class NegoProposeOffer extends NegoInfo {
766 NegoProposeOffer(Bid our, Bid opponent, AdditiveUtilitySpace us)
767 throws Exception {
768 super(our, opponent, us);
769 }
770
771 private String[] colNames = { "Issue", "Offer" };
772
773 public Component getValueAt(int row, int col) {
774 switch (col) {
775 case 0:
776 return super.getValueAt(row, col);
777 case 1:
778 return super.getValueAt(row, 2);
779 default:
780 return null;
781 }
782 }
783
784 @Override
785 public String getColumnName(int col) {
786 return colNames[col];
787 }
788
789 public boolean isCellEditable(int row, int col) {
790 return (col == 1 && row < issues.size());
791 }
792
793 @Override
794 public int getColumnCount() {
795 return 2;
796 }
797
798 @Override
799 public void actionPerformed(ActionEvent e) {
800 // System.out.println("event d!"+e);
801 // receiveMessage the cost and utility of our own bid.
802 fireTableCellUpdated(issues.size(), 1);
803 fireTableCellUpdated(issues.size() + 1, 1);
804 }
805}
806
807class NegoOffer extends NegoInfo {
808 NegoOffer(Bid our, Bid opponent, AdditiveUtilitySpace us) throws Exception {
809 super(our, opponent, us);
810 }
811
812 private String[] colNames = { "Issue", "Most recently accepted",
813 "Current offer" };
814
815 public Component getValueAt(int row, int col) {
816 if (row == issues.size()) {
817 if (col == 0)
818 return new JLabel("Utility:");
819 if (utilitySpace == null)
820 return new JLabel("No UtilSpace");
821 Bid bid;
822 if (col == 1)
823 bid = lastAccepted;
824 else
825 try {
826 bid = getBid();
827 } catch (Exception e) {
828 bid = null;
829 System.out.println("Internal err with getBid:"
830 + e.getMessage());
831 }
832 ;
833 JProgressBar bar = new JProgressBar(0, 100);
834 bar.setStringPainted(true);
835 try {
836 bar.setValue((int) (0.5 + 100.0 * utilitySpace.getUtility(bid)));
837 bar.setIndeterminate(false);
838 } catch (Exception e) {
839 new Warning("Exception during cost calculation:"
840 + e.getMessage(), false, 1);
841 bar.setIndeterminate(true);
842 }
843
844 return bar;
845 }
846 if (row == issues.size() + 1) {
847 return null;
848 }
849 switch (col) {
850 case 0:
851 return new JTextArea(issues.get(row).getName());
852 case 1:
853 Value value = null;
854 try {
855 value = getCurrentEval(lastAccepted, row);
856 } catch (Exception e) {
857 System.out.println("Err EnterBidDialog2.getValueAt: "
858 + e.getMessage());
859 }
860 if (value == null)
861 return new JTextArea("-");
862 return new JTextArea(value.toString());
863 case 2:
864 value = null;
865 try {
866 value = getCurrentEval(ourOldBid, row);
867 } catch (Exception e) {
868 System.out.println("Err EnterBidDialog2.getValueAt: "
869 + e.getMessage());
870 }
871 if (value == null)
872 return new JTextArea("-");
873 return new JTextArea(value.toString());
874
875 }
876 return null;
877 }
878
879 @Override
880 public String getColumnName(int col) {
881 return colNames[col];
882 }
883}
884
885/********************************************************************/
886
887class MyCellRenderer implements TableCellRenderer {
888 NegoInfo negoinfo;
889
890 public MyCellRenderer(NegoInfo n) {
891 negoinfo = n;
892 }
893
894 // the default converts everything to string...
895 public Component getTableCellRendererComponent(JTable table, Object value,
896 boolean isSelected, boolean hasFocus, int row, int column) {
897 return negoinfo.getValueAt(row, column);
898 }
899}
900
901/********************************************************************/
902
903class MyHistoryCellRenderer implements TableCellRenderer {
904 HistoryInfo historyinfo;
905
906 public MyHistoryCellRenderer(HistoryInfo n) {
907 historyinfo = n;
908 }
909
910 // the default converts everything to string...
911 public Component getTableCellRendererComponent(JTable table, Object value,
912 boolean isSelected, boolean hasFocus, int row, int column) {
913 return historyinfo.getValueAt(row, column);
914 }
915}
916
917/********************************************************/
918
919class MyCellEditor extends DefaultCellEditor {
920 private static final long serialVersionUID = 1L;
921 NegoInfo negoinfo;
922
923 public MyCellEditor(NegoInfo n) {
924 super(new JTextField("vaag")); // Java wants us to call super class, who
925 // cares...
926 negoinfo = n;
927 setClickCountToStart(1);
928 }
929
930 public Component getTableCellEditorComponent(JTable table, Object value,
931 boolean isSelected, int row, int column) {
932 return negoinfo.getValueAt(row, column);
933 }
934
935}
Note: See TracBrowser for help on using the repository browser.