Search in sources :

Example 1 with RouteBuilder

use of net.parostroj.timetable.actions.RouteBuilder in project grafikon by jub77.

the class CreateTrainView method okButtonActionPerformed.

private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
    // test needed values
    Integer speed = null;
    try {
        String speedText = ObjectsUtil.checkAndTrim(speedTextField.getText());
        if (speedText != null) {
            speed = Integer.valueOf(speedText);
            if (speed < 1) {
                throw new NumberFormatException();
            }
        }
    } catch (NumberFormatException e) {
        GuiComponentUtils.showError(ResourceLoader.getString("create.train.trainspeedmissing"), this.getParent());
        return;
    }
    if (nameTextField.getText() == null || nameTextField.getText().trim().equals("")) {
        GuiComponentUtils.showError(ResourceLoader.getString("create.train.trainnamemissing"), this.getParent());
        return;
    }
    if (fromComboBox.getSelectedItem() == null || toComboBox.getSelectedItem() == null) {
        GuiComponentUtils.showError("", this.getParent());
        return;
    }
    RouteBuilder routeBuilder = new RouteBuilder();
    Route route = null;
    if (throughNodes == null)
        route = routeBuilder.createRoute(null, diagram.getNet(), (Node) fromComboBox.getSelectedItem(), (Node) toComboBox.getSelectedItem());
    else {
        List<Node> r = new ArrayList<>();
        r.add((Node) fromComboBox.getSelectedItem());
        r.addAll(throughNodes);
        r.add((Node) toComboBox.getSelectedItem());
        route = routeBuilder.createRoute(null, diagram.getNet(), r);
    }
    if (route == null) {
        GuiComponentUtils.showError(ResourceLoader.getString("create.train.createtrainerror"), this.getParent());
        return;
    }
    // get start time
    int start = diagram.getTimeConverter().convertTextToInt(startTimeTextField.getText());
    if (start == -1)
        // midnight if cannot be parsed
        start = 0;
    Group group = groupComboBox.getGroupSelection().getGroup();
    // create command ...
    TrainType tType = (TrainType) typeComboBox.getSelectedItem();
    CreateTrainCommand createCommand = new CreateTrainCommand(ObjectsUtil.checkAndTrim(nameTextField.getText()), tType != NO_TYPE ? tType : null, speed, route, start, (stopTextField.getText().equals("") ? 0 : Integer.valueOf(stopTextField.getText()) * 60), ObjectsUtil.checkAndTrim(commentTextField.getText()), dieselCheckBox.isSelected(), electricCheckBox.isSelected(), true, group, managedFreightCheckBox.isSelected());
    this.createTrainCommand = createCommand;
    // hide dialog
    this.closeDialog();
}
Also used : RouteBuilder(net.parostroj.timetable.actions.RouteBuilder) CreateTrainCommand(net.parostroj.timetable.gui.commands.CreateTrainCommand) ArrayList(java.util.ArrayList)

Example 2 with RouteBuilder

use of net.parostroj.timetable.actions.RouteBuilder in project grafikon by jub77.

the class TrainListView method changeRoute.

private void changeRoute() {
    CreateRouteDialog dialog = new CreateRouteDialog();
    dialog.setLocationRelativeTo(menuButton);
    Train oldTrain = model.getSelectedTrain();
    List<Node> result = dialog.showDialog(model.getDiagram(), Arrays.asList(oldTrain.getStartNode(), oldTrain.getEndNode()));
    if (result != null) {
        try {
            RouteBuilder rb = new RouteBuilder();
            Route route = rb.createRoute(null, model.getDiagram().getNet(), result);
            if (route == null) {
                throw new IllegalArgumentException("No route available.");
            }
            TrainBuilder builder = new TrainBuilder();
            model.setSelectedTrain(null);
            this.deleteTrain(oldTrain, oldTrain.getDiagram());
            Train newTrain = builder.createTrain(oldTrain, route);
            model.getDiagram().getTrains().add(newTrain);
            model.setSelectedTrain(newTrain);
        } catch (Exception e) {
            log.warn("Error changing route of the train.", e);
            GuiComponentUtils.showError(ResourceLoader.getString("dialog.error.title") + ": " + e.getMessage(), this);
        }
    }
}
Also used : RouteBuilder(net.parostroj.timetable.actions.RouteBuilder) TrainBuilder(net.parostroj.timetable.actions.TrainBuilder)

Aggregations

RouteBuilder (net.parostroj.timetable.actions.RouteBuilder)2 ArrayList (java.util.ArrayList)1 TrainBuilder (net.parostroj.timetable.actions.TrainBuilder)1 CreateTrainCommand (net.parostroj.timetable.gui.commands.CreateTrainCommand)1