Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 92895c86898b6bd53e8d47c76f5f6e8b4d157762 (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
/***************************************************************************************************
 * Copyright (c) 2003, 2004 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.Iterator;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.jst.j2ee.internal.plugin.J2EEUIMessages;
import org.eclipse.jst.j2ee.internal.plugin.J2EEUIPlugin;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWizard;
import org.eclipse.ui.activities.WorkbenchActivityHelper;


public abstract class AbstractOpenWizardAction extends org.eclipse.jface.action.Action {
	// //$NON-NLS-1$

	private IWorkbench fWorkbench;

	private Class[] fActivatedOnTypes;

	private boolean fAcceptEmptySelection;

	protected AbstractOpenWizardAction() {
		//Default constructor
	}

	public AbstractOpenWizardAction(IWorkbench workbench, String label, Class[] activatedOnTypes, boolean acceptEmptySelection) {
		super(label);
		fWorkbench = workbench;
		fActivatedOnTypes = activatedOnTypes;
		fAcceptEmptySelection = acceptEmptySelection;
	}

	public AbstractOpenWizardAction(IWorkbench workbench, String label, boolean acceptEmptySelection) {
		this(workbench, label, null, acceptEmptySelection);
	}

	public boolean canActionBeAdded() {
		ISelection selection = getCurrentSelection();
		if (selection == null || selection.isEmpty()) {
			return fAcceptEmptySelection;
		}
		if (fActivatedOnTypes != null) {
			if (selection instanceof IStructuredSelection) {
				return isEnabled(((IStructuredSelection) selection).iterator());
			}
			return false;
		}
		return true;
	}

	/**
	 * Create the specific Wizard (to be implemented by a subclass)
	 */
	abstract protected Wizard createWizard();

	protected IStructuredSelection getCurrentSelection() {
		IWorkbenchWindow window = J2EEUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow();
		if (window != null) {
			ISelection selection = window.getSelectionService().getSelection();
			if (selection instanceof IStructuredSelection) {
				return (IStructuredSelection) selection;
			}

		}
		return null;
	}

	protected IWorkbench getWorkbench() {
		return fWorkbench;
	}

	private boolean isEnabled(Iterator iter) {
		while (iter.hasNext()) {
			Object obj = iter.next();
			if (!isOfAcceptedType(obj) || !shouldAcceptElement(obj)) {
				return false;
			}
		}
		return true;
	}

	private boolean isOfAcceptedType(Object obj) {
		for (int i = 0; i < fActivatedOnTypes.length; i++) {
			if (fActivatedOnTypes[i].isInstance(obj)) {
				return true;
			}
		}
		return false;
	}

	protected String getDialogText() {
		return J2EEUIMessages.getResourceString(J2EEUIMessages.NEW_LBL);
	}

	/**
	 * The user has invoked this action.
	 */
	public void run() {
		Wizard wizard = createWizard();
		if (wizard instanceof IWorkbenchWizard) {
			IStructuredSelection sel = null;
			if (null != getCurrentSelection())
				sel = getCurrentSelection();
			else
				sel = new StructuredSelection();
			((IWorkbenchWizard) wizard).init(J2EEUIPlugin.getDefault().getWorkbench(), sel);
		}
		if (WorkbenchActivityHelper.allowUseOf(wizard)) {
			IWorkbenchWindow window = J2EEUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow();
			WizardDialog dialog = new WizardDialog(window.getShell(), wizard);
			dialog.create();
			String text = getDialogText();
			if (null != text) {
				dialog.getShell().setText(text);
			}
			dialog.open();
		}
	}

	protected void setWorkbench(IWorkbench workbench) {
		fWorkbench = workbench;
	}

	/**
	 * can be overridden to add more checks obj is guaranteed to be instance of one of the accepted
	 * types
	 */
	protected boolean shouldAcceptElement(Object obj) {
		return true;
	}
}

Back to the top