Search in sources :

Example 6 with TrainDiagram

use of net.parostroj.timetable.model.TrainDiagram in project grafikon by jub77.

the class TCListView method createButtonActionPerformed.

private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {
    // get name from text field (ignore shorter than one character
    if (newNameTextField.getText().length() > 0 && delegate.getType() != null) {
        TrainDiagram trainDiagram = delegate.getTrainDiagram();
        TrainsCycle cycle = new TrainsCycle(IdGenerator.getInstance().getId(), trainDiagram, newNameTextField.getText(), null, delegate.getType());
        cycle.getType().getCycles().add(cycle);
        // clear field
        newNameTextField.setText("");
        // fire event
        delegate.fireEvent(TCDelegate.Action.NEW_CYCLE, cycle);
        // set selected
        int index = cycles.getIndexOfObject(cycle);
        cyclesList.setSelectedIndex(index);
        cyclesList.ensureIndexIsVisible(index);
        delegate.fireEvent(TCDelegate.Action.SELECTED_CHANGED, cycle);
    }
}
Also used : TrainsCycle(net.parostroj.timetable.model.TrainsCycle) TrainDiagram(net.parostroj.timetable.model.TrainDiagram)

Example 7 with TrainDiagram

use of net.parostroj.timetable.model.TrainDiagram in project grafikon by jub77.

the class InfoPM method writeResult.

private void writeResult() {
    TrainDiagram diagram = diagramRef.get();
    if (diagram != null) {
        // save values
        String lNumber = ObjectsUtil.checkAndTrim(this.routeNumbers.getText());
        String lNodes = ObjectsUtil.checkAndTrim(this.routeNodes.getText());
        String lValidity = ObjectsUtil.checkAndTrim(this.validity.getText());
        String lInfo = ObjectsUtil.checkAndTrim(this.info.getText());
        diagram.getAttributes().setRemove(TrainDiagram.ATTR_ROUTE_NUMBERS, lNumber);
        diagram.getAttributes().setRemove(TrainDiagram.ATTR_ROUTE_NODES, lNodes);
        diagram.getAttributes().setRemove(TrainDiagram.ATTR_ROUTE_VALIDITY, lValidity);
        diagram.getAttributes().setRemove(TrainDiagram.ATTR_INFO, lInfo);
    }
}
Also used : TrainDiagram(net.parostroj.timetable.model.TrainDiagram)

Example 8 with TrainDiagram

use of net.parostroj.timetable.model.TrainDiagram in project grafikon by jub77.

the class TrainTableModel method getValueAt.

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    TimeInterval interval = train.getTimeIntervalList().get(rowIndex);
    TrainDiagram diagram = train.getDiagram();
    Object retValue = null;
    TrainTableColumn column = TrainTableColumn.getColumn(columnIndex);
    switch(column) {
        // node name
        case NODE:
            if (interval.isNodeOwner()) {
                retValue = interval.getOwnerAsNode().getName();
            } else {
                retValue = "";
            }
            break;
        // arrival
        case START:
            if (!interval.isFirst()) {
                retValue = converter.convertIntToText(interval.getStart(), true);
            }
            break;
        // departure
        case END:
            if (!interval.isLast()) {
                retValue = converter.convertIntToText(interval.getEnd(), true);
            }
            break;
        // stop time
        case STOP:
            if (interval.isNodeOwner() && rowIndex != 0 && rowIndex != lastRow && interval.getOwnerAsNode().getType() != NodeType.SIGNAL) {
                retValue = converter.convertIntToMinutesText(interval.getLength());
            }
            break;
        // speed
        case SPEED_LIMIT:
            if (interval.isLineOwner()) {
                retValue = interval.getSpeedLimit();
            }
            break;
        // used speed
        case SPEED:
            retValue = interval.getSpeed();
            break;
        // added time
        case ADDED_TIME:
            if (interval.isLineOwner() && interval.getAddedTime() != 0) {
                retValue = converter.convertIntToMinutesText(interval.getAddedTime());
            }
            break;
        // platform
        case PLATFORM:
            if (interval.isNodeOwner()) {
                if (interval.getOwnerAsNode().getTracks().size() > 1) {
                    retValue = interval.getTrack();
                }
            } else if (interval.isLineOwner()) {
                // only for more than one track per line
                if (interval.getOwnerAsLine().getTracks().size() > 1) {
                    return interval.getTrack();
                }
            }
            break;
        // problems
        case CONFLICTS:
            StringBuilder builder = new StringBuilder();
            for (TimeInterval overlap : interval.getOverlappingIntervals()) {
                if (builder.length() != 0) {
                    builder.append(", ");
                }
                builder.append(overlap.getTrain().getDefaultName());
            }
            retValue = builder.toString();
            break;
        // comment
        case COMMENT:
            LocalizedString lComment = interval.getComment();
            retValue = lComment == null ? null : lComment.getDefaultString();
            break;
        case OCCUPIED_ENTRY:
            retValue = interval.getAttributes().getBool(TimeInterval.ATTR_OCCUPIED);
            break;
        case SHUNT:
            retValue = interval.getAttributes().getBool(TimeInterval.ATTR_SHUNT);
            break;
        case COMMENT_SHOWN:
            retValue = interval.getAttributes().getBool(TimeInterval.ATTR_COMMENT_SHOWN);
            break;
        case REAL_STOP:
            if (interval.isNodeOwner() && rowIndex != 0 && rowIndex != lastRow && interval.getOwnerAsNode().getType() != NodeType.SIGNAL) {
                int stop = interval.getLength() / 60;
                // celculate with time scale ...
                Double timeScale = diagram.getAttributes().get(TrainDiagram.ATTR_TIME_SCALE, Double.class);
                retValue = stop / timeScale;
            }
            break;
        case WEIGHT:
            // weight info
            if (interval.isLineOwner()) {
                retValue = TrainsHelper.getWeight(interval);
            }
            break;
        case LENGTH:
            // length info
            retValue = TrainsHelper.getLength(interval);
            break;
        case SET_SPEED:
            // train speed
            retValue = interval.getAttribute(TimeInterval.ATTR_SET_SPEED, Object.class);
            break;
        case IGNORE_LENGTH:
            // ignore station length
            retValue = interval.getAttributes().getBool(TimeInterval.ATTR_IGNORE_LENGTH);
            break;
        case MANAGED_FREIGHT:
            // managed freight
            retValue = false;
            if (train.isManagedFreight() && interval.isNodeOwner()) {
                retValue = (interval.getLength() > 0 || rowIndex == 0 || rowIndex == lastRow) && !interval.getAttributeAsBool(TimeInterval.ATTR_NOT_MANAGED_FREIGHT);
            }
            break;
        case REGION_CENTER_TRANSFER:
            // transfer in region center
            retValue = false;
            if (interval.isNodeOwner() && train.isManagedFreight()) {
                if (interval.getOwnerAsNode().isCenterOfRegions() && !interval.getAttributeAsBool(TimeInterval.ATTR_NO_REGION_CENTER_TRANSFER) && rowIndex != 0) {
                    retValue = true;
                }
            }
            break;
        case FREIGHT_TO_STATIONS:
            FreightConnectionStrategy strategy = train.getDiagram().getFreightNet().getConnectionStrategy();
            if (rowIndex % 2 == 0 && (interval.isFreight() || interval.isFreightConnection())) {
                StringBuilder result = new StringBuilder();
                Map<Train, List<FreightConnectionPath>> passedCargoDst = strategy.getFreightPassedInNode(interval);
                for (Map.Entry<Train, List<FreightConnectionPath>> entry : passedCargoDst.entrySet()) {
                    List<FreightConnectionPath> mList = entry.getValue();
                    result.append('(').append(freightUtil.freightListToString(mList, Locale.getDefault()));
                    result.append(" > ").append(entry.getKey().getDefaultName()).append(')');
                }
                if (interval.isFreightFrom()) {
                    List<FreightConnectionPath> cargoDst = strategy.getFreightToNodes(interval);
                    List<FreightConnectionPath> mList = cargoDst;
                    if (!cargoDst.isEmpty() && result.length() > 0) {
                        result.append(' ');
                    }
                    result.append(freightUtil.freightListToString(mList, Locale.getDefault()));
                }
                retValue = result.toString();
            }
            break;
        // default (should not be reached)
        default:
            // nothing
            assert false : "Unexpected column";
            break;
    }
    return retValue;
}
Also used : TimeInterval(net.parostroj.timetable.model.TimeInterval) FreightConnectionStrategy(net.parostroj.timetable.model.freight.FreightConnectionStrategy) LocalizedString(net.parostroj.timetable.model.LocalizedString) TrainDiagram(net.parostroj.timetable.model.TrainDiagram) FreightConnectionPath(net.parostroj.timetable.model.freight.FreightConnectionPath) List(java.util.List) Train(net.parostroj.timetable.model.Train) Map(java.util.Map)

Example 9 with TrainDiagram

use of net.parostroj.timetable.model.TrainDiagram in project grafikon by jub77.

the class NewOpenAction method create.

private void create(final Component parent) {
    // check changes
    final int result = ModelUtils.checkModelChangedContinue(model, parent);
    if (result == JOptionPane.CANCEL_OPTION) {
        return;
    }
    // save old diagram
    ActionContext context = new ActionContext(parent);
    if (result == JOptionPane.YES_OPTION) {
        ModelAction saveAction = SaveAction.getSaveModelAction(context, model.getOpenedFile(), parent, model);
        ActionHandler.getInstance().execute(saveAction);
    }
    // new
    ModelAction newAction = new EventDispatchModelAction(context) {

        @Override
        protected void eventDispatchAction() {
            try {
                // create new model
                NewModelDialog newModelDialog = new NewModelDialog((Window) parent, true);
                newModelDialog.setLocationRelativeTo(parent);
                Callable<TrainDiagram> diagramCreator = newModelDialog.showDialog(templateLoader);
                newModelDialog.dispose();
                context.setAttribute("diagramCreator", diagramCreator);
            } catch (LSException error) {
                log.warn("Cannot load template.", error);
                JOptionPane.showMessageDialog(parent, error.getMessage(), ResourceLoader.getString("dialog.error.title"), JOptionPane.ERROR_MESSAGE);
                context.setCancelled(true);
            }
        }
    };
    ModelAction createAction = new EventDispatchAfterModelAction(context) {

        private Callable<TrainDiagram> diagramCreator;

        private TrainDiagram diagram;

        private Exception error;

        @SuppressWarnings("unchecked")
        @Override
        protected boolean check() {
            diagramCreator = (Callable<TrainDiagram>) context.getAttribute("diagramCreator");
            return diagramCreator != null;
        }

        @Override
        protected void backgroundAction() {
            setWaitMessage(ResourceLoader.getString("wait.message.loadmodel"));
            setWaitDialogVisible(true);
            long time = System.currentTimeMillis();
            try {
                try {
                    diagram = diagramCreator.call();
                } catch (Exception ex) {
                    error = ex;
                    return;
                }
            } finally {
                log.debug("Template loaded in {}ms", System.currentTimeMillis() - time);
                setWaitDialogVisible(false);
            }
        }

        @Override
        protected void eventDispatchActionAfter() {
            if (diagram != null) {
                model.setDiagram(diagram);
                model.setOpenedFile(null);
                model.setModelChanged(true);
            }
            if (error != null) {
                log.warn("Cannot load template.", error);
                JOptionPane.showMessageDialog(parent, error.getMessage(), ResourceLoader.getString("dialog.error.title"), JOptionPane.ERROR_MESSAGE);
            }
        }
    };
    ActionHandler.getInstance().execute(newAction);
    ActionHandler.getInstance().execute(createAction);
}
Also used : NewModelDialog(net.parostroj.timetable.gui.dialogs.NewModelDialog) LSException(net.parostroj.timetable.model.ls.LSException) Callable(java.util.concurrent.Callable) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) LSException(net.parostroj.timetable.model.ls.LSException) TrainDiagram(net.parostroj.timetable.model.TrainDiagram)

Example 10 with TrainDiagram

use of net.parostroj.timetable.model.TrainDiagram in project grafikon by jub77.

the class ImportSelectionModelAction method eventDispatchAction.

@Override
protected void eventDispatchAction() {
    TrainDiagram diagram = (TrainDiagram) context.getAttribute("diagram");
    Library library = (Library) context.getAttribute("library");
    boolean trainImport = context.hasAttribute("trainImport");
    TrainGroupFilter filter = (TrainGroupFilter) context.getAttribute("trainFilter");
    if (diagram != null || library != null) {
        final ExportImportSelectionDialog importDialog = new ExportImportSelectionDialog(GuiComponentUtils.getWindow(context.getLocationComponent()), true);
        ExportImportSelectionSource source;
        if (trainImport) {
            source = ExportImportSelectionSource.fromDiagramSingleTypeWithFilter(diagram, ImportComponent.TRAINS, filter::apply);
        } else {
            source = diagram != null ? ExportImportSelectionSource.fromDiagramToDiagram(diagram) : ExportImportSelectionSource.fromLibraryToDiagram(library);
        }
        importDialog.setSelectionSource(source);
        importDialog.setLocationRelativeTo(context.getLocationComponent());
        importDialog.setVisible(true);
        context.setCancelled(importDialog.isCancelled());
        if (!context.isCancelled()) {
            context.setAttribute("selection", importDialog.getSelection());
        }
    }
}
Also used : ExportImportSelectionDialog(net.parostroj.timetable.gui.dialogs.ExportImportSelectionDialog) Library(net.parostroj.timetable.model.library.Library) ExportImportSelectionSource(net.parostroj.timetable.gui.components.ExportImportSelectionSource) TrainDiagram(net.parostroj.timetable.model.TrainDiagram)

Aggregations

TrainDiagram (net.parostroj.timetable.model.TrainDiagram)23 LSException (net.parostroj.timetable.model.ls.LSException)5 IOException (java.io.IOException)4 Train (net.parostroj.timetable.model.Train)4 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 TrainsCycle (net.parostroj.timetable.model.TrainsCycle)3 Component (java.awt.Component)2 List (java.util.List)2 Map (java.util.Map)2 Properties (java.util.Properties)2 ZipEntry (java.util.zip.ZipEntry)2 JOptionPane (javax.swing.JOptionPane)2 AfterLoadCheck (net.parostroj.timetable.actions.AfterLoadCheck)2 ExportImportSelection (net.parostroj.timetable.gui.components.ExportImportSelection)2 GuiComponentUtils (net.parostroj.timetable.gui.utils.GuiComponentUtils)2 LocalizedString (net.parostroj.timetable.model.LocalizedString)2 Library (net.parostroj.timetable.model.library.Library)2 ModelVersion (net.parostroj.timetable.model.ls.ModelVersion)2 Predicate (com.google.common.base.Predicate)1