Search in sources :

Example 1 with CloseableFileChooser

use of net.parostroj.timetable.gui.actions.impl.CloseableFileChooser in project grafikon by jub77.

the class MainFrame method ouputTemplatesMenuItemActionPerformed.

private void ouputTemplatesMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
    // output templates list dialog
    OutputTemplateListDialog dialog = new OutputTemplateListDialog(this, true);
    dialog.setLocationRelativeTo(this);
    dialog.registerContext(model.getGuiContext());
    OutputSettings settings = model.getOutputSettings();
    FileChooserFactory chooserFactory = FileChooserFactory.getInstance();
    File outputDirectory = chooserFactory.getLocation(FileChooserFactory.Type.OUTPUT_DIRECTORY);
    try (CloseableFileChooser allChooser = chooserFactory.getFileChooser(FileChooserFactory.Type.ALL_FILES)) {
        dialog.showDialog(model.getDiagram(), outputDirectory, allChooser, new Settings(settings.getLocale()));
        dialog.dispose();
    }
}
Also used : FileChooserFactory(net.parostroj.timetable.gui.actions.impl.FileChooserFactory) OutputSettings(net.parostroj.timetable.gui.data.OutputSettings) CloseableFileChooser(net.parostroj.timetable.gui.actions.impl.CloseableFileChooser) File(java.io.File) LSFile(net.parostroj.timetable.model.ls.LSFile) Settings(net.parostroj.timetable.output2.OutputWriter.Settings) OutputSettings(net.parostroj.timetable.gui.data.OutputSettings)

Example 2 with CloseableFileChooser

use of net.parostroj.timetable.gui.actions.impl.CloseableFileChooser in project grafikon by jub77.

the class MainFrame method ouputMenuItemActionPerformed.

private void ouputMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
    // dialog with outputs
    EditOutputsDialog dialog = EditOutputsDialog.newInstance(this, true);
    dialog.setSettings(new Settings(model.getOutputSettings().getLocale()));
    GenerateOutputPM pm = new GenerateOutputPM(model.getLanguageLoader().getAvailableLocales(), model.getDiagram().getLocales());
    try (CloseableFileChooser chooser = FileChooserFactory.getInstance().getFileChooser(FileChooserFactory.Type.OUTPUT_DIRECTORY)) {
        pm.init(model.get(), chooser);
        dialog.setPresentationModel(pm);
        dialog.setLocationRelativeTo(this);
        dialog.registerContext(model.getGuiContext());
        dialog.showDialog(model.getDiagram());
        dialog.dispose();
        pm.writeBack();
    }
}
Also used : GenerateOutputPM(net.parostroj.timetable.gui.pm.GenerateOutputPM) CloseableFileChooser(net.parostroj.timetable.gui.actions.impl.CloseableFileChooser) Settings(net.parostroj.timetable.output2.OutputWriter.Settings) OutputSettings(net.parostroj.timetable.gui.data.OutputSettings)

Example 3 with CloseableFileChooser

use of net.parostroj.timetable.gui.actions.impl.CloseableFileChooser in project grafikon by jub77.

the class ExportAction method actionPerformed.

@Override
public void actionPerformed(ActionEvent event) {
    final Component parent = GuiComponentUtils.getTopLevelComponent(event.getSource());
    ExportImportSelectionSource source = ExportImportSelectionSource.fromDiagramToLibrary(model.getDiagram());
    exportDialog.setSelectionSource(source);
    exportDialog.setLocationRelativeTo(parent);
    exportDialog.setVisible(true);
    boolean cancelled = exportDialog.isCancelled();
    if (!cancelled) {
        // saving library
        try (CloseableFileChooser gtmlFileChooser = FileChooserFactory.getInstance().getFileChooser(FileChooserFactory.Type.GTML)) {
            int retVal = gtmlFileChooser.showSaveDialog(parent);
            if (retVal == JFileChooser.APPROVE_OPTION) {
                ActionContext c = new ActionContext(parent);
                ModelAction action = getSaveModelAction(c, gtmlFileChooser.getSelectedFile(), parent, this.createLibrary(exportDialog.getSelection()));
                ActionHandler.getInstance().execute(action);
            }
        }
    }
}
Also used : EventDispatchAfterModelAction(net.parostroj.timetable.gui.actions.execution.EventDispatchAfterModelAction) ModelAction(net.parostroj.timetable.gui.actions.execution.ModelAction) CloseableFileChooser(net.parostroj.timetable.gui.actions.impl.CloseableFileChooser) ExportImportSelectionSource(net.parostroj.timetable.gui.components.ExportImportSelectionSource) Component(java.awt.Component) ActionContext(net.parostroj.timetable.gui.actions.execution.ActionContext)

Example 4 with CloseableFileChooser

use of net.parostroj.timetable.gui.actions.impl.CloseableFileChooser 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 5 with CloseableFileChooser

use of net.parostroj.timetable.gui.actions.impl.CloseableFileChooser in project grafikon by jub77.

the class SaveAction method saveAs.

private void saveAs(Component parent) {
    if (model.getDiagram() == null) {
        GuiComponentUtils.showError(ResourceLoader.getString("dialog.error.nodiagram"), parent);
        return;
    }
    // saving train diagram
    try (CloseableFileChooser gtmFileChooser = FileChooserFactory.getInstance().getFileChooser(FileChooserFactory.Type.GTM)) {
        if (model.getOpenedFile() != null) {
            gtmFileChooser.cancelSelection();
            File cDir = gtmFileChooser.getCurrentDirectory();
            gtmFileChooser.setSelectedFile(null);
            File nFile = new File(cDir, model.getOpenedFile().getName());
            gtmFileChooser.setSelectedFile(nFile);
        }
        int retVal = gtmFileChooser.showSaveDialog(parent);
        if (retVal == JFileChooser.APPROVE_OPTION) {
            model.setOpenedFile(gtmFileChooser.getSelectedFile());
            ActionContext c = new ActionContext(parent);
            ModelAction action = getSaveModelAction(c, gtmFileChooser.getSelectedFile(), parent, model);
            ActionHandler.getInstance().execute(action);
        }
    }
}
Also used : CloseableFileChooser(net.parostroj.timetable.gui.actions.impl.CloseableFileChooser) File(java.io.File)

Aggregations

CloseableFileChooser (net.parostroj.timetable.gui.actions.impl.CloseableFileChooser)5 File (java.io.File)3 OutputSettings (net.parostroj.timetable.gui.data.OutputSettings)2 LSFile (net.parostroj.timetable.model.ls.LSFile)2 Settings (net.parostroj.timetable.output2.OutputWriter.Settings)2 Component (java.awt.Component)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ActionContext (net.parostroj.timetable.gui.actions.execution.ActionContext)1 EventDispatchAfterModelAction (net.parostroj.timetable.gui.actions.execution.EventDispatchAfterModelAction)1 ModelAction (net.parostroj.timetable.gui.actions.execution.ModelAction)1 FileChooserFactory (net.parostroj.timetable.gui.actions.impl.FileChooserFactory)1 ExportImportSelectionSource (net.parostroj.timetable.gui.components.ExportImportSelectionSource)1 GenerateOutputPM (net.parostroj.timetable.gui.pm.GenerateOutputPM)1 TrainDiagram (net.parostroj.timetable.model.TrainDiagram)1 LSException (net.parostroj.timetable.model.ls.LSException)1