From 20b987f1089b8e2e38f503a460c6fddd3f7c3ccb Mon Sep 17 00:00:00 2001 From: Abraham Wolk Date: Mon, 10 Jun 2024 09:22:48 +0200 Subject: [PATCH] Revert "CSSTUDIO-1987 Remove unused code." This reverts commit ffe810a8b56103ca4fb6eb9b9cf1e24fdc92a909. --- .../runtime/app/DockItemRepresentation.java | 20 +++++++++++++++++++ .../builder/runtime/script/ScriptUtil.java | 18 +++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/app/display/runtime/src/main/java/org/csstudio/display/builder/runtime/app/DockItemRepresentation.java b/app/display/runtime/src/main/java/org/csstudio/display/builder/runtime/app/DockItemRepresentation.java index fc23c0dce4..5a13862a88 100644 --- a/app/display/runtime/src/main/java/org/csstudio/display/builder/runtime/app/DockItemRepresentation.java +++ b/app/display/runtime/src/main/java/org/csstudio/display/builder/runtime/app/DockItemRepresentation.java @@ -141,4 +141,24 @@ public void representModel(final Parent model_parent, final DisplayModel model) app_instance.trackCurrentModel(model); super.representModel(model_parent, model); } + + @Override + public void closeWindow(final DisplayModel model) throws Exception + { + // Is called from ScriptUtil, i.e. scripts, from background thread + final Parent model_parent = Objects.requireNonNull(model.getUserData(Widget.USER_DATA_TOOLKIT_PARENT)); + if (model_parent.getProperties().get(DisplayRuntimeInstance.MODEL_PARENT_DISPLAY_RUNTIME) == app_instance) + { + // Prepare-to-close, which might take time and must be called off the UI thread + final DisplayRuntimeInstance instance = (DisplayRuntimeInstance) app_instance.getRepresentation().getModelParent().getProperties().get(DisplayRuntimeInstance.MODEL_PARENT_DISPLAY_RUNTIME); + if (instance != null) + instance.getDockItem().prepareToClose(); + else + logger.log(Level.SEVERE, "Missing DisplayRuntimeInstance to prepare closing", new Exception("Stack Trace")); + // 'close' on the UI thread + execute(() -> app_instance.close()); + } + else + throw new Exception("Wrong model"); + } } diff --git a/app/display/runtime/src/main/java/org/csstudio/display/builder/runtime/script/ScriptUtil.java b/app/display/runtime/src/main/java/org/csstudio/display/builder/runtime/script/ScriptUtil.java index 600fff19bd..7f6b4b2a60 100644 --- a/app/display/runtime/src/main/java/org/csstudio/display/builder/runtime/script/ScriptUtil.java +++ b/app/display/runtime/src/main/java/org/csstudio/display/builder/runtime/script/ScriptUtil.java @@ -141,6 +141,24 @@ public static void openDisplay(final Widget widget, final String file, final Str ActionUtil.handleAction(widget, open); } + /** Close a display + * + * @param widget Widget within the display to close + */ + public static void closeDisplay(final Widget widget) + { + try + { + final DisplayModel model = widget.getTopDisplayModel(); + final ToolkitRepresentation toolkit = ToolkitRepresentation.getToolkit(model); + toolkit.closeWindow(model); + } + catch (Throwable ex) + { + logger.log(Level.WARNING, "Cannot close display", ex); + } + } + // ==================== // public alert dialog utils