MATSIM
ScheduleValidatorWindow.java
Go to the documentation of this file.
1 package org.matsim.run.gui;
2 
3 import java.awt.HeadlessException;
4 import java.io.File;
5 
6 import javax.swing.GroupLayout;
7 import javax.swing.JButton;
8 import javax.swing.JDialog;
9 import javax.swing.JFileChooser;
10 import javax.swing.JFrame;
11 import javax.swing.JLabel;
12 import javax.swing.JScrollPane;
13 import javax.swing.JTable;
14 import javax.swing.JTextField;
15 import javax.swing.SwingUtilities;
16 import javax.swing.table.DefaultTableModel;
17 
18 import org.matsim.api.core.v01.Scenario;
20 import org.matsim.core.config.Config;
27 
31 public class ScheduleValidatorWindow extends JDialog {
32 
33  private File lastUsedDirectory;
34  private final JTextField txtScheduleFilename;
35  private final JTextField txtNetworkFilename;
36  private final DefaultTableModel resultTableModel;
37  private final JTable resultTable;
38  private final JButton btnValidate;
39 
40  public ScheduleValidatorWindow(JFrame parent) throws HeadlessException {
41  super(parent);
42  setTitle("Transit Schedule Validator");
43 
44  // UI elements
45  JLabel lblSchedule = new JLabel("Transit Schedule:");
46  JLabel lblNetwork = new JLabel("Network (optional):");
47  JLabel lblOutput = new JLabel("Output");
48 
49  this.txtScheduleFilename = new JTextField("", 20);
50  this.txtNetworkFilename = new JTextField("", 20);
51 
52  JButton btnChooseSchedule = new JButton("Choose");
53  JButton btnChooseNetwork = new JButton("Choose");
54 
55  this.btnValidate = new JButton("Validate");
56 
57  this.resultTableModel = new DefaultTableModel(0, 2) {
58  @Override
59  public String getColumnName(int column) {
60  return new String[] { "Type", "Message" }[column];
61  }
62 
63  @Override
64  public boolean isCellEditable(int row, int column) {
65  return false;
66  }
67  };
68  this.resultTable = new JTable(this.resultTableModel);
69  this.resultTable.getColumnModel().getColumn(0).setWidth(150);
70  this.resultTable.getColumnModel().getColumn(0).setMaxWidth(250);
71  JScrollPane outputPane = new JScrollPane(this.resultTable);
72 
73  // behavior
74 
75  this.lastUsedDirectory = new File(".");
76 
77  btnChooseSchedule.addActionListener(e -> {
78  JFileChooser chooser = new JFileChooser();
79  chooser.setCurrentDirectory(this.lastUsedDirectory);
80  int result = chooser.showOpenDialog(null);
81  if (result == JFileChooser.APPROVE_OPTION) {
82  File f = chooser.getSelectedFile();
83  this.lastUsedDirectory = f.getParentFile();
84  this.txtScheduleFilename.setText(f.getAbsolutePath());
85  }
86  });
87 
88  btnChooseNetwork.addActionListener(e -> {
89  JFileChooser chooser = new JFileChooser();
90  chooser.setCurrentDirectory(this.lastUsedDirectory);
91  int result = chooser.showOpenDialog(null);
92  if (result == JFileChooser.APPROVE_OPTION) {
93  File f = chooser.getSelectedFile();
94  this.lastUsedDirectory = f.getParentFile();
95  this.txtNetworkFilename.setText(f.getAbsolutePath());
96  }
97  });
98 
99  this.btnValidate.addActionListener(e -> run());
100 
101  // layout
102 
103  GroupLayout groupLayout = new GroupLayout(getContentPane());
104  groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup()
105  .addContainerGap()
106  .addGroup(groupLayout.createParallelGroup()
107  .addGroup(groupLayout.createSequentialGroup()
108  .addGroup(groupLayout.createParallelGroup()
109  .addComponent(lblSchedule)
110  .addComponent(lblNetwork)
111  .addComponent(this.btnValidate)
112  .addComponent(lblOutput))
113  .addGroup(groupLayout.createParallelGroup()
114  .addComponent(this.txtScheduleFilename)
115  .addComponent(this.txtNetworkFilename))
116  .addGroup(groupLayout.createParallelGroup()
117  .addComponent(btnChooseSchedule)
118  .addComponent(btnChooseNetwork)))
119  .addComponent(outputPane, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE, Short.MAX_VALUE))
120  .addContainerGap());
121 
122  groupLayout.setVerticalGroup(groupLayout.createSequentialGroup()
123  .addContainerGap()
124  .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
125  .addComponent(lblSchedule)
126  .addComponent(this.txtScheduleFilename)
127  .addComponent(btnChooseSchedule))
128  .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
129  .addComponent(lblNetwork)
130  .addComponent(this.txtNetworkFilename)
131  .addComponent(btnChooseNetwork))
132  .addGroup(
133  groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.btnValidate))
134  .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lblOutput))
135  .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
136  .addComponent(outputPane, 200, Short.MAX_VALUE, Short.MAX_VALUE))
137  .addContainerGap());
138 
139  getContentPane().setLayout(groupLayout);
140 
141  this.pack();
142  this.setSize(800, 600);
143  }
144 
145  public void loadFromConfig(Config config, File configDirectory) {
146  String scheduleFilename = config.transit().getTransitScheduleFile();
147  if (scheduleFilename != null) {
148  this.txtScheduleFilename.setText(new File(configDirectory, scheduleFilename).getAbsolutePath());
149  }
150  String networkFilename = config.network().getInputFile();
151  if (networkFilename != null) {
152  this.txtNetworkFilename.setText(new File(configDirectory, networkFilename).getAbsolutePath());
153  }
154  }
155 
156  public void run() {
157  this.btnValidate.setEnabled(false);
158  this.resultTableModel.setRowCount(0);
159  this.resultTableModel.addRow(new Object[] { "", "Validating transit schedule..." });
160 
161  new Thread(() -> {
162  String scheduleFilename = this.txtScheduleFilename.getText();
163  String networkFilename = this.txtNetworkFilename.getText();
164 
166  TransitSchedule schedule = null;
167  Network network = null;
168 
169  if (networkFilename != null) {
170  network = scenario.getNetwork();
171  new MatsimNetworkReader(network).readFile(networkFilename);
172  }
173  if (scheduleFilename != null) {
174  schedule = scenario.getTransitSchedule();
175  new TransitScheduleReader(scenario).readFile(scheduleFilename);
176  }
177 
179  SwingUtilities.invokeLater(() -> {
180  this.resultTableModel.setRowCount(0);
181  if (result.isValid()) {
182  this.resultTableModel.addRow(new Object[] { "SUCCESS", "The schedule appears valid" });
183  }
184  for (String message : result.getWarnings()) {
185  this.resultTableModel.addRow(new Object[] { "WARNING", message });
186  }
187  for (String message : result.getErrors()) {
188  this.resultTableModel.addRow(new Object[] { "ERROR", message });
189  }
190  this.btnValidate.setEnabled(true);
191  });
192  }).start();
193 
194  }
195 
196  public static void main(String[] args) {
197  new ScheduleValidatorWindow(null).setVisible(true);
198  }
199 }
final NetworkConfigGroup network()
Definition: Config.java:411
void loadFromConfig(Config config, File configDirectory)
TransitConfigGroup transit()
Definition: Config.java:451
static ValidationResult validateAll(final TransitSchedule schedule, final Network network)
final void readFile(final String filename)
TransitSchedule getTransitSchedule()
static Scenario createScenario(final Config config)
static Config createConfig(final String context)