Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 284363 | Differences between
and this patch

Collapse All | Expand All

(-)ui/org/eclipse/debug/ui/actions/DebugCommandHandler.java (+249 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006, 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Wind River Systems - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.debug.ui.actions;
13
14
import java.util.Iterator;
15
import java.util.Map;
16
import java.util.WeakHashMap;
17
18
import org.eclipse.core.commands.AbstractHandler;
19
import org.eclipse.core.commands.ExecutionEvent;
20
import org.eclipse.core.commands.ExecutionException;
21
import org.eclipse.core.commands.HandlerEvent;
22
import org.eclipse.core.expressions.IEvaluationContext;
23
import org.eclipse.debug.internal.ui.commands.actions.DebugCommandService;
24
import org.eclipse.debug.internal.ui.commands.actions.IEnabledTarget;
25
import org.eclipse.debug.ui.DebugUITools;
26
import org.eclipse.debug.ui.contexts.DebugContextEvent;
27
import org.eclipse.debug.ui.contexts.IDebugContextListener;
28
import org.eclipse.debug.ui.contexts.IDebugContextService;
29
import org.eclipse.jface.viewers.ISelection;
30
import org.eclipse.jface.viewers.IStructuredSelection;
31
import org.eclipse.ui.ISources;
32
import org.eclipse.ui.IWindowListener;
33
import org.eclipse.ui.IWorkbenchWindow;
34
import org.eclipse.ui.PlatformUI;
35
import org.eclipse.ui.handlers.HandlerUtil;
36
37
/**
38
 * Abstract base class for re-targeting command framework handlers, which 
39
 * delegate execution to {@link org.eclipse.debug.core.commands.IDebugCommandHandler} 
40
 * handlers. The specific type of <code>IDebugCommandHandler</code> is 
41
 * determined by the abstract {@link #getCommandType()} method.    
42
 * 
43
 * <p> Note: This class is not an implementation of the <code>IDebugCommandHandler</code>
44
 * interface, which was somewhat unfortunately named.  <code>IDebugCommandHandler</code> 
45
 * is an interface that is internal to the debugger plugins.  This class implements 
46
 * {@link org.eclipse.core.commands.IHandler} interface and is to be used with the 
47
 * platform commands framework. </p>
48
 * 
49
 * @see org.eclipse.debug.core.commands.IDebugCommandHandler
50
 * @see org.eclipse.core.commands.IHandler
51
 *
52
 * @since 3.6
53
 */
54
public abstract class DebugCommandHandler extends AbstractHandler {
55
56
    private class EnabledTarget implements IEnabledTarget, IDebugContextListener, IWindowListener {
57
        boolean fEnabled = getInitialEnablement();
58
        IWorkbenchWindow fWindow;
59
        
60
        EnabledTarget(IWorkbenchWindow window) {
61
            fWindow = window;
62
            DebugCommandService.getService(fWindow).updateCommand(getCommandType(), this);
63
            getContextService(fWindow).addDebugContextListener(this);
64
            PlatformUI.getWorkbench().addWindowListener(this);
65
        }
66
        
67
        public void setEnabled(boolean enabled) {
68
            boolean oldEnabled = fEnabled;
69
            fEnabled = enabled;
70
            if (fEnabled != oldEnabled) {
71
                fireHandlerChanged(new HandlerEvent(DebugCommandHandler.this, true, false));
72
            }
73
        }
74
        
75
        public void debugContextChanged(DebugContextEvent event) {
76
            DebugCommandService.getService(fWindow).postUpdateCommand(getCommandType(), this);
77
        }
78
        
79
        public void windowOpened(IWorkbenchWindow w) {
80
        }
81
    
82
        public void windowDeactivated(IWorkbenchWindow w) {
83
        }
84
    
85
        public void windowClosed(IWorkbenchWindow w) {
86
            if (w.equals(fWindow)) {
87
                dispose();
88
            }
89
        }
90
    
91
        public void windowActivated(IWorkbenchWindow w) {
92
        }
93
        
94
        void dispose() {
95
            if (isDisposed()) {
96
                return;
97
            }
98
            PlatformUI.getWorkbench().removeWindowListener(this);
99
            getContextService(fWindow).removeDebugContextListener(this);
100
            fWindow = null;
101
        }
102
        
103
        boolean isDisposed() {
104
            return fWindow == null;
105
        }
106
    }
107
108
    private Map fEnabledTargetsMap = new WeakHashMap();
109
110
    private EnabledTarget fCurrentEnabledTarget = null;
111
    
112
    /**
113
     * Constructor
114
     */
115
    public DebugCommandHandler() {
116
        super();
117
        PlatformUI.getWorkbench().addWindowListener(new IWindowListener() {
118
            
119
            public void windowOpened(IWorkbenchWindow w) {
120
            }
121
        
122
            public void windowDeactivated(IWorkbenchWindow w) {
123
            }
124
        
125
            public void windowClosed(IWorkbenchWindow w) {
126
                EnabledTarget enabledTarget = (EnabledTarget)fEnabledTargetsMap.get(w);
127
                if (enabledTarget != null) {
128
                    enabledTarget.dispose();
129
                }
130
            }
131
        
132
            public void windowActivated(IWorkbenchWindow w) {
133
            }
134
        });
135
    }
136
    
137
    public void setEnabled(Object evaluationContext) {
138
        fCurrentEnabledTarget = null;
139
        
140
        if (!(evaluationContext instanceof IEvaluationContext)) {
141
            return;
142
        }
143
        IEvaluationContext context = (IEvaluationContext) evaluationContext;
144
        Object _window = context.getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
145
        if (_window instanceof IWorkbenchWindow) {
146
            IWorkbenchWindow window = (IWorkbenchWindow)_window;
147
            fCurrentEnabledTarget = getEnabledTarget(window);
148
        }
149
    }
150
    
151
    public boolean isEnabled() {
152
        if (fCurrentEnabledTarget == null) {
153
            return false;
154
        }
155
        return fCurrentEnabledTarget.fEnabled;
156
    }
157
    
158
    private EnabledTarget getEnabledTarget(IWorkbenchWindow window) {
159
        EnabledTarget target = (EnabledTarget)fEnabledTargetsMap.get(window);
160
        if (target == null) {
161
            target = new EnabledTarget(window);
162
            DebugCommandService service = DebugCommandService.getService(window);
163
            service.updateCommand(getCommandType(), target);
164
            
165
            DebugUITools.getDebugContextManager().getContextService(window).addDebugContextListener(target);
166
            
167
            fEnabledTargetsMap.put(window, target);
168
        }
169
        return target;
170
    }
171
    
172
    public Object execute(ExecutionEvent event) throws ExecutionException {
173
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
174
        if (window == null) {
175
            throw new ExecutionException("No active workbench window.");
176
        }
177
        fCurrentEnabledTarget = getEnabledTarget(window);
178
179
        ISelection selection = getContextService(window).getActiveContext();
180
        if (selection instanceof IStructuredSelection && isEnabled()) {
181
            IStructuredSelection ss = (IStructuredSelection) selection;
182
            boolean enabledAfterExecute = execute(window, ss.toArray());
183
            
184
            // enable/disable the action according to the command
185
            fCurrentEnabledTarget.setEnabled(enabledAfterExecute);
186
        }
187
188
        return null;
189
    }
190
    
191
    private IDebugContextService getContextService(IWorkbenchWindow window) {
192
        return DebugUITools.getDebugContextManager().getContextService(window);
193
    }
194
    
195
    /**
196
     * Executes this action on the given target object
197
     * 
198
     * @param target the target to perform the action on
199
     */
200
    private boolean execute(IWorkbenchWindow window, Object[] targets) {
201
        DebugCommandService service = DebugCommandService.getService(window); 
202
    	return service.executeCommand(getCommandType(), targets, getCommandParticipant(targets));
203
    }
204
    
205
    /**
206
     * Creates and returns the command participant or <code>null</code>.
207
     * 
208
     * @return command participant to use on command completion
209
     */
210
    protected ICommandParticipant getCommandParticipant(Object[] targets) {
211
    	return null;
212
    }
213
    
214
    /**
215
     * Returns the {@link org.eclipse.debug.core.commands.IDebugCommandHandler} 
216
     * command handler that type this action executes.
217
     * 
218
     * @return command class.
219
     * 
220
     * @see org.eclipse.debug.core.commands.IDebugCommandHandler
221
     */
222
    abstract protected Class getCommandType();  
223
224
    
225
    /**
226
     * Returns whether this action should be enabled when initialized
227
     * and there is no active debug context.
228
     * 
229
     * @return false, by default
230
     */
231
    protected boolean getInitialEnablement() {
232
    	return false;
233
    }
234
235
236
    /**
237
     * Clean up when removing
238
     */
239
    public void dispose() {
240
        for (Iterator itr = fEnabledTargetsMap.values().iterator(); itr.hasNext();) {
241
            EnabledTarget target = (EnabledTarget)itr.next();
242
            if (!target.isDisposed()) {
243
                target.dispose();
244
            }
245
        }
246
        fEnabledTargetsMap.clear();
247
        fCurrentEnabledTarget = null;
248
    }
249
}
(-)plugin.xml (+25 lines)
Lines 216-221 Link Here
216
         </adapter>
216
         </adapter>
217
      </factory>
217
      </factory>
218
-->
218
-->
219
      <factory
220
            adaptableType="org.eclipse.debug.examples.core.pda.model.PDAStackFrame"
221
            class="org.eclipse.debug.examples.ui.pda.commands.CommandAdapterFactory">
222
         <adapter
223
               type="org.eclipse.debug.examples.ui.pda.commands.PopFrameDebugCommand">
224
         </adapter>
225
      </factory>
219
   </extension>
226
   </extension>
220
   
227
   
221
   <extension
228
   <extension
Lines 335-340 Link Here
335
            id="org.eclipse.debug.examples.ui.popCommand"
342
            id="org.eclipse.debug.examples.ui.popCommand"
336
            name="Pop">
343
            name="Pop">
337
      </command>
344
      </command>
345
      <command
346
            id="org.eclipse.debug.examples.ui.popFrameCommand"
347
            name="Pop Frame">
348
      </command>
338
   </extension>
349
   </extension>
339
   <extension
350
   <extension
340
         point="org.eclipse.ui.handlers">
351
         point="org.eclipse.ui.handlers">
Lines 380-385 Link Here
380
            </and>
391
            </and>
381
         </enabledWhen>
392
         </enabledWhen>
382
      </handler>
393
      </handler>
394
      <handler
395
            class="org.eclipse.debug.examples.ui.pda.commands.PopFrameHandler"
396
            commandId="org.eclipse.debug.examples.ui.popFrameCommand">
397
      </handler>
383
   </extension>
398
   </extension>
384
   <extension
399
   <extension
385
         point="org.eclipse.core.expressions.propertyTesters">
400
         point="org.eclipse.core.expressions.propertyTesters">
Lines 427-431 Link Here
427
               style="push">
442
               style="push">
428
         </command>
443
         </command>
429
      </menuContribution>
444
      </menuContribution>
445
      <menuContribution
446
            locationURI="toolbar:org.eclipse.debug.ui.DebugView?after=additions">      
447
         <command
448
               commandId="org.eclipse.debug.examples.ui.popFrameCommand"
449
               disabledIcon="icons/full/dlcl16/pop.gif"
450
               icon="icons/full/elcl16/pop.gif"
451
               label="Pop Frame"
452
               style="push">
453
         </command>
454
      </menuContribution>      
430
   </extension>
455
   </extension>
431
</plugin>
456
</plugin>
(-)src/org/eclipse/debug/examples/ui/pda/commands/PopFrameHandler.java (+23 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Wind River Systems and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     Wind River Systems - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.debug.examples.ui.pda.commands;
12
13
import org.eclipse.debug.ui.actions.DebugCommandHandler;
14
15
/**
16
 * 
17
 */
18
public class PopFrameHandler extends DebugCommandHandler {
19
20
    protected Class getCommandType() {
21
        return PopFrameDebugCommand.class;
22
    }
23
}
(-)src/org/eclipse/debug/examples/ui/pda/commands/CommandAdapterFactory.java (+40 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006, 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *     Bjorn Freeman-Benson - initial API and implementation
11
 *******************************************************************************/
12
package org.eclipse.debug.examples.ui.pda.commands;
13
14
import org.eclipse.core.runtime.IAdapterFactory;
15
import org.eclipse.debug.examples.core.pda.model.PDAStackFrame;
16
17
18
/**
19
 * 
20
 * @since 3.6
21
 *
22
 */
23
public class CommandAdapterFactory implements IAdapterFactory {
24
	
25
	private static PopFrameDebugCommand fgPopFrameDebugCommand = new PopFrameDebugCommand();
26
27
	public Object getAdapter(Object adaptableObject, Class adapterType) {
28
		if (PopFrameDebugCommand.class.equals(adapterType)) {
29
			if (adaptableObject instanceof PDAStackFrame) {
30
				return fgPopFrameDebugCommand;
31
			}
32
		}
33
		return null;
34
	}
35
36
	public Class[] getAdapterList() {
37
		return new Class[]{PopFrameDebugCommand.class};
38
	}
39
40
}
(-)src/org/eclipse/debug/examples/ui/pda/commands/PopFrameDebugCommand.java (+56 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Wind River Systems and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     Wind River Systems - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.debug.examples.ui.pda.commands;
12
13
import org.eclipse.core.runtime.CoreException;
14
import org.eclipse.core.runtime.IProgressMonitor;
15
import org.eclipse.debug.core.IRequest;
16
import org.eclipse.debug.core.commands.AbstractDebugCommand;
17
import org.eclipse.debug.core.commands.IEnabledStateRequest;
18
import org.eclipse.debug.examples.core.pda.model.PDAStackFrame;
19
import org.eclipse.debug.examples.core.pda.model.PDAThread;
20
21
/**
22
 * 
23
 */
24
public class PopFrameDebugCommand extends AbstractDebugCommand {
25
26
    protected void doExecute(Object[] targets, IProgressMonitor monitor, IRequest request) throws CoreException {
27
        for (int i = 0; i< targets.length; i++) { 
28
            PDAStackFrame frame = (PDAStackFrame)targets[i]; 
29
            ((PDAThread)frame.getThread()).popFrame();
30
        }
31
    }
32
33
    protected Object getTarget(Object element) {
34
        if (element instanceof PDAStackFrame) {
35
            return element;
36
        }
37
        return null;
38
    }
39
40
    protected Object getUpdateJobFamily() {
41
        return PopFrameDebugCommand.class;
42
    }
43
44
    protected boolean isExecutable(Object[] targets, IProgressMonitor monitor, IEnabledStateRequest request)
45
        throws CoreException 
46
    {
47
        boolean enabled = true;
48
        for (int i = 0; i< targets.length; i++) {
49
            PDAStackFrame frame = (PDAStackFrame)targets[i]; 
50
            PDAThread thread = (PDAThread)frame.getThread();
51
            enabled = enabled && thread.canPopFrame() && thread.getTopStackFrame().equals(frame);
52
        }
53
        return enabled;
54
    }
55
56
}

Return to bug 284363