Search in sources :

Example 1 with MementoTree

use of org.phoebus.framework.persistence.MementoTree in project phoebus by ControlSystemStudio.

the class PhoebusApplication method backgroundStartup.

/**
 * Perform potentially slow startup task off the UI thread
 *
 * @param monitor
 * @param splash
 * @throws Exception
 */
private void backgroundStartup(final JobMonitor monitor, final Splash splash) throws Exception {
    // Assume there's 100 percent of work do to,
    // not knowing, yet, how many applications to start etc.
    monitor.beginTask(Messages.MonitorTaskApps, 100);
    // Locate registered applications and start them, allocating 30% to that
    startApplications(new SubJobMonitor(monitor, 30));
    // Load saved state (slow file access) off UI thread, allocating 30% to that
    monitor.beginTask(Messages.MonitorTaskSave);
    final MementoTree memento = loadDefaultMemento(application_parameters, new SubJobMonitor(monitor, 30));
    // Trigger initialization of authentication service
    AuthorizationService.init();
    // Back to UI thread
    Platform.runLater(() -> {
        try {
            // Leaving remaining 40% to the UI startup
            startUI(memento, new SubJobMonitor(monitor, 40));
        } catch (Throwable ex) {
            logger.log(Level.SEVERE, "Application cannot start up", ex);
        }
        monitor.done();
        if (splash != null)
            splash.close();
    });
}
Also used : SubJobMonitor(org.phoebus.framework.jobs.SubJobMonitor) MementoTree(org.phoebus.framework.persistence.MementoTree) XMLMementoTree(org.phoebus.framework.persistence.XMLMementoTree)

Example 2 with MementoTree

use of org.phoebus.framework.persistence.MementoTree in project phoebus by ControlSystemStudio.

the class MementoHelper method saveDockItem.

/**
 * Save state of a DockItem
 *
 *  <p>Store the application name, optionally the resource (input),
 *  and then allow the application itself to store details.
 *
 *  @param memento
 *  @param item
 */
private static void saveDockItem(final MementoTree memento, final DockItem item) {
    final AppInstance application = item.getApplication();
    if (application == null || application.isTransient())
        return;
    final MementoTree item_memento = memento.getChild(item.getID());
    item_memento.setString(DockItem.KEY_APPLICATION, application.getAppDescriptor().getName());
    if (item instanceof DockItemWithInput) {
        final URI input = ((DockItemWithInput) item).getInput();
        if (input != null)
            item_memento.setString(INPUT_URI, input.toString());
    }
    try {
        application.save(item_memento);
    } catch (Throwable ex) {
        logger.log(Level.SEVERE, "Application " + application.getAppDescriptor().getDisplayName() + " memento error", ex);
    }
}
Also used : AppInstance(org.phoebus.framework.spi.AppInstance) MementoTree(org.phoebus.framework.persistence.MementoTree) XMLMementoTree(org.phoebus.framework.persistence.XMLMementoTree) DockItemWithInput(org.phoebus.ui.docking.DockItemWithInput) URI(java.net.URI)

Example 3 with MementoTree

use of org.phoebus.framework.persistence.MementoTree in project phoebus by ControlSystemStudio.

the class PhoebusApplication method restoreState.

/**
 * Restore stages from memento
 *
 * @return <code>true</code> if any tab was restored
 */
private boolean restoreState(final MementoTree memento) {
    boolean any = false;
    if (memento == null)
        return any;
    // There should be just one, empty stage
    final List<Stage> stages = DockStage.getDockStages();
    if (stages.size() > 1 || stages.stream().map(DockStage::getDockPanes).count() > 1) {
        // More than one stage, or a stage with more than one pane
        logger.log(Level.WARNING, "Expected single, empty stage for restoring state", new Exception("Stack Trace"));
        final StringBuilder buf = new StringBuilder();
        buf.append("Found:\n");
        for (Stage stage : stages) DockStage.dump(buf, stage);
        logger.log(Level.WARNING, buf.toString());
    }
    try {
        // Global settings
        memento.getString(LAST_OPENED_FILE).ifPresent(path -> last_opened_file = new File(path));
        memento.getString(DEFAULT_APPLICATION).ifPresent(app -> default_application = app);
        memento.getBoolean(SHOW_TABS).ifPresent(show -> {
            DockPane.alwaysShowTabs(show);
            show_tabs.setSelected(show);
        });
        memento.getBoolean(SHOW_MENU).ifPresent(this::showMenu);
        memento.getBoolean(SHOW_TOOLBAR).ifPresent(show -> {
            showToolbar(show);
            show_toolbar.setSelected(show);
        });
        memento.getBoolean(SHOW_STATUSBAR).ifPresent(show -> {
            showStatusbar(show);
            show_statusbar.setSelected(show);
        });
        // Settings for each stage
        for (MementoTree stage_memento : memento.getChildren()) {
            final String id = stage_memento.getName();
            Stage stage = DockStage.getDockStageByID(id);
            if (stage == null) {
                // Create new Stage with that ID
                stage = new Stage();
                DockStage.configureStage(stage);
                stage.getProperties().put(DockStage.KEY_ID, id);
                stage.show();
            }
            any |= MementoHelper.restoreStage(stage_memento, stage);
        }
    } catch (Throwable ex) {
        logger.log(Level.WARNING, "Error restoring saved state", ex);
    }
    return any;
}
Also used : MementoTree(org.phoebus.framework.persistence.MementoTree) XMLMementoTree(org.phoebus.framework.persistence.XMLMementoTree) DockStage(org.phoebus.ui.docking.DockStage) Stage(javafx.stage.Stage) File(java.io.File)

Example 4 with MementoTree

use of org.phoebus.framework.persistence.MementoTree in project phoebus by ControlSystemStudio.

the class MementoHelper method saveStage.

/**
 * Save state of Stage to memento
 *  @param memento
 *  @param stage
 */
public static void saveStage(final MementoTree memento, final Stage stage) {
    final MementoTree stage_memento = memento.createChild(DockStage.getID(stage));
    stage_memento.setNumber(X, stage.getX());
    stage_memento.setNumber(Y, stage.getY());
    stage_memento.setNumber(WIDTH, stage.getWidth());
    stage_memento.setNumber(HEIGHT, stage.getHeight());
    if (stage.isFullScreen())
        stage_memento.setBoolean(FULLSCREEN, true);
    else if (stage.isMaximized())
        stage_memento.setBoolean(MAXIMIZED, true);
    else if (stage.isIconified())
        stage_memento.setBoolean(MINIMIZED, true);
    final Node node = DockStage.getPaneOrSplit(stage);
    savePaneOrSplit(stage_memento, node);
}
Also used : MementoTree(org.phoebus.framework.persistence.MementoTree) XMLMementoTree(org.phoebus.framework.persistence.XMLMementoTree) Node(javafx.scene.Node)

Example 5 with MementoTree

use of org.phoebus.framework.persistence.MementoTree in project phoebus by ControlSystemStudio.

the class MementoHelper method savePaneOrSplit.

/**
 * @param memento
 *  @param item DockPane or SplitDock
 */
private static void savePaneOrSplit(final MementoTree memento, final Node node) {
    if (node instanceof DockPane) {
        final DockPane pane = (DockPane) node;
        final MementoTree pane_memento = memento.createChild(PANE);
        if (pane.isFixed())
            pane_memento.setBoolean(FIXED, true);
        if (pane.getName().length() > 0)
            pane_memento.setString(NAME, pane.getName());
        pane_memento.setNumber(SELECTED, pane.getSelectionModel().getSelectedIndex());
        for (DockItem item : pane.getDockItems()) saveDockItem(pane_memento, item);
    } else if (node instanceof SplitDock) {
        final SplitDock split = (SplitDock) node;
        final MementoTree split_memento = memento.createChild(SPLIT);
        split_memento.setNumber(POS, split.getDividerPosition());
        if (!split.isHorizontal())
            split_memento.setBoolean(HORIZONTAL, false);
        for (Node sub : split.getItems()) savePaneOrSplit(split_memento, sub);
    } else
        logger.log(Level.WARNING, "Cannot persist " + node);
}
Also used : DockPane(org.phoebus.ui.docking.DockPane) MementoTree(org.phoebus.framework.persistence.MementoTree) XMLMementoTree(org.phoebus.framework.persistence.XMLMementoTree) Node(javafx.scene.Node) SplitDock(org.phoebus.ui.docking.SplitDock) DockItem(org.phoebus.ui.docking.DockItem)

Aggregations

MementoTree (org.phoebus.framework.persistence.MementoTree)8 XMLMementoTree (org.phoebus.framework.persistence.XMLMementoTree)8 Node (javafx.scene.Node)3 DockPane (org.phoebus.ui.docking.DockPane)3 File (java.io.File)2 URI (java.net.URI)2 Rectangle2D (javafx.geometry.Rectangle2D)2 Screen (javafx.stage.Screen)2 Stage (javafx.stage.Stage)2 AppInstance (org.phoebus.framework.spi.AppInstance)2 DockItem (org.phoebus.ui.docking.DockItem)2 DockItemWithInput (org.phoebus.ui.docking.DockItemWithInput)2 DockStage (org.phoebus.ui.docking.DockStage)2 SplitDock (org.phoebus.ui.docking.SplitDock)2 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TimeUnit (java.util.concurrent.TimeUnit)1 Level (java.util.logging.Level)1 Platform (javafx.application.Platform)1