From fc3f8409f975529c1d53c9b5fe39858f1e9dfca1 Mon Sep 17 00:00:00 2001
From: Phillipus
Date: Sat, 25 Jan 2025 10:11:25 +0000
Subject: [PATCH] Refactor TreeStateHelper part 2
- Store the root expanded tree elements when the DrillDownAdapter sets the tree model input
- When we save the tree state and we have drilled into the DrillDownAdapter we can save these root elements
---
.../editor/views/tree/TreeModelView.java | 21 +++----
.../editor/views/tree/TreeModelViewer.java | 24 ++++++++
.../editor/views/tree/TreeStateHelper.java | 59 ++++++-------------
3 files changed, 50 insertions(+), 54 deletions(-)
diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/views/tree/TreeModelView.java b/com.archimatetool.editor/src/com/archimatetool/editor/views/tree/TreeModelView.java
index 91cbaf398..b4b7ec501 100644
--- a/com.archimatetool.editor/src/com/archimatetool/editor/views/tree/TreeModelView.java
+++ b/com.archimatetool.editor/src/com/archimatetool/editor/views/tree/TreeModelView.java
@@ -155,11 +155,6 @@ public void doCreatePartControl(Composite parent) {
// Drill down
fDrillDownAdapter = new DrillDownAdapter(fTreeViewer);
- // Set drill down to home when disposing parent (not when the tree is disposed as the content provider is null at that point)
- parent.addDisposeListener(e -> {
- setDrillDownHome();
- });
-
// Listen to Double-click and press Return Action
fTreeViewer.addDoubleClickListener(new IDoubleClickListener() {
@Override
@@ -208,18 +203,18 @@ public void selectionChanged(SelectionChangedEvent event) {
@Override
public void init(IViewSite site, IMemento memento) throws PartInitException {
super.init(site, memento);
- // Restore expanded tree state from file when opening for first time
+ // Set memento with expanded tree state when creating the Tree
TreeStateHelper.INSTANCE.setMemento(memento);
}
-
+
+ /**
+ * This is called:
+ * 1. Every 5 minutes by the workbench autosaving the workbench state (this can be set in org.eclipse.ui.internal.IPreferenceConstants#WORKBENCH_SAVE_INTERVAL)
+ * 2. When this ViewPart is closed
+ * 3. When the app quits (which is really 2)
+ */
@Override
public void saveState(IMemento memento) {
- // saveState() is called periodically by Eclipse when auto-saving the workbench so only reset the drill-down when closing the Workbench.
- // This period is set in org.eclipse.ui.internal.IPreferenceConstants#WORKBENCH_SAVE_INTERVAL and the default is 5 minutes.
- if(PlatformUI.getWorkbench().isClosing()) {
- setDrillDownHome();
- }
-
// Save expanded tree state
TreeStateHelper.INSTANCE.saveStateToMemento(fTreeViewer, memento);
}
diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/views/tree/TreeModelViewer.java b/com.archimatetool.editor/src/com/archimatetool/editor/views/tree/TreeModelViewer.java
index da193e613..4d17945aa 100644
--- a/com.archimatetool.editor/src/com/archimatetool/editor/views/tree/TreeModelViewer.java
+++ b/com.archimatetool.editor/src/com/archimatetool/editor/views/tree/TreeModelViewer.java
@@ -75,6 +75,11 @@ public class TreeModelViewer extends TreeViewer {
*/
private SearchFilter searchFilter;
+ /**
+ * Root expanded visible tree elements, saved when DrillDownAdapter is used.
+ */
+ private Object[] rootVisibleExpandedElements;
+
/**
* Listener for theme font change
*/
@@ -197,6 +202,7 @@ public boolean canModify(Object element, String property) {
viewpointFilterProvider = null;
searchFilter = null;
+ rootVisibleExpandedElements = null;
});
}
@@ -371,6 +377,16 @@ private String getAncestorFolderRenderText(IArchimateModelObject object) {
return null;
}
+ /**
+ * Get the root expanded elements in case the DrillDownAdapter is active.
+ * Note that some of these elements might have been deleted when the tree was drilled into.
+ * Returns either the root expanded elements that were visible before the DrillDownAdapter was drilled into,
+ * or the current visible expanded elements if the DrillDownAdapter is "Home".
+ */
+ Object[] getRootVisibleExpandedElements() {
+ return rootVisibleExpandedElements != null ? rootVisibleExpandedElements : getVisibleExpandedElements();
+ }
+
// ========================= Model Providers =====================================
/**
@@ -380,6 +396,14 @@ private class ModelTreeViewerContentProvider implements ITreeContentProvider {
@Override
public void inputChanged(Viewer v, Object oldInput, Object newInput) {
+ // The DrillDownAdapter sets the new input when calling DrillDownAdapter#goInto().
+ // We save the current expanded elements in the root state so we can persist these when we save the expanded tree state
+ if(oldInput == IEditorModelManager.INSTANCE && newInput != null) { // Drilldown has moved out of "Home"
+ rootVisibleExpandedElements = getVisibleExpandedElements();
+ }
+ else if(newInput == IEditorModelManager.INSTANCE) { // Drilldown is "Home"
+ rootVisibleExpandedElements = null;
+ }
}
@Override
diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/views/tree/TreeStateHelper.java b/com.archimatetool.editor/src/com/archimatetool/editor/views/tree/TreeStateHelper.java
index ea498c6e6..e8fe050df 100644
--- a/com.archimatetool.editor/src/com/archimatetool/editor/views/tree/TreeStateHelper.java
+++ b/com.archimatetool.editor/src/com/archimatetool/editor/views/tree/TreeStateHelper.java
@@ -6,14 +6,11 @@
package com.archimatetool.editor.views.tree;
import java.io.File;
-import java.util.ArrayList;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.eclipse.emf.ecore.EObject;
-import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.ui.IMemento;
import com.archimatetool.editor.model.IEditorModelManager;
@@ -41,31 +38,31 @@ public class TreeStateHelper {
private static final String MEMENTO_FILE = "file";
private static final String MEMENTO_ELEMENTS = "elements";
- // Expanded tree elements
- private List