001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.plugin;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.core.resources.IFolder;
015: import org.eclipse.core.resources.IProject;
016: import org.eclipse.core.resources.ProjectScope;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.Path;
019: import org.eclipse.core.runtime.preferences.IEclipsePreferences;
020: import org.eclipse.jdt.core.IClassFile;
021: import org.eclipse.jdt.core.ICompilationUnit;
022: import org.eclipse.jdt.core.IJavaElement;
023: import org.eclipse.jdt.core.IJavaProject;
024: import org.eclipse.jdt.core.IPackageFragment;
025: import org.eclipse.jdt.core.IPackageFragmentRoot;
026: import org.eclipse.jdt.core.IType;
027: import org.eclipse.jdt.core.JavaCore;
028: import org.eclipse.jdt.core.JavaModelException;
029: import org.eclipse.jdt.ui.wizards.NewClassWizardPage;
030: import org.eclipse.pde.core.plugin.IPluginImport;
031: import org.eclipse.pde.core.plugin.IPluginModelBase;
032: import org.eclipse.pde.internal.core.ICoreConstants;
033: import org.eclipse.pde.internal.core.PDECore;
034: import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
035: import org.eclipse.pde.internal.ui.PDEPlugin;
036:
037: public class JavaAttributeWizardPage extends NewClassWizardPage {
038: private String className;
039: private IProject project;
040: private ISchemaAttribute attInfo;
041: private IPluginModelBase model;
042: private InitialClassProperties initialValues;
043: private IJavaProject javaProject;
044:
045: class InitialClassProperties {
046: // populate new wizard page
047: IType super ClassType;
048: String super ClassName;
049: IType interfaceType;
050: String interfaceName;
051: String className;
052: String classArgs;
053: String packageName;
054: IPackageFragmentRoot packageFragmentRoot;
055: IPackageFragment packageFragment;
056:
057: public InitialClassProperties() {
058: this .super ClassType = null;
059: this .super ClassName = ""; //$NON-NLS-1$
060: this .interfaceName = null;
061: this .interfaceType = null;
062: this .className = null;
063: this .classArgs = null;
064: this .packageName = null;
065: this .packageFragment = null;
066: this .packageFragmentRoot = null;
067: }
068: }
069:
070: public JavaAttributeWizardPage(IProject project,
071: IPluginModelBase model, ISchemaAttribute attInfo,
072: String className) {
073: super ();
074: this .className = className;
075: this .model = model;
076: this .project = project;
077: this .attInfo = attInfo;
078: try {
079: if (project.hasNature(JavaCore.NATURE_ID))
080: this .javaProject = JavaCore.create(project);
081: else
082: this .javaProject = null;
083: } catch (CoreException e) {
084: PDEPlugin.logException(e);
085: }
086: initialValues = new InitialClassProperties();
087: initialValues.className = className;
088: }
089:
090: public Object getValue() {
091: return new JavaAttributeValue(project, model, attInfo,
092: className);
093: }
094:
095: public void init() {
096: initializeExpectedValues();
097: initializeWizardPage();
098: }
099:
100: protected void initializeWizardPage() {
101: setPackageFragmentRoot(initialValues.packageFragmentRoot, true);
102: setPackageFragment(initialValues.packageFragment, true);
103: setEnclosingType(null, true);
104: setEnclosingTypeSelection(false, true);
105: setTypeName(initialValues.className, true);
106: setSuperClass(initialValues.super ClassName, true);
107: if (initialValues.interfaceName != null) {
108: ArrayList interfaces = new ArrayList();
109: interfaces.add(initialValues.interfaceName);
110: setSuperInterfaces(interfaces, true);
111: }
112: boolean hasSuperClass = initialValues.super ClassName != null
113: && initialValues.super ClassName.length() > 0;
114: boolean hasInterface = initialValues.interfaceName != null
115: && initialValues.interfaceName.length() > 0;
116: setMethodStubSelection(false, hasSuperClass, hasInterface
117: || hasSuperClass, true);
118: }
119:
120: private IType findTypeForName(String typeName)
121: throws JavaModelException {
122: if (typeName == null || typeName.length() == 0)
123: return null;
124: IType type = null;
125: String fileName = typeName.replace('.', '/') + ".java"; //$NON-NLS-1$
126: IJavaElement element = javaProject.findElement(new Path(
127: fileName));
128: if (element == null)
129: return null;
130: if (element instanceof IClassFile) {
131: type = ((IClassFile) element).getType();
132: } else if (element instanceof ICompilationUnit) {
133: IType[] types = ((ICompilationUnit) element).getTypes();
134: type = types[0];
135: }
136: return type;
137: }
138:
139: private void initializeExpectedValues() {
140:
141: // source folder name, package name, class name
142: int loc = className.indexOf(":"); //$NON-NLS-1$
143: if (loc != -1) {
144: if (loc < className.length()) {
145: initialValues.classArgs = className.substring(loc + 1,
146: className.length());
147: className = className.substring(0, loc);
148: }
149: if (loc > 0)
150: initialValues.className = className.substring(0, loc);
151: else if (loc == 0)
152: initialValues.className = ""; //$NON-NLS-1$
153: }
154:
155: loc = className.lastIndexOf('.');
156: if (loc != -1) {
157: initialValues.packageName = className.substring(0, loc);
158: initialValues.className = className.substring(loc + 1);
159: }
160: if (javaProject == null)
161: return;
162: try {
163: if (initialValues.packageFragmentRoot == null) {
164: IPackageFragmentRoot srcEntryDft = null;
165: IPackageFragmentRoot[] roots = javaProject
166: .getPackageFragmentRoots();
167: for (int i = 0; i < roots.length; i++) {
168: if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
169: srcEntryDft = roots[i];
170: break;
171: }
172: }
173: if (srcEntryDft != null)
174: initialValues.packageFragmentRoot = srcEntryDft;
175: else {
176: initialValues.packageFragmentRoot = javaProject
177: .getPackageFragmentRoot(javaProject
178: .getResource());
179: }
180: if (initialValues.packageFragment == null
181: && initialValues.packageFragmentRoot != null
182: && initialValues.packageName != null
183: && initialValues.packageName.length() > 0) {
184: IFolder packageFolder = project
185: .getFolder(initialValues.packageName);
186: initialValues.packageFragment = initialValues.packageFragmentRoot
187: .getPackageFragment(packageFolder
188: .getProjectRelativePath()
189: .toOSString());
190: }
191: }
192: // superclass and interface
193: if (attInfo == null) {
194: IEclipsePreferences prefs = new ProjectScope(project)
195: .getNode(PDECore.PLUGIN_ID);
196: if (prefs != null
197: && !prefs.getBoolean(
198: ICoreConstants.EXTENSIONS_PROPERTY,
199: true)) {
200: initialValues.interfaceName = "org.osgi.framework.BundleActivator"; //$NON-NLS-1$
201: initialValues.interfaceType = findTypeForName(initialValues.interfaceName);
202: return;
203: }
204: initialValues.super ClassName = "org.eclipse.core.runtime.Plugin"; //$NON-NLS-1$
205: if (model != null) {
206: IPluginImport[] imports = model.getPluginBase()
207: .getImports();
208: for (int i = 0; i < imports.length; i++) {
209: if (imports[i].getId().equals("org.eclipse.ui")) { //$NON-NLS-1$
210: initialValues.super ClassName = "org.eclipse.ui.plugin.AbstractUIPlugin"; //$NON-NLS-1$
211: break;
212: }
213: }
214: }
215: initialValues.super ClassType = findTypeForName(initialValues.super ClassName);
216: return;
217: }
218: String schemaBasedOn = attInfo.getBasedOn();
219: if (schemaBasedOn == null || schemaBasedOn.length() == 0) {
220: initialValues.super ClassName = "java.lang.Object"; //$NON-NLS-1$
221: initialValues.super ClassType = findTypeForName(initialValues.super ClassName);
222: return;
223: }
224: int del = schemaBasedOn.indexOf(':');
225: if (del != -1) {
226: if (del == 0) {
227: initialValues.super ClassName = "java.lang.Object"; //$NON-NLS-1$
228: } else {
229: initialValues.super ClassName = schemaBasedOn
230: .substring(0, del); //$NON-NLS-1$
231: }
232: initialValues.super ClassType = findTypeForName(initialValues.super ClassName);
233: if (del < schemaBasedOn.length() - 1) {
234: initialValues.interfaceName = schemaBasedOn
235: .substring(del + 1);
236: initialValues.interfaceType = findTypeForName(initialValues.interfaceName);
237: }
238: } else {
239: int schemaLoc = schemaBasedOn.lastIndexOf("."); //$NON-NLS-1$
240: if (schemaLoc != -1
241: && schemaLoc < schemaBasedOn.length()) {
242: IType type = findTypeForName(schemaBasedOn);
243: if (type != null && type.isInterface()) {
244: initialValues.interfaceName = schemaBasedOn;
245: initialValues.interfaceType = type;
246: } else if (type != null && type.isClass()) {
247: initialValues.super ClassName = schemaBasedOn;
248: initialValues.super ClassType = type;
249: }
250: }
251: }
252:
253: } catch (JavaModelException e) {
254: PDEPlugin.logException(e);
255: }
256: }
257:
258: public String getClassArgs() {
259: if (initialValues.classArgs == null)
260: return ""; //$NON-NLS-1$
261: return initialValues.classArgs;
262: }
263:
264: }
|