Search in sources :

Example 1 with LSFile

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

the class LoadDiagramUrlModelAction method backgroundAction.

@Override
protected void backgroundAction() {
    url = (String) getActionContext().getAttribute("diagramUrl");
    if (url == null) {
        // skip
        return;
    }
    setWaitMessage(ResourceLoader.getString("wait.message.loadmodel"));
    setWaitDialogVisible(true);
    long time = System.currentTimeMillis();
    try {
        try (ZipInputStream is = new ZipInputStream(new URL(url).openStream())) {
            LSFile ls = LSFileFactory.getInstance().createForLoad(is);
            context.setAttribute("diagram", ls.load(is));
        } catch (LSException e) {
            log.warn("Error loading model.", e);
            if (e.getCause() instanceof FileNotFoundException) {
                errorMessage = ResourceLoader.getString("dialog.error.filenotfound");
            } else {
                errorMessage = ResourceLoader.getString("dialog.error.loading");
            }
        } catch (Exception e) {
            log.warn("Error loading model.", e);
            errorMessage = ResourceLoader.getString("dialog.error.loading");
        }
    } finally {
        log.debug("Library loaded in {}ms", System.currentTimeMillis() - time);
        setWaitDialogVisible(false);
    }
}
Also used : LSFile(net.parostroj.timetable.model.ls.LSFile) ZipInputStream(java.util.zip.ZipInputStream) FileNotFoundException(java.io.FileNotFoundException) URL(java.net.URL) LSException(net.parostroj.timetable.model.ls.LSException) LSException(net.parostroj.timetable.model.ls.LSException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with LSFile

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

the class LoadDiagramModelAction method backgroundAction.

@Override
protected void backgroundAction() {
    selectedFile = (File) getActionContext().getAttribute("diagramFile");
    if (selectedFile == null) {
        // skip
        return;
    }
    setWaitMessage(ResourceLoader.getString("wait.message.loadmodel"));
    setWaitDialogVisible(true);
    long time = System.currentTimeMillis();
    try {
        try {
            LSFile ls = LSFileFactory.getInstance().createForLoad(selectedFile);
            context.setAttribute("diagram", ls.load(selectedFile));
        } catch (LSException e) {
            log.warn("Error loading model.", e);
            if (e.getCause() instanceof FileNotFoundException) {
                errorMessage = ResourceLoader.getString("dialog.error.filenotfound");
            } else {
                errorMessage = ResourceLoader.getString("dialog.error.loading");
            }
        } catch (Exception e) {
            log.warn("Error loading model.", e);
            errorMessage = ResourceLoader.getString("dialog.error.loading");
        }
    } finally {
        log.debug("Library loaded in {}ms", System.currentTimeMillis() - time);
        setWaitDialogVisible(false);
    }
}
Also used : LSFile(net.parostroj.timetable.model.ls.LSFile) FileNotFoundException(java.io.FileNotFoundException) LSException(net.parostroj.timetable.model.ls.LSException) LSException(net.parostroj.timetable.model.ls.LSException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with LSFile

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

the class ModelUtils method saveModelData.

public static void saveModelData(final ApplicationModel model, File file) throws LSException {
    // update author and date before save
    TrainDiagram diagram = model.getDiagram();
    boolean originalSkip = diagram.getAttributes().isSkipListeners();
    diagram.getAttributes().setSkipListeners(true);
    String user = model.getProgramSettings().getUserNameOrSystemUser();
    diagram.setSaveUser(user);
    final ChangesTracker tracker = diagram.getChangesTracker();
    final DiagramChangeSet set = tracker.getCurrentChangeSet();
    if (set != null && tracker.isTrackingEnabled()) {
        try {
            // do the update in event dispatch thread (because of events)
            SwingUtilities.invokeAndWait(() -> {
                tracker.updateCurrentChangeSet(set.getVersion(), user, Calendar.getInstance());
            });
        } catch (Exception e) {
            log.warn("Error updating values for current diagram change set.", e);
        }
    }
    log.info("Saving: {}", file);
    LSFile ls = LSFileFactory.getInstance().createForSave();
    ls.save(diagram, file);
    diagram.getAttributes().setSkipListeners(originalSkip);
}
Also used : LSFile(net.parostroj.timetable.model.ls.LSFile) ChangesTracker(net.parostroj.timetable.model.changes.ChangesTracker) DiagramChangeSet(net.parostroj.timetable.model.changes.DiagramChangeSet) IOException(java.io.IOException) LSException(net.parostroj.timetable.model.ls.LSException)

Example 4 with LSFile

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

the class MainFrame method aboutMenuItemActionPerformed.

private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
    // show about dialog
    ResourceBundle aboutBundle = ResourceBundle.getBundle("about");
    LSFileFactory f = LSFileFactory.getInstance();
    LSFile fls = null;
    try {
        fls = f.createForSave();
    } catch (LSException e) {
        log.warn("Cannot create FileLoadSave", e);
    }
    AboutDialog dialog = new AboutDialog(this, true, String.format(aboutBundle.getString("text"), model.getVersionInfo().getVersion(), fls == null ? "-" : fls.getSaveVersion()), getClass().getResource(aboutBundle.getString("image")), true, model.getVersionInfo());
    dialog.setLocationRelativeTo(this);
    dialog.setVisible(true);
    dialog.dispose();
}
Also used : LSFile(net.parostroj.timetable.model.ls.LSFile) LSFileFactory(net.parostroj.timetable.model.ls.LSFileFactory) LSException(net.parostroj.timetable.model.ls.LSException)

Example 5 with LSFile

use of net.parostroj.timetable.model.ls.LSFile 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)

Aggregations

LSException (net.parostroj.timetable.model.ls.LSException)5 LSFile (net.parostroj.timetable.model.ls.LSFile)5 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)2 File (java.io.File)1 URL (java.net.URL)1 ZipInputStream (java.util.zip.ZipInputStream)1 CloseableFileChooser (net.parostroj.timetable.gui.actions.impl.CloseableFileChooser)1 TrainDiagram (net.parostroj.timetable.model.TrainDiagram)1 ChangesTracker (net.parostroj.timetable.model.changes.ChangesTracker)1 DiagramChangeSet (net.parostroj.timetable.model.changes.DiagramChangeSet)1 LSFileFactory (net.parostroj.timetable.model.ls.LSFileFactory)1