1 11 package org.eclipse.debug.internal.ui.views.memory; 12 13 import java.math.BigInteger ; 14 import java.util.Enumeration ; 15 import java.util.Hashtable ; 16 17 import org.eclipse.core.runtime.SafeRunner; 18 import org.eclipse.debug.core.IMemoryBlockListener; 19 import org.eclipse.debug.core.model.IMemoryBlock; 20 import org.eclipse.debug.ui.memory.IMemoryRendering; 21 import org.eclipse.debug.ui.memory.IMemoryRenderingSynchronizationService; 22 import org.eclipse.jface.util.IPropertyChangeListener; 23 import org.eclipse.jface.util.PropertyChangeEvent; 24 import org.eclipse.swt.widgets.Display; 25 26 30 public class MemoryViewSynchronizationService implements 31 IMemoryRenderingSynchronizationService, IMemoryBlockListener, IPropertyChangeListener { 32 33 private static final int ENABLED = 0; 34 private static final int ENABLING = 1; 35 private static final int DISABLED = 2; 36 37 private Hashtable fSynchronizeInfo; 38 private int fEnableState = ENABLED; 39 private Hashtable fPropertyListeners; 40 41 private IMemoryRendering fLastChangedRendering; 42 private IMemoryRendering fSyncServiceProvider; 43 44 private static final boolean DEBUG_SYNC_SERVICE = false; 45 46 public MemoryViewSynchronizationService() 47 { 48 fSynchronizeInfo = new Hashtable (); 49 fPropertyListeners = new Hashtable (); 50 MemoryViewUtil.getMemoryBlockManager().addListener(this); 51 } 52 53 57 class PropertyListener 58 { 59 IPropertyChangeListener fListener; 60 String [] fFilters; 61 62 public PropertyListener(IPropertyChangeListener listener, String [] properties) 63 { 64 fListener = listener; 65 66 if(properties != null) 67 { 68 fFilters = properties; 69 } 70 } 71 72 78 public boolean isValidProperty(String property){ 79 if (fFilters == null) 80 return true; 81 for (int i=0; i<fFilters.length; i++) 82 { 83 if (fFilters[i].equals(property)) 84 { 85 return true; 86 } 87 } 88 return false; 89 } 90 91 96 public void setPropertyFilters(String [] filters){ 97 fFilters = filters; 98 } 99 100 103 public IPropertyChangeListener getListener() { 104 return fListener; 105 } 106 } 107 108 111 public Object getSynchronizedProperty(IMemoryBlock memoryBlock, String propertyId) 112 { 113 SynchronizeInfo info = (SynchronizeInfo)fSynchronizeInfo.get(memoryBlock); 114 115 if (info != null) 116 { 117 Object value = info.getProperty(propertyId); 118 return value; 119 } 120 121 return null; 122 } 123 124 127 public void memoryBlocksAdded(IMemoryBlock[] memoryBlocks) { 128 132 133 } 134 135 138 public void memoryBlocksRemoved(IMemoryBlock[] memoryBlocks) { 139 140 if (fSynchronizeInfo == null) 142 return; 143 144 for (int i=0; i<memoryBlocks.length; i++) 145 { 146 IMemoryBlock memory = memoryBlocks[i]; 147 148 if (fLastChangedRendering != null && fLastChangedRendering.getMemoryBlock() == memory) 149 fLastChangedRendering = null; 150 151 if (fSyncServiceProvider != null && fSyncServiceProvider.getMemoryBlock() == memory) 152 fSyncServiceProvider = null; 153 154 SynchronizeInfo info = (SynchronizeInfo)fSynchronizeInfo.get(memory); 157 158 if (info != null) 159 { 160 info.delete(); 161 fSynchronizeInfo.remove(memory); 162 } 163 } 164 } 165 166 169 public void shutdown() 170 { 171 if (fSynchronizeInfo != null) 172 { 173 Enumeration enumeration = fSynchronizeInfo.elements(); 174 175 while (enumeration.hasMoreElements()){ 177 SynchronizeInfo info = (SynchronizeInfo)enumeration.nextElement(); 178 info.delete(); 179 } 180 181 fSynchronizeInfo.clear(); 182 fSynchronizeInfo = null; 183 } 184 MemoryViewUtil.getMemoryBlockManager().removeListener(this); 185 } 186 187 188 191 public void addPropertyChangeListener(IPropertyChangeListener listener, String [] properties) { 192 193 PropertyListener propertylistener = new PropertyListener(listener, properties); 194 195 if (!fPropertyListeners.contains(propertylistener)) 196 { 197 fPropertyListeners.put(listener, propertylistener); 198 } 199 200 } 201 202 205 public void removePropertyChangeListener(IPropertyChangeListener listener) { 206 if (fPropertyListeners.containsKey(listener)) 207 { 208 fPropertyListeners.remove(listener); 209 } 210 } 211 212 216 public void firePropertyChanged(final PropertyChangeEvent evt) 217 { 218 if (fEnableState == DISABLED) 221 return; 222 223 Display.getDefault().syncExec(new Runnable () 227 { 228 public void run() 229 { 230 if (fSynchronizeInfo == null) 231 return; 232 233 IMemoryRendering rendering = (IMemoryRendering)evt.getSource(); 234 String propertyId = evt.getProperty(); 235 236 SynchronizeInfo info = (SynchronizeInfo)fSynchronizeInfo.get(rendering.getMemoryBlock()); 237 if (info != null) 238 { 239 Object value = info.getProperty(propertyId); 240 if (value != null) 241 { 242 Enumeration enumeration = fPropertyListeners.elements(); 243 244 while(enumeration.hasMoreElements()) 245 { 246 PropertyListener listener = (PropertyListener)enumeration.nextElement(); 247 248 IPropertyChangeListener origListener = listener.getListener(); 249 250 if (listener.isValidProperty(propertyId)){ 252 PropertyChangeNotifier notifier = new PropertyChangeNotifier(origListener, evt); 253 SafeRunner.run(notifier); 254 } 255 } 256 } 257 } 258 } 259 }); 260 } 261 262 265 public Object getProperty(IMemoryBlock block, String property) { 266 267 if (!isEnabled()) 273 return null; 274 275 SynchronizeInfo info = (SynchronizeInfo)fSynchronizeInfo.get(block); 276 277 if (info != null) 278 return info.getProperty(property); 279 280 return null; 281 } 282 283 284 287 public void propertyChange(PropertyChangeEvent event) { 288 if (event == null || !(event.getSource() instanceof IMemoryRendering)) 289 { 290 return; 291 } 292 293 if (fEnableState == ENABLING) 299 return; 300 301 IMemoryRendering rendering = ((IMemoryRendering)event.getSource()); 302 IMemoryBlock memoryBlock = rendering.getMemoryBlock(); 303 String propertyId = event.getProperty(); 304 Object value = event.getNewValue(); 305 306 if (DEBUG_SYNC_SERVICE) 307 { 308 System.out.println("SYNC SERVICE RECEIVED CHANGED EVENT:"); System.out.println("Source: " + rendering); System.out.println("Property: " + propertyId); System.out.println("Value: " + value); 313 if (value instanceof BigInteger ) 314 { 315 System.out.println("Value in hex: " + ((BigInteger )value).toString(16)); } 317 } 318 319 if (memoryBlock == null) 320 return; 321 322 if (propertyId == null) 323 return; 324 325 SynchronizeInfo info = (SynchronizeInfo)fSynchronizeInfo.get(memoryBlock); 327 328 if (info == null) 330 { 331 info = new SynchronizeInfo(memoryBlock); 332 fSynchronizeInfo.put(memoryBlock, info); 333 } 334 335 Object oldValue = info.getProperty(propertyId); 337 338 if (oldValue == null) 339 { 340 info.setProperty(propertyId, value); 343 fLastChangedRendering = rendering; 344 firePropertyChanged(event); 345 return; 346 } 347 else if (!oldValue.equals(value)) 348 { 349 info.setProperty(propertyId, value); 352 fLastChangedRendering = rendering; 353 firePropertyChanged(event); 354 } 355 } 356 357 public void setEnabled(boolean enabled) 358 { 359 if (enabled && fEnableState == ENABLED) 360 return; 361 362 if (!enabled && fEnableState == DISABLED) 363 return; 364 365 try 366 { 367 if (enabled) 368 { 369 fEnableState = ENABLING; 370 if (fLastChangedRendering != null) 372 { 373 IMemoryBlock memBlock = fLastChangedRendering.getMemoryBlock(); 374 SynchronizeInfo info = (SynchronizeInfo)fSynchronizeInfo.get(memBlock); 375 String [] ids = info.getPropertyIds(); 376 377 382 for (int i=0; i<ids.length; i++) 383 { 384 PropertyChangeEvent evt = new PropertyChangeEvent(fLastChangedRendering, ids[i], null, info.getProperty(ids[i])); 385 firePropertyChanged(evt); 386 } 387 } 388 } 389 } 390 finally 391 { 392 if (enabled) 393 fEnableState = ENABLED; 394 else 395 fEnableState = DISABLED; 396 } 397 } 398 399 public boolean isEnabled() 400 { 401 return fEnableState == ENABLED; 402 } 403 404 405 408 public void setSynchronizationProvider(IMemoryRendering rendering) { 409 410 if (DEBUG_SYNC_SERVICE) 411 System.out.println("SYNCHRONIZATION PROVIDER: " + rendering); 413 if (fSyncServiceProvider != null) 414 fSyncServiceProvider.removePropertyChangeListener(this); 415 416 if (rendering != null) 417 rendering.addPropertyChangeListener(this); 418 419 fSyncServiceProvider = rendering; 420 } 421 422 423 426 public IMemoryRendering getSynchronizationProvider() { 427 return fSyncServiceProvider; 428 } 429 } 430 | Popular Tags |