Skip to content
Permalink
Browse files

Add export schedules tool

  • Loading branch information...
danielboudreau danielboudreau
danielboudreau authored and danielboudreau committed Sep 25, 2018
1 parent 2f7cc46 commit bdda071e5ca6ec910683cdaa69d511140bb19583
@@ -44,6 +44,7 @@ MenuItemCopyLocation = Copy Location
MenuItemEditCarType = Edit Car Type
MenuItemCopySchedule = Copy Schedule
MenuItemShowTrackMoves = Show Track Moves
MenuItemExportSchedules = Export Schedules

Used = Used
Reserved = Reserved
@@ -498,3 +499,6 @@ ExportFailed = Export failed
SetOutRestrictions = Set Out Restrictions
PickUpRestrictions = Pick up Restrictions

# Export Schedules
ExportedSchedulesToFile = Exported {0} schedules to file: {1}

@@ -0,0 +1,189 @@
package jmri.jmrit.operations.locations.schedules;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.MessageFormat;
import java.util.List;
import javax.swing.JOptionPane;
import jmri.InstanceManager;
import jmri.jmrit.XmlFile;
import jmri.jmrit.operations.locations.tools.ExportLocations;
import jmri.jmrit.operations.routes.RouteManager;
import jmri.jmrit.operations.setup.OperationsSetupXml;
import jmri.jmrit.operations.trains.TrainManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Exports the Operation Schedules into a comma delimitated file (CSV).
*
* @author Daniel Boudreau Copyright (C) 2018
*
*/
public class ExportSchedules extends XmlFile {

static final String ESC = "\""; // escape character NOI18N
private String del = ","; // delimiter

TrainManager trainManager = InstanceManager.getDefault(TrainManager.class);
RouteManager routeManager = InstanceManager.getDefault(RouteManager.class);

public void setDeliminter(String delimiter) {
del = delimiter;
}

public void writeOperationsScheduleFile() {
makeBackupFile(defaultOperationsFilename());
try {
if (!checkFile(defaultOperationsFilename())) {
// The file does not exist, create it before writing
java.io.File file = new java.io.File(defaultOperationsFilename());
java.io.File parentDir = file.getParentFile();
if (!parentDir.exists()) {
if (!parentDir.mkdir()) {
log.error("Directory wasn't created");
}
}
if (file.createNewFile()) {
log.debug("File created");
}
}
writeFile(defaultOperationsFilename());
} catch (Exception e) {
log.error("Exception while writing the new CSV operations file, may not be complete: " + e);
}
}

public void writeFile(String name) {
log.debug("writeFile {}", name);
// This is taken in large part from "Java and XML" page 368
File file = findFile(name);
if (file == null) {
file = new File(name);
}

PrintWriter fileOut = null;

try {
fileOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")), // NOI18N
true); // NOI18N
} catch (IOException e) {
log.error("Can not open export schedules CSV file: {}", file.getName());
JOptionPane.showMessageDialog(null,
MessageFormat.format(Bundle.getMessage("ExportedSchedulesToFile"), new Object[]{
0, defaultOperationsFilename()}),
Bundle.getMessage("ExportFailed"),
JOptionPane.ERROR_MESSAGE);
return;
}

// create header
String header = Bundle.getMessage("ScheduleName") +
del +
Bundle.getMessage("Id") +
del +
Bundle.getMessage("Type") +
del +
Bundle.getMessage("Random") +
del +
Bundle.getMessage("Delivery") +
del +
Bundle.getMessage("Road") +
del +
Bundle.getMessage("Receive") +
del +
Bundle.getMessage("Ship") +
del +
Bundle.getMessage("Destination") +
del +
Bundle.getMessage("Track") +
del +
Bundle.getMessage("Pickup") +
del +
Bundle.getMessage("Count") +
del +
Bundle.getMessage("Wait") +
del +
Bundle.getMessage("Hits") +
del +
Bundle.getMessage("Comment");

fileOut.println(header);

List<Schedule> schedules = InstanceManager.getDefault(ScheduleManager.class).getSchedulesByNameList();
for (Schedule schedule : schedules) {
String scheduleName = schedule.getName();
if (scheduleName.contains(del)) {
scheduleName = ESC + scheduleName + ESC;
}
for (ScheduleItem scheduleItem : schedule.getItemsBySequenceList()) {

String line = scheduleName +
del +
scheduleItem.getId() +
del +
scheduleItem.getTypeName() +
del +
scheduleItem.getRandom() +
del +
scheduleItem.getSetoutTrainScheduleName() +
del +
scheduleItem.getRoadName() +
del +
scheduleItem.getReceiveLoadName() +
del +
scheduleItem.getShipLoadName() +
del +
scheduleItem.getDestinationName() +
del +
scheduleItem.getDestinationTrackName() +
del +
scheduleItem.getPickupTrainScheduleName() +
del +
scheduleItem.getCount() +
del +
scheduleItem.getWait() +
del +
scheduleItem.getHits() +
del +
schedule.getComment();

fileOut.println(line);
}

}
fileOut.flush();
fileOut.close();
log.info("Exported " + schedules.size() + " schedules to file " + defaultOperationsFilename());
JOptionPane.showMessageDialog(null,
MessageFormat.format(Bundle.getMessage("ExportedSchedulesToFile"), new Object[]{
schedules.size(), defaultOperationsFilename()}),
Bundle.getMessage("ExportComplete"),
JOptionPane.INFORMATION_MESSAGE);
}

// Operation files always use the same directory
public static String defaultOperationsFilename() {
return OperationsSetupXml.getFileLocation() +
OperationsSetupXml.getOperationsDirectoryName() +
File.separator +
getOperationsFileName();
}

public static void setOperationsFileName(String name) {
operationsFileName = name;
}

public static String getOperationsFileName() {
return operationsFileName;
}

private static String operationsFileName = "ExportOperationsSchedules.csv"; // NOI18N

private final static Logger log = LoggerFactory.getLogger(ExportLocations.class);

}
@@ -0,0 +1,21 @@
package jmri.jmrit.operations.locations.schedules;

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;

/**
* Starts the export schedules action
*
* @author Dan Boudreau Copyright (C) 2018
*/
public class ExportSchedulesAction extends AbstractAction {

public ExportSchedulesAction(String actionName) {
super(actionName);
}

@Override
public void actionPerformed(ActionEvent ae) {
new ExportSchedules().writeOperationsScheduleFile();
}
}
@@ -5,6 +5,8 @@
import jmri.jmrit.operations.locations.LocationManager;
import jmri.jmrit.operations.locations.Track;
import jmri.jmrit.operations.setup.Control;
import jmri.jmrit.operations.trains.timetable.TrainSchedule;
import jmri.jmrit.operations.trains.timetable.TrainScheduleManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@@ -87,6 +89,16 @@ public void setRandom(String value) {
public String getSetoutTrainScheduleId() {
return _setoutTrainScheduleId;
}

public String getSetoutTrainScheduleName() {
String name = "";
TrainSchedule sch = InstanceManager.getDefault(TrainScheduleManager.class)
.getScheduleById(getSetoutTrainScheduleId());
if (sch != null) {
name = sch.getName();
}
return name;
}

public void setSetoutTrainScheduleId(String id) {
String old = _setoutTrainScheduleId;
@@ -97,6 +109,16 @@ public void setSetoutTrainScheduleId(String id) {
public String getPickupTrainScheduleId() {
return _pickupTrainScheduleId;
}

public String getPickupTrainScheduleName() {
String name = "";
TrainSchedule sch = InstanceManager.getDefault(TrainScheduleManager.class)
.getScheduleById(getPickupTrainScheduleId());
if (sch != null) {
name = sch.getName();
}
return name;
}

public void setPickupTrainScheduleId(String id) {
String old = _pickupTrainScheduleId;
@@ -23,7 +23,6 @@
import jmri.jmrit.operations.rollingstock.cars.CarTypes;
import jmri.jmrit.operations.rollingstock.cars.tools.PrintCarLoadsAction;
import jmri.jmrit.operations.setup.Control;
import jmri.jmrit.operations.trains.timetable.TrainScheduleManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@@ -180,37 +179,20 @@ private void updateLocations() {
addItemLeft(locationsPanel,
new JLabel(spur.getName() + " (" + spur.getScheduleName() + ")"), 1, x);
// create string Receive(type, delivery, road, load)
String s = si.getTypeName();
if (!si.getSetoutTrainScheduleId().equals(ScheduleItem.NONE) &&
InstanceManager.getDefault(TrainScheduleManager.class)
.getScheduleById(si.getSetoutTrainScheduleId()) != null) {
s = s +
", " +
InstanceManager.getDefault(TrainScheduleManager.class).getScheduleById(si.getSetoutTrainScheduleId())
.getName();
} else {
s = s + ",";
}
if (!si.getRoadName().equals(ScheduleItem.NONE)) {
s = s + ", " + si.getRoadName();
} else {
s = s + ",";
}
s = s + ", " + si.getReceiveLoadName();
String s = si.getTypeName() +
", " +
si.getSetoutTrainScheduleName() +
", " +
si.getRoadName() +
", " +
si.getReceiveLoadName();
addItemLeft(locationsPanel, new JLabel(Bundle.getMessage("Receive") + " (" + s + ")"), 2, x);
// create string Ship(load, pickup)
s = "";
if (!si.getPickupTrainScheduleId().equals(ScheduleItem.NONE) &&
InstanceManager.getDefault(TrainScheduleManager.class)
.getScheduleById(si.getPickupTrainScheduleId()) != null) {
s = ", " +
InstanceManager.getDefault(TrainScheduleManager.class).getScheduleById(si.getPickupTrainScheduleId())
.getName();
}
addItemLeft(locationsPanel, new JLabel(Bundle.getMessage("Ship") +
" (" +
si.getShipLoadName() +
s +
", " +
si.getPickupTrainScheduleName() +
")"), 3, x++);
// now the destination and track
if (si.getDestination() != null) {
@@ -260,7 +242,8 @@ public void propertyChange(java.beans.PropertyChangeEvent e) {
InstanceManager.getDefault(CarTypes.class).updateComboBox(typesComboBox);
}
if (e.getSource().getClass().equals(CarLoads.class)) {
InstanceManager.getDefault(CarLoads.class).updateComboBox((String) typesComboBox.getSelectedItem(), loadsComboBox);
InstanceManager.getDefault(CarLoads.class).updateComboBox((String) typesComboBox.getSelectedItem(),
loadsComboBox);
}
if (e.getSource().getClass().equals(Schedule.class) ||
e.getSource().getClass().equals(LocationManager.class) ||
@@ -82,6 +82,7 @@ public SchedulesTableFrame() {
toolMenu.add(new ScheduleCopyAction());
toolMenu.add(new SchedulesByLoadAction(Bundle.getMessage("MenuItemShowSchedulesByLoad")));
toolMenu.add(new SchedulesResetHitsAction(Bundle.getMessage("MenuItemResetHits")));
toolMenu.add(new ExportSchedulesAction(Bundle.getMessage("MenuItemExportSchedules")));
toolMenu.addSeparator();
toolMenu.add(new PrintLocationsAction(Bundle.getMessage("MenuItemPrint"), false));
toolMenu.add(new PrintLocationsAction(Bundle.getMessage("MenuItemPreview"), true));
@@ -34,8 +34,6 @@
import jmri.jmrit.operations.trains.Train;
import jmri.jmrit.operations.trains.TrainCommon;
import jmri.jmrit.operations.trains.TrainManager;
import jmri.jmrit.operations.trains.timetable.TrainSchedule;
import jmri.jmrit.operations.trains.timetable.TrainScheduleManager;
import jmri.util.davidflanagan.HardcopyWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -366,18 +364,6 @@ private void printSchedulesSelected() throws IOException {
si.getDestinationTrackName() +
NEW_LINE;
writer.write(s);
TrainSchedule sch = InstanceManager.getDefault(TrainScheduleManager.class)
.getScheduleById(si.getSetoutTrainScheduleId());
String setoutDay = "";
if (sch != null) {
setoutDay = sch.getName();
}
sch = InstanceManager.getDefault(TrainScheduleManager.class)
.getScheduleById(si.getPickupTrainScheduleId());
String pickupDay = "";
if (sch != null) {
pickupDay = sch.getName();
}

s = padOutString("", cts.getMaxNameLength() + 1) +
padOutString(Bundle.getMessage("Random"), Bundle.getMessage("Random").length() + 1) +
@@ -387,11 +373,12 @@ private void printSchedulesSelected() throws IOException {
Bundle.getMessage("Wait") +
NEW_LINE;
writer.write(s);

s = padOutString("", cts.getMaxNameLength() + 1) +
padOutString(si.getRandom(), Bundle.getMessage("Random").length() + 1) +
padOutString(setoutDay, Bundle.getMessage("Delivery").length() + 1) +
padOutString(si.getSetoutTrainScheduleName(), Bundle.getMessage("Delivery").length() + 1) +
padOutString(si.getRoadName(), crs.getMaxNameLength() + 1) +
padOutString(pickupDay, Bundle.getMessage("Delivery").length() + 1) +
padOutString(si.getPickupTrainScheduleName(), Bundle.getMessage("Delivery").length() + 1) +
si.getWait() +
NEW_LINE;
writer.write(s);

0 comments on commit bdda071

Please sign in to comment.
You can’t perform that action at this time.