Search in sources :

Example 11 with LSException

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

the class FileLoadSaveImpl method load.

@Override
public TrainDiagram load(ZipInputStream zipInput) throws LSException {
    try {
        ZipEntry entry = null;
        TrainDiagramBuilder builder = null;
        FileLoadSaveImages loadImages = new FileLoadSaveImages(DATA_IMAGES);
        ModelVersion version = (ModelVersion) properties.get(VERSION_PROPERTY);
        while ((entry = zipInput.getNextEntry()) != null) {
            if (entry.getName().equals(METADATA)) {
                // check major and minor version (do not allow load newer versions)
                Properties props = new Properties();
                props.load(zipInput);
                version = checkVersion(props);
                continue;
            }
            if (entry.getName().equals(DATA_TRAIN_DIAGRAM)) {
                LSTrainDiagram lstd = lss.load(zipInput, LSTrainDiagram.class);
                builder = new TrainDiagramBuilder(lstd);
            }
            // test diagram
            if (builder == null) {
                throw new LSException("Train diagram builder has to be first entry: " + entry.getName());
            }
            if (entry.getName().equals(DATA_NET)) {
                builder.setNet(lss.load(zipInput, LSNet.class));
            } else if (entry.getName().startsWith(DATA_ROUTES)) {
                builder.setRoute(lss.load(zipInput, LSRoute.class));
            } else if (entry.getName().startsWith(DATA_TRAIN_TYPES)) {
                builder.setTrainType(lss.load(zipInput, LSTrainType.class));
            } else if (entry.getName().startsWith(DATA_TRAINS)) {
                builder.setTrain(lss.load(zipInput, LSTrain.class));
            } else if (entry.getName().startsWith(DATA_ENGINE_CLASSES)) {
                builder.setEngineClass(lss.load(zipInput, LSEngineClass.class));
            } else if (entry.getName().startsWith(DATA_TRAINS_CYCLES)) {
                builder.setTrainsCycle(lss.load(zipInput, LSTrainsCycle.class));
            } else if (entry.getName().startsWith(DATA_IMAGES)) {
                if (entry.getName().endsWith(".xml")) {
                    builder.addImage(lss.load(zipInput, LSImage.class));
                } else {
                    builder.addImageFile(new File(entry.getName()).getName(), loadImages.loadTimetableImage(zipInput, entry));
                }
            }
        }
        TrainDiagram trainDiagram = builder.getTrainDiagram();
        new LoadFilter().checkDiagram(trainDiagram, version);
        return trainDiagram;
    } catch (IOException e) {
        throw new LSException(e);
    }
}
Also used : Properties(java.util.Properties) ModelVersion(net.parostroj.timetable.model.ls.ModelVersion) LSFile(net.parostroj.timetable.model.ls.LSFile) LSException(net.parostroj.timetable.model.ls.LSException)

Example 12 with LSException

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

the class FileLoadSaveImpl method save.

@Override
public void save(TrainDiagram diagram, ZipOutputStream zipOutput) throws LSException {
    try {
        // save metadata
        zipOutput.putNextEntry(new ZipEntry(METADATA));
        this.createMetadata().store(zipOutput, null);
        // save train diagram
        this.save(zipOutput, DATA_TRAIN_DIAGRAM, new LSTrainDiagram(diagram));
        // save net
        this.save(zipOutput, DATA_NET, new LSNet(diagram.getNet()));
        int cnt = 0;
        // save routes
        for (Route route : diagram.getRoutes()) {
            this.save(zipOutput, this.createEntryName(DATA_ROUTES, "xml", cnt++), new LSRoute(route));
        }
        cnt = 0;
        // save train types
        for (TrainType trainType : diagram.getTrainTypes()) {
            this.save(zipOutput, this.createEntryName(DATA_TRAIN_TYPES, "xml", cnt++), new LSTrainType(trainType));
        }
        cnt = 0;
        // save trains
        for (Train train : diagram.getTrains()) {
            this.save(zipOutput, this.createEntryName(DATA_TRAINS, "xml", cnt++), new LSTrain(train));
        }
        cnt = 0;
        // save engine classes
        for (EngineClass engineClass : diagram.getEngineClasses()) {
            this.save(zipOutput, this.createEntryName(DATA_ENGINE_CLASSES, "xml", cnt++), new LSEngineClass(engineClass));
        }
        cnt = 0;
        // save trains cycles
        for (TrainsCycle cycle : diagram.getCycles()) {
            this.save(zipOutput, this.createEntryName(DATA_TRAINS_CYCLES, "xml", cnt++), new LSTrainsCycle(cycle));
        }
        // save images
        cnt = 0;
        FileLoadSaveImages saveImages = new FileLoadSaveImages(DATA_IMAGES);
        for (TimetableImage image : diagram.getImages()) {
            this.save(zipOutput, createEntryName(DATA_IMAGES, "xml", cnt++), new LSImage(image));
            saveImages.saveTimetableImage(image, zipOutput);
        }
    } catch (IOException ex) {
        throw new LSException(ex);
    }
}
Also used : LSException(net.parostroj.timetable.model.ls.LSException)

Example 13 with LSException

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

the class LSSerializer method save.

public <T> void save(OutputStream out, T saved) throws LSException {
    Writer writer = null;
    try {
        writer = new OutputStreamWriter(out, "utf-8");
        marshaller.marshal(saved, writer);
    } catch (UnsupportedEncodingException e) {
        throw new LSException("Cannot save train diagram: Unsupported enconding.", e);
    } catch (JAXBException e) {
        throw new LSException("Cannot save train diagram: JAXB exception.", e);
    }
}
Also used : JAXBException(javax.xml.bind.JAXBException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) LSException(net.parostroj.timetable.model.ls.LSException)

Example 14 with LSException

use of net.parostroj.timetable.model.ls.LSException 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 15 with LSException

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

the class SaveAction method getSaveModelAction.

public static ModelAction getSaveModelAction(ActionContext context, final File file, final Component parent, final ApplicationModel model) {
    ModelAction action = new EventDispatchAfterModelAction(context) {

        private String errorMessage;

        @Override
        protected void backgroundAction() {
            setWaitMessage(ResourceLoader.getString("wait.message.savemodel"));
            setWaitDialogVisible(true);
            long time = System.currentTimeMillis();
            try {
                ModelUtils.saveModelData(model, file);
            } catch (LSException e) {
                log.warn("Error saving model.", e);
                errorMessage = ResourceLoader.getString("dialog.error.saving");
            } catch (Exception e) {
                log.warn("Error saving model.", e);
                errorMessage = ResourceLoader.getString("dialog.error.saving");
            } finally {
                log.debug("Saved in {}ms", System.currentTimeMillis() - time);
                setWaitDialogVisible(false);
            }
        }

        @Override
        protected void eventDispatchActionAfter() {
            if (errorMessage != null) {
                GuiComponentUtils.showError(errorMessage + " " + file.getName(), parent);
            } else {
                model.fireEvent(new ApplicationModelEvent(ApplicationModelEventType.MODEL_SAVED, model));
            }
        }
    };
    return action;
}
Also used : ApplicationModelEvent(net.parostroj.timetable.gui.ApplicationModelEvent) LSException(net.parostroj.timetable.model.ls.LSException) LSException(net.parostroj.timetable.model.ls.LSException)

Aggregations

LSException (net.parostroj.timetable.model.ls.LSException)38 IOException (java.io.IOException)8 JAXBException (javax.xml.bind.JAXBException)7 LSFile (net.parostroj.timetable.model.ls.LSFile)7 FileNotFoundException (java.io.FileNotFoundException)6 ZipEntry (java.util.zip.ZipEntry)6 ModelVersion (net.parostroj.timetable.model.ls.ModelVersion)6 ZipInputStream (java.util.zip.ZipInputStream)5 TrainDiagram (net.parostroj.timetable.model.TrainDiagram)5 Properties (java.util.Properties)4 TrainType (net.parostroj.timetable.model.TrainType)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 OutputTemplate (net.parostroj.timetable.model.OutputTemplate)3 Route (net.parostroj.timetable.model.Route)3 File (java.io.File)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Writer (java.io.Writer)2 URL (java.net.URL)2 EngineClass (net.parostroj.timetable.model.EngineClass)2 DiagramChangeSet (net.parostroj.timetable.model.changes.DiagramChangeSet)2