Search in sources :

Example 36 with LSException

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

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

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

the class LSOutputTemplate method createOutputTemplate.

public OutputTemplate createOutputTemplate(PartFactory partFactory, Function<String, ObjectWithId> mapping, FileLoadSaveAttachments flsAttachments) throws LSException {
    OutputTemplate outputTemplate = partFactory.createOutputTemplate(id);
    if (name != null) {
        // name mapped to key
        outputTemplate.setKey(name);
    }
    if (this.template != null) {
        outputTemplate.setTemplate(this.template.createTextTemplate());
    }
    if (this.script != null) {
        outputTemplate.setScript(this.script.createScript());
    }
    outputTemplate.getAttributes().add(attributes.createAttributes(mapping));
    // process attachments
    if (attachments != null) {
        for (LSAttachment attachment : attachments) {
            if (attachment.getRef() != null) {
                if (flsAttachments == null) {
                    throw new LSException("Attachment loader cannot be null");
                }
                flsAttachments.addForLoad(attachment, outputTemplate);
            } else {
                // process inline data
                if (attachment.getBinaryData() != null) {
                    outputTemplate.getAttachments().add(new Attachment(attachment.getName(), attachment.getBinaryData()));
                } else if (attachment.getTextData() != null) {
                    outputTemplate.getAttachments().add(new Attachment(attachment.getName(), attachment.getTextData()));
                }
            }
        }
    }
    return outputTemplate;
}
Also used : OutputTemplate(net.parostroj.timetable.model.OutputTemplate) Attachment(net.parostroj.timetable.model.Attachment) 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