Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c396d5c4b96b380d913978db5ae281108b3fd1e9 (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
/*******************************************************************************
 * Copyright (c) 2006, 2013 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.debug.ui.actions;

import org.eclipse.debug.core.IRequest;
import org.eclipse.debug.internal.ui.commands.actions.DebugCommandService;
import org.eclipse.debug.internal.ui.commands.actions.ICommandParticipant;
import org.eclipse.debug.internal.ui.commands.actions.IEnabledTarget;
import org.eclipse.debug.ui.DebugUITools;
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.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

/**
 * Abstract base class for re-targeting actions which delegate execution to
 * {@link org.eclipse.debug.core.commands.IDebugCommandHandler} handlers.
 * The specific type of <code>IDebugCommandHandler</code> is determined by the
 * abstract {@link #getCommandType()} method.
 * <p>
 * This base class is an action which can be instantiated directly by views,
 * etc.  In order to contribute an action using an extension point, a class
 * implementing {@link org.eclipse.ui.IActionDelegate} should be created first.
 * The delegate should then use a <code>DebugCommandAction</code> to implement
 * the needed functionality. The IActionDelegate must use {@link #setActionProxy(IAction)}
 * specifying the workbench's action that is a proxy to the action delegate. This
 * way, the workbench action can be updated visually as needed.<br>
 * Note: <code>IDebugCommandHandler</code> command typically act on the active
 * debug context as opposed to the active selection in view or window.  The
 * action delegate should ignore the active window selection, and instead allow
 * the <code>DebugCommandAction</code> to update itself based on the active
 * debug context.
 * </p>
 * <p>
 * Clients may subclass this class.
 * </p>
 * @see org.eclipse.debug.core.commands.IDebugCommandHandler
 * @since 3.6
 */
public abstract class DebugCommandAction extends Action implements IDebugContextListener {

    private boolean fInitialized = false;

	/**
	 * The window this action is working for.
	 */
    private IWorkbenchWindow fWindow;

    /**
     * The part this action is working for, or <code>null</code> if global to
     * a window.
     */
    private IWorkbenchPart fPart;

    /**
     * Command service.
     */
    private DebugCommandService fUpdateService;

    /**
     * Delegate this action is working for or <code>null</code> if none.
     */
    private IAction fAction;

    private IEnabledTarget fEnabledTarget = new IEnabledTarget() {
        @Override
		public void setEnabled(boolean enabled) {
            DebugCommandAction.this.setEnabled(enabled);
        }
    };

    /**
     * Constructor
     */
    public DebugCommandAction() {
        super();
        String helpContextId = getHelpContextId();
        if (helpContextId != null) {
			PlatformUI.getWorkbench().getHelpSystem().setHelp(this, helpContextId);
		}
        setEnabled(false);
    }

	/**
     * Sets the current workbench action that is a proxy to an {@link org.eclipse.ui.IActionDelegate}
     * that is using this action to perform its actual work. This only needs to be called when
     * an {@link org.eclipse.ui.IActionDelegate} is using one of these actions to perform its
     * function.
     *
     * @param action workbench proxy action
     */
    public void setActionProxy(IAction action) {
        fAction = action;
        fAction.setEnabled(isEnabled());
    }

    /**
     * Executes this action on the given target object
     * @param targets the targets to perform the action on
     * @return if the command stays enabled while the command executes
     */
    private boolean execute(final Object[] targets) {
    	return fUpdateService.executeCommand(
    	    getCommandType(), targets,
    	    new ICommandParticipant() {
    	        @Override
				public void requestDone(org.eclipse.debug.core.IRequest request) {
    	            DebugCommandAction.this.postExecute(request, targets);
    	        }
    	    });
    }

    /**
     * This method is called after the completion of the execution of this
     * command.  Extending classes may override this method to perform additional
     * operation after command execution.
     *
     * @param request The completed request object which was given to the
     * debug command handler.
     * @param targets Objects which were the targets of this action
     */
    protected void postExecute(IRequest request, Object[] targets) {
        // do nothing by default
    }

    /**
     * Returns the {@link org.eclipse.debug.core.commands.IDebugCommandHandler}
     * command handler that type this action executes.
     *
     * @return command class.
     *
     * @see org.eclipse.debug.core.commands.IDebugCommandHandler
     */
	abstract protected Class<?> getCommandType();

    /**
     * @see org.eclipse.debug.ui.contexts.IDebugContextListener#debugContextChanged(org.eclipse.debug.ui.contexts.DebugContextEvent)
     */
    @Override
	public void debugContextChanged(DebugContextEvent event) {
    	fUpdateService.postUpdateCommand(getCommandType(), fEnabledTarget);
	}

    /**
     * @see org.eclipse.jface.action.Action#setEnabled(boolean)
     */
    @Override
	public void setEnabled(boolean enabled) {
        synchronized (this) {
            if (!fInitialized) {
                fInitialized = true;
                notifyAll();
            }
        }
        super.setEnabled(enabled);
        if (fAction != null) {
            fAction.setEnabled(enabled);
        }
    }

    /**
     * Initializes this action for a specific part.
     *
     * @param part workbench part
     */
    public void init(IWorkbenchPart part) {
    	fInitialized = false;
        fPart = part;
        fWindow = part.getSite().getWorkbenchWindow();
        fUpdateService = DebugCommandService.getService(fWindow);
        IDebugContextService service = getDebugContextService();
		String partId = part.getSite().getId();
		service.addDebugContextListener(this, partId);
        ISelection activeContext = service.getActiveContext(partId);
        if (activeContext != null) {
        	fUpdateService.updateCommand(getCommandType(), fEnabledTarget);
        } else {
        	setEnabled(getInitialEnablement());
        }
    }

    /**
     * Initializes this action for a workbench window.
     *
     * @param window the window
     */
    public void init(IWorkbenchWindow window) {
    	fInitialized = false;
        fWindow = window;
        fUpdateService = DebugCommandService.getService(fWindow);
        IDebugContextService contextService = getDebugContextService();
		contextService.addDebugContextListener(this);
        ISelection activeContext = contextService.getActiveContext();
        if (activeContext != null) {
        	fUpdateService.updateCommand(getCommandType(), fEnabledTarget);
        } else {
        	setEnabled(getInitialEnablement());
        }
    }

    /**
     * Returns whether this action should be enabled when initialized
     * and there is no active debug context. By default, <code>false</code>
     * is returned.
     *
     * @return initial enabled state when there is no active context.
     */
    protected boolean getInitialEnablement() {
    	return false;
    }

    /**
     * Returns the context (selection) this action operates on. By default
     * the active debug context in this action's associated part or window is used,
     * but subclasses may override as required.
     *
     * @return the context this action operates on
     * @since 3.7
     */
    protected ISelection getContext() {
		if (fPart != null) {
			getDebugContextService().getActiveContext(fPart.getSite().getId());
    	}
        return getDebugContextService().getActiveContext();
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.jface.action.Action#run()
     */
    @Override
	public void run() {
        synchronized (this) {
            if (!fInitialized) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }
        }

        ISelection selection = getContext();
        if (selection instanceof IStructuredSelection && isEnabled()) {
            IStructuredSelection ss = (IStructuredSelection) selection;
            boolean enabled = execute(ss.toArray());
            // disable the action according to the command
            setEnabled(enabled);
        }
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.jface.action.Action#runWithEvent(org.eclipse.swt.widgets.Event)
     */
    @Override
	public void runWithEvent(Event event) {
        run();
    }

    /**
     * Clean up when removing
     */
    public void dispose() {
        IDebugContextService service = getDebugContextService();
        if (fPart != null) {
        	service.removeDebugContextListener(this, fPart.getSite().getId());
        } else {
            service.removeDebugContextListener(this);
        }
        fWindow = null;
        fPart = null;
    }

    /**
     * Returns the context service this action linked to. By default, this actions is
     * associated with the context service for the window this action is operating in.
     *
     * @return associated context service
     */
    protected IDebugContextService getDebugContextService() {
    	return DebugUITools.getDebugContextManager().getContextService(fWindow);
    }

    /**
     * Returns the help context id for this action or <code>null</code> if none.
     *
     * @return The help context id for this action or <code>null</code>
     */
    public abstract String getHelpContextId();

    /*
     * (non-Javadoc)
     * @see org.eclipse.jface.action.Action#getId()
     */
    @Override
	public abstract String getId();

    /*
     * (non-Javadoc)
     * @see org.eclipse.jface.action.Action#getText()
     */
    @Override
	public abstract String getText();

    /*
     * (non-Javadoc)
     * @see org.eclipse.jface.action.Action#getToolTipText()
     */
    @Override
	public abstract String getToolTipText();

    /*
     * (non-Javadoc)
     * @see org.eclipse.jface.action.Action#getDisabledImageDescriptor()
     */
    @Override
	public abstract ImageDescriptor getDisabledImageDescriptor();

    /*
     * (non-Javadoc)
     * @see org.eclipse.jface.action.Action#getHoverImageDescriptor()
     */
    @Override
	public abstract ImageDescriptor getHoverImageDescriptor();

    /*
     * (non-Javadoc)
     * @see org.eclipse.jface.action.Action#getImageDescriptor()
     */
    @Override
	public abstract ImageDescriptor getImageDescriptor();

    /**
     * Returns the workbench proxy associated with this action or <code>null</code>
     * if none. This is the workbench proxy to an {@link org.eclipse.ui.IActionDelegate}
     * that is using this action to perform its actual work. This is only used when
     * an {@link org.eclipse.ui.IActionDelegate} is using one of these actions to perform its
     * function.
     *
     * @return workbench proxy action or <code>null</code>
     */
    protected IAction getActionProxy() {
    	return fAction;
    }
}

Back to the top