Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7587c6031d2ec7a7a07a1e0a0234ddaddf38bd9f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*******************************************************************************
 * Copyright (c) 2004, 2018 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Justin Kong (IBM) -  Bug 258890 -  [Memory View] MemoryViewSynchronizationService not implementing addPropertyChangeListener() correctly
 *******************************************************************************/
package org.eclipse.debug.internal.ui.views.memory;

import java.math.BigInteger;
import java.util.Enumeration;
import java.util.Hashtable;

import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.debug.core.IMemoryBlockListener;
import org.eclipse.debug.core.model.IMemoryBlock;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.ui.memory.IMemoryRendering;
import org.eclipse.debug.ui.memory.IMemoryRenderingSynchronizationService;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.widgets.Display;

/**
 * Synchronization service for the memory view.
 *
 * @since 3.1
 */
public class MemoryViewSynchronizationService implements IMemoryRenderingSynchronizationService, IMemoryBlockListener, IPropertyChangeListener {

	private static final int ENABLED = 0;
	private static final int ENABLING = 1;
	private static final int DISABLED = 2;

	private Hashtable<IMemoryBlock, SynchronizeInfo> fSynchronizeInfo;
	private int fEnableState = ENABLED;
	private Hashtable<IPropertyChangeListener, PropertyListener> fPropertyListeners;

	private IMemoryRendering fLastChangedRendering;
	private IMemoryRendering fSyncServiceProvider;

	private static final boolean DEBUG_SYNC_SERVICE = false;

	public MemoryViewSynchronizationService() {
		fSynchronizeInfo = new Hashtable<>();
		fPropertyListeners = new Hashtable<>();
		MemoryViewUtil.getMemoryBlockManager().addListener(this);
	}

	/**
	 * Wrapper for ISynchronizedMemoryBlockView Holds a list of property filters
	 * for the view.
	 */
	class PropertyListener {
		IPropertyChangeListener fListener;
		String[] fFilters;

		public PropertyListener(IPropertyChangeListener listener, String[] properties) {
			fListener = listener;

			if (properties != null) {
				fFilters = properties;
			}
		}

		/**
		 * If the property matches one of the filters, the property is valid and
		 * the view should be notified about its change.
		 *
		 * @param property the property
		 * @return if the property is specified in the filter
		 */
		public boolean isValidProperty(String property) {
			if (fFilters == null) {
				return true;
			}
			for (int i = 0; i < fFilters.length; i++) {
				if (fFilters[i].equals(property)) {
					return true;
				}
			}
			return false;
		}

		/**
		 * Set property filters, indicating what property change events the
		 * listener is interested in.
		 *
		 * @param filters the property filters or <code>null</code>
		 */
		public void setPropertyFilters(String[] filters) {
			fFilters = filters;
		}

		/**
		 * @return Returns the fListener.
		 */
		public IPropertyChangeListener getListener() {
			return fListener;
		}
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.eclipse.debug.ui.IMemoryBlockViewSynchronizer#getSynchronizedProperty
	 * (org.eclipse.debug.ui.ISynchronizedMemoryBlockView, java.lang.String)
	 */
	public Object getSynchronizedProperty(IMemoryBlock memoryBlock, String propertyId) {
		SynchronizeInfo info = fSynchronizeInfo.get(memoryBlock);

		if (info != null) {
			Object value = info.getProperty(propertyId);
			return value;
		}

		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.eclipse.debug.ui.IMemoryBlockListener#MemoryBlockAdded(org.eclipse
	 * .debug.core.model.IMemoryBlock)
	 */
	@Override
	public void memoryBlocksAdded(IMemoryBlock[] memoryBlocks) {
		// do nothing when a memory block is added
		// create a synchronize info object when there is a fView
		// tab registered to be synchronized.

	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.eclipse.debug.ui.IMemoryBlockListener#MemoryBlockRemoved(org.eclipse
	 * .debug.core.model.IMemoryBlock)
	 */
	@Override
	public void memoryBlocksRemoved(IMemoryBlock[] memoryBlocks) {

		// Sync info can be null if the service is already shut down
		if (fSynchronizeInfo == null) {
			return;
		}

		for (int i = 0; i < memoryBlocks.length; i++) {
			IMemoryBlock memory = memoryBlocks[i];

			if (fLastChangedRendering != null && fLastChangedRendering.getMemoryBlock() == memory) {
				fLastChangedRendering = null;
			}

			if (fSyncServiceProvider != null && fSyncServiceProvider.getMemoryBlock() == memory) {
				fSyncServiceProvider = null;
			}

			// delete the info object and remove it from fSynchronizeInfo
			// when the memory block is deleted
			SynchronizeInfo info = fSynchronizeInfo.get(memory);

			if (info != null) {
				info.delete();
				fSynchronizeInfo.remove(memory);
			}
		}
	}

	/**
	 * Clean up when the plug-in is shutdown
	 */
	public void shutdown() {
		if (fSynchronizeInfo != null) {
			Enumeration<SynchronizeInfo> enumeration = fSynchronizeInfo.elements();

			// clean up all synchronize info objects
			while (enumeration.hasMoreElements()) {
				SynchronizeInfo info = enumeration.nextElement();
				info.delete();
			}

			fSynchronizeInfo.clear();
			fSynchronizeInfo = null;
		}
		MemoryViewUtil.getMemoryBlockManager().removeListener(this);
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.debug.ui.memory.IMemoryRenderingSynchronizationService#
	 * addPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener,
	 * java.lang.String[])
	 */
	@Override
	public void addPropertyChangeListener(IPropertyChangeListener listener, String[] properties) {
		fPropertyListeners.put(listener, new PropertyListener(listener, properties));
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.debug.ui.memory.IMemoryRenderingSynchronizationService#
	 * removePropertyChangeListener
	 * (org.eclipse.jface.util.IPropertyChangeListener)
	 */
	@Override
	public void removePropertyChangeListener(IPropertyChangeListener listener) {
		if (fPropertyListeners.containsKey(listener)) {
			fPropertyListeners.remove(listener);
		}
	}

	/**
	 * Fire property change events
	 *
	 * @param evt the event to fire
	 */
	public void firePropertyChanged(final PropertyChangeEvent evt) {
		// do not fire property changed event if the synchronization
		// service is disabled
		if (fEnableState == DISABLED) {
			return;
		}

		// Make sure the synchronizer does not swallow any events
		// Values of the properties are updated in the syncrhonizer immediately.
		// Change events are queued up on the UI Thread.
		Display.getDefault().syncExec(() -> {
			if (fSynchronizeInfo == null) {
				return;
			}

			IMemoryRendering rendering = (IMemoryRendering) evt.getSource();
			String propertyId = evt.getProperty();

			SynchronizeInfo info = fSynchronizeInfo.get(rendering.getMemoryBlock());
			if (info != null) {
				Object value = info.getProperty(propertyId);
				if (value != null) {
					Enumeration<PropertyListener> enumeration = fPropertyListeners.elements();

					while (enumeration.hasMoreElements()) {
						PropertyListener listener = enumeration.nextElement();

						IPropertyChangeListener origListener = listener.getListener();

						// if it's a valid property - valid means that it's
						// listed in the property filters
						if (listener.isValidProperty(propertyId)) {
							PropertyChangeNotifier notifier = new PropertyChangeNotifier(origListener, evt);
							SafeRunner.run(notifier);
						}
					}
				}
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.debug.ui.memory.IMemoryRenderingSynchronizationService#
	 * getProperty(org.eclipse.debug.core.model.IMemoryBlock, java.lang.String)
	 */
	@Override
	public Object getProperty(IMemoryBlock block, String property) {

		// When the synchronization service is disabled
		// return null for all queries to properties
		// This is to ensure that renderings are not synchronized
		// to new synchronization properties when the sync service is
		// disabled.
		if (!isEnabled()) {
			return null;
		}

		SynchronizeInfo info = fSynchronizeInfo.get(block);

		if (info != null) {
			return info.getProperty(property);
		}

		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse
	 * .jface.util.PropertyChangeEvent)
	 */
	@Override
	public void propertyChange(PropertyChangeEvent event) {
		if (event == null || !(event.getSource() instanceof IMemoryRendering)) {
			return;
		}

		// Do not handle any property changed event as the
		// sync service is being enabled.
		// Otherwise, current sync info provider may overwrite
		// sync info unexpectedly. We want to sync with the rendering
		// that is last changed.
		if (fEnableState == ENABLING) {
			return;
		}

		IMemoryRendering rendering = ((IMemoryRendering) event.getSource());
		IMemoryBlock memoryBlock = rendering.getMemoryBlock();
		String propertyId = event.getProperty();
		Object value = event.getNewValue();

		if (DEBUG_SYNC_SERVICE) {
			DebugUIPlugin.trace("SYNC SERVICE RECEIVED CHANGED EVENT:"); //$NON-NLS-1$
			DebugUIPlugin.trace("Source:  " + rendering); //$NON-NLS-1$
			DebugUIPlugin.trace("Property:  " + propertyId); //$NON-NLS-1$
			DebugUIPlugin.trace("Value:  " + value); //$NON-NLS-1$

			if (value instanceof BigInteger) {
				DebugUIPlugin.trace("Value in hex:  " + ((BigInteger) value).toString(16)); //$NON-NLS-1$
			}
		}

		if (memoryBlock == null) {
			return;
		}

		if (propertyId == null) {
			return;
		}

		// find the synchronize info object for the memory block
		SynchronizeInfo info = fSynchronizeInfo.get(memoryBlock);

		// if info is not available, need to create one to hold the property
		if (info == null) {
			info = new SynchronizeInfo(memoryBlock);
			fSynchronizeInfo.put(memoryBlock, info);
		}

		// get the value of the property
		Object oldValue = info.getProperty(propertyId);

		if (oldValue == null) {
			// if the value has never been added to the info object
			// set the property and fire a change event
			info.setProperty(propertyId, value);
			fLastChangedRendering = rendering;
			firePropertyChanged(event);
			return;
		} else if (!oldValue.equals(value)) {
			// if the value has changed
			// set the property and fire a change event
			info.setProperty(propertyId, value);
			fLastChangedRendering = rendering;
			firePropertyChanged(event);
		}
	}

	public void setEnabled(boolean enabled) {
		if (enabled && fEnableState == ENABLED) {
			return;
		}

		if (!enabled && fEnableState == DISABLED) {
			return;
		}

		try {
			if (enabled) {
				fEnableState = ENABLING;
				// get sync info from the sync service provider
				if (fLastChangedRendering != null) {
					IMemoryBlock memBlock = fLastChangedRendering.getMemoryBlock();
					SynchronizeInfo info = fSynchronizeInfo.get(memBlock);
					String[] ids = info.getPropertyIds();

					// stop handling property changed event while the
					// synchronization service is being enabled
					// this is to get around problem when the last changed
					// rendering is not currently the sync info provider

					for (int i = 0; i < ids.length; i++) {
						PropertyChangeEvent evt = new PropertyChangeEvent(fLastChangedRendering, ids[i], null, info.getProperty(ids[i]));
						firePropertyChanged(evt);
					}
				}
			}
		} finally {
			if (enabled) {
				fEnableState = ENABLED;
			} else {
				fEnableState = DISABLED;
			}
		}
	}

	public boolean isEnabled() {
		return fEnableState == ENABLED;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.debug.ui.memory.IMemoryRenderingSynchronizationService#
	 * setSynchronizationProvider(org.eclipse.debug.ui.memory.IMemoryRendering)
	 */
	@Override
	public void setSynchronizationProvider(IMemoryRendering rendering) {

		if (DEBUG_SYNC_SERVICE) {
			DebugUIPlugin.trace("SYNCHRONIZATION PROVIDER: " + rendering); //$NON-NLS-1$
		}
		if (fSyncServiceProvider != null) {
			fSyncServiceProvider.removePropertyChangeListener(this);
		}
		if (rendering != null) {
			rendering.addPropertyChangeListener(this);
		}
		fSyncServiceProvider = rendering;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.debug.ui.memory.IMemoryRenderingSynchronizationService#
	 * getSynchronizationProvider()
	 */
	@Override
	public IMemoryRendering getSynchronizationProvider() {
		return fSyncServiceProvider;
	}
}

Back to the top