Skip to content

Package: NotifyChangedToViewerRefresh$1

NotifyChangedToViewerRefresh$1

nameinstructionbranchcomplexitylinemethod
run()
M: 28 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
{...}
M: 24 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

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