1 17 package org.eclipse.emf.edit.ui.provider; 18 19 20 import java.util.Collection ; 21 import java.util.Iterator ; 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 40 public class NotifyChangedToViewerRefresh 41 { 42 public static void handleNotifyChanged 43 (final Viewer viewer, 44 final Object object, 45 final int eventType, 46 final Object feature, 47 final Object oldValue, 48 final Object 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 () 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 object, 73 int eventType, 74 Object feature, 75 Object oldValue, 76 Object 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 object, 104 int eventType, 105 Object feature, 106 Object oldValue, 107 Object 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 object, 130 int eventType, 131 Object feature, 132 Object oldValue, 133 Object 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 object, 156 int eventType, 157 Object feature, 158 Object oldValue, 159 Object 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 )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 )oldValue).toArray()); 182 break; 183 } 184 case Notification.MOVE: 185 { 186 viewer.refresh(); } 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 object, 201 int eventType, 202 Object feature, 203 Object oldValue, 204 Object 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 )newValue).toArray()); 219 } 220 else 221 { 222 for (Iterator i = ((Collection )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 )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 object, 253 int eventType, 254 Object feature, 255 Object oldValue, 256 Object 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 )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 )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 object, 309 int eventType, 310 Object feature, 311 Object oldValue, 312 Object 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 [] { ((ENamedElement)feature).getName() } : null); 336 } 337 break; 338 } 339 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 object, 351 int eventType, 352 Object feature, 353 Object oldValue, 354 Object newValue, 355 int index) 356 { 357 switch (eventType) 358 { 359 case Notification.RESOLVE: 360 { 361 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 |