Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9d9a71ef2dc24e5945f1cad82626b0087d1e320b (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
/*******************************************************************************
 * Copyright (c) 2003, 2005 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.jst.j2ee.internal.actions;



import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jem.util.emf.workbench.ProjectUtilities;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jst.j2ee.internal.plugin.J2EEUIMessages;
import org.eclipse.jst.j2ee.internal.plugin.J2EEUIPlugin;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;


public abstract class AbstractActionDelegate implements org.eclipse.ui.IActionDelegate {
	protected ISelection selection;
	protected boolean allowsMultiSelect = false;
	public static final Class IPROJECT_CLASS = IProject.class;
	private final static String ERROR_OCCURRED_TITLE = J2EEUIMessages.getResourceString("ERROR_OCCURRED_TITLE"); //$NON-NLS-1$
	private final static String ERROR_OCCURRED_MESSAGE = J2EEUIMessages.getResourceString("ERROR_OCCURRED_MESSAGE"); //$NON-NLS-1$

	/**
	 * EditModuleDependencyAction constructor comment.
	 */
	public AbstractActionDelegate() {
		super();
	}

	/**
	 * Applicable only when this action does not allow multi select
	 */
	protected IProject getProject() {
		return getProject(getSelectedElement());
	}

	protected List getProjects() {
		if ((selection == null) || !(selection instanceof IStructuredSelection))
			return Collections.EMPTY_LIST;

		List result = new ArrayList();
		IStructuredSelection struct = (IStructuredSelection) selection;
		Iterator it = struct.iterator();
		while (it.hasNext()) {
			IProject p = getProject(it.next());
			if (p != null)
				result.add(p);
		}
		return result;
	}

	protected IProject getProject(Object element) {

		if (isSupportedAction(element))
			return ProjectUtilities.getProject((EObject) element);
		else if (element instanceof IAdaptable)
			return (IProject) ((IAdaptable) element).getAdapter(IPROJECT_CLASS);
		else
			return null;
	}

	protected IStructuredSelection getStructuredSelection() {
		if ((selection == null) || !(selection instanceof IStructuredSelection))
			return null;
		else if (selection.isEmpty()) {
			selection = J2EEUIPlugin.getCurrentSelection();
		}
		return (IStructuredSelection) selection;
	}

	/*
	 * Only return if there is exactly one item selected
	 */
	protected Object getSelectedElement() {
		IStructuredSelection sel = getStructuredSelection();
		return sel == null ? null : sel.getFirstElement();
	}

	protected IWorkbenchWindow getWorkbenchWindow() {
		return J2EEUIPlugin.getActiveWorkbenchWindow();
	}

	/**
	 * Can the receiver be executed for
	 * 
	 * @element
	 */
	protected abstract boolean isSupportedAction(Object element);

	/**
	 * @deprecated use {@link #primRun(Shell)}
	 */
	protected void primRun(IProject project, Shell shell) {
		//Deprecated
	}

	/**
	 * Subclasses should override this instead of {@link #run(org.eclipse.jface.action.IAction)}
	 */
	protected void primRun(Shell shell) {
		primRun(getProject(), shell);
	}

	/**
	 * Performs this action.
	 * <p>
	 * This method is called when the delegating action has been triggered. Implement this method to
	 * do the actual work.
	 * </p>
	 * 
	 * @param action
	 *            the action proxy that handles the presentation portion of the action
	 */
	public void run(org.eclipse.jface.action.IAction action) {

		Shell shell = getWorkbenchWindow().getShell();
		setActionStateFromProjects(action);
		if (!action.isEnabled())
			MessageDialog.openInformation(shell, J2EEUIMessages.getResourceString("INFORMATION_UI_"), J2EEUIMessages.getResourceString("CHOSEN_OP_NOT_AVAILABLE")); //$NON-NLS-2$ = "The chosen operation is not currently available." //$NON-NLS-1$ = "Information"
		else {
			try {
				primRun(shell);
			} catch (Throwable t) {
				org.eclipse.jst.j2ee.internal.plugin.ErrorDialog.openError(shell, ERROR_OCCURRED_TITLE, ERROR_OCCURRED_MESSAGE, t, 0, false);
			}
		}


	}

	/**
	 * Notifies this action delegate that the selection in the workbench has changed.
	 * <p>
	 * Implementers can use this opportunity to change the availability of the action or to modify
	 * other presentation properties.
	 * </p>
	 * 
	 * @param action
	 *            the action proxy that handles presentation portion of the action
	 * @param aSelection
	 *            the current selection in the workbench
	 */
	public void selectionChanged(org.eclipse.jface.action.IAction action, org.eclipse.jface.viewers.ISelection aSelection) {
		this.selection = aSelection;
		setActionState(action);
	}

	protected void setActionState(IAction action) {
		if (allowsMultiSelect)
			setActionStateForMultiSelect(action);
		else
			setActionStateForSingleSelect(action);
	}

	protected void setActionStateForSingleSelect(IAction action) {
		IStructuredSelection sel = getStructuredSelection();
		if (sel == null || sel.size() != 1) {
			action.setEnabled(false);
			return;
		}
		setActionStateFromProjects(action);
	}

	protected void setActionStateForMultiSelect(IAction action) {
		setActionStateFromProjects(action);
	}

	protected void setActionStateFromProjects(IAction action) {
		IStructuredSelection sel = getStructuredSelection();
		boolean allOk = false;
		if (sel != null && !sel.isEmpty()) {
			allOk = true;
			Iterator it = sel.iterator();
			while (it.hasNext()) {
				Object o = it.next();
				IProject project = getProject(o);
				if (project == null || !project.isOpen()) {
					allOk = false;
					break;
				}
			}
		}
		action.setEnabled(allOk);
	}

	/**
	 * Returns the allowsMultiSelect.
	 * 
	 * @return boolean
	 */
	public boolean allowsMultiSelect() {
		return allowsMultiSelect;
	}


	/**
	 * Sets the allowsMultiSelect.
	 * 
	 * @param allowsMultiSelect
	 *            The allowsMultiSelect to set
	 */
	public void setAllowsMultiSelect(boolean allowsMultiSelect) {
		this.allowsMultiSelect = allowsMultiSelect;
	}


}

Back to the top