Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 37033eabbb81056cd7e3aef1c6014f3aee2e2421 (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
/*******************************************************************************
 * 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
 *     Wind River Systems - Ted Williams - [Memory View] Memory View: Workflow Enhancements (Bug 215432)
 *******************************************************************************/
package org.eclipse.debug.internal.ui.views.memory;

import java.util.Enumeration;
import java.util.Hashtable;

import org.eclipse.debug.core.DebugEvent;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IDebugEventSetListener;
import org.eclipse.debug.core.IMemoryBlockListener;
import org.eclipse.debug.core.model.IMemoryBlock;
import org.eclipse.debug.core.model.IMemoryBlockRetrieval;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.contexts.IDebugContextListener;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPart;

public abstract class AbstractMemoryViewPane implements IMemoryBlockListener, ISelectionListener, SelectionListener, IMemoryView, ISelectionChangedListener, IMemoryViewPane, IDebugContextListener, IDebugEventSetListener {

	public static final String BEGINNING_POPUP = "popUpBegin"; //$NON-NLS-1$
	protected static final StructuredSelection EMPTY = new StructuredSelection();

	protected Composite fViewPaneCanvas;
	protected StackLayout fStackLayout;
	protected ViewTabEnablementManager fViewTabEnablementManager;
	protected CTabFolder fEmptyTabFolder;
	protected Hashtable<Integer, CTabFolder> fTabFolderForDebugView = new Hashtable<>();
	protected boolean fVisible;
	protected IMemoryBlockRetrieval fKey; // store the key for current tab
											// folder
	protected ViewPaneSelectionProvider fSelectionProvider;
	protected IViewPart fParent;
	protected String fPaneId;
	private Composite fCanvas;
	protected String fLabel;

	private volatile boolean fIsDisposed = false;

	public AbstractMemoryViewPane(IViewPart parent) {
		super();
		fParent = parent;
		fSelectionProvider = new ViewPaneSelectionProvider();
	}

	/**
	 * Create the content of the view pane
	 *
	 * @param parent the parent composite
	 * @param paneId the id of the pane to create
	 * @param label the label for the new pane
	 * @return the control of the view pane
	 */
	@Override
	public Control createViewPane(Composite parent, String paneId, String label) {
		fPaneId = paneId;
		fLabel = label;
		fCanvas = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.makeColumnsEqualWidth = false;
		layout.numColumns = 1;
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		GridData data = new GridData();
		data.grabExcessHorizontalSpace = true;
		data.grabExcessVerticalSpace = true;
		data.verticalAlignment = SWT.BEGINNING;
		data.horizontalAlignment = SWT.BEGINNING;
		fCanvas.setLayout(layout);
		fCanvas.setLayoutData(data);

		// memory view area
		Composite memoryViewAreaParent = fCanvas;
		Composite subCanvas = new Composite(memoryViewAreaParent, SWT.NONE);
		fViewPaneCanvas = subCanvas;
		fStackLayout = new StackLayout();
		GridData memoryAreaData = new GridData();
		memoryAreaData.grabExcessHorizontalSpace = true;
		memoryAreaData.grabExcessVerticalSpace = true;
		memoryAreaData.verticalAlignment = SWT.FILL;
		memoryAreaData.horizontalAlignment = SWT.FILL;
		fViewPaneCanvas.setLayout(fStackLayout);
		fViewPaneCanvas.setLayoutData(memoryAreaData);

		fViewTabEnablementManager = new ViewTabEnablementManager();

		fEmptyTabFolder = new CTabFolder(fViewPaneCanvas, SWT.NULL);
		setTabFolder(fEmptyTabFolder);

		addListeners();

		Object context = DebugUITools.getPartDebugContext(fParent.getSite());
		if (context != null) {
			IMemoryBlockRetrieval retrieval = MemoryViewUtil.getMemoryBlockRetrieval(context);
			if (retrieval != null) {
				createFolder(retrieval);
			}
		}

		fVisible = true;

		return fCanvas;
	}

	protected void addListeners() {
		MemoryViewUtil.getMemoryBlockManager().addListener(this);
		fParent.getViewSite().getPage().addSelectionListener(this);
		DebugUITools.addPartDebugContextListener(fParent.getSite(), this);
		DebugPlugin.getDefault().addDebugEventListener(this);
	}

	protected void removeListeners() {
		MemoryViewUtil.getMemoryBlockManager().removeListener(this);
		fParent.getViewSite().getPage().removeSelectionListener(this);
		DebugUITools.removePartDebugContextListener(fParent.getSite(), this);
		if (fStackLayout.topControl != null) {
			CTabFolder old = (CTabFolder) fStackLayout.topControl;

			if (!old.isDisposed()) {
				old.removeSelectionListener(this);
				old.removeSelectionListener(fViewTabEnablementManager);
			}
		}
		DebugPlugin.getDefault().removeDebugEventListener(this);
	}

	protected void setTabFolder(CTabFolder folder) {
		if (fStackLayout.topControl != null) {
			CTabFolder old = (CTabFolder) fStackLayout.topControl;

			if (!old.isDisposed()) {
				old.removeSelectionListener(this);
				old.removeSelectionListener(fViewTabEnablementManager);
			}
		}

		fStackLayout.topControl = folder;

		if (folder.getItemCount() > 0) {
			CTabItem selectedItem = folder.getSelection();

			if (selectedItem != null) {
				Object selected = getCurrentSelection();
				if (selected != null) {
					fSelectionProvider.setSelection(new StructuredSelection(selected));
				} else {
					fSelectionProvider.setSelection(AbstractMemoryViewPane.EMPTY);
				}
			}
		} else {
			fSelectionProvider.setSelection(AbstractMemoryViewPane.EMPTY);
		}

		folder.addSelectionListener(this);
		folder.addSelectionListener(fViewTabEnablementManager);
	}

	private void createFolder(IMemoryBlockRetrieval memRetrieval) {
		// if we've got a tabfolder to go with the IMemoryBlockRetrieval,
		// display it
		Integer key = MemoryViewUtil.getHashCode(memRetrieval);
		if (fTabFolderForDebugView.containsKey(key)) {
			if (fStackLayout.topControl != fTabFolderForDebugView.get(key)) {
				setTabFolder(fTabFolderForDebugView.get(key));
				fViewPaneCanvas.layout();
			}
		} else { // otherwise, add a new one
			fTabFolderForDebugView.put(key, new CTabFolder(fViewPaneCanvas, SWT.NULL));
			setTabFolder(fTabFolderForDebugView.get(key));
			fViewPaneCanvas.layout();
		}
	}

	@Override
	public IMemoryViewTab getTopMemoryTab() {

		if (fStackLayout.topControl instanceof CTabFolder) {
			CTabFolder folder = (CTabFolder) fStackLayout.topControl;
			if (!folder.isDisposed()) {
				int index = folder.getSelectionIndex();
				if (index >= 0) {
					CTabItem tab = folder.getItem(index);
					return (IMemoryViewTab) tab.getData();
				}
			}
		}
		return null;
	}

	protected void disposeTab(CTabItem tabItem) {
		if (tabItem == null) {
			return;
		}

		// dispose the tab item in case the view tab has not
		// cleaned up the tab item
		if (!tabItem.isDisposed()) {
			tabItem.dispose();
		}
	}

	protected void emptyFolder() {
		setTabFolder(fEmptyTabFolder);
		if (!fViewPaneCanvas.isDisposed()) {
			fViewPaneCanvas.layout();
		}
	}

	@Override
	public void addSelectionListener(ISelectionChangedListener listener) {
		if (fSelectionProvider == null) {
			fSelectionProvider = new ViewPaneSelectionProvider();
		}

		fSelectionProvider.addSelectionChangedListener(listener);
	}

	@Override
	public void removeSelctionListener(ISelectionChangedListener listener) {
		if (fSelectionProvider == null) {
			return;
		}

		fSelectionProvider.removeSelectionChangedListener(listener);
	}

	@Override
	public ISelectionProvider getSelectionProvider() {
		return fSelectionProvider;
	}

	@Override
	public void handleDebugEvents(DebugEvent[] events) {
		for (DebugEvent event : events) {
			Object source = event.getSource();
			if (event.getKind() == DebugEvent.TERMINATE && source instanceof IMemoryBlockRetrieval) {
				if (isDisposed()) {
					return;
				}

				// When a memory block retrieval terminates, it and its
				// tab folders should be removed from our map.
				final IMemoryBlockRetrieval ret = (IMemoryBlockRetrieval) source;
				if (ret != null) {
					Display.getDefault().asyncExec(() -> {
						if (isDisposed()) {
							return;
						}

						Integer key = MemoryViewUtil.getHashCode(ret);
						Object folder = fTabFolderForDebugView.get(key);

						if (folder != null && folder != fEmptyTabFolder) {
							// remove the tab folder , and all contained tab
							// items
							disposeOfFolder((CTabFolder) folder);
							fTabFolderForDebugView.remove(key);
						}
					});
				}
			}
		}
	}

	@Override
	public void dispose() {
		if (isDisposed()) {
			return;
		}
		fIsDisposed = true;

		removeListeners();

		// dispose empty folders
		fEmptyTabFolder.dispose();

		// dispose all other folders
		try {
			Enumeration<CTabFolder> enumeration = fTabFolderForDebugView.elements();

			while (enumeration.hasMoreElements()) {
				CTabFolder tabFolder = enumeration.nextElement();
				disposeOfFolder(tabFolder);
			}

			// Clear the table as all CTabFolder's have been dipose()d
			fTabFolderForDebugView.clear();
		} catch (Exception e) {

			DebugUIPlugin.logErrorMessage("Exception occurred when the Memory View is disposed."); //$NON-NLS-1$
		}
	}

	/**
	 * Helper method to dispose of a tab folder, and of any tab items it
	 * contains. Must be called from the UI thread.
	 *
	 * @param tabFolder the {@link CTabFolder} to dispose
	 * */
	private void disposeOfFolder(CTabFolder tabFolder) {
		if (!tabFolder.isDisposed()) {

			// if tab folder is not empty, dipose view tabs
			CTabItem[] tabs = tabFolder.getItems();

			for (CTabItem tab : tabs) {
				disposeTab(tab);
			}

			tabFolder.dispose();
		}
	}

	@Override
	public void setVisible(boolean visible) {
		fVisible = visible;

		IMemoryViewTab currentTab = getTopMemoryTab();
		if (currentTab != null) {
			currentTab.setEnabled(visible);
		}
	}

	@Override
	public void selectionChanged(SelectionChangedEvent event) {
		ISelection selection = event.getSelection();
		selectionChanged(fParent, selection);

		fSelectionProvider.setSelection(selection);
	}

	/**
	 * @return the unique identifier of the view pane
	 */
	public String getPaneId() {
		return fPaneId;
	}

	@Override
	public Control getControl() {
		return fCanvas;
	}

	@Override
	public boolean isVisible() {
		return fVisible;
	}

	public String getLabel() {
		return fLabel;
	}

	protected boolean isDisposed() {
		return fIsDisposed;
	}

	@Override
	abstract public void memoryBlocksAdded(IMemoryBlock[] memoryBlocks);

	@Override
	abstract public void memoryBlocksRemoved(final IMemoryBlock[] memoryBlocks);

	@Override
	abstract public void selectionChanged(IWorkbenchPart part, ISelection selection);

	/**
	 * @return current selection from the view pane
	 */
	abstract public Object getCurrentSelection();

	/**
	 * restore the view pane based on current selection from the debug view and the
	 * memory blocks and renderings currently exist
	 */
	@Override
	abstract public void restoreViewPane();

	/**
	 * @return actions to be contributed to the view pane's toolbar
	 */
	@Override
	abstract public IAction[] getActions();

}

Back to the top