public void displayTrain(int trainNumber) {
    this.trainNumber = trainNumber;

    trainViewJPanel1.display(trainNumber);
    String s;
    String destination = "";
    int priority = TrainModel.PRIORITY_NORMAL;
    int state = TrainModel.STATE_STOPPED;
    if (trainNumber >= 0) {
      TrainModel train =
          (TrainModel) w.get(KEY.TRAINS, trainNumber, modelRoot.getPlayerPrincipal());

      trainCargoBundleKey = train.getCargoBundle();
      CargoBundle cb = (CargoBundle) w.get(train.getCargoBundle());
      s = "Train #" + trainNumber + ": ";
      ScheduleIterator si = train.getScheduleIterator();
      TrainOrdersModel tom = si.getCurrentOrder(w);
      if (tom != null) {
        ObjectKey2 ok = tom.getStation();
        StationModel station = (StationModel) w.get(ok);
        destination = station.getStationName();
      }
      priority = train.getPriority();
      state = train.getState();
      setStatusLabel(train);
    } else {
      s = "No trains to display";
      setStatusLabel(null);
    }
    setStatusButton(priority, state);
    nameJLabel.setText(s);
    destinationJLabel.setText(destination);
  }
 public void listUpdated(KEY key, int index, FreerailsPrincipal p) {
   if (KEY.TRAINS == key && index == trainNumber && p.equals(modelRoot.getPlayerPrincipal())) {
     // The train has been updated.
     this.displayTrain(this.trainNumber);
     repaint();
   }
   trainViewJPanel1.listUpdated(key, index, p);
 }
  public void setup() {
    ReadOnlyWorld w = modelRoot.getWorld();
    this.w = w;
    trainModelViewer = new TrainModelViewer(w);

    org.railz.client.renderer.ViewLists vl = modelRoot.getViewLists();
    trainViewJPanel1 = new TrainViewJPanel(modelRoot);
    trainViewJPanel1.setHeight(20);
    trainViewJPanel1.setCenterTrain(true);
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.weightx = 1.0;
    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.gridwidth = 2;
    add(trainViewJPanel1, gbc);

    modelRoot.getMoveChainFork().addListListener(this);
    guiRoot.addRefreshListener(this);
  }
        /**
         * Cycle through train status in the following order: Priority express, standard, slow,
         * stopped
         */
        public void actionPerformed(ActionEvent e) {
          if (trainNumber == -1) return;

          TrainModel tm =
              (TrainModel) w.get(KEY.TRAINS, trainNumber, modelRoot.getPlayerPrincipal());

          int priority = tm.getPriority();
          int state = tm.getState();

          if (state == TrainModel.STATE_STOPPED) {
            state = TrainModel.STATE_RUNNABLE;
            priority = TrainModel.PRIORITY_EXPRESS;
          } else {
            switch (priority) {
              case TrainModel.PRIORITY_EXPRESS:
                priority = TrainModel.PRIORITY_NORMAL;
                break;
              case TrainModel.PRIORITY_NORMAL:
                priority = TrainModel.PRIORITY_SLOW;
                break;
              case TrainModel.PRIORITY_SLOW:
                state = TrainModel.STATE_STOPPED;
                break;
              default:
                throw new IllegalStateException();
            }
          }
          Move m;
          GameTime now = (GameTime) w.get(ITEM.TIME, Player.AUTHORITATIVE);
          if (tm.getState() != state) {
            m =
                ChangeTrainMove.generateMove(
                    trainNumber, modelRoot.getPlayerPrincipal(), tm, state, now);
            modelRoot.getReceiver().processMove(m);
            tm = (TrainModel) w.get(KEY.TRAINS, trainNumber, modelRoot.getPlayerPrincipal());
          }
          if (tm.getPriority() != priority) {
            m =
                ChangeTrainMove.generatePriorityMove(
                    trainNumber, modelRoot.getPlayerPrincipal(), tm, priority);
            modelRoot.getReceiver().processMove(m);
          }
        }