Example #1
0
 // returns one of two possible departure strings for a train
 protected String getStatus(RouteLocation rl, boolean isManifest) {
   if (rl == _train.getRoute().getTerminatesRouteLocation()) {
     return MessageFormat.format(
         TrainManifestText.getStringTrainTerminates(),
         new Object[] {_train.getTrainTerminatesName()});
   }
   if (rl != _train.getCurrentLocation()
       && _train.getExpectedArrivalTime(rl).equals(Train.ALREADY_SERVICED)) {
     return MessageFormat.format(
         TrainSwitchListText.getStringTrainDone(), new Object[] {_train.getName()});
   }
   if (!_train.isBuilt()) {
     return _train.getStatus();
   }
   if (Setup.isPrintLoadsAndEmptiesEnabled()) {
     int emptyCars = _train.getNumberEmptyCarsInTrain(rl);
     String text;
     if (isManifest) {
       text = TrainManifestText.getStringTrainDepartsLoads();
     } else {
       text = TrainSwitchListText.getStringTrainDepartsLoads();
     }
     return MessageFormat.format(
         text,
         new Object[] {
           TrainCommon.splitString(rl.getName()),
           rl.getTrainDirectionString(),
           _train.getNumberCarsInTrain(rl) - emptyCars,
           emptyCars,
           _train.getTrainLength(rl),
           Setup.getLengthUnit().toLowerCase(),
           _train.getTrainWeight(rl),
           _train.getTrainTerminatesName(),
           _train.getName()
         });
   } else {
     String text;
     if (isManifest) {
       text = TrainManifestText.getStringTrainDepartsCars();
     } else {
       text = TrainSwitchListText.getStringTrainDepartsCars();
     }
     return MessageFormat.format(
         text,
         new Object[] {
           TrainCommon.splitString(rl.getName()),
           rl.getTrainDirectionString(),
           _train.getNumberCarsInTrain(rl),
           _train.getTrainLength(rl),
           Setup.getLengthUnit().toLowerCase(),
           _train.getTrainWeight(rl),
           _train.getTrainTerminatesName(),
           _train.getName()
         });
   }
 }
Example #2
0
  public void writeFile(String name) {
    if (log.isDebugEnabled()) {
      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 cars CSV file: " + file.getName());
      return;
    }

    // create header
    String header =
        Bundle.getMessage("Name")
            + del
            + Bundle.getMessage("Description")
            + del
            + Bundle.getMessage("Time")
            + del
            + Bundle.getMessage("Route")
            + del
            + Bundle.getMessage("Departs")
            + del
            + Bundle.getMessage("Terminates")
            + del
            + Bundle.getMessage("Status")
            + del
            + Bundle.getMessage("Comment");
    fileOut.println(header);

    int count = 0;

    for (Train train : TrainManager.instance().getTrainsByTimeList()) {
      if (!train.isBuildEnabled()) continue;
      count++;
      String routeName = "";
      if (train.getRoute() != null) routeName = train.getRoute().getName();
      String line =
          ESC
              + train.getName()
              + ESC
              + del
              + ESC
              + train.getDescription()
              + ESC
              + del
              + ESC
              + train.getDepartureTime()
              + ESC
              + del
              + ESC
              + routeName
              + ESC
              + del
              + ESC
              + train.getTrainDepartsName()
              + ESC
              + del
              + ESC
              + train.getTrainTerminatesName()
              + ESC
              + del
              + ESC
              + train.getStatus()
              + ESC
              + del
              + ESC
              + train.getComment()
              + ESC;
      fileOut.println(line);
    }

    fileOut.println();
    // second create header for built trains
    header =
        Bundle.getMessage("Name")
            + del
            + Bundle.getMessage("csvParameters")
            + del
            + Bundle.getMessage("Attributes");
    fileOut.println(header);

    for (Train train : TrainManager.instance().getTrainsByTimeList()) {
      if (!train.isBuildEnabled()) continue;

      if (train.isBuilt() && train.getRoute() != null) {
        StringBuffer line =
            new StringBuffer(ESC + train.getName() + ESC + del + Bundle.getMessage("Route"));
        for (RouteLocation rl : train.getRoute().getLocationsBySequenceList()) {
          line.append(del + ESC + rl.getName() + ESC);
        }
        fileOut.println(line);

        line =
            new StringBuffer(
                ESC + train.getName() + ESC + del + Bundle.getMessage("csvArrivalTime"));
        for (RouteLocation rl : train.getRoute().getLocationsBySequenceList()) {
          line.append(del + ESC + train.getExpectedArrivalTime(rl) + ESC);
        }
        fileOut.println(line);

        line =
            new StringBuffer(
                ESC + train.getName() + ESC + del + Bundle.getMessage("csvDepartureTime"));
        for (RouteLocation rl : train.getRoute().getLocationsBySequenceList()) {
          line.append(del + ESC + train.getExpectedDepartureTime(rl) + ESC);
        }
        fileOut.println(line);

        line =
            new StringBuffer(
                ESC + train.getName() + ESC + del + Bundle.getMessage("csvTrainDirection"));
        for (RouteLocation rl : train.getRoute().getLocationsBySequenceList()) {
          line.append(del + ESC + rl.getTrainDirectionString() + ESC);
        }
        fileOut.println(line);

        line =
            new StringBuffer(
                ESC + train.getName() + ESC + del + Bundle.getMessage("csvTrainWeight"));
        for (RouteLocation rl : train.getRoute().getLocationsBySequenceList()) {
          line.append(del + ESC + train.getTrainWeight(rl) + ESC);
        }
        fileOut.println(line);

        line =
            new StringBuffer(
                ESC + train.getName() + ESC + del + Bundle.getMessage("csvTrainLength"));
        for (RouteLocation rl : train.getRoute().getLocationsBySequenceList()) {
          line.append(del + ESC + train.getTrainLength(rl) + ESC);
        }
        fileOut.println(line);

        line = new StringBuffer(ESC + train.getName() + ESC + del + Bundle.getMessage("Cars"));
        for (RouteLocation rl : train.getRoute().getLocationsBySequenceList()) {
          line.append(del + ESC + train.getNumberCarsInTrain(rl) + ESC);
        }
        fileOut.println(line);

        line =
            new StringBuffer(ESC + train.getName() + ESC + del + Bundle.getMessage("csvEmpties"));
        for (RouteLocation rl : train.getRoute().getLocationsBySequenceList()) {
          line.append(del + ESC + train.getNumberEmptyCarsInTrain(rl) + ESC);
        }
        fileOut.println(line);
        fileOut.println();
      }
    }

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