KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > AbstractViewerState


1 /**********************************************************************
2  * Copyright (c) 2004, 2005 QNX Software Systems and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * QNX Software Systems - Initial API and implementation
10 ***********************************************************************/

11 package org.eclipse.debug.internal.ui.views;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.debug.core.DebugException;
20 import org.eclipse.debug.internal.ui.viewers.AsynchronousTreeViewer;
21 import org.eclipse.jface.viewers.TreePath;
22 import org.eclipse.jface.viewers.TreeSelection;
23 import org.eclipse.swt.widgets.TreeItem;
24
25 /**
26  * The abstract superclass for mementos of the expanded and
27  * selected items in a tree viewer.
28  */

29 public abstract class AbstractViewerState implements Cloneable JavaDoc {
30
31     // paths to expanded elements
32
private List JavaDoc fSavedExpansion = null;
33     private IPath[] fSelection;
34     
35     public AbstractViewerState() {
36     }
37     
38     /**
39      * Constructs a memento for the given viewer.
40      */

41     public AbstractViewerState(AsynchronousTreeViewer viewer) {
42         saveState(viewer);
43     }
44
45     /**
46      * Saves the current state of the given viewer into
47      * this memento.
48      *
49      * @param viewer viewer of which to save the state
50      */

51     public void saveState(AsynchronousTreeViewer viewer) {
52         List JavaDoc expanded = new ArrayList JavaDoc();
53         fSavedExpansion = null;
54         TreeItem[] items = viewer.getTree().getItems();
55         try {
56             for (int i = 0; i < items.length; i++) {
57                 collectExpandedItems(items[i], expanded);
58             }
59             if (expanded.size() > 0) {
60                 fSavedExpansion = expanded;
61             }
62         } catch (DebugException e) {
63             fSavedExpansion = null;
64         }
65         TreeItem[] selection = viewer.getTree().getSelection();
66         fSelection = new IPath[selection.length];
67         try {
68             for (int i = 0; i < selection.length; i++) {
69                 fSelection[i] = encodeElement(selection[i]);
70                 if (fSelection[i] == null) {
71                     fSelection = null;
72                     return;
73                 }
74             }
75         } catch (DebugException e) {
76             fSelection = null;
77         }
78     }
79
80     /**
81      * Collects paths to expanded children of the given element and returns
82      * whether any paths were expanded.
83      *
84      * @param item item to collect expanded paths for
85      * @param expanded list to add to
86      * @return whether any paths were found expanded
87      * @throws DebugException
88      */

89     protected boolean collectExpandedItems(TreeItem item, List JavaDoc expanded) throws DebugException {
90         if (item.getExpanded()) {
91             boolean childExpanded = false;
92             TreeItem[] items = item.getItems();
93             for (int i = 0; i < items.length; i++) {
94                 childExpanded = collectExpandedItems(items[i], expanded) || childExpanded;
95             }
96             if (!childExpanded) {
97                 IPath path = encodeElement(item);
98                 expanded.add(path);
99             }
100         } else {
101             return false;
102         }
103         return true;
104     }
105
106     /**
107      * Constructs a path representing the given tree item. The segments in the
108      * path denote parent items, and the last segment is the name of
109      * the given item.
110      *
111      * @param item tree item to encode
112      * @return path encoding the given item
113      * @throws DebugException if unable to generate a path
114      */

115     protected abstract IPath encodeElement(TreeItem item) throws DebugException;
116
117     /**
118      * Restores the state of the given viewer to this memento's
119      * saved state.
120      *
121      * @param viewer viewer to which state is restored
122      */

123     public void restoreState(AsynchronousTreeViewer viewer) {
124         boolean expansionComplete = true;
125         if (fSavedExpansion != null && fSavedExpansion.size() > 0) {
126             for (int i = 0; i < fSavedExpansion.size(); i++) {
127                 IPath path = (IPath) fSavedExpansion.get(i);
128                 if (path != null) {
129                     try {
130                         TreePath treePath = decodePath(path, viewer);
131                         if (treePath != null) {
132                             viewer.expand(new TreeSelection(new TreePath[] { treePath }));
133                             
134                             if (treePath.getSegmentCount()-1 != path.segmentCount()) {
135                                 expansionComplete = false;
136                             }
137                         } else {
138                             expansionComplete =false;
139                         }
140                     } catch (DebugException e) {
141                     }
142                 }
143             }
144             if (expansionComplete) {
145                 fSavedExpansion = null;
146             }
147         }
148         
149         boolean selectionComplete = true;
150         if (fSelection != null && fSelection.length > 0) {
151             List JavaDoc selection = new ArrayList JavaDoc(fSelection.length);
152             for (int i = 0; i < fSelection.length; i++) {
153                 IPath path = fSelection[i];
154                 TreePath obj;
155                 try {
156                     obj = decodePath(path, viewer);
157                     if (obj != null && obj.getSegmentCount()-1 == path.segmentCount()) {
158                         selection.add(obj);
159                     } else {
160                         selectionComplete = false;
161                     }
162                 } catch (DebugException e) {
163                 }
164             }
165             if (selection.size() > 0) {
166                     TreePath[] treePaths = (TreePath[]) selection.toArray(new TreePath[0]);
167                 viewer.setSelection(new TreeSelection(treePaths));
168             }
169             if (selectionComplete) {
170                 fSelection = null;
171             }
172         }
173     }
174     
175     /**
176      * Returns an element in the given viewer that corresponds to the given
177      * path, or <code>null</code> if none.
178      *
179      * @param path encoded element path
180      * @param viewer viewer to search for the element in
181      * @return element represented by the path, or <code>null</code> if none
182      * @throws DebugException if unable to locate a variable
183      */

184     protected abstract TreePath decodePath(IPath path, AsynchronousTreeViewer viewer) throws DebugException;
185
186     /* (non-Javadoc)
187      * @see java.lang.Object#clone()
188      */

189     public Object JavaDoc clone() {
190         AbstractViewerState clone = null;
191         try {
192             clone = (AbstractViewerState) this.getClass().newInstance();
193         } catch (InstantiationException JavaDoc e) {
194             return null;
195         } catch (IllegalAccessException JavaDoc e) {
196             return null;
197         }
198         if (fSavedExpansion != null) {
199             clone.fSavedExpansion = new ArrayList JavaDoc(fSavedExpansion.size());
200             Iterator JavaDoc iterator = fSavedExpansion.iterator();
201             while (iterator.hasNext()) {
202                 IPath path = (IPath) iterator.next();
203                 IPath clonePath = Path.fromPortableString(path.toPortableString());
204                 clone.fSavedExpansion.add(clonePath);
205             }
206         }
207         if (fSelection != null) {
208             clone.fSelection = new IPath[fSelection.length];
209             for (int i = 0; i < fSelection.length; i++) {
210                 clone.fSelection[i] = Path.fromPortableString(fSelection[i].toPortableString());
211             }
212         }
213         return clone;
214     }
215 }
Popular Tags