KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > edit > ui > provider > NotifyChangedToViewerRefresh


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: NotifyChangedToViewerRefresh.java,v 1.2 2005/06/08 06:20:52 nickb Exp $
16  */

17 package org.eclipse.emf.edit.ui.provider;
18
19
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import org.eclipse.jface.viewers.AbstractTreeViewer;
24 import org.eclipse.jface.viewers.ListViewer;
25 import org.eclipse.jface.viewers.StructuredViewer;
26 import org.eclipse.jface.viewers.TableTreeViewer;
27 import org.eclipse.jface.viewers.TableViewer;
28 import org.eclipse.jface.viewers.TreeViewer;
29 import org.eclipse.jface.viewers.Viewer;
30 import org.eclipse.swt.widgets.Display;
31
32 import org.eclipse.emf.common.notify.Notification;
33 import org.eclipse.emf.ecore.ENamedElement;
34 import org.eclipse.emf.ecore.EReference;
35
36
37 /**
38  * This class calls optimized refresh apis for all the standard Viewer subclasses.
39  */

40 public class NotifyChangedToViewerRefresh
41 {
42   public static void handleNotifyChanged
43     (final Viewer viewer,
44      final Object JavaDoc object,
45      final int eventType,
46      final Object JavaDoc feature,
47      final Object JavaDoc oldValue,
48      final Object JavaDoc newValue,
49      final int index)
50   {
51     if (viewer.getControl() == null || viewer.getControl().isDisposed()) return;
52     Display d = viewer.getControl().getDisplay();
53     if (d != Display.getCurrent())
54     {
55       d.asyncExec
56         (new Runnable JavaDoc()
57          {
58            public void run()
59            {
60              new NotifyChangedToViewerRefresh().refresh(viewer, object, eventType, feature, oldValue, newValue, index);
61            }
62          });
63     }
64     else
65     {
66       new NotifyChangedToViewerRefresh().refresh(viewer, object, eventType, feature, oldValue, newValue, index);
67     }
68   }
69
70   public void refresh
71     (Viewer viewer,
72      Object JavaDoc object,
73      int eventType,
74      Object JavaDoc feature,
75      Object JavaDoc oldValue,
76      Object JavaDoc newValue,
77      int index)
78   {
79     if (viewer instanceof TreeViewer)
80     {
81       refreshTreeViewer((TreeViewer)viewer, object, eventType, feature, oldValue, newValue, index);
82     }
83     else if (viewer instanceof TableTreeViewer)
84     {
85       refreshTableTreeViewer((TableTreeViewer)viewer, object, eventType, feature, oldValue, newValue, index);
86     }
87     else if (viewer instanceof TableViewer)
88     {
89       refreshTableViewer((TableViewer)viewer, object, eventType, feature, oldValue, newValue, index);
90     }
91     else if (viewer instanceof ListViewer)
92     {
93       refreshListViewer((ListViewer)viewer, object, eventType, feature, oldValue, newValue, index);
94     }
95     else
96     {
97       refreshViewer(viewer, object, eventType, feature, oldValue, newValue, index);
98     }
99   }
100
101   public void refreshTreeViewer
102     (TreeViewer viewer,
103      Object JavaDoc object,
104      int eventType,
105      Object JavaDoc feature,
106      Object JavaDoc oldValue,
107      Object JavaDoc newValue,
108      int index)
109   {
110     switch (eventType)
111     {
112       case Notification.ADD:
113       case Notification.ADD_MANY:
114       case Notification.REMOVE:
115       case Notification.REMOVE_MANY:
116       case Notification.MOVE:
117       case Notification.UNSET:
118       case Notification.SET:
119       default:
120       {
121         refreshAbstractTreeViewer(viewer, object, eventType, feature, oldValue, newValue, index);
122         break;
123       }
124     }
125   }
126
127   public void refreshTableTreeViewer
128     (TableTreeViewer viewer,
129      Object JavaDoc object,
130      int eventType,
131      Object JavaDoc feature,
132      Object JavaDoc oldValue,
133      Object JavaDoc newValue,
134      int index)
135   {
136     switch (eventType)
137     {
138       case Notification.ADD:
139       case Notification.ADD_MANY:
140       case Notification.REMOVE:
141       case Notification.REMOVE_MANY:
142       case Notification.MOVE:
143       case Notification.UNSET:
144       case Notification.SET:
145       default:
146       {
147         refreshAbstractTreeViewer(viewer, object, eventType, feature, oldValue, newValue, index);
148         break;
149       }
150     }
151   }
152
153   public void refreshListViewer
154     (ListViewer viewer,
155      Object JavaDoc object,
156      int eventType,
157      Object JavaDoc feature,
158      Object JavaDoc oldValue,
159      Object JavaDoc newValue,
160      int index)
161   {
162     switch (eventType)
163     {
164       case Notification.ADD:
165       {
166         viewer.add(newValue);
167         break;
168       }
169       case Notification.ADD_MANY:
170       {
171         viewer.add(((Collection JavaDoc)newValue).toArray());
172         break;
173       }
174       case Notification.REMOVE:
175       {
176         viewer.remove(oldValue);
177         break;
178       }
179       case Notification.REMOVE_MANY:
180       {
181         viewer.remove(((Collection JavaDoc)oldValue).toArray());
182         break;
183       }
184       case Notification.MOVE:
185       {
186         viewer.refresh(); // ???
187
}
188       case Notification.UNSET:
189       case Notification.SET:
190       default:
191       {
192         refreshStructuredViewer(viewer, object, eventType, feature, oldValue, newValue, index);
193         break;
194       }
195     }
196   }
197
198   public void refreshTableViewer
199     (TableViewer viewer,
200      Object JavaDoc object,
201      int eventType,
202      Object JavaDoc feature,
203      Object JavaDoc oldValue,
204      Object JavaDoc newValue,
205      int index)
206   {
207     switch (eventType)
208     {
209       case Notification.ADD:
210       {
211         viewer.insert(newValue, index);
212         break;
213       }
214       case Notification.ADD_MANY:
215       {
216         if (index == -1)
217         {
218           viewer.add(((Collection JavaDoc)newValue).toArray());
219         }
220         else
221         {
222           for (Iterator JavaDoc i = ((Collection JavaDoc)newValue).iterator(); i.hasNext(); )
223           {
224             viewer.insert(i.next(), index++);
225           }
226         }
227         break;
228       }
229       case Notification.REMOVE:
230       {
231         viewer.remove(oldValue);
232         break;
233       }
234       case Notification.REMOVE_MANY:
235       {
236         viewer.remove(((Collection JavaDoc)oldValue).toArray());
237         break;
238       }
239       case Notification.MOVE:
240       case Notification.UNSET:
241       case Notification.SET:
242       default:
243       {
244         refreshStructuredViewer(viewer, object, eventType, feature, oldValue, newValue, index);
245         break;
246       }
247     }
248   }
249
250   public void refreshAbstractTreeViewer
251     (AbstractTreeViewer viewer,
252      Object JavaDoc object,
253      int eventType,
254      Object JavaDoc feature,
255      Object JavaDoc oldValue,
256      Object JavaDoc newValue,
257      int index)
258   {
259     switch (eventType)
260     {
261       case Notification.ADD:
262       {
263         if (newValue == null)
264         {
265           viewer.refresh(object);
266         }
267         else
268         {
269           viewer.add(object, newValue);
270         }
271         break;
272       }
273       case Notification.ADD_MANY:
274       {
275         viewer.add(object, ((Collection JavaDoc)newValue).toArray());
276         break;
277       }
278       case Notification.REMOVE:
279       {
280         if (oldValue == null)
281         {
282           viewer.refresh(object);
283         }
284         else
285         {
286           viewer.remove(oldValue);
287         }
288         break;
289       }
290       case Notification.REMOVE_MANY:
291       {
292         viewer.remove(((Collection JavaDoc)oldValue).toArray());
293         break;
294       }
295       case Notification.MOVE:
296       case Notification.UNSET:
297       case Notification.SET:
298       default:
299       {
300         refreshStructuredViewer(viewer, object, eventType, feature, oldValue, newValue, index);
301         break;
302       }
303     }
304   }
305
306   public void refreshStructuredViewer
307     (StructuredViewer viewer,
308      Object JavaDoc object,
309      int eventType,
310      Object JavaDoc feature,
311      Object JavaDoc oldValue,
312      Object JavaDoc newValue,
313      int index)
314   {
315     switch (eventType)
316     {
317       case Notification.ADD:
318       case Notification.ADD_MANY:
319       case Notification.REMOVE:
320       case Notification.REMOVE_MANY:
321       case Notification.MOVE:
322       {
323         viewer.refresh(object);
324         break;
325       }
326       case Notification.UNSET:
327       case Notification.SET:
328       {
329         if (feature instanceof EReference)
330         {
331           viewer.refresh(object);
332         }
333         else
334         {
335           viewer.update(object, feature instanceof ENamedElement ? new String JavaDoc[] { ((ENamedElement)feature).getName() } : null);
336         }
337         break;
338       }
339 // case Notification.TOUCH:
340
default:
341       {
342         refreshViewer(viewer, object, eventType, feature, oldValue, newValue, index);
343         break;
344       }
345     }
346   }
347
348   public void refreshViewer
349     (Viewer viewer,
350      Object JavaDoc object,
351      int eventType,
352      Object JavaDoc feature,
353      Object JavaDoc oldValue,
354      Object JavaDoc newValue,
355      int index)
356   {
357     switch (eventType)
358     {
359       case Notification.RESOLVE:
360       {
361         // We ignore non-changes for now.
362
//
363
break;
364       }
365       case Notification.ADD:
366       case Notification.ADD_MANY:
367       case Notification.REMOVE:
368       case Notification.REMOVE_MANY:
369       case Notification.MOVE:
370       case Notification.UNSET:
371       case Notification.SET:
372       default:
373       {
374         viewer.refresh();
375         break;
376       }
377     }
378   }
379 }
380
Popular Tags