summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/wizards/install/OptionsPage.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/wizards/install/OptionsPage.java')
-rw-r--r--plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/wizards/install/OptionsPage.java327
1 files changed, 327 insertions, 0 deletions
diff --git a/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/wizards/install/OptionsPage.java b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/wizards/install/OptionsPage.java
new file mode 100644
index 0000000..75d9f99
--- /dev/null
+++ b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/wizards/install/OptionsPage.java
@@ -0,0 +1,327 @@
1/*******************************************************************************
2 * Copyright (c) 2013 Intel Corporation.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Intel - initial API and implementation
10 * Ioana Grigoropol (Intel) - adapt class for remote support
11 *******************************************************************************/
12package org.yocto.bc.ui.wizards.install;
13
14import java.io.IOException;
15import java.io.File;
16import java.net.URI;
17import java.net.URISyntaxException;
18import java.util.ArrayList;
19import java.util.Hashtable;
20import java.util.Iterator;
21import java.util.List;
22import java.util.Map;
23import java.util.regex.Matcher;
24import java.util.regex.Pattern;
25
26import org.eclipse.core.resources.IProject;
27import org.eclipse.core.resources.IProjectDescription;
28import org.eclipse.core.resources.IResource;
29import org.eclipse.core.resources.IWorkspaceRoot;
30import org.eclipse.core.resources.ResourcesPlugin;
31import org.eclipse.core.runtime.CoreException;
32import org.eclipse.core.runtime.IStatus;
33import org.eclipse.core.runtime.NullProgressMonitor;
34import org.eclipse.ptp.rdt.ui.wizards.RemoteProjectContentsLocationArea;
35import org.eclipse.ptp.rdt.ui.wizards.RemoteProjectContentsLocationArea.IErrorMessageReporter;
36import org.eclipse.ptp.remote.core.IRemoteConnection;
37import org.eclipse.ptp.internal.remote.rse.core.RSEConnection;
38import org.eclipse.rse.core.model.IHost;
39import org.eclipse.rse.services.files.IFileService;
40import org.eclipse.rse.services.files.IHostFile;
41import org.eclipse.swt.SWT;
42import org.eclipse.swt.events.SelectionAdapter;
43import org.eclipse.swt.events.SelectionEvent;
44import org.eclipse.swt.layout.GridData;
45import org.eclipse.swt.layout.GridLayout;
46import org.eclipse.swt.widgets.Button;
47import org.eclipse.swt.widgets.Composite;
48import org.eclipse.swt.widgets.Control;
49import org.eclipse.swt.widgets.DirectoryDialog;
50import org.eclipse.swt.widgets.FileDialog;
51import org.eclipse.swt.widgets.Label;
52import org.eclipse.swt.widgets.Text;
53import org.eclipse.ui.PlatformUI;
54
55import org.yocto.bc.ui.wizards.FiniteStateWizard;
56import org.yocto.bc.ui.wizards.FiniteStateWizardPage;
57import org.yocto.bc.ui.wizards.FiniteStateWizardPage.ValidationListener;
58import org.yocto.remote.utils.RemoteHelper;
59
60/**
61 * Select which flavor of OE is to be installed.
62 *
63 * @author kgilmer
64 *
65 * Setting up the parameters for creating the new Yocto Bitbake project
66 *
67 * @modified jzhang
68 */
69public class OptionsPage extends FiniteStateWizardPage {
70
71 public static final String URI_SEPARATOR = "/";
72 public static final String LOCALHOST = "LOCALHOST";
73
74 private Composite top;
75
76 private RemoteProjectContentsLocationArea locationArea;
77
78 private ValidationListener validationListener;
79 private Text txtProjectName;
80 private Button btnGit;
81
82 protected OptionsPage(Map model) {
83 super("Options", model);
84 //setTitle("Create new yocto bitbake project");
85 setMessage("Enter these parameters to create new Yocto Project BitBake commander project");
86 }
87
88 @Override
89 public void createControl(Composite parent) {
90 top = new Composite(parent, SWT.None);
91 top.setLayout(new GridLayout());
92 top.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
93
94 GridData gdFillH = new GridData(GridData.FILL_HORIZONTAL);
95
96 Composite projectNameComp = new Composite(top, SWT.NONE);
97 GridData gdProjName = new GridData(GridData.FILL_HORIZONTAL);
98 projectNameComp.setLayoutData(gdProjName);
99 projectNameComp.setLayout(new GridLayout(2, false));
100 Label lblProjectName = new Label(projectNameComp, SWT.NONE);
101 lblProjectName.setText("Project N&ame:");
102
103 txtProjectName = new Text(projectNameComp, SWT.BORDER);
104 txtProjectName.setLayoutData(gdFillH);
105 txtProjectName.setFocus();
106 validationListener = new ValidationListener();
107
108 txtProjectName.addModifyListener(validationListener);
109
110 IErrorMessageReporter errorReporter = new IErrorMessageReporter() {
111
112 @Override
113 public void reportError(String errorMessage, boolean infoOnly) {
114 setMessage(errorMessage);
115 if (validatePage()) {
116 updateModel();
117 setPageComplete(true);
118 return;
119 }
120
121 setPageComplete(false);
122 }
123 };
124
125 locationArea = new RemoteProjectContentsLocationArea(errorReporter, top, null);
126
127 btnGit = new Button(top, SWT.CHECK);
128 btnGit.setText("Clone from Yocto Project &Git Repository into new location");
129 btnGit.setEnabled(true);
130 btnGit.setSelection(true);
131 btnGit.addSelectionListener(validationListener);
132 GridData gd = new GridData(GridData.VERTICAL_ALIGN_END | GridData.FILL_HORIZONTAL);
133 btnGit.setLayoutData(gd);
134
135 setControl(top);
136 }
137
138 @Override
139 public void pageCleanup() {
140
141 }
142
143 @Override
144 public void pageDisplay() {
145 }
146
147 @Override
148
149 protected void updateModel() {
150 try {
151 URI uri = getProjectLocationURI();
152 if (uri != null)
153 model.put(InstallWizard.INSTALL_DIRECTORY, getProjectLocationURI());
154 } catch (Exception e){
155 e.printStackTrace();
156 }
157 model.put(InstallWizard.PROJECT_NAME, txtProjectName.getText());
158 model.put(InstallWizard.GIT_CLONE, new Boolean(btnGit.getSelection()));
159 model.put(InstallWizard.SELECTED_CONNECTION, locationArea.getRemoteConnection());
160 model.put(InstallWizard.SELECTED_REMOTE_SERVICE, locationArea.getRemoteServices());
161 }
162
163 public URI getProjectLocationURI() throws URISyntaxException {
164 URI uri = locationArea.getProjectLocationURI();
165
166 if (uri != null) {
167 String location = locationArea.getProjectLocation();
168 if (!uri.getPath().isEmpty()) {
169 String separator = uri.getPath().endsWith(URI_SEPARATOR) ? "" : URI_SEPARATOR;
170
171 return new URI( uri.getScheme(),
172 uri.getHost(),
173 uri.getPath() + separator + txtProjectName.getText(),
174 uri.getFragment());
175 } else {
176 return null;
177 }
178 } else {
179 String location = locationArea.getProjectLocation();
180 String separator = location.endsWith(URI_SEPARATOR) ? "" : URI_SEPARATOR;
181
182 IRemoteConnection conn = locationArea.getConnection();
183 if (conn instanceof RSEConnection) {
184 RSEConnection rseConn = (RSEConnection)conn;
185 return new URI("rse", rseConn.getHost().getHostName(), location);
186 } else {
187 return new URI( "file", location + separator + txtProjectName.getText(),"");
188 }
189 }
190 }
191
192 private boolean isValidProjectName(String projectName) {
193 if (projectName.indexOf('$') > -1) {
194 return false;
195 }
196
197 return true;
198 }
199
200 private boolean validateProjectName() {
201 IWorkspaceRoot wsroot = ResourcesPlugin.getWorkspace().getRoot();
202
203 IStatus validate = ResourcesPlugin.getWorkspace().validateName(txtProjectName.getText(), IResource.PROJECT);
204
205 if (txtProjectName.getText().trim().isEmpty()) {
206 setErrorMessage("Project name cannot be empty!");
207 return false;
208 }
209
210 if (!validate.isOK() || !isValidProjectName(txtProjectName.getText())) {
211 setErrorMessage("Invalid project name: " + txtProjectName.getText());
212 return false;
213 }
214
215 IProject proj = wsroot.getProject(txtProjectName.getText());
216 if (proj.exists()) {
217 setErrorMessage("A project with the name " + txtProjectName.getText() + " already exists");
218 return false;
219 }
220 return true;
221 }
222 private String convertToRealPath(String path) {
223 String patternStr = File.separator + File.separator;
224 if (patternStr.equals(URI_SEPARATOR))
225 return path;
226 String replaceStr = URI_SEPARATOR;
227 String convertedpath;
228
229 //Compile regular expression
230 Pattern pattern = Pattern.compile(patternStr); //pattern to look for
231
232 //replace all occurance of percentage character to file separator
233 Matcher matcher = pattern.matcher(path);
234 convertedpath = matcher.replaceAll(replaceStr);
235
236 return convertedpath;
237 }
238
239 public String getProjectName(){
240 return txtProjectName.getText().trim();
241 }
242
243 protected boolean validateProjectLocation() {
244
245 String projectLoc = locationArea.getProjectLocation().trim();
246
247 IRemoteConnection remoteConnection = locationArea.getRemoteConnection();
248 if (remoteConnection == null)
249 return false;
250
251 if (projectLoc.isEmpty())
252 return true;
253
254 IHost connection = RemoteHelper.getRemoteConnectionByName(remoteConnection.getName());
255
256 projectLoc = convertToRealPath(projectLoc);
257 String separator = projectLoc.endsWith(URI_SEPARATOR) ? "" : URI_SEPARATOR;
258 String projectPath = projectLoc + separator + getProjectName();
259 IHostFile repoDest = RemoteHelper.getRemoteHostFile(connection, projectPath, new NullProgressMonitor());
260
261 if(!btnGit.getSelection()) {
262 if (repoDest == null || !repoDest.exists()) {
263 setErrorMessage("Directory " + projectPath + " does not exist, please select git clone.");
264 return false;
265 }
266
267 IHostFile validationFile = RemoteHelper.getRemoteHostFile(connection, projectPath + URI_SEPARATOR + InstallWizard.VALIDATION_FILE, new NullProgressMonitor());
268 if (validationFile == null || !validationFile.exists()) {
269 setErrorMessage("Directory " + projectPath + " seems invalid, please use other directory or project name.");
270 return false;
271 }
272 } else { //git clone
273 if (repoDest != null && repoDest.exists() && repoDest.isDirectory()) {
274 IHostFile[] hostFiles = RemoteHelper.getRemoteDirContent(connection, repoDest.getAbsolutePath(), "", IFileService.FILE_TYPE_FILES_AND_FOLDERS, new NullProgressMonitor());
275 if (hostFiles.length != 0) {
276 setErrorMessage("Directory " + projectPath + " is not empty, please choose another location.");
277 return false;
278 }
279 IHostFile gitDescr = RemoteHelper.getRemoteHostFile(connection, projectPath + "/.git", new NullProgressMonitor());
280 if (gitDescr != null && gitDescr.exists()) {
281 setErrorMessage("Directory " + projectPath + " contains a repository, please choose another location or skip cloning the repository.");
282 return false;
283 }
284 }
285 }
286
287 try {
288 String projName = txtProjectName.getText();
289 if (!projName.trim().isEmpty() && validateProjectName()) {
290 IWorkspaceRoot wsroot = ResourcesPlugin.getWorkspace().getRoot();
291 IProject proj = wsroot.getProject();
292 if (proj != null && proj.exists()) {
293 setErrorMessage("A project with the name " + projName + " already exists");
294 return false;
295 }
296 URI location = new URI("file:" + URI_SEPARATOR + URI_SEPARATOR + convertToRealPath(projectLoc) + URI_SEPARATOR + txtProjectName.getText());
297
298 IStatus status = ResourcesPlugin.getWorkspace().validateProjectLocationURI(proj, location);
299 if (!status.isOK()) {
300 setErrorMessage(status.getMessage());
301 return false;
302 }
303 }
304 } catch (Exception e) {
305 e.printStackTrace();
306 setErrorMessage("Run into error while trying to validate entries!");
307 return false;
308 }
309
310 setErrorMessage(null);
311 return true;
312 }
313 @Override
314 protected boolean validatePage() {
315 if (!validateProjectName())
316 return false;
317
318 if (!validateProjectLocation())
319 return false;
320
321 setErrorMessage(null);
322 setMessage("All the entries are valid, press \"Finish\" to start the process, "+
323 "this will take a while. Please don't interrupt till there's output in the Yocto Console window...");
324 return true;
325 }
326
327}