Debian Codegraph @

plugins/org.eclipse.emf.edit.ui/src/org/eclipse/emf/edit/ui/provider/NotifyChangedToViewerRefresh.java

raw
1 /**
2 * Copyright (c) 2002-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 v2.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v20.html
7 *
8 * Contributors:
9 * IBM - Initial API and implementation
10 */
11 package org.eclipse.emf.edit.ui.provider;
12
13
14 import java.util.Collection;
15
16 import org.eclipse.jface.viewers.AbstractTreeViewer;
17 import org.eclipse.jface.viewers.ListViewer;
18 import org.eclipse.jface.viewers.StructuredViewer;
19 import org.eclipse.jface.viewers.TableViewer;
20 import org.eclipse.jface.viewers.TreeViewer;
21 import org.eclipse.jface.viewers.Viewer;
22 import org.eclipse.swt.widgets.Display;
23
24 import org.eclipse.emf.common.notify.Notification;
25 import org.eclipse.emf.ecore.ENamedElement;
26 import org.eclipse.emf.ecore.EReference;
27
28
29 /**
30 * This class calls optimized refresh APIs for all the standard Viewer subclasses.
31 */
32 public class NotifyChangedToViewerRefresh
33 {
34 public static void handleNotifyChanged
35 (final Viewer viewer,
36 final Object object,
37 final int eventType,
38 final Object feature,
39 final Object oldValue,
40 final Object newValue,
41 final int index)
42 {
43 if (viewer.getControl() == null || viewer.getControl().isDisposed()) return;
44 Display d = viewer.getControl().getDisplay();
45 if (d != Display.getCurrent())
46 {
47 d.asyncExec
48 (new Runnable()
49 {
50 public void run()
51 {
52 if (viewer.getControl() != null && !viewer.getControl().isDisposed())
53 {
54 new NotifyChangedToViewerRefresh().refresh(viewer, object, eventType, feature, oldValue, newValue, index);
55 }
56 }
57 });
58 }
59 else
60 {
61 new NotifyChangedToViewerRefresh().refresh(viewer, object, eventType, feature, oldValue, newValue, index);
62 }
63 }
64
65 public void refresh
66 (Viewer viewer,
67 Object object,
68 int eventType,
69 Object feature,
70 Object oldValue,
71 Object newValue,
72 int index)
73 {
74 if (viewer instanceof TreeViewer)
75 {
76 refreshTreeViewer((TreeViewer)viewer, object, eventType, feature, oldValue, newValue, index);
77 }
78 else if (viewer instanceof TableViewer)
79 {
80 refreshTableViewer((TableViewer)viewer, object, eventType, feature, oldValue, newValue, index);
81 }
82 else if (viewer instanceof ListViewer)
83 {
84 refreshListViewer((ListViewer)viewer, object, eventType, feature, oldValue, newValue, index);
85 }
86 else
87 {
88 refreshViewer(viewer, object, eventType, feature, oldValue, newValue, index);
89 }
90 }
91
92 public void refreshTreeViewer
93 (TreeViewer viewer,
94 Object object,
95 int eventType,
96 Object feature,
97 Object oldValue,
98 Object newValue,
99 int index)
100 {
101 switch (eventType)
102 {
103 case Notification.ADD:
104 case Notification.ADD_MANY:
105 case Notification.REMOVE:
106 case Notification.REMOVE_MANY:
107 case Notification.MOVE:
108 case Notification.UNSET:
109 case Notification.SET:
110 default:
111 {
112 refreshAbstractTreeViewer(viewer, object, eventType, feature, oldValue, newValue, index);
113 break;
114 }
115 }
116 }
117
118 public void refreshListViewer
119 (ListViewer viewer,
120 Object object,
121 int eventType,
122 Object feature,
123 Object oldValue,
124 Object newValue,
125 int index)
126 {
127 switch (eventType)
128 {
129 case Notification.ADD:
130 {
131 viewer.add(newValue);
132 break;
133 }
134 case Notification.ADD_MANY:
135 {
136 viewer.add(((Collection<?>)newValue).toArray());
137 break;
138 }
139 case Notification.REMOVE:
140 {
141 viewer.remove(oldValue);
142 break;
143 }
144 case Notification.REMOVE_MANY:
145 {
146 viewer.remove(((Collection<?>)oldValue).toArray());
147 break;
148 }
149 case Notification.MOVE:
150 {
151 viewer.refresh(); // ???
152 }
153 case Notification.UNSET:
154 case Notification.SET:
155 default:
156 {
157 refreshStructuredViewer(viewer, object, eventType, feature, oldValue, newValue, index);
158 break;
159 }
160 }
161 }
162
163 public void refreshTableViewer
164 (TableViewer viewer,
165 Object object,
166 int eventType,
167 Object feature,
168 Object oldValue,
169 Object newValue,
170 int index)
171 {
172 switch (eventType)
173 {
174 case Notification.ADD:
175 {
176 viewer.insert(newValue, index);
177 break;
178 }
179 case Notification.ADD_MANY:
180 {
181 if (index == -1)
182 {
183 viewer.add(((Collection<?>)newValue).toArray());
184 }
185 else
186 {
187 for (Object value : (Collection<?>)newValue)
188 {
189 viewer.insert(value, index++);
190 }
191 }
192 break;
193 }
194 case Notification.REMOVE:
195 {
196 viewer.remove(oldValue);
197 break;
198 }
199 case Notification.REMOVE_MANY:
200 {
201 viewer.remove(((Collection<?>)oldValue).toArray());
202 break;
203 }
204 case Notification.MOVE:
205 case Notification.UNSET:
206 case Notification.SET:
207 default:
208 {
209 refreshStructuredViewer(viewer, object, eventType, feature, oldValue, newValue, index);
210 break;
211 }
212 }
213 }
214
215 public void refreshAbstractTreeViewer
216 (AbstractTreeViewer viewer,
217 Object object,
218 int eventType,
219 Object feature,
220 Object oldValue,
221 Object newValue,
222 int index)
223 {
224 switch (eventType)
225 {
226 case Notification.ADD:
227 {
228 if (newValue == null)
229 {
230 viewer.refresh(object);
231 }
232 else
233 {
234 viewer.add(object, newValue);
235 }
236 break;
237 }
238 case Notification.ADD_MANY:
239 {
240 viewer.add(object, ((Collection<?>)newValue).toArray());
241 break;
242 }
243 case Notification.REMOVE:
244 {
245 if (oldValue == null)
246 {
247 viewer.refresh(object);
248 }
249 else
250 {
251 viewer.remove(oldValue);
252 }
253 break;
254 }
255 case Notification.REMOVE_MANY:
256 {
257 viewer.remove(((Collection<?>)oldValue).toArray());
258 break;
259 }
260 case Notification.MOVE:
261 case Notification.UNSET:
262 case Notification.SET:
263 default:
264 {
265 refreshStructuredViewer(viewer, object, eventType, feature, oldValue, newValue, index);
266 break;
267 }
268 }
269 }
270
271 public void refreshStructuredViewer
272 (StructuredViewer viewer,
273 Object object,
274 int eventType,
275 Object feature,
276 Object oldValue,
277 Object newValue,
278 int index)
279 {
280 switch (eventType)
281 {
282 case Notification.ADD:
283 case Notification.ADD_MANY:
284 case Notification.REMOVE:
285 case Notification.REMOVE_MANY:
286 case Notification.MOVE:
287 {
288 viewer.refresh(object);
289 break;
290 }
291 case Notification.UNSET:
292 case Notification.SET:
293 {
294 if (feature instanceof EReference)
295 {
296 viewer.refresh(object);
297 }
298 else
299 {
300 viewer.update(object, feature instanceof ENamedElement ? new String[] { ((ENamedElement)feature).getName() } : null);
301 }
302 break;
303 }
304 // case Notification.TOUCH:
305 default:
306 {
307 refreshViewer(viewer, object, eventType, feature, oldValue, newValue, index);
308 break;
309 }
310 }
311 }
312
313 public void refreshViewer
314 (Viewer viewer,
315 Object object,
316 int eventType,
317 Object feature,
318 Object oldValue,
319 Object newValue,
320 int index)
321 {
322 switch (eventType)
323 {
324 case Notification.RESOLVE:
325 {
326 // We ignore non-changes for now.
327 //
328 break;
329 }
330 case Notification.ADD:
331 case Notification.ADD_MANY:
332 case Notification.REMOVE:
333 case Notification.REMOVE_MANY:
334 case Notification.MOVE:
335 case Notification.UNSET:
336 case Notification.SET:
337 default:
338 {
339 viewer.refresh();
340 break;
341 }
342 }
343 }
344 }