summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/wizards/install/OptionsPage.java
blob: 75d9f995e5777214dac2faae6d7ca6f64a53aaa6 (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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*******************************************************************************
 * Copyright (c) 2013 Intel Corporation.
 * 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:
 * Intel - initial API and implementation
 * Ioana Grigoropol (Intel) - adapt class for remote support
 *******************************************************************************/
package org.yocto.bc.ui.wizards.install;

import java.io.IOException;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.ptp.rdt.ui.wizards.RemoteProjectContentsLocationArea;
import org.eclipse.ptp.rdt.ui.wizards.RemoteProjectContentsLocationArea.IErrorMessageReporter;
import org.eclipse.ptp.remote.core.IRemoteConnection;
import org.eclipse.ptp.internal.remote.rse.core.RSEConnection;
import org.eclipse.rse.core.model.IHost;
import org.eclipse.rse.services.files.IFileService;
import org.eclipse.rse.services.files.IHostFile;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

import org.yocto.bc.ui.wizards.FiniteStateWizard;
import org.yocto.bc.ui.wizards.FiniteStateWizardPage;
import org.yocto.bc.ui.wizards.FiniteStateWizardPage.ValidationListener;
import org.yocto.remote.utils.RemoteHelper;

/**
 * Select which flavor of OE is to be installed.
 * 
 * @author kgilmer
 * 
 * Setting up the parameters for creating the new Yocto Bitbake project
 * 
 * @modified jzhang
 */
public class OptionsPage extends FiniteStateWizardPage {

	public static final String URI_SEPARATOR = "/";
	public static final String LOCALHOST = "LOCALHOST";

	private Composite top;

	private RemoteProjectContentsLocationArea locationArea;

	private ValidationListener validationListener;
	private Text txtProjectName;
	private Button btnGit;

	protected OptionsPage(Map model) {
		super("Options", model);
		//setTitle("Create new yocto bitbake project");
		setMessage("Enter these parameters to create new Yocto Project BitBake commander project");
	}

	@Override
	public void createControl(Composite parent) {
		top = new Composite(parent, SWT.None);
		top.setLayout(new GridLayout());
		top.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		GridData gdFillH = new GridData(GridData.FILL_HORIZONTAL);

		Composite projectNameComp = new Composite(top, SWT.NONE);
		GridData gdProjName = new GridData(GridData.FILL_HORIZONTAL);
		projectNameComp.setLayoutData(gdProjName);
		projectNameComp.setLayout(new GridLayout(2, false));
		Label lblProjectName = new Label(projectNameComp, SWT.NONE);
		lblProjectName.setText("Project N&ame:");

		txtProjectName = new Text(projectNameComp, SWT.BORDER);
		txtProjectName.setLayoutData(gdFillH);
		txtProjectName.setFocus();
		validationListener = new ValidationListener();
		
		txtProjectName.addModifyListener(validationListener);

		IErrorMessageReporter errorReporter = new IErrorMessageReporter() {

			@Override
			public void reportError(String errorMessage, boolean infoOnly) {
				setMessage(errorMessage);
				if (validatePage()) {
	                updateModel();
	                setPageComplete(true);
	                return;
	            }

	            setPageComplete(false);
			}
		};

		locationArea = new RemoteProjectContentsLocationArea(errorReporter, top, null);

		btnGit = new Button(top, SWT.CHECK);
		btnGit.setText("Clone from Yocto Project &Git Repository into new location");
		btnGit.setEnabled(true);
		btnGit.setSelection(true);
		btnGit.addSelectionListener(validationListener);
		GridData gd = new GridData(GridData.VERTICAL_ALIGN_END | GridData.FILL_HORIZONTAL);
		btnGit.setLayoutData(gd);

		setControl(top);
	}

	@Override
	public void pageCleanup() {

	}

	@Override
	public void pageDisplay() {
	}

	@Override
	
	protected void updateModel() {
		try {
			URI uri = getProjectLocationURI();
			if (uri != null)
				model.put(InstallWizard.INSTALL_DIRECTORY, getProjectLocationURI());
		} catch (Exception e){
			e.printStackTrace();
		}
		model.put(InstallWizard.PROJECT_NAME, txtProjectName.getText());
		model.put(InstallWizard.GIT_CLONE, new Boolean(btnGit.getSelection()));
		model.put(InstallWizard.SELECTED_CONNECTION, locationArea.getRemoteConnection());
		model.put(InstallWizard.SELECTED_REMOTE_SERVICE, locationArea.getRemoteServices());
	}

	public URI getProjectLocationURI() throws URISyntaxException {
		URI uri = locationArea.getProjectLocationURI();

		if (uri != null) {
			String location = locationArea.getProjectLocation();
			if (!uri.getPath().isEmpty()) {
				String separator = uri.getPath().endsWith(URI_SEPARATOR) ? "" : URI_SEPARATOR;

				return new URI( uri.getScheme(),
								uri.getHost(),
								uri.getPath() + separator + txtProjectName.getText(),
								uri.getFragment());
			} else {
				return null;
			}
		} else {
			String location = locationArea.getProjectLocation();
			String separator = location.endsWith(URI_SEPARATOR) ? "" : URI_SEPARATOR;

			IRemoteConnection conn = locationArea.getConnection();
			if (conn instanceof RSEConnection) {
				RSEConnection rseConn = (RSEConnection)conn;
				return new URI("rse", rseConn.getHost().getHostName(), location);
			} else {
				return new URI( "file", location + separator + txtProjectName.getText(),"");
			}
		}
	}

	private boolean isValidProjectName(String projectName) {
		if (projectName.indexOf('$') > -1) {
			return false;
		}

		return true;
	}

	private boolean validateProjectName() {
		IWorkspaceRoot wsroot = ResourcesPlugin.getWorkspace().getRoot();

		IStatus validate = ResourcesPlugin.getWorkspace().validateName(txtProjectName.getText(), IResource.PROJECT);

		if (txtProjectName.getText().trim().isEmpty()) {
			setErrorMessage("Project name cannot be empty!");
			return false;
		}

		if (!validate.isOK() || !isValidProjectName(txtProjectName.getText())) {
			setErrorMessage("Invalid project name: " + txtProjectName.getText());
			return false;
		}

		IProject proj = wsroot.getProject(txtProjectName.getText());
		if (proj.exists()) {
			setErrorMessage("A project with the name " + txtProjectName.getText() + " already exists");
			return false;
		}
		return true;
	}
	private String convertToRealPath(String path) {
	    String patternStr = File.separator + File.separator;
	    if (patternStr.equals(URI_SEPARATOR))
	        return path;
	    String replaceStr = URI_SEPARATOR;
	    String convertedpath;

	    //Compile regular expression
	    Pattern pattern = Pattern.compile(patternStr); //pattern to look for

	    //replace all occurance of percentage character to file separator
	    Matcher matcher = pattern.matcher(path);
	    convertedpath = matcher.replaceAll(replaceStr);

	    return convertedpath;
	}

	public String getProjectName(){
		return txtProjectName.getText().trim();
	}

	protected boolean validateProjectLocation() {

		String projectLoc = locationArea.getProjectLocation().trim();

		IRemoteConnection remoteConnection = locationArea.getRemoteConnection();
		if (remoteConnection == null)
			return false;

		if (projectLoc.isEmpty())
			return true;

		IHost connection = RemoteHelper.getRemoteConnectionByName(remoteConnection.getName());

		projectLoc = convertToRealPath(projectLoc);
		String separator = projectLoc.endsWith(URI_SEPARATOR) ? "" : URI_SEPARATOR;
		String projectPath = projectLoc + separator + getProjectName();
		IHostFile repoDest = RemoteHelper.getRemoteHostFile(connection, projectPath, new NullProgressMonitor());

		if(!btnGit.getSelection()) {
			if (repoDest == null || !repoDest.exists()) {
				setErrorMessage("Directory " + projectPath + " does not exist, please select git clone.");
				return false;
			}

			IHostFile validationFile = RemoteHelper.getRemoteHostFile(connection, projectPath + URI_SEPARATOR + InstallWizard.VALIDATION_FILE, new NullProgressMonitor());
			if (validationFile == null || !validationFile.exists()) {
				setErrorMessage("Directory " + projectPath + " seems invalid, please use other directory or project name.");
				return false;
			}
		} else { //git clone
			if (repoDest != null && repoDest.exists() && repoDest.isDirectory()) {
				IHostFile[] hostFiles = RemoteHelper.getRemoteDirContent(connection, repoDest.getAbsolutePath(), "", IFileService.FILE_TYPE_FILES_AND_FOLDERS, new NullProgressMonitor());
				if (hostFiles.length != 0) {
					setErrorMessage("Directory " + projectPath + " is not empty, please choose another location.");
					return false;
				}
				IHostFile gitDescr = RemoteHelper.getRemoteHostFile(connection, projectPath + "/.git", new NullProgressMonitor());
				if (gitDescr != null && gitDescr.exists()) {
					setErrorMessage("Directory " + projectPath + " contains a repository, please choose another location or skip cloning the repository.");
					return false;
				}
			}
		}

		try {
			String projName = txtProjectName.getText();
			if (!projName.trim().isEmpty() && validateProjectName()) {
				IWorkspaceRoot wsroot = ResourcesPlugin.getWorkspace().getRoot();
				IProject proj = wsroot.getProject();
				if (proj != null && proj.exists()) {
					setErrorMessage("A project with the name " + projName + " already exists");
					return false;
				}
				URI location = new URI("file:" + URI_SEPARATOR + URI_SEPARATOR + convertToRealPath(projectLoc) + URI_SEPARATOR + txtProjectName.getText());

				IStatus status = ResourcesPlugin.getWorkspace().validateProjectLocationURI(proj, location);
				if (!status.isOK()) {
					setErrorMessage(status.getMessage());
					return false;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			setErrorMessage("Run into error while trying to validate entries!");
			return false;
		}

		setErrorMessage(null);
		return true;
	}
	@Override
	protected boolean validatePage() {
		if  (!validateProjectName())
			return false;

		if (!validateProjectLocation())
			return false;

		setErrorMessage(null);
		setMessage("All the entries are valid, press \"Finish\" to start the process, "+
				"this will take a while. Please don't interrupt till there's output in the Yocto Console window...");
		return true;
	}
	
}