Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d1fe45658e1843a6f872d6b8c411a6a2ab9ce6fb (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
/*******************************************************************************
 * Copyright (c) 2008 SAP AG 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:
 * Kaloyan Raev, kaloyan.raev@sap.com - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.servlet.ui.internal.wizard;

import static org.eclipse.jst.j2ee.internal.common.operations.INewJavaClassDataModelProperties.CLASS_NAME;
import static org.eclipse.jst.j2ee.internal.common.operations.INewJavaClassDataModelProperties.OPEN_IN_EDITOR;
import static org.eclipse.jst.j2ee.internal.common.operations.INewJavaClassDataModelProperties.PROJECT;
import static org.eclipse.jst.j2ee.internal.common.operations.INewJavaClassDataModelProperties.QUALIFIED_CLASS_NAME;
import static org.eclipse.jst.j2ee.internal.web.operations.INewWebClassDataModelProperties.USE_EXISTING_CLASS;

import java.net.URL;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jst.j2ee.internal.plugin.J2EEEditorUtility;
import org.eclipse.jst.j2ee.internal.plugin.J2EEPlugin;
import org.eclipse.jst.servlet.ui.internal.plugin.ServletUIPlugin;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.eclipse.wst.common.componentcore.ComponentCore;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
import org.eclipse.wst.common.frameworks.datamodel.IDataModel;

public abstract class NewWebArtifactWizard extends NewWebWizard {
	
	protected static final String PAGE_ONE = "pageOne"; //$NON-NLS-1$
	protected static final String PAGE_TWO = "pageTwo"; //$NON-NLS-1$
	protected static final String PAGE_THREE = "pageThree"; //$NON-NLS-1$

	public NewWebArtifactWizard(IDataModel model) {
		super(model);
		setWindowTitle(getTitle());
		setDefaultPageImageDescriptor(getImage());
	}

	@Override
	protected boolean runForked() {
		return false;
	}
	
	@Override
	public boolean canFinish() {
		return getDataModel().isValid();
	}
	
	protected abstract String getTitle();
	
	protected abstract ImageDescriptor getImage();
	
	protected ImageDescriptor getImageFromJ2EEPlugin(String key) {
		URL url = (URL) J2EEPlugin.getDefault().getImage(key);
		return ImageDescriptor.createFromURL(url);
	}
	
	protected void openJavaClass() {
		try {
			String className;
			if (getDataModel().getBooleanProperty(USE_EXISTING_CLASS)) {
				className = getDataModel().getStringProperty(CLASS_NAME);
			} else {
				className = getDataModel().getStringProperty(QUALIFIED_CLASS_NAME);
			}
			IProject p = (IProject) getDataModel().getProperty(PROJECT);
			IJavaProject javaProject = J2EEEditorUtility.getJavaProject(p);
			IFile file = (IFile) javaProject.findType(className).getResource();
			openEditor(file);
		} catch (Exception cantOpen) {
			ServletUIPlugin.log(cantOpen);
		}	
	}
	
	protected void openWebFile() {
		try {
			String className = getDataModel().getStringProperty(CLASS_NAME);
			IProject p = (IProject) getDataModel().getProperty(PROJECT);
			IVirtualComponent component = ComponentCore.createComponent(p);
			IContainer webContent = component.getRootFolder().getUnderlyingFolder();
			IFile file = webContent.getFile(new Path(className));
			openEditor(file);
		} catch (Exception cantOpen) {
			ServletUIPlugin.log(cantOpen);
		}	
	}

	protected void openEditor(final IFile file) {
		if (getDataModel().getBooleanProperty(OPEN_IN_EDITOR)) {
			if (file != null) {
				getShell().getDisplay().asyncExec(new Runnable() {
					public void run() {
						try {
							IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
							IDE.openEditor(page, file, true);
						}
						catch (PartInitException e) {
							ServletUIPlugin.log(e);
						}
					}
				});
			}
		}
	}

}

Back to the top