Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fab435003dff9f7f2ac5efd92420706a7483f8a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**********************************************************************
 * Copyright (c) 2004, 2005 QNX Software Systems and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 
 * QNX Software Systems - Initial API and implementation
***********************************************************************/
package org.eclipse.debug.internal.ui.views;

import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.IPath;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.internal.ui.views.variables.VariablesViewer;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.TreeItem;

/**
 * The abstract superclass for mementos of the expanded and 
 * selected items in a tree viewer.
 */
public abstract class AbstractViewerState {

	// paths to expanded elements
	protected List fSavedExpansion = null;
    protected IPath[] fSelection;
	
	/**
	 * Constructs a memento for the given viewer.
	 */
	public AbstractViewerState(TreeViewer viewer) {
		saveState(viewer);
	}
    
    public AbstractViewerState() {
        
    }

	/**
	 * Saves the current state of the given viewer into
	 * this memento.
	 * 
	 * @param viewer viewer of which to save the state
	 */
	public void saveState(TreeViewer viewer) {
		List expanded = new ArrayList();
		fSavedExpansion = null;
		TreeItem[] items = viewer.getTree().getItems();
		try {
			for (int i = 0; i < items.length; i++) {
				collectExpandedItems(items[i], expanded);
			}
			if (expanded.size() > 0) {
				fSavedExpansion = expanded;
			}
		} catch (DebugException e) {
			fSavedExpansion = null;
		}
		TreeItem[] selection = viewer.getTree().getSelection();
		fSelection = new IPath[selection.length];
		try {
		    for (int i = 0; i < selection.length; i++) {
		        fSelection[i] = encodeElement(selection[i]);
		        if (fSelection[i] == null) {
		            fSelection = null;
		            return;
		        }
		    }
		} catch (DebugException e) {
		    fSelection = null;
		}
	}

	protected void collectExpandedItems(TreeItem item, List expanded) throws DebugException {
        if (item.getExpanded()) {
            IPath path = encodeElement(item);
            if (path != null) {
                expanded.add(path);
                TreeItem[] items = item.getItems();
                for (int i = 0; i < items.length; i++) {
                    collectExpandedItems(items[i], expanded);
                }
            }
        }
    }

	/**
	 * Constructs a path representing the given tree item. The segments in the
	 * path denote parent items, and the last segment is the name of
	 * the given item.
	 *   
	 * @param item tree item to encode
	 * @return path encoding the given item
	 * @throws DebugException if unable to generate a path
	 */
	protected abstract IPath encodeElement(TreeItem item) throws DebugException;

	/**
	 * Restores the state of the given viewer to this memento's
	 * saved state.
	 * 
	 * @param viewer viewer to which state is restored
	 */
	public void restoreState(TreeViewer viewer) {
	    boolean expansionComplete = true;
	    if (fSavedExpansion != null && fSavedExpansion.size() > 0) {		
	        for (int i = 0; i < fSavedExpansion.size(); i++) {
	            IPath path = (IPath) fSavedExpansion.get(i);
	            if (path != null) {
	                if (viewer instanceof VariablesViewer) {
                        VariablesViewer variablesViewer = (VariablesViewer) viewer;
                        boolean complete = variablesViewer.expandPath(path);
                        if (!complete)
                            expansionComplete = false; 
	                } else {
	                    Object obj;
	                    try {
	                        obj = decodePath(path, viewer);
	                        if (obj != null) {
	                            viewer.expandToLevel(obj, 1);
	                        } else {
	                            expansionComplete = false;                  
	                        }
	                    } catch (DebugException e) {
	                    }
	                }
	            }
	        }
	        if (expansionComplete) {
	            fSavedExpansion = null;
	        }
	    }
	    
	    boolean selectionComplete = true;
	    if (fSelection != null && fSelection.length > 0) {
	        List selection = new ArrayList(fSelection.length);
	        for (int i = 0; i < fSelection.length; i++) {
	            IPath path = fSelection[i];
	            Object obj;
	            try {
	                obj = decodePath(path, viewer);
	                if (obj != null) {
	                    selection.add(obj);
	                } else {
	                    selectionComplete = false;               
	                }
	            } catch (DebugException e) {
	            }
	        }
            if (selection.size() > 0) {
                viewer.setSelection(new StructuredSelection(selection));
            }
	        if (selectionComplete) {
	            fSelection = null;
	        }
	    }
	}
	
	/**
	 * Returns an element in the given viewer that corresponds to the given
	 * path, or <code>null</code> if none.
	 * 
	 * @param path encoded element path
	 * @param viewer viewer to search for the element in
	 * @return element represented by the path, or <code>null</code> if none
	 * @throws DebugException if unable to locate a variable
	 */
	protected abstract Object decodePath(IPath path, TreeViewer viewer) throws DebugException;

    public abstract AbstractViewerState copy();
    
    
}

Back to the top