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 297899
Collapse All | Expand All

(-)ui/org/eclipse/debug/ui/actions/DebugCommandAction.java (-343 / +346 lines)
Lines 1-343 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2006, 2009 IBM Corporation and others.
2
 * Copyright (c) 2006, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
10
 *******************************************************************************/
11
11
12
package org.eclipse.debug.ui.actions;
12
package org.eclipse.debug.ui.actions;
13
13
14
import org.eclipse.debug.core.IRequest;
14
import org.eclipse.debug.core.IRequest;
15
import org.eclipse.debug.internal.ui.commands.actions.DebugCommandService;
15
import org.eclipse.debug.internal.ui.commands.actions.DebugCommandService;
16
import org.eclipse.debug.internal.ui.commands.actions.ICommandParticipant;
16
import org.eclipse.debug.internal.ui.commands.actions.ICommandParticipant;
17
import org.eclipse.debug.internal.ui.commands.actions.IEnabledTarget;
17
import org.eclipse.debug.internal.ui.commands.actions.IEnabledTarget;
18
import org.eclipse.debug.ui.DebugUITools;
18
import org.eclipse.debug.ui.DebugUITools;
19
import org.eclipse.debug.ui.contexts.DebugContextEvent;
19
import org.eclipse.debug.ui.contexts.DebugContextEvent;
20
import org.eclipse.debug.ui.contexts.IDebugContextListener;
20
import org.eclipse.debug.ui.contexts.IDebugContextListener;
21
import org.eclipse.debug.ui.contexts.IDebugContextService;
21
import org.eclipse.debug.ui.contexts.IDebugContextService;
22
import org.eclipse.jface.action.Action;
22
import org.eclipse.jface.action.Action;
23
import org.eclipse.jface.action.IAction;
23
import org.eclipse.jface.action.IAction;
24
import org.eclipse.jface.resource.ImageDescriptor;
24
import org.eclipse.jface.resource.ImageDescriptor;
25
import org.eclipse.jface.viewers.ISelection;
25
import org.eclipse.jface.viewers.ISelection;
26
import org.eclipse.jface.viewers.IStructuredSelection;
26
import org.eclipse.jface.viewers.IStructuredSelection;
27
import org.eclipse.swt.widgets.Event;
27
import org.eclipse.swt.widgets.Event;
28
import org.eclipse.ui.IWorkbenchPart;
28
import org.eclipse.ui.IWorkbenchPart;
29
import org.eclipse.ui.IWorkbenchWindow;
29
import org.eclipse.ui.IWorkbenchWindow;
30
import org.eclipse.ui.PlatformUI;
30
import org.eclipse.ui.PlatformUI;
31
31
32
/**
32
/**
33
 * Abstract base class for re-targeting actions which delegate execution to 
33
 * Abstract base class for re-targeting actions which delegate execution to 
34
 * {@link org.eclipse.debug.core.commands.IDebugCommandHandler} handlers.  
34
 * {@link org.eclipse.debug.core.commands.IDebugCommandHandler} handlers.  
35
 * The specific type of <code>IDebugCommandHandler</code> is determined by the 
35
 * The specific type of <code>IDebugCommandHandler</code> is determined by the 
36
 * abstract {@link #getCommandType()} method.    
36
 * abstract {@link #getCommandType()} method.    
37
 * <p>
37
 * <p>
38
 * This base class is an action which can be instantiated directly by views, 
38
 * This base class is an action which can be instantiated directly by views, 
39
 * etc.  In order to contribute an action using an extension point, a class 
39
 * etc.  In order to contribute an action using an extension point, a class 
40
 * implementing {@link org.eclipse.ui.IActionDelegate} should be created first.
40
 * implementing {@link org.eclipse.ui.IActionDelegate} should be created first.
41
 * The delegate should then use a <code>DebugCommandAction</code> to implement 
41
 * The delegate should then use a <code>DebugCommandAction</code> to implement 
42
 * the needed functionality. The IActionDelegate must use {@link #setActionProxy(IAction)}
42
 * the needed functionality. The IActionDelegate must use {@link #setActionProxy(IAction)}
43
 * specifying the workbench's action that is a proxy to the action delegate. This
43
 * specifying the workbench's action that is a proxy to the action delegate. This
44
 * way, the workbench action can be updated visually as needed.<br>
44
 * way, the workbench action can be updated visually as needed.<br>
45
 * Note: <code>IDebugCommandHandler</code> command typically act on the active
45
 * Note: <code>IDebugCommandHandler</code> command typically act on the active
46
 * debug context as opposed to the active selection in view or window.  The 
46
 * debug context as opposed to the active selection in view or window.  The 
47
 * action delegate should ignore the active window selection, and instead allow 
47
 * action delegate should ignore the active window selection, and instead allow 
48
 * the <code>DebugCommandAction</code> to update itself based on the active 
48
 * the <code>DebugCommandAction</code> to update itself based on the active 
49
 * debug context. 
49
 * debug context. 
50
 * </p>
50
 * </p>
51
 * <p>
51
 * <p>
52
 * Clients may subclass this class.
52
 * Clients may subclass this class.
53
 * </p>
53
 * </p>
54
 * @see org.eclipse.debug.core.commands.IDebugCommandHandler
54
 * @see org.eclipse.debug.core.commands.IDebugCommandHandler
55
 * @since 3.6
55
 * @since 3.6
56
 */
56
 */
57
public abstract class DebugCommandAction extends Action implements IDebugContextListener {
57
public abstract class DebugCommandAction extends Action implements IDebugContextListener {
58
58
59
    private boolean fInitialized = false;
59
    private boolean fInitialized = false;
60
    
60
    
61
	/**
61
	/**
62
	 * The window this action is working for.
62
	 * The window this action is working for.
63
	 */
63
	 */
64
    private IWorkbenchWindow fWindow;
64
    private IWorkbenchWindow fWindow;
65
    
65
    
66
    /**
66
    /**
67
     * The part this action is working for, or <code>null</code> if global to
67
     * The part this action is working for, or <code>null</code> if global to
68
     * a window.
68
     * a window.
69
     */
69
     */
70
    private IWorkbenchPart fPart;
70
    private IWorkbenchPart fPart;
71
    
71
    
72
    /**
72
    /**
73
     * Command service.
73
     * Command service.
74
     */
74
     */
75
    private DebugCommandService fUpdateService;
75
    private DebugCommandService fUpdateService;
76
    
76
    
77
    /**
77
    /**
78
     * Delegate this action is working for or <code>null</code> if none.
78
     * Delegate this action is working for or <code>null</code> if none.
79
     */
79
     */
80
    private IAction fAction;
80
    private IAction fAction;
81
81
82
    private IEnabledTarget fEnabledTarget = new IEnabledTarget() {
82
    private IEnabledTarget fEnabledTarget = new IEnabledTarget() {
83
        public void setEnabled(boolean enabled) {
83
        public void setEnabled(boolean enabled) {
84
            DebugCommandAction.this.setEnabled(enabled);
84
            DebugCommandAction.this.setEnabled(enabled);
85
        }
85
        }
86
    };
86
    };
87
    
87
    
88
    /**
88
    /**
89
     * Constructor
89
     * Constructor
90
     */
90
     */
91
    public DebugCommandAction() {
91
    public DebugCommandAction() {
92
        super();
92
        super();
93
        String helpContextId = getHelpContextId();
93
        String helpContextId = getHelpContextId();
94
        if (helpContextId != null)
94
        if (helpContextId != null)
95
            PlatformUI.getWorkbench().getHelpSystem().setHelp(this, helpContextId);
95
            PlatformUI.getWorkbench().getHelpSystem().setHelp(this, helpContextId);
96
        setEnabled(false);
96
        setEnabled(false);
97
    }
97
    }
98
98
99
	/**
99
	/**
100
     * Sets the current workbench action that is a proxy to an {@link org.eclipse.ui.IActionDelegate}
100
     * Sets the current workbench action that is a proxy to an {@link org.eclipse.ui.IActionDelegate}
101
     * that is using this action to perform its actual work. This only needs to be called when
101
     * that is using this action to perform its actual work. This only needs to be called when
102
     * an {@link org.eclipse.ui.IActionDelegate} is using one of these actions to perform its
102
     * an {@link org.eclipse.ui.IActionDelegate} is using one of these actions to perform its
103
     * function.
103
     * function.
104
     * 
104
     * 
105
     * @param action workbench proxy action
105
     * @param action workbench proxy action
106
     */
106
     */
107
    public void setActionProxy(IAction action) {
107
    public void setActionProxy(IAction action) {
108
        fAction = action;
108
        fAction = action;
109
        fAction.setEnabled(isEnabled());
109
        fAction.setEnabled(isEnabled());
110
    }
110
    }
111
111
112
    /**
112
    /**
113
     * Executes this action on the given target object
113
     * Executes this action on the given target object
114
     * 
114
     * 
115
     * @param target the target to perform the action on
115
     * @param target the target to perform the action on
116
     */
116
     */
117
    private boolean execute(final Object[] targets) {
117
    private boolean execute(final Object[] targets) {
118
    	return fUpdateService.executeCommand(
118
    	return fUpdateService.executeCommand(
119
    	    getCommandType(), targets, 
119
    	    getCommandType(), targets, 
120
    	    new ICommandParticipant() {
120
    	    new ICommandParticipant() {
121
    	        public void requestDone(org.eclipse.debug.core.IRequest request) {
121
    	        public void requestDone(org.eclipse.debug.core.IRequest request) {
122
    	            DebugCommandAction.this.postExecute(request, targets);
122
    	            DebugCommandAction.this.postExecute(request, targets);
123
    	        }    	      
123
    	        }    	      
124
    	    });
124
    	    });
125
    }
125
    }
126
        
126
        
127
    /**
127
    /**
128
     * This method is called after the completion of the execution of this 
128
     * This method is called after the completion of the execution of this 
129
     * command.  Extending classes may override this method to perform additional
129
     * command.  Extending classes may override this method to perform additional
130
     * operation after command execution. 
130
     * operation after command execution. 
131
     * 
131
     * 
132
     * @param request The completed request object which was given to the 
132
     * @param request The completed request object which was given to the 
133
     * debug command handler.
133
     * debug command handler.
134
     * @param targets Objects which were the targets of this action
134
     * @param targets Objects which were the targets of this action
135
     */
135
     */
136
    protected void postExecute(IRequest request, Object[] targets) {
136
    protected void postExecute(IRequest request, Object[] targets) {
137
        // do nothing by default
137
        // do nothing by default
138
    }
138
    }
139
    
139
    
140
    /**
140
    /**
141
     * Returns the {@link org.eclipse.debug.core.commands.IDebugCommandHandler} 
141
     * Returns the {@link org.eclipse.debug.core.commands.IDebugCommandHandler} 
142
     * command handler that type this action executes.
142
     * command handler that type this action executes.
143
     * 
143
     * 
144
     * @return command class.
144
     * @return command class.
145
     * 
145
     * 
146
     * @see org.eclipse.debug.core.commands.IDebugCommandHandler
146
     * @see org.eclipse.debug.core.commands.IDebugCommandHandler
147
     */
147
     */
148
    abstract protected Class getCommandType();  
148
    abstract protected Class getCommandType();  
149
149
150
    /**
150
    /**
151
     * @see org.eclipse.debug.ui.contexts.IDebugContextListener#debugContextChanged(org.eclipse.debug.ui.contexts.DebugContextEvent)
151
     * @see org.eclipse.debug.ui.contexts.IDebugContextListener#debugContextChanged(org.eclipse.debug.ui.contexts.DebugContextEvent)
152
     */
152
     */
153
    public void debugContextChanged(DebugContextEvent event) {
153
    public void debugContextChanged(DebugContextEvent event) {
154
    	fUpdateService.postUpdateCommand(getCommandType(), fEnabledTarget);
154
    	fUpdateService.postUpdateCommand(getCommandType(), fEnabledTarget);
155
	}
155
	}
156
156
157
    /**
157
    /**
158
     * @see org.eclipse.jface.action.Action#setEnabled(boolean)
158
     * @see org.eclipse.jface.action.Action#setEnabled(boolean)
159
     */
159
     */
160
    public void setEnabled(boolean enabled) {
160
    public void setEnabled(boolean enabled) {
161
        synchronized (this) {
161
        synchronized (this) {
162
            if (!fInitialized) {
162
            if (!fInitialized) {
163
                fInitialized = true;
163
                fInitialized = true;
164
                notifyAll();
164
                notifyAll();
165
            }
165
            }
166
        }        
166
        }        
167
        super.setEnabled(enabled);
167
        super.setEnabled(enabled);
168
        if (fAction != null) {
168
        if (fAction != null) {
169
            fAction.setEnabled(enabled);
169
            fAction.setEnabled(enabled);
170
        }
170
        }
171
    }
171
    }
172
172
173
    /**
173
    /**
174
     * Initializes this action for a specific part.
174
     * Initializes this action for a specific part.
175
     * 
175
     * 
176
     * @param part workbench part
176
     * @param part workbench part
177
     */
177
     */
178
    public void init(IWorkbenchPart part) {
178
    public void init(IWorkbenchPart part) {
179
        fPart = part;
179
    	fInitialized = false;
180
        fWindow = part.getSite().getWorkbenchWindow();
180
        fPart = part;
181
        fUpdateService = DebugCommandService.getService(fWindow);
181
        fWindow = part.getSite().getWorkbenchWindow();
182
        IDebugContextService service = getDebugContextService();
182
        fUpdateService = DebugCommandService.getService(fWindow);
183
		String partId = part.getSite().getId();
183
        IDebugContextService service = getDebugContextService();
184
		service.addDebugContextListener(this, partId);
184
		String partId = part.getSite().getId();
185
        ISelection activeContext = service.getActiveContext(partId);
185
		service.addDebugContextListener(this, partId);
186
        if (activeContext != null) {
186
        ISelection activeContext = service.getActiveContext(partId);
187
        	fUpdateService.updateCommand(getCommandType(), fEnabledTarget);
187
        if (activeContext != null) {
188
        } else {
188
        	fUpdateService.updateCommand(getCommandType(), fEnabledTarget);
189
        	setEnabled(getInitialEnablement());
189
        } else {
190
        }
190
        	setEnabled(getInitialEnablement());
191
    }
191
        }
192
    
192
    }
193
    /**
193
    
194
     * Initializes the context action
194
    /**
195
     * @param window the window
195
     * Initializes this action for a workbench window.
196
     */
196
     * 
197
    public void init(IWorkbenchWindow window) {
197
     * @param window the window
198
        fWindow = window;
198
     */
199
        fUpdateService = DebugCommandService.getService(fWindow);
199
    public void init(IWorkbenchWindow window) {
200
        IDebugContextService contextService = getDebugContextService();
200
    	fInitialized = false;
201
		contextService.addDebugContextListener(this);
201
        fWindow = window;
202
        ISelection activeContext = contextService.getActiveContext();
202
        fUpdateService = DebugCommandService.getService(fWindow);
203
        if (activeContext != null) {
203
        IDebugContextService contextService = getDebugContextService();
204
        	fUpdateService.updateCommand(getCommandType(), fEnabledTarget);
204
		contextService.addDebugContextListener(this);
205
        } else {
205
        ISelection activeContext = contextService.getActiveContext();
206
        	setEnabled(getInitialEnablement());
206
        if (activeContext != null) {
207
        }
207
        	fUpdateService.updateCommand(getCommandType(), fEnabledTarget);
208
    }
208
        } else {
209
    
209
        	setEnabled(getInitialEnablement());
210
    /**
210
        }
211
     * Returns whether this action should be enabled when initialized
211
    }
212
     * and there is no active debug context. By default, <code>false</code>
212
    
213
     * is returned.
213
    /**
214
     * 
214
     * Returns whether this action should be enabled when initialized
215
     * @return initial enabled state when there is no active context.
215
     * and there is no active debug context. By default, <code>false</code>
216
     */
216
     * is returned.
217
    protected boolean getInitialEnablement() {
217
     * 
218
    	return false;
218
     * @return initial enabled state when there is no active context.
219
    }
219
     */
220
220
    protected boolean getInitialEnablement() {
221
    /**
221
    	return false;
222
     * Returns the context this action operates on as a selection.
222
    }
223
     * 
223
224
     * @return the context this action operates on
224
    /**
225
     */
225
     * Returns the context this action operates on as a selection.
226
    private ISelection getContext() {
226
     * 
227
		if (fPart != null) {
227
     * @return the context this action operates on
228
			getDebugContextService().getActiveContext(fPart.getSite().getId());
228
     */
229
    	}
229
    private ISelection getContext() {
230
        return getDebugContextService().getActiveContext();
230
		if (fPart != null) {
231
    }
231
			getDebugContextService().getActiveContext(fPart.getSite().getId());
232
232
    	}
233
    /*
233
        return getDebugContextService().getActiveContext();
234
     * (non-Javadoc)
234
    }
235
     * @see org.eclipse.jface.action.Action#run()
235
236
     */
236
    /*
237
    public void run() {
237
     * (non-Javadoc)
238
        synchronized (this) {
238
     * @see org.eclipse.jface.action.Action#run()
239
            if (!fInitialized) {
239
     */
240
                try {
240
    public void run() {
241
                    wait();
241
        synchronized (this) {
242
                } catch (InterruptedException e) {
242
            if (!fInitialized) {
243
                }
243
                try {
244
            }           
244
                    wait();
245
        }        
245
                } catch (InterruptedException e) {
246
        
246
                }
247
        ISelection selection = getContext();
247
            }           
248
        if (selection instanceof IStructuredSelection && isEnabled()) {
248
        }        
249
            IStructuredSelection ss = (IStructuredSelection) selection;
249
        
250
            boolean enabled = execute(ss.toArray());
250
        ISelection selection = getContext();
251
            // disable the action according to the command
251
        if (selection instanceof IStructuredSelection && isEnabled()) {
252
            setEnabled(enabled);
252
            IStructuredSelection ss = (IStructuredSelection) selection;
253
        }
253
            boolean enabled = execute(ss.toArray());
254
    }
254
            // disable the action according to the command
255
255
            setEnabled(enabled);
256
    /*
256
        }
257
     * (non-Javadoc)
257
    }
258
     * @see org.eclipse.jface.action.Action#runWithEvent(org.eclipse.swt.widgets.Event)
258
259
     */
259
    /*
260
    public void runWithEvent(Event event) {
260
     * (non-Javadoc)
261
        run();
261
     * @see org.eclipse.jface.action.Action#runWithEvent(org.eclipse.swt.widgets.Event)
262
    }
262
     */
263
263
    public void runWithEvent(Event event) {
264
    /**
264
        run();
265
     * Clean up when removing
265
    }
266
     */
266
267
    public void dispose() {
267
    /**
268
        IDebugContextService service = getDebugContextService();
268
     * Clean up when removing
269
        if (fPart != null) {
269
     */
270
        	service.removeDebugContextListener(this, fPart.getSite().getId());
270
    public void dispose() {
271
        } else {
271
        IDebugContextService service = getDebugContextService();
272
            service.removeDebugContextListener(this);
272
        if (fPart != null) {
273
        }
273
        	service.removeDebugContextListener(this, fPart.getSite().getId());
274
        fWindow = null;
274
        } else {
275
        fPart = null;
275
            service.removeDebugContextListener(this);
276
    }
276
        }
277
    
277
        fWindow = null;
278
    /**
278
        fPart = null;
279
     * Returns the context service this action linked to. By default, this actions is
279
    }
280
     * associated with the context service for the window this action is operating in.
280
    
281
     * 
281
    /**
282
     * @return associated context service
282
     * Returns the context service this action linked to. By default, this actions is
283
     */
283
     * associated with the context service for the window this action is operating in.
284
    protected IDebugContextService getDebugContextService() {
284
     * 
285
    	return DebugUITools.getDebugContextManager().getContextService(fWindow);
285
     * @return associated context service
286
    }
286
     */
287
287
    protected IDebugContextService getDebugContextService() {
288
    /**
288
    	return DebugUITools.getDebugContextManager().getContextService(fWindow);
289
     * Returns the help context id for this action or <code>null</code> if none.
289
    }
290
     * 
290
291
     * @return The help context id for this action or <code>null</code>
291
    /**
292
     */ 
292
     * Returns the help context id for this action or <code>null</code> if none.
293
    public abstract String getHelpContextId();
293
     * 
294
294
     * @return The help context id for this action or <code>null</code>
295
    /*
295
     */ 
296
     * (non-Javadoc)
296
    public abstract String getHelpContextId();
297
     * @see org.eclipse.jface.action.Action#getId()
297
298
     */
298
    /*
299
    public abstract String getId();
299
     * (non-Javadoc)
300
300
     * @see org.eclipse.jface.action.Action#getId()
301
    /*
301
     */
302
     * (non-Javadoc)
302
    public abstract String getId();
303
     * @see org.eclipse.jface.action.Action#getText()
303
304
     */
304
    /*
305
    public abstract String getText();
305
     * (non-Javadoc)
306
306
     * @see org.eclipse.jface.action.Action#getText()
307
    /*
307
     */
308
     * (non-Javadoc)
308
    public abstract String getText();
309
     * @see org.eclipse.jface.action.Action#getToolTipText()
309
310
     */
310
    /*
311
    public abstract String getToolTipText();
311
     * (non-Javadoc)
312
312
     * @see org.eclipse.jface.action.Action#getToolTipText()
313
    /*
313
     */
314
     * (non-Javadoc)
314
    public abstract String getToolTipText();
315
     * @see org.eclipse.jface.action.Action#getDisabledImageDescriptor()
315
316
     */
316
    /*
317
    public abstract ImageDescriptor getDisabledImageDescriptor();
317
     * (non-Javadoc)
318
318
     * @see org.eclipse.jface.action.Action#getDisabledImageDescriptor()
319
    /*
319
     */
320
     * (non-Javadoc)
320
    public abstract ImageDescriptor getDisabledImageDescriptor();
321
     * @see org.eclipse.jface.action.Action#getHoverImageDescriptor()
321
322
     */
322
    /*
323
    public abstract ImageDescriptor getHoverImageDescriptor();
323
     * (non-Javadoc)
324
324
     * @see org.eclipse.jface.action.Action#getHoverImageDescriptor()
325
    /*
325
     */
326
     * (non-Javadoc)
326
    public abstract ImageDescriptor getHoverImageDescriptor();
327
     * @see org.eclipse.jface.action.Action#getImageDescriptor()
327
328
     */
328
    /*
329
    public abstract ImageDescriptor getImageDescriptor();
329
     * (non-Javadoc)
330
    
330
     * @see org.eclipse.jface.action.Action#getImageDescriptor()
331
    /**
331
     */
332
     * Returns the workbench proxy associated with this action or <code>null</code>
332
    public abstract ImageDescriptor getImageDescriptor();
333
     * if none. This is the workbench proxy to an {@link org.eclipse.ui.IActionDelegate}
333
    
334
     * that is using this action to perform its actual work. This is only used when
334
    /**
335
     * an {@link org.eclipse.ui.IActionDelegate} is using one of these actions to perform its
335
     * Returns the workbench proxy associated with this action or <code>null</code>
336
     * function.
336
     * if none. This is the workbench proxy to an {@link org.eclipse.ui.IActionDelegate}
337
     * 
337
     * that is using this action to perform its actual work. This is only used when
338
     * @return workbench proxy action or <code>null</code>
338
     * an {@link org.eclipse.ui.IActionDelegate} is using one of these actions to perform its
339
     */
339
     * function.
340
    protected IAction getActionProxy() {
340
     * 
341
    	return fAction;
341
     * @return workbench proxy action or <code>null</code>
342
    }
342
     */
343
}
343
    protected IAction getActionProxy() {
344
    	return fAction;
345
    }
346
}

Return to bug 297899