List of usage examples for org.eclipse.jdt.core IJavaProject setRawClasspath
void setRawClasspath(IClasspathEntry[] entries, IProgressMonitor monitor) throws JavaModelException;
From source file:org.eclipse.emf.cdo.dawn.codegen.util.ProjectCreationHelper.java
License:Open Source License
/** * @param javaProject/*from ww w . j a va 2 s .c o m*/ * @throws JavaModelException */ private void clearSourcePath(IJavaProject javaProject) throws JavaModelException { javaProject.setRawClasspath(new IClasspathEntry[] {}, new NullProgressMonitor()); // clean classpath, means remove // Project root from classpath }
From source file:org.eclipse.emf.cheatsheets.actions.NewJavaProjectAction.java
License:Open Source License
/** * Create a new Java project// ww w. j av a 2s .c o m * @param projectName Name of the project * @param monitor Monitoring the action * @return Java project */ @Override protected IProject createProject(String projectName, IProgressMonitor monitor) throws CoreException { monitor.beginTask( CheatSheetsPlugin.INSTANCE.getString("_UI_CreateJavaProject_message", new String[] { projectName }), 5); IProject project = super.createProject(projectName, BasicMonitor.subProgress(monitor, 1)); if (project != null) { IProjectDescription description = project.getDescription(); if (!description.hasNature(JavaCore.NATURE_ID)) { IJavaProject javaProject = JavaCore.create(project); if (javaProject != null) { String[] natures = description.getNatureIds(); String[] javaNatures = new String[natures.length + 1]; System.arraycopy(natures, 0, javaNatures, 0, natures.length); javaNatures[natures.length] = JavaCore.NATURE_ID; description.setNatureIds(javaNatures); project.setDescription(description, BasicMonitor.subProgress(monitor, 1)); IFolder sourceFolder = project.getFolder(SOURCE_FOLDER); if (!sourceFolder.exists()) { sourceFolder.create(true, true, BasicMonitor.subProgress(monitor, 1)); } javaProject.setOutputLocation(project.getFolder(OUTPUT_FOLDER).getFullPath(), BasicMonitor.subProgress(monitor, 1)); IClasspathEntry[] entries = new IClasspathEntry[] { JavaCore.newSourceEntry(sourceFolder.getFullPath()), JavaCore.newContainerEntry(new Path("org.eclipse.jdt.launching.JRE_CONTAINER")) }; javaProject.setRawClasspath(entries, BasicMonitor.subProgress(monitor, 1)); } } } monitor.done(); return project; }
From source file:org.eclipse.emf.ecore.xcore.ui.EmptyXcoreProjectWizard.java
License:Open Source License
@Override public void modifyWorkspace(IProgressMonitor progressMonitor) throws CoreException, UnsupportedEncodingException, IOException { super.modifyWorkspace(progressMonitor); IProjectDescription projectDescription = project.getDescription(); String[] natureIds = projectDescription.getNatureIds(); String[] newNatureIds = new String[natureIds.length + 1]; System.arraycopy(natureIds, 0, newNatureIds, 0, natureIds.length); newNatureIds[natureIds.length] = XtextProjectHelper.NATURE_ID; projectDescription.setNatureIds(newNatureIds); project.setDescription(projectDescription, progressMonitor); IJavaProject javaProject = JavaCore.create(project); IClasspathEntry[] classpath = javaProject.getRawClasspath(); IClasspathEntry[] newClasspath = new IClasspathEntry[classpath.length + 1]; for (int i = 0, index = 0, length = newClasspath.length; index < length; ++i, ++index) { newClasspath[index] = classpath[i]; if (classpath[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) { IPath path = classpath[i].getPath(); IPath srcGenPath = path.removeLastSegments(1).append(path.lastSegment() + "-gen"); IClasspathEntry srcGen = JavaCore.newSourceEntry(srcGenPath); CodeGenUtil.EclipseUtil.findOrCreateContainer(srcGenPath, true, genModelProjectLocation, progressMonitor);//ww w .ja v a 2 s . c om newClasspath[++index] = srcGen; } } javaProject.setRawClasspath(newClasspath, progressMonitor); }
From source file:org.eclipse.emf.ecore.xcore.ui.quickfix.XcoreClasspathUpdater.java
License:Open Source License
protected boolean addJarToClasspath(IJavaProject javaProject, IPath location, IProgressMonitor monitor) throws JavaModelException { IClasspathEntry newClasspthEntry = JavaCore.newLibraryEntry(location, null, null); IClasspathEntry[] rawClasspath = javaProject.getRawClasspath(); IClasspathEntry[] newRawClasspath = new IClasspathEntry[rawClasspath.length + 1]; for (int i = 0; i < rawClasspath.length; ++i) { IClasspathEntry entry = rawClasspath[i]; if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY && entry.getPath().equals(location)) { return false; }//from w w w. j a v a 2 s. com newRawClasspath[i + 1] = entry; } newRawClasspath[0] = newClasspthEntry; javaProject.setRawClasspath(newRawClasspath, monitor); return true; }
From source file:org.eclipse.emf.search.codegen.engine.AbstractModelSearchCodeGenerator.java
License:Open Source License
/** * Create an empty EMF Project with default values. * //from w w w. j a v a 2 s . c om * @param projectName the name of the project to create. * @return the newly created project */ protected static IProject createEMFProject(String projectName) { IPath javaSource = new Path(IPath.SEPARATOR + projectName + IPath.SEPARATOR + SOURCE_DIRECTORY); IPath projectLocationPath = null; IProgressMonitor progressMonitor = new NullProgressMonitor(); IProject project = null; try { List<IClasspathEntry> classpathEntries = new UniqueEList<IClasspathEntry>(); project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); IJavaProject javaProject = JavaCore.create(project); IProjectDescription projectDescription = null; if (!project.exists()) { projectDescription = project.getWorkspace().newProjectDescription(projectName); projectDescription.setLocation(projectLocationPath); project.create(projectDescription, new NullProgressMonitor()); } else { projectDescription = project.getDescription(); classpathEntries.addAll(Arrays.asList(javaProject.getRawClasspath())); } boolean isInitiallyEmpty = classpathEntries.isEmpty(); // add the natures String[] natureIds = addDefaultNatures(projectDescription); projectDescription.setNatureIds(natureIds); // add the builders ICommand[] builders = addDefaultBuilders(projectDescription); projectDescription.setBuildSpec(builders); // open the project and apply the new Descriptions project.open(new NullProgressMonitor()); project.setDescription(projectDescription, new NullProgressMonitor()); // initialize the directory which will contains the sources 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 = (IClasspathEntry) 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 = (IClasspathEntry) i.next(); if (classpathEntry.getPath().isPrefixOf(jreClasspathEntry.getPath())) { i.remove(); } } classpathEntries.add(jreClasspathEntry); classpathEntries.add(JavaCore.newContainerEntry(new Path("org.eclipse.pde.core.requiredPlugins"))); //$NON-NLS-1$ } javaProject.setRawClasspath( (IClasspathEntry[]) classpathEntries.toArray(new IClasspathEntry[classpathEntries.size()]), new SubProgressMonitor(progressMonitor, 1)); } catch (CoreException e) { IStatus s = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.getString("AbstractEMFCodeGenerator.PluginCreationError"), e); //$NON-NLS-1$ Activator.getDefault().getLog().log(s); } finally { progressMonitor.done(); } return project; }
From source file:org.eclipse.emf.texo.eclipse.generator.TexoGeneratorApplication.java
License:Open Source License
private IJavaProject createJavaProject() { try {/*from w ww . ja va2 s . com*/ final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); final IProgressMonitor progressMonitor = new NullProgressMonitor(); final IProject project = root.getProject(JAVA_PROJECT_NAME); if (project.exists()) { project.delete(true, progressMonitor); } project.create(progressMonitor); project.open(progressMonitor); // swt the java nature final IProjectDescription description = project.getDescription(); String[] natures = description.getNatureIds(); String[] newNatures = new String[natures.length + 1]; System.arraycopy(natures, 0, newNatures, 0, natures.length); newNatures[natures.length] = JavaCore.NATURE_ID; description.setNatureIds(newNatures); project.setDescription(description, progressMonitor); // create the javaproject final IJavaProject javaProject = JavaCore.create(project); // set the classpath final Set<IClasspathEntry> entries = new HashSet<IClasspathEntry>(); entries.addAll(Arrays.asList(javaProject.getRawClasspath())); entries.add(JavaRuntime.getDefaultJREContainerEntry()); javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), progressMonitor); return javaProject; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.eclipse.etrice.core.ui.newwizard.ProjectCreator.java
License:Open Source License
public static IProject createETriceProject(IPath javaSource, IPath javaSourceGen, URI projectLocationURI, IProject runtimeProject, List<String> naturesToAdd, List<String> buildersToAdd, Monitor monitor) { IProgressMonitor progressMonitor = BasicMonitor.toIProgressMonitor(monitor); String projectName = javaSource.segment(0); IProject project = null;/*from w w w . j av a 2 s.c om*/ try { List<IClasspathEntry> classpathEntries = new UniqueEList<IClasspathEntry>(); progressMonitor.beginTask("", 10); progressMonitor.subTask("Creating eTrice project " + 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)); project.open(new SubProgressMonitor(progressMonitor, 1)); } else { projectDescription = project.getDescription(); project.open(new SubProgressMonitor(progressMonitor, 1)); if (project.hasNature(JavaCore.NATURE_ID)) { classpathEntries.addAll(Arrays.asList(javaProject.getRawClasspath())); } } boolean isInitiallyEmpty = classpathEntries.isEmpty(); { ArrayList<IProject> referencedProjects = new ArrayList<IProject>(); if (runtimeProject != null) referencedProjects.add(runtimeProject); if (!referencedProjects.isEmpty()) { 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[0]; } for (String nature : naturesToAdd) { natureIds = addNature(nature, natureIds, project); } projectDescription.setNatureIds(natureIds); ICommand[] builders = projectDescription.getBuildSpec(); if (builders == null) { builders = new ICommand[0]; } for (String builder : buildersToAdd) { builders = addBuilder(builder, builders, projectDescription); } projectDescription.setBuildSpec(builders); project.setDescription(projectDescription, new SubProgressMonitor(progressMonitor, 1)); createSrcFolder(progressMonitor, project, classpathEntries, javaSource); createSrcFolder(progressMonitor, project, classpathEntries, javaSourceGen); if (isInitiallyEmpty) { 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; jreContainer += "/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"; classpathEntries.add(JavaCore.newContainerEntry(new Path(jreContainer))); } 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)); } 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 e) { Logger.getLogger(ProjectCreator.class).error(e.getMessage(), e); } finally { progressMonitor.done(); } return project; }
From source file:org.eclipse.fx.ide.jdt.ui.internal.wizard.JavaFXProjectWizard.java
License:Open Source License
@Override public boolean performFinish() { boolean res = super.performFinish(); if (res) {// www. j a v a 2 s . c om final IJavaElement newElement = getCreatedElement(); IWorkingSet[] workingSets = fFirstPage.getWorkingSets(); if (workingSets.length > 0) { PlatformUI.getWorkbench().getWorkingSetManager().addToWorkingSets(newElement, workingSets); } try { IJavaProject p = (IJavaProject) newElement; IClasspathEntry[] current = p.getRawClasspath(); int i = current.length + 1; if (projectData.mainApp.equals(NewJavaFXProjectWizardPageThree.MOBILE)) { i += 1; } IClasspathEntry[] currentFX = new IClasspathEntry[i]; System.arraycopy(current, 0, currentFX, 0, current.length); currentFX[current.length] = JavaCore.newContainerEntry(JavaFXCore.JAVAFX_CONTAINER_PATH); if (projectData.mainApp.equals(NewJavaFXProjectWizardPageThree.MOBILE)) { currentFX[current.length + 1] = JavaCore.newContainerEntry(JavaFXCore.MOBILE_CONTAINER_PATH); } p.setRawClasspath(currentFX, new NullProgressMonitor()); } catch (JavaModelException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } if (projectData.mainApp.equals(NewJavaFXProjectWizardPageThree.DESKTOP)) { final IFile buildFile = fSecondPage.getJavaProject().getProject() .getFile(new Path("build.fxbuild")); AntTask task = AntTasksFactory.eINSTANCE.createAntTask(); task.setBuildDirectory("${project}/build"); task.setDeploy(AntTasksFactory.eINSTANCE.createDeploy()); task.getDeploy().setApplication(ParametersFactory.eINSTANCE.createApplication()); task.getDeploy().getApplication().setName(fFirstPage.getProjectName()); task.getDeploy().setInfo(ParametersFactory.eINSTANCE.createInfo()); task.setSignjar(AntTasksFactory.eINSTANCE.createSignJar()); final XMIResource resource = new XMIResourceImpl(); resource.getContents().add(task); WorkspaceModifyOperation operation = new WorkspaceModifyOperation() { @Override public void execute(IProgressMonitor monitor) { if (!resource.getContents().isEmpty()) { Map<Object, Object> options = new HashMap<Object, Object>(); options.put(XMIResource.OPTION_USE_XMI_TYPE, Boolean.TRUE); ByteArrayOutputStream streamOut = null; ByteArrayInputStream streamIn = null; try { streamOut = new ByteArrayOutputStream(); resource.save(streamOut, options); streamIn = new ByteArrayInputStream(streamOut.toByteArray()); buildFile.create(streamIn, true, monitor); } catch (IOException | CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { options.clear(); if (streamOut != null) { try { streamOut.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (streamIn != null) { try { streamIn.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } }; try { new ProgressMonitorDialog(getShell()).run(true, false, operation); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { IJavaProject p = fSecondPage.getJavaProject(); IPackageFragment f = p.getPackageFragments()[0]; IPath path = f.getPath(); for (String s : projectData.packageName.split("\\.")) { path = path.append(s); p.getProject().getWorkspace().getRoot().getFolder(path).create(true, true, null); } { IFile cssFile = p.getProject().getWorkspace().getRoot().getFile(path.append("application.css")); ByteArrayInputStream in = new ByteArrayInputStream( "/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */" .getBytes()); cssFile.create(in, IFile.FORCE | IFile.KEEP_HISTORY, null); in.close(); } if (projectData.mainApp.equals(NewJavaFXProjectWizardPageThree.DESKTOP)) { IFile mainClass = p.getProject().getWorkspace().getRoot().getFile(path.append("Main.java")); ByteArrayInputStream in = new ByteArrayInputStream( new FXProjectMainClassTemplate().generate(projectData).toString().getBytes()); mainClass.create(in, IFile.FORCE | IFile.KEEP_HISTORY, null); in.close(); } else if (projectData.mainApp.equals(NewJavaFXProjectWizardPageThree.MOBILE)) { IFile mainClass = p.getProject().getWorkspace().getRoot().getFile(path.append("Main.java")); ByteArrayInputStream in = new ByteArrayInputStream( new FXProjectMainMobileClassTemplate().generate(projectData).toString().getBytes()); mainClass.create(in, IFile.FORCE | IFile.KEEP_HISTORY, null); in.close(); } if (!projectData.declarativeUiType.equals("None")) { if (!projectData.declarativeUiController.trim().isEmpty()) { IFile ctrlClass = p.getProject().getWorkspace().getRoot() .getFile(path.append(projectData.declarativeUiController + ".java")); ByteArrayInputStream in = new ByteArrayInputStream( new FXProjectCtrlClassTemplate().generate(projectData).toString().getBytes()); ctrlClass.create(in, IFile.FORCE | IFile.KEEP_HISTORY, null); in.close(); } IFile declarativeUi = p.getProject().getWorkspace().getRoot() .getFile(path.append(projectData.declarativeUiName + "." + (projectData.declarativeUiType.endsWith("FXML") ? "fxml" : "fxgraph"))); ByteArrayInputStream in = new ByteArrayInputStream( (projectData.declarativeUiType.endsWith("FXML") ? new FXProjectFXMLTemplate().generate(projectData).toString().getBytes() : new FXProjectFXGraphTemplate().generate(projectData).toString().getBytes())); declarativeUi.create(in, IFile.FORCE | IFile.KEEP_HISTORY, null); in.close(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } BasicNewProjectResourceWizard.updatePerspective(fConfigElement); selectAndReveal(fSecondPage.getJavaProject().getProject()); Display.getDefault().asyncExec(new Runnable() { public void run() { IWorkbenchPart activePart = getActivePart(); if (activePart instanceof IPackagesViewPart) { PackageExplorerPart view = PackageExplorerPart.openInActivePerspective(); view.tryToReveal(newElement); } } }); } return res; }
From source file:org.eclipse.gemoc.execution.concurrent.ccsljavaxdsml.ui.builder.ToggleNatureAction.java
License:Open Source License
public void addSourceFolder(IProject project, String folder) { try {/*from www.java 2s. c om*/ IJavaProject javaProject = JavaCore.create(project); IClasspathEntry[] entries = javaProject.getRawClasspath(); IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1]; System.arraycopy(entries, 0, newEntries, 0, entries.length); IPath srcPath = javaProject.getPath().append(folder); IClasspathEntry srcEntry = JavaCore.newSourceEntry(srcPath, null); boolean entryfound = false; for (IClasspathEntry cpe : entries) { if (cpe.equals(srcEntry)) { entryfound = true; } } if (!entryfound) { newEntries[entries.length] = JavaCore.newSourceEntry(srcEntry.getPath()); javaProject.setRawClasspath(newEntries, null); } } catch (JavaModelException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:org.eclipse.gmf.internal.common.codegen.GeneratorBase.java
License:Open Source License
/** * @param javaSource workspace absolute path to java source folder of the generated project, e.g. '/org.sample.aaa/sources'. * @param projectLocation {@link IPath} to folder where <code>.project</code> file would reside. Use <code>null</code> to use default workspace location. * @param referencedProjects collection of {@link IProject} * @throws UnexpectedBehaviourException something goes really wrong * @throws InterruptedException user canceled operation *//*from ww w. j a v a 2s . c o m*/ protected final void initializeEditorProject(IPath javaSource, IPath projectLocation, List<IProject> referencedProjects) throws UnexpectedBehaviourException, InterruptedException { myDestProject = ResourcesPlugin.getWorkspace().getRoot().getProject(javaSource.segment(0)); final int style = org.eclipse.emf.codegen.ecore.Generator.EMF_PLUGIN_PROJECT_STYLE; // pluginVariables is NOT used when style is EMF_PLUGIN_PROJECT_STYLE final List<?> pluginVariables = null; final IProgressMonitor pm = getNextStepMonitor(); setProgressTaskName(Messages.initproject); org.eclipse.emf.codegen.ecore.Generator.createEMFProject(javaSource, projectLocation, referencedProjects, pm, style, pluginVariables); try { final IJavaProject jp = JavaCore.create(myDestProject); myDestRoot = jp.findPackageFragmentRoot(javaSource); // createEMFProject doesn't create source entry in case project exists and has some classpath entries already, // though the folder gets created. if (myDestRoot == null) { IClasspathEntry[] oldCP = jp.getRawClasspath(); IClasspathEntry[] newCP = new IClasspathEntry[oldCP.length + 1]; System.arraycopy(oldCP, 0, newCP, 0, oldCP.length); newCP[oldCP.length] = JavaCore.newSourceEntry(javaSource); jp.setRawClasspath(newCP, new NullProgressMonitor()); myDestRoot = jp.findPackageFragmentRoot(javaSource); } } catch (JavaModelException ex) { throw new UnexpectedBehaviourException(ex.getMessage()); } if (myDestRoot == null) { throw new UnexpectedBehaviourException("no source root can be found"); } }