aboutsummaryrefslogtreecommitdiffstats
path: root/org.openembedded.bc.ui/src/org/openembedded/bc/ui/wizards/newproject/CreateBBCProjectOperation.java
blob: 9e67e5ba1bd731dc2aad4fdb4dedca91488db512 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*****************************************************************************
 * Copyright (c) 2009 Ken Gilmer
 * 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
 *
 * Contributors:
 *     Ken Gilmer - initial API and implementation
 *******************************************************************************/
package org.openembedded.bc.ui.wizards.newproject;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Vector;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.openembedded.bc.bitbake.ProjectInfoHelper;
import org.openembedded.bc.ui.Activator;
import org.openembedded.bc.ui.builder.BitbakeCommanderNature;
import org.openembedded.bc.ui.model.ProjectInfo;



/**
 * Creates a bbc project
 * @author kgilmer
 *
 */
public class CreateBBCProjectOperation extends WorkspaceModifyOperation {

	public static final String OEFS_SCHEME = "OEFS://";
	public static final QualifiedName BBC_PROJECT_INIT = new QualifiedName(null, "BBC_PROJECT_INIT");
	public static void addNatureToProject(IProject proj, String nature_id, IProgressMonitor monitor) throws CoreException {
		IProjectDescription desc = proj.getDescription();
		Vector natureIds = new Vector();
		
		natureIds.add(nature_id);
		natureIds.addAll(Arrays.asList(desc.getNatureIds()));
		desc.setNatureIds((String[]) natureIds.toArray(new String[natureIds.size()]));
		
		proj.setDescription(desc, monitor);
	}
	
	private ProjectInfo projInfo;

	public CreateBBCProjectOperation(ProjectInfo projInfo) {
		this.projInfo = projInfo;
	}
	
	protected void addNatures(IProject proj, IProgressMonitor monitor) throws CoreException {
		addNatureToProject(proj, BitbakeCommanderNature.NATURE_ID, monitor);
	}

	private IProjectDescription createProjectDescription(IWorkspace workspace, ProjectInfo projInfo2) throws CoreException {
		IProjectDescription desc = workspace.newProjectDescription(projInfo2.getProjectName());
		
		try {
			System.out.println("Calling creageProjeDesc");
			desc.setLocationURI(new URI(OEFS_SCHEME + projInfo2.getRootPath()));
		} catch (URISyntaxException e) {
			throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Unable to load filesystem.", e));
		}
		
		return desc;
	}

	@Override
	protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
		System.out.println("Init: " + projInfo.getInitScriptPath());
		System.out.println("Proj: " + projInfo.getProjectName());
		System.out.println("Root: " + projInfo.getRootPath());
		
		IProjectDescription desc = createProjectDescription(ResourcesPlugin.getWorkspace(), projInfo);
		
		IWorkspaceRoot wsroot = ResourcesPlugin.getWorkspace().getRoot();

		IProject proj = wsroot.getProject(projInfo.getProjectName());
		proj.create(desc, monitor);
		try {
			ProjectInfoHelper.store(proj.getLocationURI().getPath(), projInfo);
		} catch (IOException e) {
			throw new InvocationTargetException(e);
		}
		
		proj.open(monitor);

		addNatures(proj, monitor);
	}
	
	public ProjectInfo getProjectInfo() {
		return projInfo;
	}
}