Skip to main content
summaryrefslogtreecommitdiffstats
blob: e278cba95cf8f52f06b1fd20e913dd2fa80651b7 (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
/**********************************************************************
 * Copyright (c) 2004 QNX Software Systems and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors: 
 * QNX Software Systems - Initial API and implementation
 ***********************************************************************/
package org.eclipse.cdt.debug.internal.ui.views.memory;

import org.eclipse.cdt.debug.core.ICMemoryManager;
import org.eclipse.cdt.debug.core.model.IFormattedMemoryBlock;
import org.eclipse.cdt.debug.internal.ui.ICDebugHelpContextIds;
import org.eclipse.cdt.debug.internal.ui.actions.AutoRefreshMemoryAction;
import org.eclipse.cdt.debug.internal.ui.actions.ClearMemoryAction;
import org.eclipse.cdt.debug.internal.ui.actions.MemoryActionSelectionGroup;
import org.eclipse.cdt.debug.internal.ui.actions.MemoryFormatAction;
import org.eclipse.cdt.debug.internal.ui.actions.MemoryNumberOfColumnAction;
import org.eclipse.cdt.debug.internal.ui.actions.MemorySizeAction;
import org.eclipse.cdt.debug.internal.ui.actions.RefreshMemoryAction;
import org.eclipse.cdt.debug.internal.ui.actions.ShowAsciiAction;
import org.eclipse.cdt.debug.internal.ui.preferences.ICDebugPreferenceConstants;
import org.eclipse.cdt.debug.internal.ui.views.AbstractDebugEventHandler;
import org.eclipse.cdt.debug.internal.ui.views.AbstractDebugEventHandlerView;
import org.eclipse.cdt.debug.internal.ui.views.IDebugExceptionHandler;
import org.eclipse.cdt.debug.ui.CDebugUIPlugin;
import org.eclipse.cdt.debug.ui.ICDebugUIConstants;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.IDebugElement;
import org.eclipse.debug.ui.IDebugModelPresentation;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.text.ITextOperationTarget;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
import org.eclipse.ui.texteditor.IUpdate;

/**
 * This view shows the content of the memory blocks associated with the selected debug target.
 */
public class MemoryView extends AbstractDebugEventHandlerView implements ISelectionListener, IPropertyChangeListener, IDebugExceptionHandler {

	private IDebugModelPresentation fModelPresentation = null;

	private MemoryActionSelectionGroup fMemoryFormatGroup = null;

	private MemoryActionSelectionGroup fMemorySizeGroup = null;

	private MemoryActionSelectionGroup fMemoryNumberOfColumnsGroup = null;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.debug.ui.AbstractDebugView#createViewer(Composite)
	 */
	protected Viewer createViewer( Composite parent ) {
		CDebugUIPlugin.getDefault().getPreferenceStore().addPropertyChangeListener( this );
		final MemoryViewer viewer = new MemoryViewer( parent, this );
		viewer.setContentProvider( createContentProvider() );
		viewer.setLabelProvider( getModelPresentation() );
		getSite().getPage().addSelectionListener( IDebugUIConstants.ID_DEBUG_VIEW, this );
		setEventHandler( createEventHandler( viewer ) );
		return viewer;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.debug.ui.AbstractDebugView#createActions()
	 */
	protected void createActions() {
		IAction action = null;
		action = new MemoryViewAction( this, ITextOperationTarget.CUT );
		String cutStr = MemoryViewMessages.getString( "MemoryView.0" ); //$NON-NLS-1$
		action.setText( cutStr );
		action.setToolTipText( cutStr );
		action.setDescription( cutStr );
		setGlobalAction( ITextEditorActionConstants.CUT, (MemoryViewAction)action );
		action = new MemoryViewAction( this, ITextOperationTarget.COPY );
		String copyStr = MemoryViewMessages.getString( "MemoryView.1" ); //$NON-NLS-1$
		action.setText( copyStr );
		action.setToolTipText( copyStr );
		action.setDescription( copyStr );
		setGlobalAction( ITextEditorActionConstants.COPY, (MemoryViewAction)action );
		action = new MemoryViewAction( this, ITextOperationTarget.PASTE );
		String pasteStr = MemoryViewMessages.getString( "MemoryView.2" ); //$NON-NLS-1$
		action.setText( pasteStr );
		action.setToolTipText( pasteStr );
		action.setDescription( pasteStr );
		setGlobalAction( ITextEditorActionConstants.PASTE, (MemoryViewAction)action );
		action = new MemoryViewAction( this, ITextOperationTarget.SELECT_ALL );
		String selectAllStr = MemoryViewMessages.getString( "MemoryView.3" ); //$NON-NLS-1$
		action.setText( selectAllStr );
		action.setToolTipText( selectAllStr );
		action.setDescription( selectAllStr );
		setGlobalAction( ITextEditorActionConstants.SELECT_ALL, (MemoryViewAction)action );
		action = new RefreshMemoryAction( (MemoryViewer)getViewer() );
		action.setEnabled( false );
		setAction( "RefreshMemory", action ); //$NON-NLS-1$
		add( (RefreshMemoryAction)action );
		action = new AutoRefreshMemoryAction( (MemoryViewer)getViewer() );
		action.setEnabled( false );
		action.setChecked( CDebugUIPlugin.getDefault().getPreferenceStore().getBoolean( ICDebugPreferenceConstants.PREF_MEMORY_AUTO_REFRESH ) );
		setAction( "AutoRefreshMemory", action ); //$NON-NLS-1$
		add( (AutoRefreshMemoryAction)action );
		action = new ClearMemoryAction( (MemoryViewer)getViewer() );
		action.setEnabled( false );
		setAction( "ClearMemory", action ); //$NON-NLS-1$
		add( (ClearMemoryAction)action );
		action = new ShowAsciiAction( (MemoryViewer)getViewer() );
		action.setEnabled( false );
		action.setChecked( CDebugUIPlugin.getDefault().getPreferenceStore().getBoolean( ICDebugPreferenceConstants.PREF_MEMORY_SHOW_ASCII ) );
		setAction( "ShowAscii", action ); //$NON-NLS-1$
		add( (ShowAsciiAction)action );
		fMemoryFormatGroup = new MemoryActionSelectionGroup();
		createFormatActionGroup( fMemoryFormatGroup );
		fMemorySizeGroup = new MemoryActionSelectionGroup();
		createSizeActionGroup( fMemorySizeGroup );
		fMemoryNumberOfColumnsGroup = new MemoryActionSelectionGroup();
		createNumberOfColumnsActionGroup( fMemoryNumberOfColumnsGroup );
		// set initial content here, as viewer has to be set
		setInitialContent();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.debug.ui.AbstractDebugView#getHelpContextId()
	 */
	protected String getHelpContextId() {
		return ICDebugHelpContextIds.MEMORY_VIEW;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.debug.ui.AbstractDebugView#fillContextMenu(IMenuManager)
	 */
	protected void fillContextMenu( IMenuManager menu ) {
		menu.add( new Separator( ICDebugUIConstants.EMPTY_MEMORY_GROUP ) );
		menu.add( new Separator( ICDebugUIConstants.MEMORY_GROUP ) );
		menu.add( new Separator( ICDebugUIConstants.EMPTY_FORMAT_GROUP ) );
		menu.add( new Separator( ICDebugUIConstants.FORMAT_GROUP ) );
		menu.add( new Separator( IDebugUIConstants.EMPTY_RENDER_GROUP ) );
		menu.add( new Separator( IDebugUIConstants.RENDER_GROUP ) );
		menu.add( new Separator( IWorkbenchActionConstants.MB_ADDITIONS ) );
		menu.appendToGroup( ICDebugUIConstants.MEMORY_GROUP, getAction( "AutoRefreshMemory" ) ); //$NON-NLS-1$
		menu.appendToGroup( ICDebugUIConstants.MEMORY_GROUP, getAction( "RefreshMemory" ) ); //$NON-NLS-1$
		menu.appendToGroup( ICDebugUIConstants.MEMORY_GROUP, getAction( "ClearMemory" ) ); //$NON-NLS-1$
		MenuManager subMenu = new MenuManager( MemoryViewMessages.getString( "MemoryView.4" ) ); //$NON-NLS-1$
		{
			IAction[] actions = fMemoryFormatGroup.getActions();
			for( int i = 0; i < actions.length; ++i ) {
				subMenu.add( actions[i] );
			}
		}
		menu.appendToGroup( ICDebugUIConstants.FORMAT_GROUP, subMenu );
		subMenu = new MenuManager( MemoryViewMessages.getString( "MemoryView.5" ) ); //$NON-NLS-1$
		{
			IAction[] actions = fMemorySizeGroup.getActions();
			for( int i = 0; i < actions.length; ++i ) {
				subMenu.add( actions[i] );
			}
		}
		menu.appendToGroup( ICDebugUIConstants.FORMAT_GROUP, subMenu );
		subMenu = new MenuManager( MemoryViewMessages.getString( "MemoryView.6" ) ); //$NON-NLS-1$
		{
			IAction[] actions = fMemoryNumberOfColumnsGroup.getActions();
			for( int i = 0; i < actions.length; ++i ) {
				subMenu.add( actions[i] );
			}
		}
		menu.appendToGroup( ICDebugUIConstants.FORMAT_GROUP, subMenu );
		menu.appendToGroup( IDebugUIConstants.RENDER_GROUP, getAction( "ShowAscii" ) ); //$NON-NLS-1$
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.debug.ui.AbstractDebugView#configureToolBar(IToolBarManager)
	 */
	protected void configureToolBar( IToolBarManager tbm ) {
		tbm.add( new Separator( this.getClass().getName() ) );
		tbm.add( new Separator( ICDebugUIConstants.MEMORY_GROUP ) );
		tbm.add( getAction( "AutoRefreshMemory" ) ); //$NON-NLS-1$
		tbm.add( getAction( "RefreshMemory" ) ); //$NON-NLS-1$
		tbm.add( getAction( "ClearMemory" ) ); //$NON-NLS-1$
		tbm.add( new Separator( IDebugUIConstants.RENDER_GROUP ) );
		tbm.add( getAction( "ShowAscii" ) ); //$NON-NLS-1$
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.ISelectionListener#selectionChanged(IWorkbenchPart, ISelection)
	 */
	public void selectionChanged( IWorkbenchPart part, ISelection selection ) {
		if ( selection instanceof IStructuredSelection ) {
			setViewerInput( (IStructuredSelection)selection );
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(PropertyChangeEvent)
	 */
	public void propertyChange( PropertyChangeEvent event ) {
		((MemoryViewer)getViewer()).propertyChange( event );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.cdt.debug.internal.ui.views.IDebugExceptionHandler#handleException(DebugException)
	 */
	public void handleException( DebugException e ) {
	}

	/**
	 * Remove myself as a selection listener and preference change listener.
	 * 
	 * @see IWorkbenchPart#dispose()
	 */
	public void dispose() {
		removeActionGroup( fMemoryFormatGroup );
		fMemoryFormatGroup.dispose();
		removeActionGroup( fMemorySizeGroup );
		fMemorySizeGroup.dispose();
		removeActionGroup( fMemoryNumberOfColumnsGroup );
		fMemoryNumberOfColumnsGroup.dispose();
		remove( (ShowAsciiAction)getAction( "ShowAscii" ) ); //$NON-NLS-1$
		remove( (ClearMemoryAction)getAction( "ClearMemory" ) ); //$NON-NLS-1$
		remove( (RefreshMemoryAction)getAction( "RefreshMemory" ) ); //$NON-NLS-1$
		remove( (AutoRefreshMemoryAction)getAction( "AutoRefreshMemory" ) ); //$NON-NLS-1$
		getSite().getPage().removeSelectionListener( IDebugUIConstants.ID_DEBUG_VIEW, this );
		CDebugUIPlugin.getDefault().getPreferenceStore().removePropertyChangeListener( this );
		((MemoryViewer)getViewer()).dispose();
		super.dispose();
	}

	protected void setViewerInput( IStructuredSelection ssel ) {
		ICMemoryManager mm = null;
		if ( ssel != null && ssel.size() == 1 ) {
			Object input = ssel.getFirstElement();
			if ( input instanceof IDebugElement ) {
				mm = (ICMemoryManager)((IDebugElement)input).getDebugTarget().getAdapter( ICMemoryManager.class );
			}
		}
		Object current = getViewer().getInput();
		if ( current != null && current.equals( mm ) ) {
			return;
		}
		showViewer();
		getViewer().setInput( mm );
		updateObjects();
	}

	private IContentProvider createContentProvider() {
		return new MemoryViewContentProvider();
	}

	private IDebugModelPresentation getModelPresentation() {
		if ( fModelPresentation == null ) {
			fModelPresentation = CDebugUIPlugin.getDebugModelPresentation();
		}
		return fModelPresentation;
	}

	/**
	 * Creates this view's event handler.
	 * 
	 * @param viewer
	 *            the viewer associated with this view
	 * @return an event handler
	 */
	protected AbstractDebugEventHandler createEventHandler( Viewer viewer ) {
		return new MemoryViewEventHandler( this );
	}

	/**
	 * Initializes the viewer input on creation
	 */
	protected void setInitialContent() {
		ISelection selection = getSite().getPage().getSelection( IDebugUIConstants.ID_DEBUG_VIEW );
		if ( selection instanceof IStructuredSelection && !selection.isEmpty() ) {
			setViewerInput( (IStructuredSelection)selection );
		}
		else {
			setViewerInput( null );
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.debug.ui.AbstractDebugView#createContextMenu(Control)
	 */
	protected void createContextMenu( Control menuControl ) {
		CTabItem[] items = ((MemoryViewer)getViewer()).getTabFolder().getItems();
		for( int i = 0; i < items.length; ++i ) {
			super.createContextMenu( ((MemoryControlArea)items[i].getControl()).getMemoryText().getControl() );
		}
	}

	private void createFormatActionGroup( MemoryActionSelectionGroup group ) {
		int[] formats = new int[]{ IFormattedMemoryBlock.MEMORY_FORMAT_HEX, IFormattedMemoryBlock.MEMORY_FORMAT_SIGNED_DECIMAL, IFormattedMemoryBlock.MEMORY_FORMAT_UNSIGNED_DECIMAL };
		for( int i = 0; i < formats.length; ++i ) {
			MemoryFormatAction action = new MemoryFormatAction( group, (MemoryViewer)getViewer(), formats[i] );
			action.setEnabled( false );
			setAction( action.getActionId(), action ); //$NON-NLS-1$
			add( action );
			group.addAction( action );
		}
	}

	private void createSizeActionGroup( MemoryActionSelectionGroup group ) {
		int[] ids = new int[]{ IFormattedMemoryBlock.MEMORY_SIZE_BYTE, IFormattedMemoryBlock.MEMORY_SIZE_HALF_WORD, IFormattedMemoryBlock.MEMORY_SIZE_WORD, IFormattedMemoryBlock.MEMORY_SIZE_DOUBLE_WORD };
		for( int i = 0; i < ids.length; ++i ) {
			MemorySizeAction action = new MemorySizeAction( group, (MemoryViewer)getViewer(), ids[i] );
			action.setEnabled( false );
			setAction( action.getActionId(), action ); //$NON-NLS-1$
			add( action );
			group.addAction( action );
		}
	}

	private void createNumberOfColumnsActionGroup( MemoryActionSelectionGroup group ) {
		int[] nocs = new int[]{ IFormattedMemoryBlock.MEMORY_NUMBER_OF_COLUMNS_1, IFormattedMemoryBlock.MEMORY_NUMBER_OF_COLUMNS_2, IFormattedMemoryBlock.MEMORY_NUMBER_OF_COLUMNS_4, IFormattedMemoryBlock.MEMORY_NUMBER_OF_COLUMNS_8, IFormattedMemoryBlock.MEMORY_NUMBER_OF_COLUMNS_16 };
		for( int i = 0; i < nocs.length; ++i ) {
			MemoryNumberOfColumnAction action = new MemoryNumberOfColumnAction( group, (MemoryViewer)getViewer(), nocs[i] );
			action.setEnabled( false );
			setAction( action.getActionId(), action ); //$NON-NLS-1$
			add( action );
			group.addAction( action );
		}
	}

	private void removeActionGroup( MemoryActionSelectionGroup group ) {
		IAction[] actions = group.getActions();
		for( int i = 0; i < actions.length; ++i ) {
			remove( (IUpdate)actions[i] );
		}
	}

	private void setGlobalAction( String actionId, MemoryViewAction action ) {
		add( action );
		getViewSite().getActionBars().setGlobalActionHandler( actionId, action );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
	 */
	public Object getAdapter( Class adapter ) {
		if ( ITextOperationTarget.class.equals( adapter ) ) {
			return ((MemoryViewer)getViewer()).getTextOperationTarget();
		}
		return super.getAdapter( adapter );
	}
}

Back to the top