KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17
18 /**
19  * @since 3.2
20  *
21  */

22 public class AsynchronousTableModel extends AsynchronousModel {
23
24     /**
25      * Constructs a new table model.
26      *
27      * @param viewer
28      */

29     public AsynchronousTableModel(AsynchronousViewer viewer) {
30         super(viewer);
31     }
32
33     /* (non-Javadoc)
34      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.viewers.AsynchronousModel#add(org.eclipse.debug.internal.ui.viewers.model.provisional.viewers.ModelNode, java.lang.Object)
35      */

36     protected void add(ModelNode parent, Object JavaDoc element) {}
37     
38     /**
39      * Adds the given elements to the table.
40      *
41      * @param elements
42      */

43     public void add(Object JavaDoc[] elements) {
44         TableAddRequestMonitor update = new TableAddRequestMonitor(getRootNode(), elements, this);
45         requestScheduled(update);
46         update.done();
47     }
48     
49     /**
50      * Notification add request is complete.
51      *
52      * @param elements elements to add
53      */

54     protected void added(Object JavaDoc[] elements) {
55         List JavaDoc kids = null;
56         boolean changed = false;
57         synchronized (this) {
58             ModelNode[] childrenNodes = getRootNode().getChildrenNodes();
59             if (childrenNodes == null) {
60                 kids = new ArrayList JavaDoc(elements.length);
61             } else {
62                 kids = new ArrayList JavaDoc(elements.length + childrenNodes.length);
63                 for (int i = 0; i < childrenNodes.length; i++) {
64                     kids.add(childrenNodes[i].getElement());
65                 }
66             }
67             for (int i = 0; i < elements.length; i++) {
68                 if (!kids.contains(elements[i])) {
69                     kids.add(elements[i]);
70                     changed = true;
71                 }
72             }
73         }
74         if (changed) {
75             setChildren(getRootNode(), kids);
76         }
77     }
78     
79     /**
80      * Inserts the given elements to the table.
81      *
82      * @param elements
83      * @param index
84      */

85     public void insert(Object JavaDoc[] elements, int index) {
86         TableAddRequestMonitor update = new TableInsertRequestMonitor(getRootNode(), elements, index, this);
87         requestScheduled(update);
88         update.done();
89     }
90     
91     /**
92      * Notification insert request is complete.
93      *
94      * @param elements elements to add
95      * @param index index to insert at
96      */

97     protected void inserted(Object JavaDoc[] elements, int index) {
98         List JavaDoc kids = null;
99         boolean changed = false;
100         synchronized (this) {
101             ModelNode[] childrenNodes = getRootNode().getChildrenNodes();
102             if (childrenNodes == null) {
103                 kids = new ArrayList JavaDoc(elements.length);
104             } else {
105                 kids = new ArrayList JavaDoc(elements.length + childrenNodes.length);
106                 for (int i = 0; i < childrenNodes.length; i++) {
107                     kids.add(childrenNodes[i].getElement());
108                 }
109             }
110             for (int i = 0; i < elements.length; i++) {
111                 if (!kids.contains(elements[i])) {
112                     kids.add(index, elements[i]);
113                     index++;
114                     changed = true;
115                 }
116             }
117         }
118         if (changed) {
119             setChildren(getRootNode(), kids);
120         }
121     }
122
123     /**
124      * Removes the given elements from the table.
125      *
126      * @param elements
127      */

128     public void remove(Object JavaDoc[] elements) {
129         TableRemoveRequestMonitor update = new TableRemoveRequestMonitor(getRootNode(), elements, this);
130         requestScheduled(update);
131         update.done();
132     }
133     
134     /**
135      * Notification remove request is complete.
136      *
137      * @param elements elements to remove
138      */

139     protected void removed(Object JavaDoc[] elements) {
140         List JavaDoc kids = null;
141         boolean changed = false;
142         synchronized (this) {
143             ModelNode[] childrenNodes = getRootNode().getChildrenNodes();
144             if (childrenNodes != null) {
145                 kids = new ArrayList JavaDoc(childrenNodes.length);
146                 for (int i = 0; i < childrenNodes.length; i++) {
147                     kids.add(childrenNodes[i].getElement());
148                 }
149             }
150             for (int i = 0; i < elements.length; i++) {
151                 if (kids.remove(elements[i])) {
152                     changed = true;
153                 }
154             }
155         }
156         if (changed) {
157             setChildren(getRootNode(), kids);
158         }
159     }
160     
161     /**
162      * Adds the given elements to the table.
163      *
164      * @param elements
165      */

166     public void replace(Object JavaDoc element, Object JavaDoc replacement) {
167         TableReplaceRequestMonitor update = new TableReplaceRequestMonitor(getRootNode(), element, replacement, this);
168         requestScheduled(update);
169         update.done();
170     }
171     
172     /**
173      * Notification add request is complete.
174      *
175      * @param elements elements to add
176      */

177     protected void replaced(Object JavaDoc element, Object JavaDoc replacement) {
178         Object JavaDoc[] filtered = filter(getRootNode().getElement(), new Object JavaDoc[] { replacement });
179         if (filtered.length == 0) {
180             remove(new Object JavaDoc[]{element});
181             return;
182         }
183         List JavaDoc list = new ArrayList JavaDoc();
184         synchronized (this) {
185             ModelNode[] nodes = getNodes(element);
186             for (int i = 0; i < nodes.length; i++) {
187                 ModelNode node = nodes[i];
188                 node.remap(replacement);
189                 list.add(node);
190             }
191         }
192         if (!list.isEmpty()) {
193             Iterator JavaDoc iterator = list.iterator();
194             while (iterator.hasNext()) {
195                 ModelNode node = (ModelNode) iterator.next();
196                 getViewer().nodeChanged(node);
197             }
198         }
199     }
200 }
201
Popular Tags