Search in sources :

Example 1 with NewModelDialog

use of net.parostroj.timetable.gui.dialogs.NewModelDialog 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)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Callable (java.util.concurrent.Callable)1 NewModelDialog (net.parostroj.timetable.gui.dialogs.NewModelDialog)1 TrainDiagram (net.parostroj.timetable.model.TrainDiagram)1 LSException (net.parostroj.timetable.model.ls.LSException)1