1 | package genius.gui.progress;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.Panel;
|
---|
5 |
|
---|
6 | import javax.swing.JLabel;
|
---|
7 | import javax.swing.JPanel;
|
---|
8 | import javax.swing.JProgressBar;
|
---|
9 | import javax.swing.JScrollPane;
|
---|
10 | import javax.swing.JTable;
|
---|
11 |
|
---|
12 | import genius.core.events.NegotiationEvent;
|
---|
13 | import genius.core.events.TournamentEndedEvent;
|
---|
14 | import genius.core.events.TournamentSessionStartedEvent;
|
---|
15 | import genius.core.listener.Listener;
|
---|
16 |
|
---|
17 | @SuppressWarnings("serial")
|
---|
18 | public class MultiPartyTournamentProgressUI extends Panel implements Listener<NegotiationEvent> {
|
---|
19 |
|
---|
20 | Progress progress = new Progress();
|
---|
21 |
|
---|
22 | DataKeyTableModel model;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * @param m
|
---|
26 | * the {@link DataKeyTableModel} that holds the table to be
|
---|
27 | * logged.
|
---|
28 | */
|
---|
29 | public MultiPartyTournamentProgressUI(DataKeyTableModel m) {
|
---|
30 | model = m;
|
---|
31 | setLayout(new BorderLayout());
|
---|
32 |
|
---|
33 | add(progress, BorderLayout.NORTH);
|
---|
34 |
|
---|
35 | // table must be inside scrollpane, otherwise the headers do not show.
|
---|
36 | JTable resultsTable = new JTable(model);
|
---|
37 | resultsTable.setShowGrid(true); // no effect?
|
---|
38 | resultsTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
|
---|
39 | JScrollPane tableScrollPane = new JScrollPane(resultsTable);
|
---|
40 |
|
---|
41 | add(tableScrollPane, BorderLayout.CENTER);
|
---|
42 | }
|
---|
43 |
|
---|
44 | /***************
|
---|
45 | * implements Listener
|
---|
46 | *********************/
|
---|
47 |
|
---|
48 | // we only listen to update the progress bar. The model updates itself.
|
---|
49 | @Override
|
---|
50 | public void notifyChange(NegotiationEvent e) {
|
---|
51 | if (e instanceof TournamentEndedEvent) {
|
---|
52 | progress.finish();
|
---|
53 | } else if (e instanceof TournamentSessionStartedEvent) {
|
---|
54 | TournamentSessionStartedEvent e1 = (TournamentSessionStartedEvent) e;
|
---|
55 | progress.update(e1.getCurrentSession(), e1.getTotalSessions());
|
---|
56 | }
|
---|
57 |
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * progress panel, shows progress bar and text n/N
|
---|
63 | *
|
---|
64 | */
|
---|
65 | @SuppressWarnings("serial")
|
---|
66 | class Progress extends JPanel {
|
---|
67 | private final int SCALE = 1000000;
|
---|
68 | private JProgressBar progressbar = new JProgressBar(0, SCALE);
|
---|
69 | private JLabel label = new JLabel("starting tournament");
|
---|
70 |
|
---|
71 | public Progress() {
|
---|
72 | setLayout(new BorderLayout());
|
---|
73 | add(label, BorderLayout.EAST);
|
---|
74 | add(progressbar, BorderLayout.CENTER);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Shows progress bar when n of total have been started. We are still
|
---|
79 | * working on the nth, even if it equals the total. Therefore the progress
|
---|
80 | * bar never will go exactly to 100%
|
---|
81 | *
|
---|
82 | * @param n
|
---|
83 | * @param total
|
---|
84 | */
|
---|
85 | public void update(int n, int total) {
|
---|
86 | progressbar.setValue(Math.min(SCALE - 1, (int) (SCALE * n / (total + 1))));
|
---|
87 | label.setText("" + n + "/" + total);
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Set progress bar to 100% of total.
|
---|
92 | *
|
---|
93 | * @param total
|
---|
94 | */
|
---|
95 | public void finish() {
|
---|
96 | progressbar.setValue(SCALE);
|
---|
97 | }
|
---|
98 |
|
---|
99 | } |
---|