Skip to main content
summaryrefslogtreecommitdiffstats
blob: 08bc3742ae0df3fd4dbcd3891c782ea6fd5a9018 (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
/*******************************************************************************
 * 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.wizard;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jst.j2ee.internal.plugin.CommonEditorUtility;
import org.eclipse.ui.IImportWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;
import org.eclipse.wst.common.frameworks.datamodel.IDataModel;
import org.eclipse.wst.common.frameworks.internal.datamodel.ui.DataModelWizard;
import org.eclipse.wst.web.internal.DelegateConfigurationElement;

/**
 * <p>
 * Serves as a base class for Wizards which import J2EE artifact structures into Eclipse projects.
 * </p>
 * <p>
 * Subclasses must provide the methods that are required by
 * {@see org.eclipse.wst.common.frameworks.internal.ui.WTPWizard}.
 * </p>
 * <p>
 * Optionally, subclasses may also override the following methods:
 * <ul>
 * <li>{@link #getFinalPerspectiveID()}
 * <li>{@link #doInit()()}
 * <li>{@link #doDispose()()}
 * </ul>
 * </p>
 * <p>
 * The base class will ensure that the Wizard is not spawned unless all open editors are in a
 * non-dirty state. Additionally, the selection from the active view which spanwed the wizard will
 * be made available to subclasses via {@link #getCurrentSelection()}.
 * </p>
 */
public abstract class J2EEArtifactImportWizard extends DataModelWizard implements IImportWizard, IExecutableExtension {

	/**
	 * <p>
	 * Constant used to identify the key of the main page of the Wizard.
	 * </p>
	 */
	protected static final String MAIN_PG = "main"; //$NON-NLS-1$

	private static final String FINAL_PERSPECTIVE_ID = "org.eclipse.jst.j2ee.J2EEPerspective"; //$NON-NLS-1$ 

	private IConfigurationElement configurationElement;
	private IStructuredSelection selection;

	/**
	 * <p>
	 * The default constructor. Creates a wizard with no selection, no model instance, and no
	 * operation instance. The model and operation will be created as needed.
	 * </p>
	 */
	public J2EEArtifactImportWizard() {
		super();
	}

	/**
	 * <p>
	 * The model is used to prepopulate the wizard controls and interface with the operation.
	 * </p>
	 * 
	 * @param model
	 *            The model parameter is used to pre-populate wizard controls and interface with the
	 *            operation
	 */
	public J2EEArtifactImportWizard(IDataModel model) {
		super(model);
	}

	/**
	 * <p>
	 * The selection is used to pre-populate values in the Wizard dialog controls.
	 * </p>
	 * 
	 * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench,
	 *      org.eclipse.jface.viewers.IStructuredSelection)
	 * 
	 * @param workbench
	 *            the current workbench parent of the wizard
	 * @param aSelection
	 *            the selection from the view used to start the wizard (if any)
	 */
	public final void init(IWorkbench workbench, IStructuredSelection aSelection) {
		this.selection = aSelection;
		doInit();
	}

	/**
	 * <p>
	 * Calls {@link #doDispose()}and then nulls out fields that are no longer needed once the
	 * wizard completes.
	 * </p>
	 * 
	 * @see com.ibm.etools.j2ee.common.wizard.datamodel.WTPWizard#dispose()
	 */
	public final void dispose() {
		super.dispose();
		doDispose();
		this.selection = null;
		this.configurationElement = null;
	}


	/**
	 * <p>
	 * Invoked after the selection has been saved off in
	 * {@link #init(IWorkbench, IStructuredSelection)}. Should be used to handle any specific
	 * Wizard initialization.
	 * </p>
	 */
	protected void doInit() {
		// init
	}

	/**
	 * <p>
	 * Invoked from {@link #dispose()}. Should be used to handle any specific Wizard disposal.
	 * </p>
	 */
	protected void doDispose() {
		// dispose
	}

	/**
	 * <p>
	 * The return value of this method will be used to suggest a final perspective to the user once
	 * the wizard completes.
	 * </p>
	 * 
	 * @return Returns the J2EE Perpsective ID by default
	 */
	protected String getFinalPerspectiveID() {
		return FINAL_PERSPECTIVE_ID;
	}

	/**
	 * <p>
	 * Prompts the user to save open, dirty editors.
	 * </p>
	 * 
	 * @return true only if all editors are saved
	 */
	protected final boolean prePerformFinish() {
		if (!CommonEditorUtility.promptToSaveAllDirtyEditors()) {
			return false;
		}
		if (CommonEditorUtility.getDirtyEditors().length != 0) { // all checkboxes were not
																	// selected
			return false;
		}
		return super.prePerformFinish();
	}

	/**
	 * <p>
	 * Invoked after the user has clicked the "Finish" button of the wizard. The default
	 * implementation will attempt to update the final perspective to the value specified by
	 * {@link #getFinalPerspectiveID() }
	 * </p>
	 * 
	 * @throws InvocationTargetException
	 * 
	 * @see org.eclipse.wst.common.frameworks.internal.ui.wizard.WTPWizard#postPerformFinish()
	 */
	protected final void postPerformFinish() throws InvocationTargetException {
		super.postPerformFinish();
		if (getFinalPerspectiveID() != null && getFinalPerspectiveID().length() > 0) {

			IConfigurationElement element = new DelegateConfigurationElement(configurationElement) {
				public String getAttribute(String aName) {
					if (aName.equals("finalPerspective")) { //$NON-NLS-1$
						return getFinalPerspectiveID();
					}
					return super.getAttribute(aName);
				}

			};
			BasicNewProjectResourceWizard.updatePerspective(element);
		} else
			BasicNewProjectResourceWizard.updatePerspective(configurationElement);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * <p>
	 * The configuration element is saved to use when the wizard completes in order to change the
	 * current perspective using either (1) the value specified by {@see #getFinalPerspectiveID()}
	 * or (2) the value specified by the finalPerspective attribute in the Wizard's configuration
	 * element.
	 * </p>
	 * 
	 * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement,
	 *      java.lang.String, java.lang.Object)
	 */
	public final void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
		this.configurationElement = config;
	}


	/**
	 * @return Returns the selection.
	 */
	protected final IStructuredSelection getSelection() {
		return selection;
	}

}

Back to the top