Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 64f1a4e5cc3d478303fd2bd511bb8fbcad769657 (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
/*******************************************************************************
 * Copyright (c) 2010, 2011 Ericsson 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:
 *     Ericsson - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.debug.internal.ui.commands;

import java.util.Map;

import org.eclipse.cdt.debug.core.model.IReverseToggleHandler;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IAdapterManager;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.IRequest;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.actions.DebugCommandHandler;
import org.eclipse.debug.ui.contexts.DebugContextEvent;
import org.eclipse.debug.ui.contexts.IDebugContextListener;
import org.eclipse.debug.ui.contexts.IDebugContextService;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.commands.IElementUpdater;
import org.eclipse.ui.menus.UIElement;
import org.eclipse.ui.progress.WorkbenchJob;
import org.eclipse.ui.services.IEvaluationService;

/**
 * Command handler to toggle reverse debugging mode
 *
 * @since 7.0
 */
public class ReverseToggleCommandHandler extends DebugCommandHandler implements IDebugContextListener, IElementUpdater {
	@Override
	protected Class<?> getCommandType() {
		return IReverseToggleHandler.class;
	}

    //
    // The below logic allows us to keep the checked state of the toggle button
    // properly set.  This is because in some case, the checked state may change
    // without the user actually pressing the button.  For instance, if we restart
    // the inferior, the toggle may automatically turn off.
    // To figure this out, whenever a debug context changes, we make sure we are
    // showing the proper checked state.
    //

    // We must hard-code the command id so as to know it from the very start (bug 290699)
    private static final String REVERSE_TOGGLE_COMMAND_ID = "org.eclipse.cdt.debug.ui.command.reverseToggle"; //$NON-NLS-1$

    private Object fActiveContext = null;
    private IReverseToggleHandler fTargetAdapter = null;
    private IDebugContextService fContextService = null;

    public ReverseToggleCommandHandler() {
       IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
       if (window != null) {
    	   fContextService = DebugUITools.getDebugContextManager().getContextService(window);
    	   if (fContextService != null) {
    		   fContextService.addPostDebugContextListener(this);

    		   // This constructor might be called after the launch, so we must refresh here too.
    		   // This can happen if we activate the action set after the launch.
    		   refresh(fContextService.getActiveContext());
    	   }
       }
    }

    @Override
    public void dispose() {
        // Must use the stored context service.  If we try to fetch the service
        // again with the workbenchWindow, it may fail if the window is
        // already closed.
        if (fContextService != null) {
            fContextService.removePostDebugContextListener(this);
        }
        fTargetAdapter = null;
        fActiveContext = null;
        super.dispose();
    }

    @Override
	public void debugContextChanged(DebugContextEvent event) {
        refresh(event.getContext());
    }

    private void refresh(ISelection selection) {
       fTargetAdapter = null;
       fActiveContext = null;
        if (selection instanceof IStructuredSelection) {
            IStructuredSelection ss = (IStructuredSelection) selection;
            if (!ss.isEmpty()) {
                fActiveContext = ss.getFirstElement();
                if (fActiveContext instanceof IAdaptable) {
                   fTargetAdapter = getAdapter((IAdaptable) fActiveContext);
                }
            }
        }

        ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
        if (commandService != null) {
           commandService.refreshElements(REVERSE_TOGGLE_COMMAND_ID, null);
        }
    }

    private IReverseToggleHandler getAdapter(IAdaptable adaptable) {
       IReverseToggleHandler adapter  = (IReverseToggleHandler)adaptable.getAdapter(IReverseToggleHandler.class);
        if (adapter == null) {
            IAdapterManager adapterManager = Platform.getAdapterManager();
            if (adapterManager.hasAdapter(adaptable, getCommandType().getName())) {
                adapter = (IReverseToggleHandler)adapterManager.loadAdapter(adaptable, IReverseToggleHandler.class.getName());
            }
        }
        return adapter;
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.debug.ui.actions.DebugCommandHandler#postExecute(org.eclipse.debug.core.IRequest, java.lang.Object[])
     * 
     * We keep this logic for users that may not do the refresh themselves.
     */
    @Override
    protected void postExecute(IRequest request, Object[] targets) {
    	super.postExecute(request, targets);
    	new WorkbenchJob("") { //$NON-NLS-1$
			@Override
			public IStatus runInUIThread(IProgressMonitor monitor) {
		        // Request re-evaluation of property "org.eclipse.cdt.debug.ui.isReverseDebuggingEnabled" to update 
			    // visibility of reverse stepping commands.
			    IEvaluationService exprService = (IEvaluationService) PlatformUI.getWorkbench().getService(IEvaluationService.class);
			    if (exprService != null) {
			        exprService.requestEvaluation("org.eclipse.cdt.debug.ui.isReverseDebuggingEnabled"); //$NON-NLS-1$
			    }
			    // Refresh reverse toggle commands with the new state of reverse enabled. 
			    // This is in order to keep multiple toggle actions in UI in sync.
			    ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
		        if (commandService != null) {
		           commandService.refreshElements(REVERSE_TOGGLE_COMMAND_ID, null);
		        }
		        
				return Status.OK_STATUS;
			}
		}.schedule();
    }

    @Override
	public void updateElement(UIElement element,
                              @SuppressWarnings("rawtypes") Map parameters) {
       // Make sure the toggle state reflects the actual state
       // We must check this, in case we have multiple launches
       // or if we re-launch (restart)
       if (fTargetAdapter != null && fTargetAdapter.toggleNeedsUpdating()){
           boolean toggled = fTargetAdapter.isReverseToggled(fActiveContext);
           element.setChecked(toggled);
       }
   }
}

Back to the top