Search in sources :

Example 21 with TrainDiagram

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

the class NewOpenAction method open.

private void open(final Component parent, final File preselectedFile) {
    // 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);
    }
    ModelAction openAction = new CombinedModelAction(context) {

        private int retVal;

        private TrainDiagram diagram;

        private String errorMessage;

        private Exception errorException;

        private File selectedFile;

        @Override
        protected void eventDispatchActionBefore() {
            if (preselectedFile == null) {
                try (CloseableFileChooser modelFileChooser = FileChooserFactory.getInstance().getFileChooser(FileChooserFactory.Type.GTM)) {
                    retVal = modelFileChooser.showOpenDialog(parent);
                    if (retVal == JFileChooser.APPROVE_OPTION) {
                        selectedFile = modelFileChooser.getSelectedFile();
                    }
                }
            } else {
                selectedFile = preselectedFile;
                retVal = JFileChooser.APPROVE_OPTION;
            }
        }

        @Override
        protected void backgroundAction() {
            if (retVal != JFileChooser.APPROVE_OPTION) {
                return;
            }
            setWaitMessage(ResourceLoader.getString("wait.message.loadmodel"));
            setWaitDialogVisible(true);
            long time = System.currentTimeMillis();
            try {
                try {
                    model.setOpenedFile(selectedFile);
                    log.info("Loading: {}", selectedFile);
                    LSFile ls = LSFileFactory.getInstance().createForLoad(selectedFile);
                    diagram = ls.load(selectedFile);
                } catch (LSException e) {
                    log.warn("Error loading model.", e);
                    if (e.getCause() instanceof FileNotFoundException) {
                        // remove from last opened
                        model.removeLastOpenedFile(selectedFile);
                        // create error message
                        errorMessage = ResourceLoader.getString("dialog.error.filenotfound");
                    } else if (e.getCause() instanceof IOException) {
                        errorMessage = ResourceLoader.getString("dialog.error.loading");
                    } else {
                        errorMessage = ResourceLoader.getString("dialog.error.loading");
                        errorException = e;
                    }
                } catch (Exception e) {
                    log.warn("Error loading model.", e);
                    errorMessage = ResourceLoader.getString("dialog.error.loading");
                }
            } finally {
                log.debug("Loaded in {}ms", System.currentTimeMillis() - time);
            }
        }

        @Override
        protected void eventDispatchActionAfter() {
            try {
                if (retVal != JFileChooser.APPROVE_OPTION) {
                    return;
                }
                if (diagram != null) {
                    model.setDiagram(diagram);
                } else {
                    String text = errorMessage + " " + selectedFile.getName();
                    if (errorException != null) {
                        text = text + "\n(" + errorException.getMessage() + ")";
                    }
                    context.setAttribute("error", text);
                    model.setDiagram(null);
                }
            } finally {
                setWaitDialogVisible(false);
            }
        }
    };
    ActionHandler.getInstance().execute(openAction);
    ActionHandler.getInstance().execute(ModelAction.newEdtAction(context, () -> {
        // show error
        if (context.getAttribute("error") != null) {
            GuiComponentUtils.showError((String) context.getAttribute("error"), parent);
        }
    }));
}
Also used : LSFile(net.parostroj.timetable.model.ls.LSFile) FileNotFoundException(java.io.FileNotFoundException) CloseableFileChooser(net.parostroj.timetable.gui.actions.impl.CloseableFileChooser) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) LSException(net.parostroj.timetable.model.ls.LSException) TrainDiagram(net.parostroj.timetable.model.TrainDiagram) File(java.io.File) LSFile(net.parostroj.timetable.model.ls.LSFile) LSException(net.parostroj.timetable.model.ls.LSException)

Example 22 with TrainDiagram

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

the class NewModelPM method create.

private void create() {
    final Template templateInstance = template.getValue();
    final Scale scaleValue = scale.getValue();
    final double timeScaleValue = timeScale.getBigDecimal().doubleValue();
    this.createTask = new Callable<TrainDiagram>() {

        @Override
        public TrainDiagram call() throws LSException {
            TrainDiagram diagram = templateLoader.loadTemplate(templateInstance);
            diagram.setAttribute(TrainDiagram.ATTR_SCALE, scaleValue);
            diagram.setAttribute(TrainDiagram.ATTR_TIME_SCALE, timeScaleValue);
            return diagram;
        }
    };
}
Also used : Scale(net.parostroj.timetable.model.Scale) LSException(net.parostroj.timetable.model.ls.LSException) Template(net.parostroj.timetable.model.templates.Template) TrainDiagram(net.parostroj.timetable.model.TrainDiagram)

Example 23 with TrainDiagram

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

the class TrainDiagramBuilder method getTrainDiagram.

public TrainDiagram getTrainDiagram() throws LSException {
    if (diagram == null) {
        throw new IllegalStateException("Diagram already created");
    }
    this.finishDelaydObjectWithIds();
    this.finishCirculationSequences();
    // after load check
    (new AfterLoadCheck()).check(diagram);
    // tracking of changes has to be enabled at the end, otherwise
    // it would also track changes caused by loading of the diagram
    diagram.getChangesTracker().setTrackingEnabled(trackChanges);
    if (trackChanges) {
        diagram.getChangesTracker().addVersion(null, null, null);
        diagram.getChangesTracker().setLastAsCurrent();
    }
    TrainDiagram retValue = diagram;
    diagram = null;
    return retValue;
}
Also used : AfterLoadCheck(net.parostroj.timetable.actions.AfterLoadCheck) 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