Skip to content

Commit

Permalink
Refactor TreeStateHelper part 2
Browse files Browse the repository at this point in the history
- Store the 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
- No need to use dispose listeners
  • Loading branch information
Phillipus committed Jan 24, 2025
1 parent bd8b535 commit 7ffac42
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -214,12 +209,6 @@ public void init(IViewSite site, IMemento memento) throws PartInitException {

@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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,21 @@ private String getAncestorFolderRenderText(IArchimateModelObject object) {

// ========================= Model Providers =====================================


private Object[] rootExpandedTreeElements;

Object[] getRootExpandedElements() {
return rootExpandedTreeElements;
}

/**
* Content Provider
*/
private class ModelTreeViewerContentProvider implements ITreeContentProvider {

@Override
public void inputChanged(Viewer v, Object oldInput, Object newInput) {
rootExpandedTreeElements = oldInput == IEditorModelManager.INSTANCE ? getVisibleExpandedElements() : null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
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;
Expand Down Expand Up @@ -44,9 +43,6 @@ public class TreeStateHelper {
// Expanded tree elements
private List<Object> expandedElements;

// State has been restored from memento the first time the Tree is created
private boolean restoredFromMemento;

private TreeStateHelper() {}

/**
Expand All @@ -56,12 +52,10 @@ private TreeStateHelper() {}
* This is called from {@link TreeModelView#init(org.eclipse.ui.IViewSite, IMemento)}, but we only want to do this once.
*/
void setMemento(IMemento memento) {
if(restoredFromMemento || memento == null) {
if(memento == null) {
return;
}

restoredFromMemento = true;

IMemento expandedMem = memento.getChild(MEMENTO_EXPANDED);
if(expandedMem != null) {
expandedElements = new ArrayList<>();
Expand Down Expand Up @@ -101,16 +95,7 @@ void setMemento(IMemento memento) {
* Restore expanded elements on TreeView part creation.
* This is called from {@link TreeModelView#doCreatePartControl(org.eclipse.swt.widgets.Composite)
*/
void restoreExpandedTreeElements(TreeViewer viewer) {
// Store expanded tree elements if the TreeViewer is closed so they can be restored when it's re-opened
// We could restore from the memento but this is more efficient and the drill-down is reset when closing the Tree.
viewer.getTree().addDisposeListener(e -> {
expandedElements = new ArrayList<>();
for(Object element : viewer.getVisibleExpandedElements()) {
expandedElements.add(element);
}
});

void restoreExpandedTreeElements(TreeModelViewer viewer) {
if(expandedElements != null) {
for(Object element : expandedElements) {
viewer.expandToLevel(element, 1);
Expand All @@ -123,10 +108,12 @@ void restoreExpandedTreeElements(TreeViewer viewer) {
* Save expanded state of tree elements to a memento.
* This is called from {@link TreeModelView#saveState(IMemento)}
*/
void saveStateToMemento(TreeViewer viewer, IMemento memento) {
void saveStateToMemento(TreeModelViewer viewer, IMemento memento) {
Map<File, String> map = new HashMap<>();

for(Object object : viewer.getVisibleExpandedElements()) {
Object[] elements = viewer.getRootExpandedElements() == null ? viewer.getVisibleExpandedElements() : viewer.getRootExpandedElements();

for(Object object : elements) {
if(object instanceof IArchimateModelObject modelObject) {
// Only store if model has been saved to file
File file = modelObject.getArchimateModel().getFile();
Expand Down

0 comments on commit 7ffac42

Please sign in to comment.