Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f6824a05bae06a9eb4460dc2281b1c09cf6a718f (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
/*******************************************************************************
 * Copyright (c) 2000, 2011 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.ui.launchConfigurations;


import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.actions.SelectionListenerAction;

/**
 * Common function/behavior for launch configuration view actions
 */
public abstract class AbstractLaunchConfigurationAction extends SelectionListenerAction {

	/**
	 * The mode this action was created in (run, debug, ...)
	 */
	private String fMode;

	/**
	 * Allows a requestor to abort this action.
	 */
	public interface IConfirmationRequestor {
		/**
		 * Returns whether this action should proceed. Confirmation is requested
		 * when an action is run.
		 *
		 * @return whether this action should proceed
		 */
		boolean getConfirmation();
	}

	/**
	 * This action's confirmation requestor or <code>null</code> if none
	 */
	private IConfirmationRequestor fConfirmationRequestor;

	/**
	 * The viewer this action is working on
	 */
	private Viewer fViewer;

	/**
	 * Constructor for AbstractLaunchConfigurationAction.
	 * @param text the action label
	 * @param viewer the viewer the action belongs to
	 * @param mode the mode the action applies to
	 */
	public AbstractLaunchConfigurationAction(String text, Viewer viewer, String mode) {
		super(text);
		fViewer = viewer;
		fViewer.addSelectionChangedListener(this);
		fMode = mode;
	}

	/**
	 * Returns the shell this action is contained in.
	 *
	 * @return the shell this action is contained in
	 */
	protected Shell getShell() {
		return getViewer().getControl().getShell();
	}

	/**
	 * Returns the viewer this action is working on
	 *
	 * @return the viewer this action is working on
	 */
	protected Viewer getViewer() {
		return fViewer;
	}

	/**
	 * Performs this action once confirmation has been aquired. Subclasses
	 * should override this method.
	 */
	protected abstract void performAction();

	/**
	 * @see org.eclipse.jface.action.IAction#run()
	 */
	@Override
	public final void run() {
		if (fConfirmationRequestor != null) {
			if (!fConfirmationRequestor.getConfirmation()) {
				return;
			}
		}
		Runnable r = new Runnable() {
			/**
			 * @see java.lang.Runnable#run()
			 */
			@Override
			public void run() {
				performAction();
			}
		};
		BusyIndicator.showWhile(getShell().getDisplay(), r);
	}

	/**
	 * Sets this action's confirmation requestor.
	 *
	 * @param confirmationRequestor the new {@link IConfirmationRequestor}
	 */
	public void setConfirmationRequestor(IConfirmationRequestor confirmationRequestor) {
		fConfirmationRequestor = confirmationRequestor;
	}

	/**
	 * Disposes this action
	 */
	public void dispose() {
		fViewer.removeSelectionChangedListener(this);
	}

	/**
	 * Show an error dialog on the given exception.
	 *
	 * @param exception the exception to show in the dialog
	 */
	protected void errorDialog(CoreException exception) {
		ErrorDialog.openError(getShell(), null, null, exception.getStatus());
	}

	/**
	 * Return this action's mode.
	 *
	 * @return launch mode
	 */
	protected String getMode() {
		return fMode;
	}
}

Back to the top