| [news.eclipse.tools.jdt] Re: eclipse plugin generator problem |
Hannes,
I tried to understand the code, but is was very dificult.
I allready had a dirty solution, without the EMF. The deadline for this project is way to short to chance to EMF now. so I will have to do it quick and dirty.
But the problem with the wrong build path still exists.
Is there someone who can tell me where (so in which file) I can adapt the build path.
Thanx in advance
Hannes
Ed Merks wrote:Hannes,
Hopefully the JDT guys won't look at this and say, gross! ;-) Here's an example of how we create a plugin project in EMF (and you're probably missing the JRE setup):
public static IProject createEMFProject
(IPath javaSource,
URI projectLocationURI,
List<IProject> referencedProjects,
Monitor monitor,
int style,
List<?> pluginVariables)
{
IProgressMonitor progressMonitor =
BasicMonitor.toIProgressMonitor(monitor);
String projectName = javaSource.segment(0);
IProject project = null;
try
{
List<IClasspathEntry> classpathEntries = new
UniqueEList<IClasspathEntry>();
progressMonitor.beginTask("", 10);
progressMonitor.subTask
(CodeGenEcorePlugin.INSTANCE.getString
("_UI_CreatingEMFProject_message",
new Object [] { projectName, projectLocationURI !=
null ? projectLocationURI.toString() : projectName }));
IWorkspace workspace = ResourcesPlugin.getWorkspace();
project = workspace.getRoot().getProject(projectName);
// Clean up any old project information.
//
if (!project.exists())
{
URI location = projectLocationURI;
if (location == null)
{
location =
URI.createFileURI(workspace.getRoot().getLocation().append(projectName).toOSString());
}
location = location.appendSegment(".project");
File projectFile = new File(location.toString());
if (projectFile.exists())
{
projectFile.renameTo(new File(location.toString() +
".old"));
}
}
IJavaProject javaProject = JavaCore.create(project);
IProjectDescription projectDescription = null;
if (!project.exists())
{
projectDescription =
ResourcesPlugin.getWorkspace().newProjectDescription(projectName);
if (projectLocationURI != null)
{
projectDescription.setLocationURI(new
java.net.URI(projectLocationURI.toString()));
}
project.create(projectDescription, new
SubProgressMonitor(progressMonitor, 1));
}
else
{
projectDescription = project.getDescription();
classpathEntries.addAll(Arrays.asList(javaProject.getRawClasspath()));
}
boolean isInitiallyEmpty = classpathEntries.isEmpty();
{
if (referencedProjects.size() != 0 && (style &
(EMF_PLUGIN_PROJECT_STYLE | EMF_EMPTY_PROJECT_STYLE)) == 0)
{
projectDescription.setReferencedProjects
(referencedProjects.toArray(new
IProject[referencedProjects.size()]));
for (IProject referencedProject : referencedProjects)
{
IClasspathEntry referencedProjectClasspathEntry =
JavaCore.newProjectEntry(referencedProject.getFullPath());
classpathEntries.add(referencedProjectClasspathEntry);
}
}
String [] natureIds = projectDescription.getNatureIds();
if (natureIds == null)
{
natureIds = new String [] { JavaCore.NATURE_ID };
}
else
{
boolean hasJavaNature = false;
boolean hasPDENature = false;
for (int i = 0; i < natureIds.length; ++i)
{
if (JavaCore.NATURE_ID.equals(natureIds[i]))
{
hasJavaNature = true;
}
if ("org.eclipse.pde.PluginNature".equals(natureIds[i]))
{
hasPDENature = true;
}
}
if (!hasJavaNature)
{
String [] oldNatureIds = natureIds;
natureIds = new String [oldNatureIds.length + 1];
System.arraycopy(oldNatureIds, 0, natureIds, 0,
oldNatureIds.length);
natureIds[oldNatureIds.length] = JavaCore.NATURE_ID;
}
if (!hasPDENature)
{
String [] oldNatureIds = natureIds;
natureIds = new String [oldNatureIds.length + 1];
System.arraycopy(oldNatureIds, 0, natureIds, 0,
oldNatureIds.length);
natureIds[oldNatureIds.length] =
"org.eclipse.pde.PluginNature";
}
}
projectDescription.setNatureIds(natureIds);
ICommand [] builders = projectDescription.getBuildSpec();
if (builders == null)
{
builders = new ICommand [0];
}
boolean hasManifestBuilder = false;
boolean hasSchemaBuilder = false;
for (int i = 0; i < builders.length; ++i)
{
if
("org.eclipse.pde.ManifestBuilder".equals(builders[i].getBuilderName()))
{
hasManifestBuilder = true;
}
if
("org.eclipse.pde.SchemaBuilder".equals(builders[i].getBuilderName()))
{
hasSchemaBuilder = true;
}
}
if (!hasManifestBuilder)
{
ICommand [] oldBuilders = builders;
builders = new ICommand [oldBuilders.length + 1];
System.arraycopy(oldBuilders, 0, builders, 0,
oldBuilders.length);
builders[oldBuilders.length] =
projectDescription.newCommand();
builders[oldBuilders.length].setBuilderName("org.eclipse.pde.ManifestBuilder");
}
if (!hasSchemaBuilder)
{
ICommand [] oldBuilders = builders;
builders = new ICommand [oldBuilders.length + 1];
System.arraycopy(oldBuilders, 0, builders, 0,
oldBuilders.length);
builders[oldBuilders.length] =
projectDescription.newCommand();
builders[oldBuilders.length].setBuilderName("org.eclipse.pde.SchemaBuilder");
}
projectDescription.setBuildSpec(builders);
project.open(new SubProgressMonitor(progressMonitor, 1));
project.setDescription(projectDescription, new
SubProgressMonitor(progressMonitor, 1));
IContainer sourceContainer = project;
if (javaSource.segmentCount() > 1)
{
sourceContainer =
project.getFolder(javaSource.removeFirstSegments(1).makeAbsolute());
if (!sourceContainer.exists())
{
((IFolder)sourceContainer).create(false, true, new
SubProgressMonitor(progressMonitor, 1));
}
}
if (isInitiallyEmpty)
{
IClasspathEntry sourceClasspathEntry =
JavaCore.newSourceEntry(javaSource);
for (Iterator<IClasspathEntry> i =
classpathEntries.iterator(); i.hasNext(); )
{
IClasspathEntry classpathEntry = i.next();
if (classpathEntry.getPath().isPrefixOf(javaSource))
{
i.remove();
}
}
classpathEntries.add(0, sourceClasspathEntry);
IClasspathEntry jreClasspathEntry =
JavaCore.newVariableEntry
(new Path(JavaRuntime.JRELIB_VARIABLE), new
Path(JavaRuntime.JRESRC_VARIABLE), new
Path(JavaRuntime.JRESRCROOT_VARIABLE));
for (Iterator<IClasspathEntry> i =
classpathEntries.iterator(); i.hasNext(); )
{
IClasspathEntry classpathEntry = i.next();
if
(classpathEntry.getPath().isPrefixOf(jreClasspathEntry.getPath()))
{
i.remove();
}
}
String jreContainer = JavaRuntime.JRE_CONTAINER;
String complianceLevel =
CodeGenUtil.EclipseUtil.getJavaComplianceLevel(project);
if ("1.5".equals(complianceLevel))
{
jreContainer +=
"/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5";
}
else if ("1.6".equals(complianceLevel))
{
jreContainer +=
"/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6";
}
classpathEntries.add(JavaCore.newContainerEntry(new
Path(jreContainer)));
}
if ((style & EMF_EMPTY_PROJECT_STYLE) == 0)
{
if ((style & EMF_PLUGIN_PROJECT_STYLE) != 0)
{
classpathEntries.add(JavaCore.newContainerEntry(new
Path("org.eclipse.pde.core.requiredPlugins")));
// Remove variables since the plugin.xml should
provide the complete path information.
//
for (Iterator<IClasspathEntry> i =
classpathEntries.iterator(); i.hasNext(); )
{
IClasspathEntry classpathEntry = i.next();
if (classpathEntry.getEntryKind() ==
IClasspathEntry.CPE_VARIABLE &&
!JavaRuntime.JRELIB_VARIABLE.equals(classpathEntry.getPath().toString())
||
classpathEntry.getEntryKind() ==
IClasspathEntry.CPE_PROJECT)
{
i.remove();
}
}
}
else
{
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"ECLIPSE_CORE_RUNTIME", "org.eclipse.core.runtime");
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"ECLIPSE_CORE_RESOURCES", "org.eclipse.core.resources");
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"EMF_COMMON", "org.eclipse.emf.common");
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"EMF_ECORE", "org.eclipse.emf.ecore");
if ((style & EMF_XML_PROJECT_STYLE) != 0)
{
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"EMF_ECORE_XMI", "org.eclipse.emf.ecore.xmi");
}
if ((style & EMF_MODEL_PROJECT_STYLE) == 0)
{
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"EMF_EDIT", "org.eclipse.emf.edit");
if ((style & EMF_EDIT_PROJECT_STYLE) == 0)
{
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"ECLIPSE_SWT", "org.eclipse.swt");
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"ECLIPSE_JFACE", "org.eclipse.jface");
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"ECLIPSE_UI_VIEWS", "org.eclipse.ui.views");
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"ECLIPSE_UI_EDITORS", "org.eclipse.ui.editors");
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"ECLIPSE_UI_IDE", "org.eclipse.ui.ide");
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"ECLIPSE_UI_WORKBENCH", "org.eclipse.ui.workbench");
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"EMF_COMMON_UI", "org.eclipse.emf.common.ui");
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"EMF_EDIT_UI", "org.eclipse.emf.edit.ui");
if ((style & EMF_XML_PROJECT_STYLE) == 0)
{
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"EMF_ECORE_XMI", "org.eclipse.emf.ecore.xmi");
}
}
}
if ((style & EMF_TESTS_PROJECT_STYLE) != 0)
{
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"JUNIT", "org.junit");
}
if (pluginVariables != null)
{
for (Iterator<?> i = pluginVariables.iterator();
i.hasNext(); )
{
Object variable = i.next();
if (variable instanceof IClasspathEntry)
{
classpathEntries.add((IClasspathEntry)variable);
}
else if (variable instanceof String)
{
String pluginVariable = (String)variable;
String name;
String id;
int index = pluginVariable.indexOf("=");
if (index == -1)
{
name =
pluginVariable.replace('.','_').toUpperCase();
id = pluginVariable;
}
else
{
name = pluginVariable.substring(0, index);
id = pluginVariable.substring(index + 1);
}
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries, name, id);
}
}
}
}
}
javaProject.setRawClasspath
(classpathEntries.toArray(new
IClasspathEntry[classpathEntries.size()]),
new SubProgressMonitor(progressMonitor, 1));
}
if (isInitiallyEmpty)
{
javaProject.setOutputLocation(new Path("/" +
javaSource.segment(0) + "/bin"), new
SubProgressMonitor(progressMonitor, 1));
}
}
catch (Exception exception)
{
exception.printStackTrace();
CodeGenEcorePlugin.INSTANCE.log(exception);
}
finally
{
progressMonitor.done();
}
return project;
}
hannes wrote:I am writing a plugin generator. the generated plugin is an editor. the Idea is, you walk trough a wizard, and afterwards, you have a generated plugin.
The code is generated, but now I have following problem,
"The project was not built since its build path is incomplete. Cannot find the class file for java.lang.Object. Fix the build path then try building this project"
what should I do, in which file can I specify the build-path?
thanx in advance.
Hannes