KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > variables > ViewerState


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation 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  * IBM Corporation - initial API and implementation
10  * QNX Software Systems - Mikhail Khodjaiants - Registers View (Bug 53640)
11  *******************************************************************************/

12 package org.eclipse.debug.internal.ui.views.variables;
13
14 import java.util.ArrayList 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.debug.internal.ui.views.AbstractViewerState;
22 import org.eclipse.jface.viewers.TreePath;
23 import org.eclipse.swt.widgets.Tree;
24 import org.eclipse.swt.widgets.TreeItem;
25
26 /**
27  * Memento of the expanded and selected items in a variables viewer.
28  *
29  * @since 2.1
30  */

31 public class ViewerState extends AbstractViewerState {
32
33     public ViewerState() {
34     }
35     
36     /**
37      * Constructs a memento for the given viewer.
38      */

39     public ViewerState(AsynchronousTreeViewer viewer) {
40         super(viewer);
41     }
42
43     /**
44      * @see org.eclipse.debug.internal.ui.views.AbstractViewerState#encodeElement(org.eclipse.swt.widgets.TreeItem)
45      */

46     protected IPath encodeElement(TreeItem item) throws DebugException {
47         StringBuffer JavaDoc path = new StringBuffer JavaDoc();
48         TreeItem parent = item.getParentItem();
49         while (parent != null) {
50             int i = parent.indexOf(item);
51             path.insert(0, i);
52             path.insert(0, '/');
53             item = parent;
54             parent = item.getParentItem();
55         }
56         Tree tree = item.getParent();
57         int i = tree.indexOf(item);
58         path.insert(0, i);
59         return new Path(path.toString());
60     }
61
62     /**
63      * @see org.eclipse.debug.internal.ui.views.AbstractViewerState#decodePath(org.eclipse.core.runtime.IPath,
64      * org.eclipse.jface.viewers.TreeViewer)
65      */

66     protected TreePath decodePath(IPath path, AsynchronousTreeViewer viewer) throws DebugException {
67         String JavaDoc[] indicies = path.segments();
68         if (indicies.length == 0) {
69             return null;
70         }
71         Tree tree = viewer.getTree();
72         int index = -1;
73         try {
74             index = Integer.parseInt(indicies[0]);
75         } catch (NumberFormatException JavaDoc e) {
76             return null;
77         }
78         TreeItem item = null;
79         if (index < tree.getItemCount()) {
80             item = tree.getItem(index);
81         } else {
82             return null;
83         }
84         
85         List JavaDoc elements = new ArrayList JavaDoc();
86         elements.add(viewer.getInput());
87         Object JavaDoc element = item.getData();
88         if (element != null) {
89             elements.add(element);
90         } else {
91             return null;
92         }
93         
94         for (int i = 1; i < indicies.length; i++) {
95             try {
96                 index = Integer.parseInt(indicies[i]);
97             } catch (NumberFormatException JavaDoc e) {
98                 return null;
99             }
100             if (index < item.getItemCount()) {
101                 item = item.getItem(index);
102             } else {
103                 break; // return partial path
104
}
105             element = item.getData();
106             if (element != null) {
107                 elements.add(element);
108             } else {
109                 break; // return partial path
110
}
111         }
112         
113         return new TreePath(elements.toArray());
114     }
115
116
117 }
118
Popular Tags