Esempio n. 1
0
 // Determines if all car checkboxes are selected. Disables the Set button if
 // all checkbox are selected.
 protected void check() {
   Enumeration<JCheckBox> en = checkBoxes.elements();
   while (en.hasMoreElements()) {
     JCheckBox checkBox = en.nextElement();
     if (!checkBox.isSelected()) {
       // log.debug("Checkbox (" + checkBox.getText() + ") isn't selected ");
       moveButton.setEnabled(false);
       setButton.setEnabled(true);
       return;
     }
   }
   // all selected, work done!
   moveButton.setEnabled(_train != null && _train.isBuilt());
   setButton.setEnabled(false);
   isSetMode = false;
   setButtonText();
 }
Esempio n. 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);
  }
Esempio n. 3
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()
         });
   }
 }
Esempio n. 4
0
 /**
  * Block cars by track (optional), then pick up and set out for each location in a train's route.
  * This shows each car with a check box or with a set button. The set button is displayed when the
  * checkbox isn't selected and the display is in "set" mode. If the car is a utility. Show the
  * number of cars that have the same attributes, and not the car's road and number. Each car is
  * displayed only once in one of three panes. The three panes are pick up, set out, or local move.
  * To keep track of each car and which pane to use, they are placed in the list "rollingStock"
  * with the prefix "p", "s" or "m" and the car's unique id.
  */
 protected void blockCars(RouteLocation rl, boolean isManifest) {
   if (Setup.isPrintHeadersEnabled()) {
     JLabel header =
         new JLabel(
             Tab + trainCommon.getPickupCarHeader(isManifest, !TrainCommon.IS_TWO_COLUMN_TRACK));
     setLabelFont(header);
     pPickups.add(header);
     header =
         new JLabel(
             Tab + trainCommon.getDropCarHeader(isManifest, !TrainCommon.IS_TWO_COLUMN_TRACK));
     setLabelFont(header);
     pSetouts.add(header);
     header = new JLabel(Tab + trainCommon.getLocalMoveHeader(isManifest));
     setLabelFont(header);
     pMoves.add(header);
   }
   List<Track> tracks = rl.getLocation().getTrackByNameList(null);
   List<RouteLocation> routeList = _train.getRoute().getLocationsBySequenceList();
   List<Car> carList = carManager.getByTrainDestinationList(_train);
   for (Track track : tracks) {
     for (RouteLocation rld : routeList) {
       for (Car car : carList) {
         // determine if car is a pick up from the right track
         // caboose or FRED is placed at end of the train
         // passenger trains are already blocked in the car list
         if (car.getTrack() != null
             && car.getRouteLocation() == rl
             && car.getRouteDestination() != rl
             && (!Setup.isSortByTrackEnabled() || car.getTrackName().equals(track.getName()))
             && ((car.getRouteDestination() == rld && !car.isCaboose() && !car.hasFred())
                 || (rld == routeList.get(routeList.size() - 1)
                     && (car.isCaboose() || car.hasFred()))
                 || car.isPassenger())) {
           // yes we have a pick up
           pWorkPanes.setVisible(true);
           pickupPane.setVisible(true);
           if (!rollingStock.contains(car)) {
             rollingStock.add(car);
             car.addPropertyChangeListener(this);
           }
           // did we already process this car?
           if (checkBoxes.containsKey("p" + car.getId())) {
             if (isSetMode && !checkBoxes.get("p" + car.getId()).isSelected()) {
               // change to set button so user can remove car from train
               pPickups.add(addSet(car));
             } else {
               pPickups.add(checkBoxes.get("p" + car.getId()));
             }
             // figure out the checkbox text, either single car or utility
           } else {
             String text;
             if (car.isUtility()) {
               text =
                   trainCommon.pickupUtilityCars(
                       carList, car, isManifest, !TrainCommon.IS_TWO_COLUMN_TRACK);
               if (text == null) {
                 continue; // this car type has already been processed
               }
             } else {
               text = trainCommon.pickupCar(car, isManifest, !TrainCommon.IS_TWO_COLUMN_TRACK);
             }
             JCheckBox checkBox = new JCheckBox(text);
             setCheckBoxFont(checkBox);
             addCheckBoxAction(checkBox);
             pPickups.add(checkBox);
             checkBoxes.put("p" + car.getId(), checkBox);
           }
         }
       }
     }
     // set outs and local moves
     for (Car car : carList) {
       if (car.getRouteDestination() != rl || car.getDestinationTrack() == null) {
         continue;
       }
       // car in train if track null, second check is for yard master window
       if (car.getTrack() == null || car.getTrack() != null && (car.getRouteLocation() != rl)) {
         if (Setup.isSortByTrackEnabled()
             && !car.getDestinationTrack().getName().equals(track.getName())) {
           continue;
         }
         // we have set outs
         pWorkPanes.setVisible(true);
         setoutPane.setVisible(true);
         if (!rollingStock.contains(car)) {
           rollingStock.add(car);
           car.addPropertyChangeListener(this);
         }
         if (checkBoxes.containsKey("s" + car.getId())) {
           if (isSetMode && !checkBoxes.get("s" + car.getId()).isSelected()) {
             // change to set button so user can remove car from train
             pSetouts.add(addSet(car));
           } else {
             pSetouts.add(checkBoxes.get("s" + car.getId()));
           }
         } else {
           String text;
           if (car.isUtility()) {
             text = trainCommon.setoutUtilityCars(carList, car, !TrainCommon.LOCAL, isManifest);
             if (text == null) {
               continue; // this car type has already been processed
             }
           } else {
             text = trainCommon.dropCar(car, isManifest, !TrainCommon.IS_TWO_COLUMN_TRACK);
           }
           JCheckBox checkBox = new JCheckBox(text);
           setCheckBoxFont(checkBox);
           addCheckBoxAction(checkBox);
           pSetouts.add(checkBox);
           checkBoxes.put("s" + car.getId(), checkBox);
         }
         // local move?
       } else if (car.getTrack() != null
           && car.getRouteLocation() == rl
           && (!Setup.isSortByTrackEnabled()
               || car.getTrack().getName().equals(track.getName()))) {
         movePane.setVisible(true);
         if (!rollingStock.contains(car)) {
           rollingStock.add(car);
           car.addPropertyChangeListener(this);
         }
         if (checkBoxes.containsKey("m" + car.getId())) {
           if (isSetMode && !checkBoxes.get("m" + car.getId()).isSelected()) {
             // change to set button so user can remove car from train
             pMoves.add(addSet(car));
           } else {
             pMoves.add(checkBoxes.get("m" + car.getId()));
           }
         } else {
           String text;
           if (car.isUtility()) {
             text = trainCommon.setoutUtilityCars(carList, car, TrainCommon.LOCAL, isManifest);
             if (text == null) {
               continue; // this car type has already been processed
             }
           } else {
             text = trainCommon.localMoveCar(car, isManifest);
           }
           JCheckBox checkBox = new JCheckBox(text);
           setCheckBoxFont(checkBox);
           addCheckBoxAction(checkBox);
           pMoves.add(checkBox);
           checkBoxes.put("m" + car.getId(), checkBox);
         }
       }
     }
     // if not sorting by track, we're done
     if (!Setup.isSortByTrackEnabled()) {
       break;
     }
   }
   // pad the panels in case the horizontal scroll bar appears
   pPickups.add(new JLabel(Space));
   pSetouts.add(new JLabel(Space));
   pMoves.add(new JLabel(Space));
 }
 @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
     value = "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE",
     justification = "CarManager only provides Car Objects")
 private void runUpdate() {
   log.debug("run update");
   removePropertyChangeListerners();
   trainCommon.clearUtilityCarTypes(); // reset the utility car counts
   carCheckBoxes.clear();
   pTrack.removeAll();
   boolean pickup = false;
   boolean setout = false;
   if (_track != null) {
     pTrackPane.setBorder(BorderFactory.createTitledBorder(_track.getName()));
     textTrackCommentPane.setText(_track.getComment());
     textTrackCommentPane.setVisible(!_track.getComment().equals(Track.NONE));
     textTrackCommentWorkPane.setText("");
     for (Train train : trainManager.getTrainsArrivingThisLocationList(_track.getLocation())) {
       JPanel pTrain = new JPanel();
       pTrain.setLayout(new BoxLayout(pTrain, BoxLayout.Y_AXIS));
       pTrain.setBorder(
           BorderFactory.createTitledBorder(
               MessageFormat.format(
                   TrainSwitchListText.getStringScheduledWork(),
                   new Object[] {train.getName(), train.getDescription()})));
       // List locos first
       List<Engine> engList = engManager.getByTrainBlockingList(train);
       if (Setup.isPrintHeadersEnabled()) {
         for (Engine engine : engList) {
           if (engine.getTrack() == _track) {
             JLabel header = new JLabel(Tab + trainCommon.getPickupEngineHeader());
             setLabelFont(header);
             pTrain.add(header);
             break;
           }
         }
       }
       for (Engine engine : engList) {
         if (engine.getTrack() == _track) {
           engine.addPropertyChangeListener(this);
           rollingStock.add(engine);
           JCheckBox checkBox = new JCheckBox(trainCommon.pickupEngine(engine));
           setCheckBoxFont(checkBox);
           pTrain.add(checkBox);
           carCheckBoxes.put(engine.getId(), checkBox);
           pTrack.add(pTrain);
         }
       }
       if (Setup.isPrintHeadersEnabled()) {
         for (Engine engine : engList) {
           if (engine.getDestinationTrack() == _track) {
             JLabel header = new JLabel(Tab + trainCommon.getDropEngineHeader());
             setLabelFont(header);
             pTrain.add(header);
             break;
           }
         }
       }
       for (Engine engine : engList) {
         if (engine.getDestinationTrack() == _track) {
           engine.addPropertyChangeListener(this);
           rollingStock.add(engine);
           JCheckBox checkBox = new JCheckBox(trainCommon.dropEngine(engine));
           setCheckBoxFont(checkBox);
           pTrain.add(checkBox);
           carCheckBoxes.put(engine.getId(), checkBox);
           pTrack.add(pTrain);
         }
       }
       List<Car> carList = carManager.getByTrainDestinationList(train);
       if (Setup.isPrintHeadersEnabled()) {
         for (Car car : carList) {
           if (car.getTrack() == _track && car.getRouteDestination() != car.getRouteLocation()) {
             JLabel header =
                 new JLabel(
                     Tab
                         + trainCommon.getPickupCarHeader(
                             !IS_MANIFEST, !TrainCommon.IS_TWO_COLUMN_TRACK));
             setLabelFont(header);
             pTrain.add(header);
             break;
           }
         }
       }
       // sort car pick ups by their destination
       List<RouteLocation> routeList = train.getRoute().getLocationsBySequenceList();
       for (RouteLocation rl : routeList) {
         for (Car car : carList) {
           if (car.getTrack() == _track
               && car.getRouteDestination() != car.getRouteLocation()
               && car.getRouteDestination() == rl) {
             car.addPropertyChangeListener(this);
             rollingStock.add(car);
             String text;
             if (car.isUtility()) {
               text =
                   trainCommon.pickupUtilityCars(
                       carList, car, !IS_MANIFEST, !TrainCommon.IS_TWO_COLUMN_TRACK);
               if (text == null) {
                 continue; // this car type has already been processed
               }
             } else {
               text = trainCommon.pickupCar(car, !IS_MANIFEST, !TrainCommon.IS_TWO_COLUMN_TRACK);
             }
             pickup = true;
             JCheckBox checkBox = new JCheckBox(text);
             setCheckBoxFont(checkBox);
             pTrain.add(checkBox);
             carCheckBoxes.put(car.getId(), checkBox);
             pTrack.add(pTrain);
           }
         }
       }
       // now do car set outs
       if (Setup.isPrintHeadersEnabled()) {
         for (Car car : carList) {
           if (car.getDestinationTrack() == _track
               && car.getRouteDestination() != car.getRouteLocation()) {
             JLabel header =
                 new JLabel(
                     Tab
                         + trainCommon.getDropCarHeader(
                             !IS_MANIFEST, !TrainCommon.IS_TWO_COLUMN_TRACK));
             setLabelFont(header);
             pTrain.add(header);
             break;
           }
         }
       }
       for (Car car : carList) {
         if (car.getDestinationTrack() == _track
             && car.getRouteLocation() != car.getRouteDestination()) {
           car.addPropertyChangeListener(this);
           rollingStock.add(car);
           String text;
           if (car.isUtility()) {
             text = trainCommon.setoutUtilityCars(carList, car, !TrainCommon.LOCAL, !IS_MANIFEST);
             if (text == null) {
               continue; // this car type has already been processed
             }
           } else {
             text = trainCommon.dropCar(car, !IS_MANIFEST, !TrainCommon.IS_TWO_COLUMN_TRACK);
           }
           setout = true;
           JCheckBox checkBox = new JCheckBox(text);
           setCheckBoxFont(checkBox);
           pTrain.add(checkBox);
           carCheckBoxes.put(car.getId(), checkBox);
           pTrack.add(pTrain);
         }
       }
       // now do local car moves
       if (Setup.isPrintHeadersEnabled()) {
         for (Car car : carList) {
           if ((car.getTrack() == _track || car.getDestinationTrack() == _track)
               && car.getRouteDestination() == car.getRouteLocation()) {
             JLabel header = new JLabel(Tab + trainCommon.getLocalMoveHeader(!IS_MANIFEST));
             setLabelFont(header);
             pTrain.add(header);
             break;
           }
         }
       }
       for (Car car : carList) {
         if ((car.getTrack() == _track || car.getDestinationTrack() == _track)
             && car.getRouteLocation() != null
             && car.getRouteLocation() == car.getRouteDestination()) {
           car.addPropertyChangeListener(this);
           rollingStock.add(car);
           String text;
           if (car.isUtility()) {
             text = trainCommon.setoutUtilityCars(carList, car, TrainCommon.LOCAL, !IS_MANIFEST);
             if (text == null) {
               continue; // this car type has already been processed
             }
           } else {
             text = trainCommon.localMoveCar(car, !IS_MANIFEST);
           }
           setout = true;
           JCheckBox checkBox = new JCheckBox(text);
           setCheckBoxFont(checkBox);
           pTrain.add(checkBox);
           carCheckBoxes.put(car.getId(), checkBox);
           pTrack.add(pTrain);
         }
       }
     }
     // now do car holds
     // we only need the cars on this track
     List<RollingStock> rsList = carManager.getByTrainList();
     List<Car> carList = new ArrayList<Car>();
     for (RollingStock rs : rsList) {
       if (rs.getTrack() != _track || rs.getRouteLocation() != null) continue;
       carList.add((Car) rs);
     }
     JPanel pHoldCars = new JPanel();
     pHoldCars.setLayout(new BoxLayout(pHoldCars, BoxLayout.Y_AXIS));
     pHoldCars.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("HoldCars")));
     for (Car car : carList) {
       String text;
       if (car.isUtility()) {
         String s =
             trainCommon.pickupUtilityCars(
                 carList, car, !IS_MANIFEST, !TrainCommon.IS_TWO_COLUMN_TRACK);
         if (s == null) continue;
         text = TrainSwitchListText.getStringHoldCar().split("\\{")[0] + s.trim();
       } else {
         text =
             MessageFormat.format(
                 TrainSwitchListText.getStringHoldCar(),
                 new Object[] {
                   TrainCommon.padAndTruncateString(
                       car.getRoadName(), CarRoads.instance().getMaxNameLength()),
                   TrainCommon.padAndTruncateString(
                       car.getNumber(), Control.max_len_string_print_road_number),
                   TrainCommon.padAndTruncateString(
                       car.getTypeName().split("-")[0], CarTypes.instance().getMaxNameLength()),
                   TrainCommon.padAndTruncateString(
                       car.getLength() + TrainCommon.LENGTHABV,
                       Control.max_len_string_length_name),
                   TrainCommon.padAndTruncateString(
                       car.getLoadName(), CarLoads.instance().getMaxNameLength()),
                   TrainCommon.padAndTruncateString(
                       _track.getName(), LocationManager.instance().getMaxTrackNameLength()),
                   TrainCommon.padAndTruncateString(
                       car.getColor(), CarColors.instance().getMaxNameLength())
                 });
       }
       JCheckBox checkBox = new JCheckBox(text);
       setCheckBoxFont(checkBox);
       pHoldCars.add(checkBox);
       carCheckBoxes.put(car.getId(), checkBox);
       pTrack.add(pHoldCars);
     }
     pTrackPane.validate();
     if (pickup && !setout) {
       textTrackCommentWorkPane.setText(_track.getCommentPickup());
     } else if (!pickup && setout) {
       textTrackCommentWorkPane.setText(_track.getCommentSetout());
     } else if (pickup && setout) {
       textTrackCommentWorkPane.setText(_track.getCommentBoth());
     }
     textTrackCommentWorkPane.setVisible(!textTrackCommentWorkPane.getText().equals(""));
   } else {
     pTrackPane.setBorder(BorderFactory.createTitledBorder(""));
     textTrackCommentPane.setVisible(false);
     textTrackCommentWorkPane.setVisible(false);
   }
 }