Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b4380c784d5731671f7b1b60e3aa39cb9d5bd150 (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
/*****************************************************************************
 * Copyright (c) 2016 CEA LIST 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
 *
 * Contributors:
 *   Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Initial API and implementation
 *   Vincent LORENZO (CEA-LIST) vincent.lorenzo@cea.fr - Bug 493756
 *****************************************************************************/

package org.eclipse.papyrus.customization.nattableconfiguration.wizards;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.papyrus.customization.nattableconfiguration.Activator;
import org.eclipse.papyrus.customization.nattableconfiguration.messages.Messages;
import org.eclipse.papyrus.customization.nattableconfiguration.utils.NattableConfigurationConstants;
import org.eclipse.pde.internal.ui.wizards.IProjectProvider;
import org.eclipse.pde.internal.ui.wizards.plugin.NewProjectCreationOperation;
import org.eclipse.pde.internal.ui.wizards.plugin.NewProjectCreationPage;
import org.eclipse.pde.internal.ui.wizards.plugin.PluginContentPage;
import org.eclipse.pde.internal.ui.wizards.plugin.PluginFieldData;
import org.eclipse.ui.IWorkbenchWizard;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.PlatformUI;

/**
 * The wizard used to create a Papyrus Table Configuration Project
 */
public class CreateNattableConfigurationProjectWizard extends CreateAndEditTableConfigurationWizard implements IWorkbenchWizard {


	/**
	 * The page for the project creation.
	 */
	private NewProjectCreationPage nattableConfigurationProjectCreationPage;

	/**
	 * The project provider.
	 */
	private IProjectProvider projectProvider;

	/**
	 * The content page for the project creation.
	 */
	protected PluginContentPage contentPage;

	/**
	 * The fields data to manage the project creation
	 */
	private PluginFieldData pluginData;

	/**
	 * @see org.eclipse.papyrus.customization.nattableconfiguration.EditTableConfigurationWizard#addPages()
	 *
	 */
	@Override
	public void addPages() {
		pluginData = new PluginFieldData();

		nattableConfigurationProjectCreationPage = new NewProjectCreationPage(Messages.NattableConfigurationProjectCreationPage_pageName, pluginData, false,new StructuredSelection());
		addPage(nattableConfigurationProjectCreationPage);

		projectProvider = new IProjectProvider() {
			@Override
			public String getProjectName() {
				return nattableConfigurationProjectCreationPage.getProjectName();
			}

			@Override
			public IProject getProject() {
				return nattableConfigurationProjectCreationPage.getProjectHandle();
			}

			@Override
			public IPath getLocationPath() {
				return nattableConfigurationProjectCreationPage.getLocationPath();
			}
		};

		contentPage = new PluginContentPage("page2", projectProvider, nattableConfigurationProjectCreationPage, pluginData); //$NON-NLS-1$

		addPage(contentPage);
		super.addPages();
	}




	/**
	 * @see org.eclipse.papyrus.customization.nattableconfiguration.EditTableConfigurationWizard#performFinish()
	 *
	 * @return
	 */
	@Override
	public boolean performFinish() {
		boolean result = false;

		IProject createdProject = null;

		try {

			// Create the project
			getContainer().run(false, true, new NewProjectCreationOperation(pluginData, projectProvider, null));

			createdProject = getProject();

			// Set the project into the working sets
			final IWorkingSet[] workingSets = nattableConfigurationProjectCreationPage.getSelectedWorkingSets();
			if (0 < workingSets.length) {
				PlatformUI.getWorkbench().getWorkingSetManager().addToWorkingSets(createdProject, workingSets);
			}

			// Copy the about file
			copyAboutFile(createdProject);
			result = super.performFinish();
		} catch (final InvocationTargetException e) {
			Activator.log.error(e);
		} catch (final InterruptedException e) {
			Activator.log.error(e);
		}

		if (result) {
			result = saveResource();
		}
		refreshProject();
		return result;
	}
	
	/**
	 * This allows to get the project created folder.
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.papyrus.customization.nattableconfiguration.wizards.CreateAndEditTableConfigurationWizard#getPathForTableConfigurationCreation()
	 */
	@Override
	protected String getPathForTableConfigurationCreation() {
		final String projectLocation = getProjectLocation();
		final StringBuilder builder = new StringBuilder(projectLocation);
		builder.append(NattableConfigurationConstants.CONFIG_FOLDER);
		
		return builder.toString();
	}

	/**
	 * This allows to copy the about file in the created project.
	 * 
	 * @param createdProject
	 *            The created project.
	 */
	protected void copyAboutFile(final IProject createdProject) {
		InputStream inputStream = null;
		OutputStream outputStream = null;
		try {
			final URL url = Activator.getDefault().getBundle().getResource(NattableConfigurationConstants.ABOUT_FILE_NAME);
			inputStream = url.openStream();

			final java.io.File newAboutFile = new java.io.File(createdProject.getLocation().toOSString() + java.io.File.separator + NattableConfigurationConstants.ABOUT_FILE_NAME);
			newAboutFile.createNewFile();

			outputStream = new FileOutputStream(newAboutFile);

			int read = 0;
			byte[] bytes = new byte[1024];

			while ((read = inputStream.read(bytes)) != -1) {
				outputStream.write(bytes, 0, read);
			}
		} catch (final IOException e) {
			Activator.log.error(e);
		} finally {
			if (null != inputStream) {
				try {
					inputStream.close();
				} catch (final IOException e) {
					Activator.log.error(e);
				}
			}
			if (null != outputStream) {
				try {
					// outputStream.flush();
					outputStream.close();
				} catch (final IOException e) {
					Activator.log.error(e);
				}

			}
		}
	}

	
	/**
	 * @see org.eclipse.papyrus.customization.nattableconfiguration.wizards.CreateAndEditTableConfigurationWizard#getProject()
	 *
	 * @return
	 */
	@Override
	protected IProject getProject() {
		return this.projectProvider.getProject();
	}
	
	/**
	 * @see org.eclipse.papyrus.customization.nattableconfiguration.wizards.CreateAndEditTableConfigurationWizard#getProjectLocation()
	 *
	 * @return
	 */
	@Override
	protected String getProjectLocation() {
		IProject createdProject = getProject();
		return createdProject.getLocation().toString();
	}
}

Back to the top