1 11 package org.eclipse.pde.internal.ui.editor.plugin; 12 import java.util.ArrayList ; 13 14 import org.eclipse.core.resources.IFolder; 15 import org.eclipse.core.resources.IProject; 16 import org.eclipse.core.resources.ProjectScope; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.Path; 19 import org.eclipse.core.runtime.preferences.IEclipsePreferences; 20 import org.eclipse.jdt.core.IClassFile; 21 import org.eclipse.jdt.core.ICompilationUnit; 22 import org.eclipse.jdt.core.IJavaElement; 23 import org.eclipse.jdt.core.IJavaProject; 24 import org.eclipse.jdt.core.IPackageFragment; 25 import org.eclipse.jdt.core.IPackageFragmentRoot; 26 import org.eclipse.jdt.core.IType; 27 import org.eclipse.jdt.core.JavaCore; 28 import org.eclipse.jdt.core.JavaModelException; 29 import org.eclipse.jdt.ui.wizards.NewClassWizardPage; 30 import org.eclipse.pde.core.plugin.IPluginImport; 31 import org.eclipse.pde.core.plugin.IPluginModelBase; 32 import org.eclipse.pde.internal.core.ICoreConstants; 33 import org.eclipse.pde.internal.core.PDECore; 34 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute; 35 import org.eclipse.pde.internal.ui.PDEPlugin; 36 37 public class JavaAttributeWizardPage extends NewClassWizardPage { 38 private String className; 39 private IProject project; 40 private ISchemaAttribute attInfo; 41 private IPluginModelBase model; 42 private InitialClassProperties initialValues; 43 private IJavaProject javaProject; 44 45 class InitialClassProperties { 46 IType superClassType; 48 String superClassName; 49 IType interfaceType; 50 String interfaceName; 51 String className; 52 String classArgs; 53 String packageName; 54 IPackageFragmentRoot packageFragmentRoot; 55 IPackageFragment packageFragment; 56 public InitialClassProperties() { 57 this.superClassType = null; 58 this.superClassName = ""; this.interfaceName = null; 60 this.interfaceType = null; 61 this.className = null; 62 this.classArgs = null; 63 this.packageName = null; 64 this.packageFragment = null; 65 this.packageFragmentRoot = null; 66 } 67 } 68 public JavaAttributeWizardPage(IProject project, IPluginModelBase model, 69 ISchemaAttribute attInfo, String className) { 70 super(); 71 this.className = className; 72 this.model = model; 73 this.project = project; 74 this.attInfo = attInfo; 75 try { 76 if (project.hasNature(JavaCore.NATURE_ID)) 77 this.javaProject = JavaCore.create(project); 78 else 79 this.javaProject = null; 80 } catch (CoreException e) { 81 PDEPlugin.logException(e); 82 } 83 initialValues = new InitialClassProperties(); 84 initialValues.className = className; 85 } 86 87 public Object getValue() { 88 return new JavaAttributeValue(project, model, attInfo, className); 89 } 90 public void init() { 91 initializeExpectedValues(); 92 initializeWizardPage(); 93 } 94 protected void initializeWizardPage() { 95 setPackageFragmentRoot(initialValues.packageFragmentRoot, true); 96 setPackageFragment(initialValues.packageFragment, true); 97 setEnclosingType(null, true); 98 setEnclosingTypeSelection(false, true); 99 setTypeName(initialValues.className, true); 100 setSuperClass(initialValues.superClassName, true); 101 if (initialValues.interfaceName != null) { 102 ArrayList interfaces = new ArrayList (); 103 interfaces.add(initialValues.interfaceName); 104 setSuperInterfaces(interfaces, true); 105 } 106 boolean hasSuperClass = initialValues.superClassName != null 107 && initialValues.superClassName.length() > 0; 108 boolean hasInterface = initialValues.interfaceName != null 109 && initialValues.interfaceName.length() > 0; 110 setMethodStubSelection(false, hasSuperClass, hasInterface 111 || hasSuperClass, true); 112 } 113 private IType findTypeForName(String typeName) throws JavaModelException { 114 if (typeName == null || typeName.length() == 0) 115 return null; 116 IType type = null; 117 String fileName = typeName.replace('.', '/') + ".java"; IJavaElement element = javaProject.findElement(new Path(fileName)); 119 if (element == null) 120 return null; 121 if (element instanceof IClassFile) { 122 type = ((IClassFile) element).getType(); 123 } else if (element instanceof ICompilationUnit) { 124 IType[] types = ((ICompilationUnit) element).getTypes(); 125 type = types[0]; 126 } 127 return type; 128 } 129 private void initializeExpectedValues() { 130 131 132 int loc = className.indexOf(":"); if (loc != -1) { 135 if (loc < className.length()) { 136 initialValues.classArgs = className.substring(loc + 1, 137 className.length()); 138 className = className.substring(0, loc); 139 } 140 if (loc > 0) 141 initialValues.className = className.substring(0, loc); 142 else if (loc == 0) 143 initialValues.className = ""; } 145 146 loc = className.lastIndexOf('.'); 147 if (loc != -1) { 148 initialValues.packageName = className.substring(0, loc); 149 initialValues.className = className.substring(loc + 1); 150 } 151 if (javaProject == null) 152 return; 153 try { 154 if (initialValues.packageFragmentRoot == null) { 155 IPackageFragmentRoot srcEntryDft = null; 156 IPackageFragmentRoot[] roots = javaProject 157 .getPackageFragmentRoots(); 158 for (int i = 0; i < roots.length; i++) { 159 if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) { 160 srcEntryDft = roots[i]; 161 break; 162 } 163 } 164 if (srcEntryDft != null) 165 initialValues.packageFragmentRoot = srcEntryDft; 166 else { 167 initialValues.packageFragmentRoot = javaProject.getPackageFragmentRoot(javaProject.getResource()); 168 } 169 if (initialValues.packageFragment == null 170 && initialValues.packageFragmentRoot != null 171 && initialValues.packageName != null 172 && initialValues.packageName.length() > 0) { 173 IFolder packageFolder = project 174 .getFolder(initialValues.packageName); 175 initialValues.packageFragment = initialValues.packageFragmentRoot 176 .getPackageFragment(packageFolder 177 .getProjectRelativePath().toOSString()); 178 } 179 } 180 if (attInfo == null) { 182 IEclipsePreferences prefs = new ProjectScope(project).getNode(PDECore.PLUGIN_ID); 183 if (prefs != null && !prefs.getBoolean(ICoreConstants.EXTENSIONS_PROPERTY, true)) { 184 initialValues.interfaceName = "org.osgi.framework.BundleActivator"; initialValues.interfaceType = findTypeForName(initialValues.interfaceName); 186 return; 187 } 188 initialValues.superClassName = "org.eclipse.core.runtime.Plugin"; if (model != null) { 190 IPluginImport[] imports = model.getPluginBase().getImports(); 191 for (int i = 0; i < imports.length; i++) { 192 if (imports[i].getId().equals("org.eclipse.ui")) { initialValues.superClassName = "org.eclipse.ui.plugin.AbstractUIPlugin"; break; 195 } 196 } 197 } 198 initialValues.superClassType = findTypeForName(initialValues.superClassName); 199 return; 200 } 201 String schemaBasedOn = attInfo.getBasedOn(); 202 if (schemaBasedOn == null || schemaBasedOn.length() == 0) { 203 initialValues.superClassName = "java.lang.Object"; initialValues.superClassType = findTypeForName(initialValues.superClassName); 205 return; 206 } 207 int del = schemaBasedOn.indexOf(':'); 208 if (del != -1) { 209 if(del == 0) 210 { initialValues.superClassName = "java.lang.Object"; } 212 else 213 { initialValues.superClassName = schemaBasedOn.substring(0, del); } 215 initialValues.superClassType = findTypeForName(initialValues.superClassName); 216 if(del < schemaBasedOn.length() - 1) 217 { initialValues.interfaceName = schemaBasedOn.substring(del + 1); 218 initialValues.interfaceType = findTypeForName(initialValues.interfaceName); 219 } 220 } else { 221 int schemaLoc = schemaBasedOn.lastIndexOf("."); if (schemaLoc != -1 && schemaLoc < schemaBasedOn.length()) { 223 IType type = findTypeForName(schemaBasedOn); 224 if (type!=null && type.isInterface()){ 225 initialValues.interfaceName = schemaBasedOn; 226 initialValues.interfaceType = type; 227 } else if (type!=null && type.isClass()) { 228 initialValues.superClassName = schemaBasedOn; 229 initialValues.superClassType = type; 230 } 231 } 232 } 233 234 } catch (JavaModelException e) { 235 PDEPlugin.logException(e); 236 } 237 } 238 239 public String getClassArgs(){ 240 if (initialValues.classArgs == null) 241 return ""; return initialValues.classArgs; 243 } 244 245 } 246 | Popular Tags |