Search in sources :

Example 1 with ScriptAction

use of net.parostroj.timetable.actions.scripts.ScriptAction in project grafikon by jub77.

the class MainFrame method initializeFrame.

/**
 * initializes frame.
 */
private void initializeFrame() {
    provider.setPresentationModel(model);
    lastOpened = new HashMap<>();
    // set local before anything else
    String loadedLocale = null;
    try {
        IniConfigSection section = AppPreferences.getPreferences().getSection("main");
        loadedLocale = section.get("locale.program");
        String templateLocale = section.get("locale.output");
        if (loadedLocale != null) {
            Locale locale = ModelUtils.parseLocale(loadedLocale);
            model.setLocale(locale);
            Locale.setDefault(locale);
        } else {
            model.setLocale(null);
        }
        if (templateLocale != null) {
            model.getOutputSettings().setLocale(ModelUtils.parseLocale(templateLocale));
        }
    } catch (IOException e) {
        log.warn("Cannot load preferences.", e);
    }
    log.debug("Locale: {}", Locale.getDefault());
    executeScriptAction = new ExecuteScriptAction(model);
    initComponents();
    this.addWindowListener(new MainFrameWindowListener(model, this));
    trainsPane.setModel(model);
    engineCyclesPane.setModel(new EngineCycleDelegate(model), interval -> {
        TrainsCycleType type = interval.getTrain().getDiagram().getEngineCycleType();
        if (!interval.getTrain().isCovered(type, interval)) {
            return Color.black;
        } else {
            return Color.gray;
        }
    });
    trainUnitCyclesPane.setModel(new TrainUnitCycleDelegate(model), interval -> {
        TrainsCycleType type = interval.getTrain().getDiagram().getTrainUnitCycleType();
        if (!interval.getTrain().isCovered(type, interval)) {
            return Color.black;
        } else {
            return Color.gray;
        }
    });
    driverCyclesPane.setModel(new DriverCycleDelegate(model), interval -> {
        TrainsCycleType type = interval.getTrain().getDiagram().getDriverCycleType();
        if (!interval.getTrain().isCovered(type, interval)) {
            return Color.black;
        } else {
            return Color.gray;
        }
    });
    circulationPane.setModel(model);
    freightNetPane2.setModel(model);
    model.addListener(this);
    model.addListener(statusBar);
    netPane.setModel(model);
    this.updateView();
    model.setDiagram(null);
    // apply preferences
    try {
        this.loadFromPreferences(AppPreferences.getPreferences());
    } catch (IOException e) {
        log.error("Error loading preferences.", e);
    }
    // add predefined scripts
    for (ScriptAction sd : model.getScriptsLoader().getScriptActions()) {
        addScriptAction(sd, ExecuteScriptAction.MODEL_PREFIX, scriptsMenuModel);
    }
    // add gui scripts
    for (ScriptAction sd : model.getGuiScriptsLoader().getScriptActions()) {
        addScriptAction(sd, ExecuteScriptAction.GUI_PREFIX, scriptsMenuGui);
    }
    statusBar.setModel(model);
}
Also used : EngineCycleDelegate(net.parostroj.timetable.gui.views.EngineCycleDelegate) DriverCycleDelegate(net.parostroj.timetable.gui.views.DriverCycleDelegate) IOException(java.io.IOException) ScriptAction(net.parostroj.timetable.actions.scripts.ScriptAction) IniConfigSection(net.parostroj.timetable.gui.ini.IniConfigSection) TrainUnitCycleDelegate(net.parostroj.timetable.gui.views.TrainUnitCycleDelegate)

Example 2 with ScriptAction

use of net.parostroj.timetable.actions.scripts.ScriptAction in project grafikon by jub77.

the class ExecuteScriptAction method predefinedExecution.

private void predefinedExecution(ScriptsLoader loader, Component comp, String scriptId) {
    Component parent = GuiComponentUtils.getTopLevelComponent(comp);
    parent.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    long time = System.currentTimeMillis();
    try {
        try {
            ScriptAction scriptAction = loader.getScriptAction(scriptId);
            scriptAction.execute(model.getDiagram(), Collections.<String, Object>singletonMap("parent", parent));
        } finally {
            parent.setCursor(Cursor.getDefaultCursor());
            log.debug("Script {} finished in {}ms", scriptId, System.currentTimeMillis() - time);
        }
    } catch (GrafikonException ex) {
        log.error("Error executing script.", ex);
        String message = ex.getCause() != null ? ex.getCause().getMessage() : ex.getMessage();
        GuiComponentUtils.showError(message, parent);
    }
}
Also used : GrafikonException(net.parostroj.timetable.model.GrafikonException) ScriptAction(net.parostroj.timetable.actions.scripts.ScriptAction) Component(java.awt.Component)

Aggregations

ScriptAction (net.parostroj.timetable.actions.scripts.ScriptAction)2 Component (java.awt.Component)1 IOException (java.io.IOException)1 IniConfigSection (net.parostroj.timetable.gui.ini.IniConfigSection)1 DriverCycleDelegate (net.parostroj.timetable.gui.views.DriverCycleDelegate)1 EngineCycleDelegate (net.parostroj.timetable.gui.views.EngineCycleDelegate)1 TrainUnitCycleDelegate (net.parostroj.timetable.gui.views.TrainUnitCycleDelegate)1 GrafikonException (net.parostroj.timetable.model.GrafikonException)1