KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > viewers > TreeUpdatePolicy


1 /*******************************************************************************
2  * Copyright (c) 2005, 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  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.viewers;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.debug.internal.ui.viewers.provisional.IModelChangedListener;
16 import org.eclipse.debug.internal.ui.viewers.provisional.IModelDelta;
17 import org.eclipse.jface.viewers.TreePath;
18 import org.eclipse.jface.viewers.TreeSelection;
19
20 /**
21  * Default update policy updates a viewer based on model deltas.
22  *
23  * @since 3.2
24  */

25 public class TreeUpdatePolicy extends AbstractUpdatePolicy implements IModelChangedListener {
26     
27     // cache of latest tree path for a node
28
private TreePath fTreePath;
29     private IModelDelta fNode;
30
31     public void modelChanged(IModelDelta delta) {
32         updateNodes(new IModelDelta[] { delta });
33         fTreePath = null;
34         fNode = null;
35     }
36
37     protected void updateNodes(IModelDelta[] nodes) {
38         AsynchronousTreeViewer viewer = (AsynchronousTreeViewer) getViewer();
39         if (viewer == null) {
40             return;
41         }
42
43         for (int i = 0; i < nodes.length; i++) {
44             IModelDelta node = nodes[i];
45             int flags = node.getFlags();
46
47             if ((flags & IModelDelta.ADDED) != 0) {
48                 handleAdd(viewer, node);
49             }
50             if ((flags & IModelDelta.REMOVED) != 0) {
51                 handleRemove(viewer, node);
52             }
53             if ((flags & IModelDelta.CONTENT) != 0) {
54                 handleContent(viewer, node);
55             }
56             if ((flags & IModelDelta.EXPAND) != 0) {
57                 handleExpand(viewer, node);
58             }
59             if ((flags & IModelDelta.SELECT) != 0) {
60                 handleSelect(viewer, node);
61             }
62             if ((flags & IModelDelta.STATE) != 0) {
63                 handleState(viewer, node);
64             }
65             if ((flags & IModelDelta.INSERTED) != 0) {
66                 // TODO
67
}
68             if ((flags & IModelDelta.REPLACED) != 0) {
69                 // TODO
70
}
71
72             updateNodes(node.getNodes());
73         }
74     }
75
76     protected void handleState(AsynchronousTreeViewer viewer, IModelDelta delta) {
77         viewer.update(delta.getElement());
78     }
79
80     protected void handleSelect(AsynchronousTreeViewer viewer, IModelDelta delta) {
81         viewer.setSelection(new TreeSelection(getTreePath(delta)));
82     }
83
84     protected void handleExpand(AsynchronousTreeViewer viewer, IModelDelta delta) {
85         viewer.expand(new TreeSelection(getTreePath(delta)));
86     }
87
88     protected void handleContent(AsynchronousTreeViewer viewer, IModelDelta delta) {
89         viewer.refresh(delta.getElement());
90     }
91
92     protected void handleRemove(AsynchronousTreeViewer viewer, IModelDelta delta) {
93         viewer.remove(getTreePath(delta));
94     }
95
96     protected void handleAdd(AsynchronousTreeViewer viewer, IModelDelta delta) {
97         viewer.add(getTreePath(delta));
98     }
99
100     protected TreePath getTreePath(IModelDelta node) {
101         if (node != fNode) {
102             ArrayList JavaDoc list = new ArrayList JavaDoc();
103             list.add(0, node.getElement());
104             while (node.getParent() != null) {
105                 node = node.getParent();
106                 list.add(0, node.getElement());
107             }
108
109             fTreePath = new TreePath(list.toArray());
110             fNode = node;
111         }
112         return fTreePath;
113     }
114
115 }
116
Popular Tags