[1] | 1 | package genius.gui.progress;
|
---|
| 2 |
|
---|
| 3 | import java.io.IOException;
|
---|
| 4 | import java.util.ArrayList;
|
---|
| 5 | import java.util.List;
|
---|
| 6 |
|
---|
| 7 | import javax.swing.event.TableModelEvent;
|
---|
| 8 | import javax.swing.event.TableModelListener;
|
---|
| 9 |
|
---|
| 10 | import genius.core.logging.CsvLogger;
|
---|
| 11 | import genius.core.session.MultipartyNegoEventLoggerData;
|
---|
| 12 | import genius.core.session.TournamentManager;
|
---|
| 13 | import genius.gui.negosession.MultiPartyDataModel;
|
---|
| 14 | import genius.gui.tournament.MultiTournamentPanel;
|
---|
| 15 | import joptsimple.internal.Strings;
|
---|
| 16 |
|
---|
| 17 | /**
|
---|
| 18 | * Logger for MultiPartyNegotiationEvents. Currently only for hook into the
|
---|
| 19 | * {@link TournamentManager} but may be generalizable and eg used in
|
---|
| 20 | * {@link MultiTournamentPanel}. The logger simply listens to changes in the
|
---|
| 21 | * tableModel.
|
---|
| 22 | *
|
---|
| 23 | *
|
---|
| 24 | * @author W.Pasman 18jun15
|
---|
| 25 | *
|
---|
| 26 | */
|
---|
| 27 | public class MultipartyNegoEventLogger implements TableModelListener {
|
---|
| 28 |
|
---|
| 29 | private MultipartyNegoEventLoggerData data = new MultipartyNegoEventLoggerData();
|
---|
| 30 |
|
---|
| 31 | MultiPartyDataModel model;
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | *
|
---|
| 35 | * @param name
|
---|
| 36 | * filename but without .csv extension.
|
---|
| 37 | * @param numAgents
|
---|
| 38 | * @param m
|
---|
| 39 | * @throws IOException
|
---|
| 40 | * if log file can't be created
|
---|
| 41 | */
|
---|
| 42 | public MultipartyNegoEventLogger(String name, int numAgents, MultiPartyDataModel m) throws IOException {
|
---|
| 43 | model = m;
|
---|
| 44 | data.logger = new CsvLogger(name + ".csv");
|
---|
| 45 | logHeader();
|
---|
| 46 |
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /**
|
---|
| 50 | * write the header to the log file.
|
---|
| 51 | */
|
---|
| 52 | private void logHeader() {
|
---|
| 53 | List<String> headers = new ArrayList<String>();
|
---|
| 54 | for (int col = 0; col < model.getColumnCount(); col++) {
|
---|
| 55 | headers.add(model.getColumnName(col));
|
---|
| 56 | }
|
---|
| 57 | data.logger.logLine(Strings.join(headers, ";"));
|
---|
| 58 |
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | * Any insert in the model is caught here, to be logged. All values in the
|
---|
| 63 | * new row are added, converted to string
|
---|
| 64 | */
|
---|
| 65 | @Override
|
---|
| 66 | public void tableChanged(TableModelEvent evt) {
|
---|
| 67 | if (evt.getType() == TableModelEvent.INSERT) {
|
---|
| 68 | int row = evt.getFirstRow();
|
---|
| 69 |
|
---|
| 70 | List<String> elements = new ArrayList<String>();
|
---|
| 71 | for (int col = 0; col < model.getColumnCount(); col++) {
|
---|
| 72 | Object value = model.getValueAt(row, col);
|
---|
| 73 | elements.add(value == null ? "" : value.toString());
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | data.logger.logLine(Strings.join(elements, ";"));
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | public void close() {
|
---|
| 81 | if (data.logger != null) {
|
---|
| 82 | try {
|
---|
| 83 | data.logger.close();
|
---|
| 84 | } catch (IOException e) {
|
---|
| 85 | e.printStackTrace();
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | } |
---|