Skip to main content
summaryrefslogtreecommitdiffstats
blob: 13ea988ea2374754eaac0f2b4be0feede89c1bd3 (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
/***************************************************************************************************
 * 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.web.operations;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jem.util.emf.workbench.ProjectUtilities;
import org.eclipse.jst.j2ee.internal.plugin.J2EEPlugin;
import org.eclipse.jst.j2ee.internal.project.IWebNatureConstants;
import org.eclipse.wst.web.internal.operation.IBaseWebNature;


public class J2EEWebNatureRuntimeUtilities {
	/**
	 * WebNatureRuntimeHelper constructor comment.
	 */
	public J2EEWebNatureRuntimeUtilities() {
		super();
	}

	/**
	 * Adds a nauture to a project
	 */
	protected static void addNatureToProject(IProject proj, String natureId) throws CoreException {
		ProjectUtilities.addNatureToProject(proj, natureId);
	}

	/**
	 * Create a new nature runtime from the project info
	 */
	public static IBaseWebNature createRuntime(WebProjectInfo info) throws CoreException {
		IProject project = info.getProject();

		// refresh WebProjectInfo for existing runtime
		if (hasRuntime(project)) {
			// this is for when the web nature is being changed, such
			// as from converting from static to j2ee
			IBaseWebNature runtime = getRuntime(project);
			if (J2EEWebNatureRuntime.hasRuntime(project))
				((J2EEWebNatureRuntime) runtime).initializeFromInfo(info);

			return runtime;
		}
		if (info.getWebProjectType() == IWebNatureConstants.J2EE_WEB_PROJECT)
			addNatureToProject(project, IWebNatureConstants.J2EE_NATURE_ID);
		IBaseWebNature runtime = getRuntime(project);
		if (info.getWebProjectType() == IWebNatureConstants.J2EE_WEB_PROJECT)
			((J2EEWebNatureRuntime) runtime).initializeFromInfo(info);
		return runtime;

	}

	/**
	 * Creation date: (10/22/2001 2:17:25 PM)
	 * 
	 * @return org.eclipse.jst.j2ee.internal.internal.internal.web.operations.IBaseWebNature
	 */
	public static IBaseWebNature getRuntime(IProject project) {
		if (project == null)
			return null;
		try {
			IBaseWebNature nature = null;
			if (project.hasNature(IWebNatureConstants.J2EE_NATURE_ID))
				nature = (IBaseWebNature) project.getNature(IWebNatureConstants.J2EE_NATURE_ID);
			return nature;
		} catch (CoreException e) {
			return null;
		}
	}


	/**
	 * Return the J2EE Web Nature for the given project. If the the project does not have a J2EE Web
	 * Nature, then return null.
	 * 
	 * @param project
	 *            The project to get the nature from
	 * @return IJ2EEWebNature The J2EE Web Nature
	 */
	public static J2EEWebNatureRuntime getJ2EERuntime(IProject project) {
		IBaseWebNature nature = getRuntime(project);
		if (nature != null) {
			if (nature.isJ2EE())
				return (J2EEWebNatureRuntime) nature;
		}
		return null;
	}

	/**
	 * Return whether or not the project has a runtime created on it.
	 * 
	 * @return boolean
	 * @param project
	 *            com.ibm.itp.core.api.resources.IProject
	 * @deprecated
	 */
	public static boolean hasRuntime(IProject project) {
		try {
			return project.hasNature(IWebNatureConstants.J2EE_NATURE_ID);
		} catch (CoreException e) {
			return false;
		}
	}

	/**
	 * Return whether or not the project has a runtime created on it.
	 * 
	 * @return boolean
	 * @param project
	 *            com.ibm.itp.core.api.resources.IProject
	 */
	public static boolean hasJ2EERuntime(IProject project) {
		if (project == null || !project.exists()) {
			return false;
		}
		try {
			return project.hasNature(IWebNatureConstants.J2EE_NATURE_ID);
		} catch (CoreException e) {
			return false;
		}
	}

	public static String getDefaultJ2EEWebContentName() {
		return J2EEPlugin.getDefault().getJ2EEPreferences().getJ2EEWebContentFolderName();
	}

	public static String getDefaultStaticWebContentName() {
		return J2EEPlugin.getDefault().getJ2EEPreferences().getStaticWebContentFolderName();
	}

	public static IPath getDefaultJ2EEWebContentPath() {
		return new Path(getDefaultJ2EEWebContentName());
	}

	public static String getContextRootFromWebProject(IProject project) {

		// get uri from web app display name, and get context root from web nature
		IBaseWebNature nature = J2EEWebNatureRuntimeUtilities.getRuntime(project);
		String contextRoot = ""; //$NON-NLS-1$
		contextRoot = nature.getContextRoot();

		//	WebEditModel webEditModel = null;
		//	try {
		//		webEditModel = (nature.isStatic())
		//						? null
		//						:
		// ((J2EEWebNatureRuntime)nature).getWebAppEditModelForRead(WebNatureRuntimeUtilities.this);
		//		contextRoot = nature.getContextRoot();
		//	} catch (Exception e) {
		//		e.printStackTrace();
		//	}
		//	finally {
		//		if (webEditModel != null)
		//			webEditModel.releaseAccess(WebNatureRuntimeUtilities.this);
		//	}
		return contextRoot;
	}

	public static IPath getDefaultWEBINFPath() {
		return getDefaultJ2EEWebContentPath().append(IWebNatureConstants.INFO_DIRECTORY);
	}

	public static IPath getDefaultWebOutputFolderPath() {
		return getDefaultWEBINFPath().append(IWebNatureConstants.CLASSES_DIRECTORY);
	}

	public static IPath getWebOutputFolderPath(String webContentName) {
		return new Path(webContentName).append(IWebNatureConstants.INFO_DIRECTORY).append(IWebNatureConstants.CLASSES_DIRECTORY);
	}

	public static String getDefaultJavaSourceName() {
		return J2EEPlugin.getDefault().getJ2EEPreferences().getJavaSourceFolderName();
	}

}

Back to the top