source: src/main/java/genius/gui/tournament/MultiTournamentPanel.java@ 345

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

Initial import : Genius 9.0.0

File size: 4.8 KB
Line 
1package genius.gui.tournament;
2
3import java.awt.BorderLayout;
4import java.io.File;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.text.DateFormat;
8import java.text.SimpleDateFormat;
9import java.util.Date;
10
11import javax.swing.JFrame;
12import javax.swing.JOptionPane;
13import javax.swing.JPanel;
14import javax.xml.stream.XMLStreamException;
15
16import genius.core.config.MultilateralTournamentConfiguration;
17import genius.core.events.NegotiationEvent;
18import genius.core.events.TournamentEndedEvent;
19import genius.core.exceptions.InstantiateException;
20import genius.core.listener.Listener;
21import genius.core.logging.StatisticsLogger;
22import genius.core.logging.XmlLogger;
23import genius.core.session.TournamentManager;
24import genius.gui.negosession.MultiPartyDataModel;
25import genius.gui.progress.MultiPartyTournamentProgressUI;
26import genius.gui.progress.MultipartyNegoEventLogger;
27
28/**
29 * Panel that first asks user to adjust tournament settings. Then when user
30 * presses start, a tournament is launched and the panel is changed to show the
31 * ProgressUI.
32 *
33 */
34@SuppressWarnings("serial")
35public class MultiTournamentPanel extends JPanel {
36
37 private static final String LOGDIR = "log";
38 private TournamentManager manager;
39 private XmlLogger xmlLogger;
40 private MultipartyNegoEventLogger myLogger;
41 private MultiPartyDataModel dataModel;
42 private StatisticsLogger statisticsLogger;
43
44 public MultiTournamentPanel() {
45 setLayout(new BorderLayout());
46 MultiTournamentModel model = new MultiTournamentModel();
47 add(new MultiTournamentSettingsPanel(model), BorderLayout.CENTER);
48
49 model.addListener(new Listener<MultilateralTournamentConfiguration>() {
50 @Override
51 public void notifyChange(MultilateralTournamentConfiguration config) {
52 // called by model when the model is ready for run.
53 try {
54 runTournament(config);
55 } catch (XMLStreamException | IOException | InstantiateException e) {
56 JOptionPane.showMessageDialog(MultiTournamentPanel.this, "Failed to run tournament:" + e);
57 e.printStackTrace();
58
59 }
60 }
61 });
62
63 }
64
65 /**
66 * Start a new tournament and log the results to panels and files. Replaces
67 * our panel with MultiPartyTournamentProgressUI.
68 *
69 * @param config
70 * a {@link MultilateralTournamentConfiguration}
71 * @throws XMLStreamException
72 * @throws IOException
73 * @throws InstantiateException
74 */
75 private void runTournament(MultilateralTournamentConfiguration config)
76 throws XMLStreamException, IOException, InstantiateException {
77
78 manager = new TournamentManager(config);
79
80 removeAll();
81 // init data model, GUI, logger.
82 int numPartiesWithMediator = config.getNumPartiesPerSession();
83 if (config.getProtocolItem().getHasMediator()) {
84 numPartiesWithMediator++;
85 }
86 dataModel = new MultiPartyDataModel(numPartiesWithMediator);
87
88 MultiPartyTournamentProgressUI progressUI = new MultiPartyTournamentProgressUI(dataModel);
89 add(progressUI, BorderLayout.CENTER);
90
91 DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
92 String logName = config.getProfileItems().get(0).getDomain().getName();
93 // bit strange to set up logging system here... move it?
94 logName = String.format("log/tournament-%s-%s.log", dateFormat.format(new Date()), logName);
95 new File(LOGDIR).mkdir();
96 xmlLogger = new XmlLogger(new FileOutputStream(logName + ".xml"), "Tournament");
97 statisticsLogger = new StatisticsLogger(new FileOutputStream(logName + "Stats.xml"));
98
99 myLogger = new MultipartyNegoEventLogger(logName, config.getNumPartiesPerSession(), dataModel);
100 dataModel.addTableModelListener(myLogger);
101
102 manager.addListener(progressUI);
103 manager.addListener(dataModel);
104 manager.addListener(xmlLogger);
105 manager.addListener(statisticsLogger);
106 // manager.addListener(new ConsoleLogger()); //only works combined when
107 // you disable TournamentManager useConsoleOut(false);
108
109 manager.addListener(new Listener<NegotiationEvent>() {
110 @Override
111 public void notifyChange(NegotiationEvent e) {
112 if (e instanceof TournamentEndedEvent) {
113 finishTournament();
114 }
115 }
116
117 });
118
119 manager.start(); // runs the manager thread async
120 }
121
122 private void finishTournament() {
123
124 if (myLogger != null) {
125 if (dataModel != null)
126 dataModel.removeTableModelListener(myLogger);
127 myLogger.close();
128 }
129 if (xmlLogger != null) {
130 if (manager != null)
131 manager.removeListener(xmlLogger);
132 try {
133 xmlLogger.close();
134 } catch (IOException e) {
135 e.printStackTrace();
136 }
137 }
138 if (statisticsLogger != null) {
139 statisticsLogger.close();
140 }
141
142 }
143
144 /**
145 * simple stub to run this stand-alone (for testing).
146 *
147 * @param args
148 */
149 public static void main(String[] args) {
150 final JFrame gui = new JFrame();
151 gui.setLayout(new BorderLayout());
152 gui.getContentPane().add(new MultiTournamentPanel(), BorderLayout.CENTER);
153 gui.pack();
154 gui.setVisible(true);
155 }
156}
Note: See TracBrowser for help on using the repository browser.