Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e3e31cf5e86e8c2d77fcfe695515d086acaaeaf1 (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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
/*******************************************************************************
 * Copyright (c) 2004, 2006 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * QNX Software Systems - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.debug.internal.ui.views.disassembly;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.cdt.debug.core.CDebugCorePlugin;
import org.eclipse.cdt.debug.core.ICDebugConstants;
import org.eclipse.cdt.debug.core.model.ICStackFrame;
import org.eclipse.cdt.debug.core.model.IDisassembly;
import org.eclipse.cdt.debug.internal.ui.ICDebugHelpContextIds;
import org.eclipse.cdt.debug.internal.ui.IInternalCDebugUIConstants;
import org.eclipse.cdt.debug.internal.ui.actions.CBreakpointPropertiesRulerAction;
import org.eclipse.cdt.debug.internal.ui.actions.EnableDisableBreakpointRulerAction;
import org.eclipse.cdt.debug.internal.ui.actions.ToggleBreakpointRulerAction;
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.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.model.IThread;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuListener;
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.preference.IPreferenceStore;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.TextPresentation;
import org.eclipse.jface.text.source.IAnnotationAccess;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.IOverviewRuler;
import org.eclipse.jface.text.source.ISharedTextColors;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.jface.text.source.OverviewRuler;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.jface.text.source.VerticalRuler;
import org.eclipse.jface.util.Assert;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.INullSelectionListener;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
import org.eclipse.ui.texteditor.AnnotationPreference;
import org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess;
import org.eclipse.ui.texteditor.DefaultRangeIndicator;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
import org.eclipse.ui.texteditor.IUpdate;
import org.eclipse.ui.texteditor.MarkerAnnotationPreferences;
import org.eclipse.ui.texteditor.SourceViewerDecorationSupport;

/**
 * This view shows disassembly for a particular stack frame.
 */
public class DisassemblyView extends AbstractDebugEventHandlerView 
							 implements ISelectionListener, 
							 			INullSelectionListener, 
										IPropertyChangeListener, 
										IDebugExceptionHandler,
										IDisassemblyListener {

	/**
	 * Creates and returns the listener on this view's context menus.
	 *
	 * @return the menu listener
	 */
	protected final IMenuListener getContextMenuListener() {
		if ( fMenuListener == null ) {
			fMenuListener = new IMenuListener() {

				public void menuAboutToShow( IMenuManager menu ) {
					String id = menu.getId();
					if ( getRulerContextMenuId().equals( id ) ) {
						setFocus();
						rulerContextMenuAboutToShow( menu );
					}
					else if ( getViewContextMenuId().equals( id ) ) {
						setFocus();
						viewContextMenuAboutToShow( menu );
					}
				}
			};
		}
		return fMenuListener;
	}

	/**
	 * Creates and returns the listener on this editor's vertical ruler.
	 *
	 * @return the mouse listener
	 */
	protected final MouseListener getRulerMouseListener() {
		if ( fMouseListener == null ) {
			fMouseListener = new MouseListener() {

				private boolean fDoubleClicked = false;

				private void triggerAction( String actionID ) {
					IAction action = getAction( actionID );
					if ( action != null ) {
						if ( action instanceof IUpdate )
							((IUpdate)action).update();
						if ( action.isEnabled() )
							action.run();
					}
				}

				public void mouseUp( MouseEvent e ) {
					setFocus();
					if ( 1 == e.button && !fDoubleClicked )
						triggerAction( ITextEditorActionConstants.RULER_CLICK );
					fDoubleClicked = false;
				}

				public void mouseDoubleClick( MouseEvent e ) {
					if ( 1 == e.button ) {
						fDoubleClicked = true;
						triggerAction( IInternalCDebugUIConstants.ACTION_TOGGLE_BREAKPOINT );
					}
				}

				public void mouseDown( MouseEvent e ) {
					StyledText text = getSourceViewer().getTextWidget();
					if ( text != null && !text.isDisposed() ) {
						Display display = text.getDisplay();
						Point location = display.getCursorLocation();
						getRulerContextMenu().setLocation( location.x, location.y );
					}
				}
			};
		}
		return fMouseListener;
	}

	/**
	 * The width of the vertical ruler.
	 */
	private final static int VERTICAL_RULER_WIDTH = 12;

	/**
	 * Preference key for highlighting current line.
	 */
	private final static String CURRENT_LINE = AbstractDecoratedTextEditorPreferenceConstants.EDITOR_CURRENT_LINE;

	/**
	 * Preference key for highlight color of current line.
	 */
	private final static String CURRENT_LINE_COLOR = AbstractDecoratedTextEditorPreferenceConstants.EDITOR_CURRENT_LINE_COLOR;

	/** 
	 * The view's context menu id. 
	 */
	private String fViewContextMenuId;

	/** 
	 * The ruler's context menu id. 
	 */
	private String fRulerContextMenuId;

	/**
	 * The vertical ruler.
	 */
	private IVerticalRuler fVerticalRuler;
	
	/**
	 * The overview ruler.
	 */
	private IOverviewRuler fOverviewRuler;

	/**
	 * The last stack frame for which the disassembly storage has 
	 * been requested.
	 */
	protected ICStackFrame fLastStackFrame = null;

	/**
	 * Helper for managing the decoration support of this view's viewer.
	 */
	private SourceViewerDecorationSupport fSourceViewerDecorationSupport;

	/**
	 * Helper for accessing annotation from the perspective of this view.
	 */
	private IAnnotationAccess fAnnotationAccess;

	/**
	 * The annotation preferences.
	 */
	private MarkerAnnotationPreferences fAnnotationPreferences;

	/**
	 * Disassembly document provider.
	 */
	private DisassemblyDocumentProvider fDocumentProvider;

	/**
	 * Current instruction pointer nnotation.
	 */
	private DisassemblyInstructionPointerAnnotation fInstrPointerAnnotation;

	/** 
	 * Context menu listener. 
	 */
	private IMenuListener fMenuListener;

	/** 
	 * Vertical ruler mouse listener. 
	 */
	private MouseListener fMouseListener;

	/** 
	 * The ruler context menu to be disposed. 
	 */
	private Menu fRulerContextMenu;

	/** 
	 * The text context menu to be disposed. 
	 */
	private Menu fTextContextMenu;

	/** 
	 * The actions registered with the view. 
	 */	
	private Map fActions = new HashMap( 10 );

	/**
	 * Constructor for DisassemblyView.
	 */
	public DisassemblyView() {
		super();
		setViewContextMenuId( "#DisassemblyViewContext" ); //$NON-NLS-1$
		setRulerContextMenuId( "#DisassemblyViewRulerContext" ); //$NON-NLS-1$
		fAnnotationPreferences = new MarkerAnnotationPreferences();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.AbstractDebugView#createViewer(org.eclipse.swt.widgets.Composite)
	 */
	protected Viewer createViewer( Composite parent ) {
		fVerticalRuler = createVerticalRuler();
		fOverviewRuler = createOverviewRuler( getSharedColors() );
		
		SourceViewer viewer = createSourceViewer( parent, fVerticalRuler, getOverviewRuler() );
		viewer.configure( new DisassemblyViewerConfiguration() );
		getSourceViewerDecorationSupport( viewer );
		
		getEditorPreferenceStore().addPropertyChangeListener( this );
		CDebugUIPlugin.getDefault().getPreferenceStore().addPropertyChangeListener( this );
		getSite().getPage().addSelectionListener( IDebugUIConstants.ID_DEBUG_VIEW, this );
		getSite().setSelectionProvider( viewer.getSelectionProvider() );
		setEventHandler( createEventHandler() );
		
		viewer.setDocument( getDocumentProvider().getDocument( DisassemblyEditorInput.EMPTY_EDITOR_INPUT ), getDocumentProvider().getAnnotationModel( null ) );

		resetViewerInput();

		return viewer;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.AbstractDebugView#createActions()
	 */
	protected void createActions() {
		IAction action;
		IVerticalRuler ruler = getVerticalRuler();
		action= new ToggleBreakpointRulerAction( this, ruler );
		setAction( IInternalCDebugUIConstants.ACTION_TOGGLE_BREAKPOINT, action );
		action= new EnableDisableBreakpointRulerAction( this, ruler );
		setAction( IInternalCDebugUIConstants.ACTION_ENABLE_DISABLE_BREAKPOINT, action );
		action= new CBreakpointPropertiesRulerAction( this, ruler );
		setAction( IInternalCDebugUIConstants.ACTION_BREAKPOINT_PROPERTIES, action );
	}

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

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.AbstractDebugView#fillContextMenu(org.eclipse.jface.action.IMenuManager)
	 */
	protected void fillContextMenu( IMenuManager menu ) {
		menu.add( new Separator( IWorkbenchActionConstants.MB_ADDITIONS ) );
		updateObjects();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.AbstractDebugView#configureToolBar(org.eclipse.jface.action.IToolBarManager)
	 */
	protected void configureToolBar( IToolBarManager tbm ) {
		// TODO Auto-generated method stub
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
	 */
	public void selectionChanged( IWorkbenchPart part, ISelection selection ) {
		if ( !isAvailable() || !isVisible() )
			return;
		if ( selection == null )
			resetViewerInput();
		else if ( selection instanceof IStructuredSelection )
			computeInput( (IStructuredSelection)selection );
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
	 */
	public void propertyChange( PropertyChangeEvent event ) {
		String propertyName = event.getProperty();
		if ( IInternalCDebugUIConstants.DISASSEMBLY_SOURCE_LINE_COLOR.equals( propertyName ) ) {
			IEditorInput input = getInput();
			if ( input instanceof DisassemblyEditorInput )
				getSourceViewer().changeTextPresentation( createTextPresentation( ((DisassemblyEditorInput)input).getSourceRegions() ), true );
		}
		else if ( IInternalCDebugUIConstants.DISASSEMBLY_FONT.equals( propertyName ) ) {
			getSourceViewer().getTextWidget().setFont( JFaceResources.getFont( IInternalCDebugUIConstants.DISASSEMBLY_FONT ) );			
		}
	}

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

	/**
	 * Creates the vertical ruler to be used by this view.
	 *
	 * @return the vertical ruler
	 */
	protected IVerticalRuler createVerticalRuler() {
		IVerticalRuler ruler = new VerticalRuler( VERTICAL_RULER_WIDTH, getAnnotationAccess() );
		return ruler;
	}

	private IOverviewRuler createOverviewRuler( ISharedTextColors sharedColors ) {
		IOverviewRuler ruler = new OverviewRuler( getAnnotationAccess(), VERTICAL_RULER_WIDTH, sharedColors );
		Iterator e = fAnnotationPreferences.getAnnotationPreferences().iterator();
		while( e.hasNext() ) {
			AnnotationPreference preference = (AnnotationPreference)e.next();
			if ( preference.contributesToHeader() )
				ruler.addHeaderAnnotationType( preference.getAnnotationType() );
		}
		return ruler;
	}

	/**
	 * Creates the source viewer to be used by this view.
	 *
	 * @param parent the parent control
	 * @param ruler the vertical ruler
	 * @param styles style bits
	 * @return the source viewer
	 */
	private SourceViewer createSourceViewer( Composite parent, IVerticalRuler vertRuler, IOverviewRuler ovRuler ) {
		DisassemblyViewer viewer = new DisassemblyViewer( parent, vertRuler, ovRuler );
		viewer.setRangeIndicator( new DefaultRangeIndicator() );
		JFaceResources.getFontRegistry().addListener( this );
		JFaceResources.getColorRegistry().addListener( this );
		return viewer;
	}

	protected SourceViewer getSourceViewer() {
		return (SourceViewer)getViewer();
	}

	/**
	 * Creates this view's event handler.
	 * 
	 * @return an event handler
	 */
	protected AbstractDebugEventHandler createEventHandler() {
		return new DisassemblyViewEventHandler( this );
	}	

	protected void computeInput( IStructuredSelection ssel ) {
		SourceViewer viewer = getSourceViewer();
		if ( viewer == null )
			return;

		fLastStackFrame = null;
		if ( ssel != null && ssel.size() == 1 ) {
			Object element = ssel.getFirstElement();
			if ( element instanceof ICStackFrame ) {
				fLastStackFrame = (ICStackFrame)element;
				IEditorInput input = getInput();
				if ( input instanceof DisassemblyEditorInput && 
					 !((DisassemblyEditorInput)input).contains( (ICStackFrame)element ) )
					setViewerInput( DisassemblyEditorInput.PENDING_EDITOR_INPUT );
				computeInput( input, (ICStackFrame)element, this );
				return;
			}
		}
		resetViewerInput();
	}

	protected void setViewerInput( IEditorInput input ) {
		SourceViewer viewer = getSourceViewer();
		if ( viewer == null )
			return;

		if ( input == null )
			input = DisassemblyEditorInput.EMPTY_EDITOR_INPUT;

		IEditorInput current = getInput();
		if ( current != null && current.equals( input ) ) {
			updateObjects();
			return;
		}

		setInput( input );
		showViewer();
		try {
			getDocumentProvider().connect( input );
		}
		catch( CoreException e ) {
			// never happens
		}
		getSourceViewer().setDocument( getDocumentProvider().getDocument( input ), 
									   getDocumentProvider().getAnnotationModel( input ) );
		if ( input instanceof DisassemblyEditorInput ) {
			// Workaround for bug #69728
			IRegion[] sourceRegions = ((DisassemblyEditorInput)input).getSourceRegions();
			if ( sourceRegions.length > 0 ) {
				getSourceViewer().changeTextPresentation( createTextPresentation( sourceRegions ), true );
			}
		}
		updateObjects();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchPart#dispose()
	 */
	public void dispose() {
		getSite().getPage().removeSelectionListener( IDebugUIConstants.ID_DEBUG_VIEW, this );
		JFaceResources.getFontRegistry().removeListener( this );
		JFaceResources.getColorRegistry().removeListener( this );
		getEditorPreferenceStore().removePropertyChangeListener( this );
		CDebugUIPlugin.getDefault().getPreferenceStore().removePropertyChangeListener( this );

		if ( fSourceViewerDecorationSupport != null ) {
			fSourceViewerDecorationSupport.dispose();
			fSourceViewerDecorationSupport = null;
		}

		if ( fDocumentProvider != null ) {
			fDocumentProvider.dispose();
			fDocumentProvider = null;
		}

		if ( fActions != null ) {
			fActions.clear();
			fActions = null;
		}

		super.dispose();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.internal.ui.views.disassembly.IDisassemblyListener#inputComputed(org.eclipse.cdt.debug.core.model.ICStackFrame, org.eclipse.core.runtime.IStatus, org.eclipse.ui.IEditorInput)
	 */
	public void inputComputed( final ICStackFrame frame, final IStatus status, final IEditorInput input ) {
		Runnable runnable = new Runnable() {
			public void run() {
				if ( isAvailable() ) {
					if ( fLastStackFrame != null && fLastStackFrame.equals( frame ) ) {
						fLastStackFrame = null;
						if ( !status.isOK() ) {
							setInput( null );
							getViewer().setInput( null );
							showMessage( status.getMessage() );
							return;
						}
					}
					if ( input != null ) {
						setViewerInput( input );
						selectAndReveal( frame, input );
					}
					else {
						resetViewerInput();
					}
				}
			}
		};
		asyncExec( runnable );		
	}

	/**
	 * Asynchronousy computes the editor input for the given stack frame.
	 * 
	 * @param current the current editor input
	 * @param frame the stack frame for which the input is required
	 * @param listener the listener to be notified when the computation is completed
	 */
	public void computeInput( final Object current, 
							  final ICStackFrame frame, 
							  final IDisassemblyListener listener ) {
		Runnable runnable = new Runnable() {
			public void run() {
				IStatus status = Status.OK_STATUS;
				IEditorInput input = null;
				if ( current instanceof DisassemblyEditorInput && 
					 ((DisassemblyEditorInput)current).contains( frame ) ) {
					input = (IEditorInput)current;
				}
				else {
					try {
						input = DisassemblyEditorInput.create( frame );
					}
					catch( DebugException e ) {
						status = new Status( IStatus.ERROR, 
											 CDebugUIPlugin.getUniqueIdentifier(),
											 0,
											 e.getMessage(),
											 null );
					}
					
				}
				listener.inputComputed( frame, status, input );
			}
		};
		DebugPlugin.getDefault().asyncExec( runnable );
	}

	protected void selectAndReveal( ICStackFrame frame, IEditorInput input ) {
		IRegion region = getLineInformation( frame, input );
		if ( region != null ) {
			int start = region.getOffset();
			int length = region.getLength();			
			StyledText widget = getSourceViewer().getTextWidget();
			widget.setRedraw( false );
			{
				getSourceViewer().revealRange( start, length );
				getSourceViewer().setSelectedRange( start, 0 );
			}
			widget.setRedraw( true );
			setInstructionPointer( frame, start, length, getDocumentProvider().getAnnotationModel( input ) );
		}
	}

	/**
	 * Returns the line information for the given line in the given editor
	 */
	private IRegion getLineInformation( ICStackFrame frame, IEditorInput input ) {
		if ( input instanceof DisassemblyEditorInput ) {
			int line = ((DisassemblyEditorInput)input).getInstructionLine( frame.getAddress() );
			if ( line > 0 ) {
				try {
					return getSourceViewer().getDocument().getLineInformation( --line );
				}
				catch( BadLocationException e1 ) {
				}
			}
		}
		return null;
	}

	public IEditorInput getInput() {
		if ( getSourceViewer() != null ) {
			Object input = getSourceViewer().getInput();
			if ( input instanceof IEditorInput )
				return (IEditorInput)input;
		}
		return null;
	}

	protected void setInput( IEditorInput input ) {
		getSourceViewer().setInput( input );
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createPartControl( Composite parent ) {
		super.createPartControl( parent );
		createViewContextMenu();
		createRulerContextMenu();
		if ( fSourceViewerDecorationSupport != null )
			fSourceViewerDecorationSupport.install( getEditorPreferenceStore() );
		}

	/**
	 * Returns the source viewer decoration support.
	 * 
	 * @return the source viewer decoration support
	 */
	protected SourceViewerDecorationSupport getSourceViewerDecorationSupport( ISourceViewer viewer ) {
		if ( fSourceViewerDecorationSupport == null ) {
			fSourceViewerDecorationSupport = new SourceViewerDecorationSupport( viewer, getOverviewRuler(), getAnnotationAccess(), getSharedColors() );
			configureSourceViewerDecorationSupport( fSourceViewerDecorationSupport );
		}
		return fSourceViewerDecorationSupport;
	}

	/**
	 * Creates the annotation access for this view.
	 * 
	 * @return the created annotation access
	 */
	private IAnnotationAccess createAnnotationAccess() {
		return new DefaultMarkerAnnotationAccess();
	}

	/**
	 * Configures the decoration support for this view's the source viewer.
	 */
	private void configureSourceViewerDecorationSupport( SourceViewerDecorationSupport support ) {
		Iterator e = fAnnotationPreferences.getAnnotationPreferences().iterator();
		while( e.hasNext() )
			support.setAnnotationPreference( (AnnotationPreference)e.next() );
		support.setCursorLinePainterPreferenceKeys( CURRENT_LINE, CURRENT_LINE_COLOR );
	}

	/**
	 * Returns the annotation access. 
	 * 
	 * @return the annotation access
	 */
	private IAnnotationAccess getAnnotationAccess() {
		if ( fAnnotationAccess == null )
			fAnnotationAccess = createAnnotationAccess();
		return fAnnotationAccess;
	}

	private ISharedTextColors getSharedColors() {
		ISharedTextColors sharedColors = CDebugUIPlugin.getDefault().getSharedTextColors();
		return sharedColors;
	}

	private IPreferenceStore getEditorPreferenceStore() {
		return EditorsUI.getPreferenceStore();
	}

	public DisassemblyDocumentProvider getDocumentProvider() {
		if ( this.fDocumentProvider == null )
			this.fDocumentProvider = new DisassemblyDocumentProvider();
		return this.fDocumentProvider;
	}

	protected void setInstructionPointer( ICStackFrame frame, int start, int length, IAnnotationModel model ) {
		Assert.isNotNull( model );
		boolean tos = isTopStackFrame( frame );
		DisassemblyInstructionPointerAnnotation instPtrAnnotation = new DisassemblyInstructionPointerAnnotation( frame, tos );
		Position position = new Position( start, length );		
		DisassemblyInstructionPointerAnnotation oldPointer = getCurrentInstructionPointer();
		if ( oldPointer != null )
			model.removeAnnotation( oldPointer );
		model.addAnnotation( instPtrAnnotation, position );
		setCurrentInstructionPointer( instPtrAnnotation );
	}

	private boolean isTopStackFrame( ICStackFrame stackFrame ) {
		IThread thread = stackFrame.getThread();
		boolean tos = false;
		try {
			tos = stackFrame.equals( thread.getTopStackFrame() );
		}
		catch( DebugException e ) {
		}
		return tos;
	}

	private DisassemblyInstructionPointerAnnotation getCurrentInstructionPointer() {
		return fInstrPointerAnnotation;
	}

	private void setCurrentInstructionPointer( DisassemblyInstructionPointerAnnotation instrPointer ) {
		fInstrPointerAnnotation = instrPointer;
	}

	protected void removeCurrentInstructionPointer( IAnnotationModel model ) {
		Assert.isNotNull( model );
		DisassemblyInstructionPointerAnnotation instrPointer = getCurrentInstructionPointer();
		if ( instrPointer != null ) {
			model.removeAnnotation( instrPointer );
			setCurrentInstructionPointer( null );
		}
	}

	protected void resetViewerInput() {
		SourceViewer viewer = getSourceViewer();
		if ( viewer == null )
			return;

		IEditorInput input = DisassemblyEditorInput.EMPTY_EDITOR_INPUT; 
		setInput( input );
		showViewer();
		try {
			getDocumentProvider().connect( input );
		}
		catch( CoreException e ) {
			// never happens
		}
		IAnnotationModel model = getDocumentProvider().getAnnotationModel( input );
		getSourceViewer().setDocument( getDocumentProvider().getDocument( input ), model );
		removeCurrentInstructionPointer( model );

		updateObjects();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.AbstractDebugView#becomesHidden()
	 */
	protected void becomesHidden() {
		selectionChanged( null, new StructuredSelection() );
		super.becomesHidden();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.AbstractDebugView#becomesVisible()
	 */
	protected void becomesVisible() {
		super.becomesVisible();
		IViewPart part = getSite().getPage().findView( IDebugUIConstants.ID_DEBUG_VIEW );
		if ( part != null ) {
			ISelection selection = getSite().getPage().getSelection( IDebugUIConstants.ID_DEBUG_VIEW );
			selectionChanged( part, selection );
		}
	}

	/**
	 * Returns the overview ruler.
	 * 
	 * @return the overview ruler
	 */
	private IOverviewRuler getOverviewRuler() {
		if ( fOverviewRuler == null )
			fOverviewRuler = createOverviewRuler( getSharedColors() );
		return fOverviewRuler;
	}

	protected String getRulerContextMenuId() {
		return this.fRulerContextMenuId;
	}

	private void setRulerContextMenuId( String rulerContextMenuId ) {
		Assert.isNotNull( rulerContextMenuId );
		this.fRulerContextMenuId = rulerContextMenuId;
	}

	protected String getViewContextMenuId() {
		return this.fViewContextMenuId;
	}

	private void setViewContextMenuId( String viewContextMenuId ) {
		Assert.isNotNull( viewContextMenuId );
		this.fViewContextMenuId = viewContextMenuId;
	}

	/**
	 * Sets up the ruler context menu before it is made visible.
	 * 
	 * @param menu the menu
	 */
	protected void rulerContextMenuAboutToShow( IMenuManager menu ) {
		menu.add( new Separator( ITextEditorActionConstants.GROUP_REST ) );
		menu.add( new Separator( IWorkbenchActionConstants.MB_ADDITIONS ) );
		addAction( menu, IInternalCDebugUIConstants.ACTION_TOGGLE_BREAKPOINT );
		addAction( menu, IInternalCDebugUIConstants.ACTION_ENABLE_DISABLE_BREAKPOINT );
		addAction( menu, IInternalCDebugUIConstants.ACTION_BREAKPOINT_PROPERTIES );
	}

	/**
	 * Sets up the view context menu before it is made visible.
	 * 
	 * @param menu the menu
	 */
	protected void viewContextMenuAboutToShow( IMenuManager menu ) {
		menu.add( new Separator( ITextEditorActionConstants.GROUP_REST ) );
		menu.add( new Separator( IWorkbenchActionConstants.MB_ADDITIONS ) );
	}

	/**
	 * Convenience method to add the action installed under the given action id to the specified group of the menu.
	 * @param menu the menu to add the action to
	 * @param group the group in the menu
	 * @param actionId the id of the action to add
	 */
	protected final void addAction( IMenuManager menu, String group, String actionId ) {
		IAction action = getAction( actionId );
		if ( action != null ) {
			if ( action instanceof IUpdate )
				((IUpdate)action).update();
			IMenuManager subMenu = menu.findMenuUsingPath( group );
			if ( subMenu != null )
				subMenu.add( action );
			else
				menu.appendToGroup( group, action );
		}
	}

	/**
	 * Convenience method to add the action installed under the given action id to the given menu.
	 * @param menu the menu to add the action to
	 * @param actionId the id of the action to be added
	 */
	protected final void addAction( IMenuManager menu, String actionId ) {
		IAction action = getAction( actionId );
		if ( action != null ) {
			if ( action instanceof IUpdate )
				((IUpdate)action).update();
			menu.add( action );
		}
	}

	protected Menu getRulerContextMenu() {
		return this.fRulerContextMenu;
	}

	private void setRulerContextMenu( Menu rulerContextMenu ) {
		this.fRulerContextMenu = rulerContextMenu;
	}

	private void createViewContextMenu() {
		String id = getViewContextMenuId();
		MenuManager manager = new MenuManager( id, id );
		manager.setRemoveAllWhenShown( true );
		manager.addMenuListener( getContextMenuListener() );
		StyledText styledText = getSourceViewer().getTextWidget();
		setTextContextMenu( manager.createContextMenu( styledText ) );
		styledText.setMenu( getTextContextMenu() );

		// register the context menu such that other plugins may contribute to it
		if ( getSite() != null ) {
			getSite().registerContextMenu( id, manager, getSourceViewer() );
		}
	}

	private void createRulerContextMenu() {
		String id = getRulerContextMenuId();
		MenuManager manager = new MenuManager( id, id );
		manager.setRemoveAllWhenShown( true );
		manager.addMenuListener( getContextMenuListener() );
		Control rulerControl = fVerticalRuler.getControl();
		setRulerContextMenu( manager.createContextMenu( rulerControl ) );
		rulerControl.setMenu( getRulerContextMenu() );
		rulerControl.addMouseListener( getRulerMouseListener() );

		if ( getSite() != null ) {
            getSite().registerContextMenu( id, manager, getSourceViewer() );
        }
	}

	private Menu getTextContextMenu() {
		return this.fTextContextMenu;
	}

	private void setTextContextMenu( Menu textContextMenu ) {
		this.fTextContextMenu = textContextMenu;
	}

	public void setAction( String actionID, IAction action ) {
		Assert.isNotNull( actionID );
		if ( action == null ) {
			action = (IAction)fActions.remove( actionID );
		}
		else {
			fActions.put( actionID, action );
		}
	}

	public IAction getAction( String actionID ) {
		Assert.isNotNull( actionID );
		return (IAction)fActions.get( actionID );
	}

	private IVerticalRuler getVerticalRuler() {
		return this.fVerticalRuler;
	}

	private TextPresentation createTextPresentation( IRegion[] regions ) {
		TextPresentation p = new TextPresentation();
		for ( int i = 0; i < regions.length; ++i ) {
			p.addStyleRange( new StyleRange( regions[i].getOffset(), 
											 regions[i].getLength(), 
											 CDebugUIPlugin.getPreferenceColor( IInternalCDebugUIConstants.DISASSEMBLY_SOURCE_LINE_COLOR ),
											 null ) );
		}
		return p;
	}

	protected void refresh( IDisassembly disassembly ) {
		if ( !(getInput() instanceof DisassemblyEditorInput) || !disassembly.equals( ((DisassemblyEditorInput)getInput()).getDisassembly() ) )
			return;
		resetViewerInput();
		if ( !isAvailable() || !isVisible() )
			return;
		IAdaptable context = DebugUITools.getDebugContext();
		if ( context instanceof ICStackFrame ) {
			fLastStackFrame = (ICStackFrame)context;
			IEditorInput input = getInput();
			if ( input instanceof DisassemblyEditorInput )
				setViewerInput( DisassemblyEditorInput.PENDING_EDITOR_INPUT );
			computeInput( input, (ICStackFrame)context, this );
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.PageBookView#partActivated(org.eclipse.ui.IWorkbenchPart)
	 */
	public void partActivated( IWorkbenchPart part ) {
		if ( this.equals( part ) ) {
			CDebugCorePlugin.getDefault().getPluginPreferences().setValue( ICDebugConstants.PREF_INSTRUCTION_STEP_MODE_ON, true );
		}
		super.partActivated( part );
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.PageBookView#partDeactivated(org.eclipse.ui.IWorkbenchPart)
	 */
	public void partDeactivated( IWorkbenchPart part ) {
		if ( this.equals( part ) ) {
			CDebugCorePlugin.getDefault().getPluginPreferences().setValue( ICDebugConstants.PREF_INSTRUCTION_STEP_MODE_ON, false );
		}
		super.partDeactivated( part );
	}
}

Back to the top