Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 422bde58e80bc9eb70003f5a84fbf23eb64d0f10 (plain) (blame)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*******************************************************************************
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * 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.etrice.generator.ui.wizard;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.util.BasicMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jdt.core.IAccessRule;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.part.ISetSelectionTarget;

/**
 * @author Henrik Rentz-Reichert (initial contribution and API)
 *
 */
public class EmptyProjectWizard extends Wizard implements INewWizard {
	protected IWorkbench workbench;
	protected IPath projectLocation;
	protected IPath sourcePath;
	protected IPath sourceGenPath;
	protected IProject project;
	protected IProject runtimeProject;
	protected String initialProjectName;
	protected URI modelURI;
	private EmptyProjectConfigPage config;
	
	private static final String[] additionalLaunchConfigLines = new String[] {
		"<stringAttribute key=\"org.eclipse.debug.core.ATTR_REFRESH_SCOPE\" value=\"${workspace}\"/>"
	};

	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.workbench = workbench;
		setDefaultPageImageDescriptor(ProjectCreator.getImageDescriptor("icons/NewETriceProjectWizban.gif"));
		setWindowTitle("New Empty eTrice Project");

		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		runtimeProject = workspace.getRoot().getProject("org.eclipse.etrice.runtime.java");
	}

	@Override
	public void addPages() {
		WizardNewProjectCreationPage newProjectCreationPage = new WizardNewProjectCreationPage(
				"NewProjectCreationPage") {
			@Override
			protected boolean validatePage() {
				if (super.validatePage()) {
					IPath locationPath = getLocationPath();
					projectLocation = Platform.getLocation().equals(
							locationPath) ? null : locationPath;
					IPath projectPath = getProjectHandle().getFullPath();
					sourcePath = projectPath.append("src");
					sourceGenPath = projectPath.append("src-gen");
					return true;
				} else {
					return false;
				}
			}
		};

		newProjectCreationPage.setInitialProjectName(initialProjectName);
		newProjectCreationPage.setTitle("Empty eTrice Project");
		newProjectCreationPage
				.setDescription("Create an empty Java project with eTrice dependencies");
		addPage(newProjectCreationPage);
		
		config = new EmptyProjectConfigPage("config", runtimeProject);
		config.setTitle("Project Configuration");
		config.setDescription("Choose a build type for the project");
		addPage(config);
	}
	
	@Override
	public boolean performFinish() {
		WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {

			@Override
			protected void execute(IProgressMonitor progressMonitor) {
				try {
					if (!config.useJDTBuild())
						runtimeProject = null;
					
					ArrayList<String> natures = new ArrayList<String>(ProjectCreator.getCommonNatureIDs());
					if (config.useMVNBuild())
						natures.add("org.eclipse.m2e.core.maven2Nature");

					ArrayList<String> builders = new ArrayList<String>(ProjectCreator.getCommonBuilderIDs());
					if (config.useMVNBuild())
						builders.add("org.eclipse.m2e.core.maven2Builder");
					
					ArrayList<IClasspathEntry> pathEntries = new ArrayList<IClasspathEntry>();
					if (config.useMVNBuild()) {
						IClasspathEntry mvnContainer = JavaCore.newContainerEntry(
								new Path("org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"),
								new IAccessRule[] {},
								new IClasspathAttribute[] {JavaCore.newClasspathAttribute("maven.pomderived", "true")},
								false);
						pathEntries.add(mvnContainer);
					}
					
					URI modelProjectURI = (projectLocation==null) ? null : URI.createFileURI(projectLocation.toOSString());
					project = ProjectCreator.createETriceProject(
							new Path(sourcePath.toString()),
							new Path(sourceGenPath.toString()),
							modelProjectURI,
							runtimeProject,
							natures,
							builders,
							pathEntries,
							BasicMonitor.toMonitor(progressMonitor)
						);

					String baseName = sourcePath.segment(0);
					ProjectCreator.findOrCreateContainer(new Path("/"
							+ baseName + "/model"),
							true, projectLocation, progressMonitor);
					modelURI = URI.createPlatformResourceURI("/"
							+ baseName
							+ "/model/"+baseName+".room", true);
					ProjectCreator.createModel(modelURI,
							baseName);

					URI physModelURI = URI.createPlatformResourceURI("/"
							+ baseName
							+ "/model/"+baseName+".etphys", true);
					ProjectCreator.createPhysicalModel(physModelURI,
							baseName);

					URI mapModelURI = URI.createPlatformResourceURI("/"
							+ baseName
							+ "/model/"+baseName+".etmap", true);
					ProjectCreator.createMappingModel(mapModelURI,
							baseName);
					
					ProjectCreator.createBuildProperties(URI.createPlatformResourceURI("/"
							+baseName+"/build.properties", true),
							baseName);

					ProjectCreator.createLaunchGeneratorConfig(URI.createPlatformResourceURI("/"
							+baseName+"/gen_"+baseName+".launch", true),
							"java",
							"/"+baseName+"/model",
							baseName,
							additionalLaunchConfigLines);

					ProjectCreator.createLaunchJavaApplicationConfig(URI.createPlatformResourceURI("/"
							+baseName+"/run_"+baseName+".launch", true),
							baseName,
							baseName,
							"Node_nodeRef1_subSysRef1Runner");

					ProjectCreator.findOrCreateContainer(new Path("/"
							+ baseName + "/tmp/log"),
							true, projectLocation, progressMonitor);
					
					if (config.useMVNBuild()) {
						ProjectCreator.createMavenPOM(URI.createPlatformResourceURI("/"
								+baseName+"/pom.xml", true),
								baseName,
								baseName,
								"Node_nodeRef1_subSysRef1Runner");
						ProjectCreator.createMavenBuilder(URI.createPlatformResourceURI("/"
								+baseName+"/build_"+baseName+".launch", true),
								baseName);
						ProjectCreator.createMavenLauncher(URI.createPlatformResourceURI("/"
								+baseName+"/runjar_"+baseName+".launch", true),
								baseName,
								baseName);
					}
					
				} catch (Exception e) {
					Logger.getLogger(getClass()).error(e.getMessage(), e);
				} finally {
					progressMonitor.done();
				}
			}
		};

		try {
			getContainer().run(false, false, operation);
		} catch (Exception e) {
			Logger.getLogger(getClass()).error(e.getMessage(), e);
			return false;
		}

		if (project != null) {
			IWorkbenchPage page = workbench.getActiveWorkbenchWindow()
					.getActivePage();
			final IWorkbenchPart activePart = page.getActivePart();
			if (activePart instanceof ISetSelectionTarget) {
				IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
				IFile model = workspaceRoot.getFile(new Path(modelURI.toPlatformString(true)));
				final ISelection targetSelection = new StructuredSelection(model);
				final IFileEditorInput input = new FileEditorInput(model);
				getShell().getDisplay().asyncExec(new Runnable() {
					public void run() {
						((ISetSelectionTarget) activePart)
								.selectReveal(targetSelection);
						try {
							PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(input, "org.eclipse.etrice.core.Room");
						} catch (PartInitException e) {
						}
					}
				});
			}
		}

		return true;
	}

	public void setInitialProjectName(String value) {
		initialProjectName = value;
	}
}

Back to the top