Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010 itemis AG (http://www.itemis.eu) 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 /
  
  package org.eclipse.xtext.ui.util;
  
 import java.util.List;
 
 import  org.eclipse.core.resources.ICommand;
 import  org.eclipse.core.resources.IContainer;
 import  org.eclipse.core.resources.IFile;
 import  org.eclipse.core.resources.IFolder;
 import  org.eclipse.core.resources.IProject;
 import  org.eclipse.core.resources.IProjectDescription;
 import  org.eclipse.core.resources.IWorkspace;
 import  org.eclipse.core.runtime.CoreException;
 import  org.eclipse.core.runtime.IPath;
 import  org.eclipse.core.runtime.IProgressMonitor;
 import  org.eclipse.core.runtime.NullProgressMonitor;
 import  org.eclipse.core.runtime.Path;
 import  org.eclipse.core.runtime.Platform;
 import  org.eclipse.core.runtime.SubMonitor;
 import  org.eclipse.jface.dialogs.MessageDialog;
 import  org.eclipse.swt.widgets.Shell;
 import  org.eclipse.ui.IWorkbench;
 import  org.eclipse.ui.IWorkingSet;
 

Author(s):
Holger Schill - Initial contribution and API
Sebastian Zarnekow
 
 public class ProjectFactory {
 
 	private static final Logger logger = Logger.getLogger(ProjectFactory.class);
 
 	@Inject(optional = true)
 	protected IWorkbench workbench;
 
 	protected IWorkspace workspace;
 
 	protected String projectName;
 	protected IPath location;
 	protected List<Stringfolders;
 	protected List<IProject> referencedProjects;
 	protected List<StringprojectNatures;
 	protected List<StringbuilderIds;
 	protected List<IWorkingSet> workingSets;

Since:
2.4
 
 	protected String defaultCharset;
 
 
 	public ProjectFactory addBuilderIds(String... builderIds) {
 		if (this. == null)
 			this. = Lists.newArrayList();
 		this..addAll(Arrays.asList(builderIds));
 		return this;
 	}
 
 	public ProjectFactory addProjectNatures(String... projectNatures) {
 		if (this. == null)
 			this. = Lists.newArrayList();
 		this..addAll(Arrays.asList(projectNatures));
 		return this;
 	}
 
 	public ProjectFactory setProjectName(String projectName) {
 		this. = projectName;
 		return this;
 	}
 
 	public ProjectFactory setLocation(IPath location) {
 		this. = location;
 		return this;
 	}
 
 	public ProjectFactory addFolders(List<Stringfolders) {
 		if (this. == null)
 			this. = Lists.newArrayList();
 		this..addAll(folders);
 		return this;
 	}
 
 	public ProjectFactory addReferencedProjects(List<IProject> referencedProjects) {
 		if (this. == null)
		this..addAll(referencedProjects);
		return this;
	}
	public ProjectFactory addWorkingSets(List<IWorkingSet> workingSets) {
		if (this. == null)
			this. = Lists.newArrayList();
		this..addAll(workingSets);
		return this;
	}

Since:
2.3
	public void addContributor(IProjectFactoryContributor projectFactoryContributor) {
		if (this. == null) {
		}
		.add(projectFactoryContributor);
	}

Since:
2.4
	public void setProjectDefaultCharset(String encoding) {
		this. = encoding;
	}
	public IProject createProject(IProgressMonitor monitor, Shell shell) {
		final SubMonitor subMonitor = SubMonitor.convert(monitor, 10);
		try {
			final IProjectDescription description = createProjectDescription();
			subMonitor.subTask(. + description.getName());
			final IProject project = .getRoot().getProject();
			if (!deleteExistingProject(projectshellsubMonitor)) {
				return null;
			}
			project.create(descriptionsubMonitor.newChild(1));
			project.open(subMonitor.newChild(1));
			project.setDescription(descriptionsubMonitor.newChild(1));
			project.setDefaultCharset(subMonitor.newChild(1));
			createFolders(projectsubMonitorshell);
			enhanceProject(projectsubMonitorshell);
			if ( != null) {
				IFileCreator fileCreator = new IFileCreator() {
					public IFile writeToFile(CharSequence charsString fileName) {
						return ProjectFactory.this.writeToFile(charsfileNameprojectsubMonitor);
					}
				};
				for (IProjectFactoryContributor contributor : ) {
					contributor.contributeFiles(projectfileCreator);
				}
			}
			return project;
catch (final CoreException exception) {
			.error(exception.getMessage(), exception);
			return null;
finally {
			subMonitor.done();
		}
	}
	protected void createFolders(IProject project, SubMonitor subMonitor, Shell shellthrows CoreException {
		if ( != null) {
			for (final String folderName : ) {
				final IFolder folder = project.getFolder(folderName);
				if (!folder.exists()) {
					folder.create(falsetruesubMonitor.newChild(1));
				}
			}
		}
	}
	protected void enhanceProject(final IProject projectfinal SubMonitor subMonitor, Shell shell)
			throws CoreException {
		if ( != null &&  != null) {
			.getWorkingSetManager().addToWorkingSets(project,
					.toArray(new IWorkingSet[.size()]));
		}
	}
	protected boolean deleteExistingProject(IProject projectfinal Shell theShell, SubMonitor subMonitor)
			throws CoreException {
		// Clean up any old project information.
		if (project.exists()) {
			final boolean[] result = new boolean[1];
			if ( != null && theShell != null) {
				.getDisplay().syncExec(new Runnable() {
					public void run() {
						result[0] = MessageDialog.openQuestion(theShell. + ,
					}
				});
else {
				result[0] = true;
			}
			if (result[0]) {
				project.delete(truetruesubMonitor.newChild(1));
else {
				return false;
			}
		}
		return true;
	}
	protected IProjectDescription createProjectDescription() {
		final IProjectDescription projectDescription = .newProjectDescription();
		if ( != null && !Platform.getLocation().equals(.removeLastSegments(1))) {
			projectDescription.setLocation();
		}
		if ( != null && .size() != 0) {
			projectDescription
					.setReferencedProjects(.toArray(new IProject[.size()]));
		}
		if ( != null)
			projectDescription.setNatureIds(.toArray(new String[.size()]));
		if ( != null)
			setBuilder(projectDescription.toArray(new String[.size()]));
		return projectDescription;
	}
	protected void setBuilder(final IProjectDescription projectDescriptionfinal String[] builders) {
		List<ICommand> commands = Lists.newArrayList();
		for (int i = 0; i < builders.lengthi++) {
			ICommand command = projectDescription.newCommand();
			command.setBuilderName(builders[i]);
			commands.add(command);
		}
		projectDescription.setBuildSpec(commands.toArray(new ICommand[commands.size()]));
	}
	protected IFile createFile(final String namefinal IContainer containerfinal String content,
			final IProgressMonitor progressMonitor) {
		final IFile file = container.getFile(new Path(name));
		createRecursive(file.getParent());
		SubMonitor subMonitor = SubMonitor.convert(progressMonitor, 1);
		try {
			final InputStream stream = new ByteArrayInputStream(content.getBytes(file.getCharset()));
			if (file.exists()) {
				.debug("Overwriting content of '" + file.getFullPath() + "'");
				file.setContents(streamtruetruesubMonitor.newChild(1));
else {
				file.create(streamtruesubMonitor.newChild(1));
			}
			stream.close();
catch (final Exception e) {
finally {
			subMonitor.done();
		}
		return file;
	}

Shortcut method to be used with Xtend rich strings

Since:
2.3
	protected IFile writeToFile(CharSequence chrSeqString fileName, IProject project, IProgressMonitor progrMonitor) {
		return createFile(fileNameprojectchrSeq.toString(), progrMonitor);
	}
	protected void createRecursive(final IContainer resource) {
		if (!resource.exists()) {
			if (!resource.getParent().exists()) {
				createRecursive(resource.getParent());
			}
			if (resource instanceof IFolder) {
				try {
					((IFolder) resource).create(falsetruenew NullProgressMonitor());
catch (CoreException e) {
					.error(e.getMessage(), e);
				}
			}
		}
	}
New to GrepCode? Check out our FAQ X