aboutsummaryrefslogtreecommitdiffstats
path: root/org.openembedded.bc.ui/src/org/openembedded/bc/ui/wizards/install/BBCProjectPage.java
blob: a234bcaecbd13ce281aa18842fd863f5090aa9bb (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
/*****************************************************************************
 * 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.install;

import java.io.File;
import java.util.Hashtable;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IStatus;
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.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.openembedded.bc.ui.wizards.FiniteStateWizardPage;


/**
 * Main property page for new project wizard.
 * @author kgilmer
 *
 */
public class BBCProjectPage extends FiniteStateWizardPage {

	private class FileOpenSelectionAdapter extends SelectionAdapter {
		@Override
		public void widgetSelected(SelectionEvent e) {
			FileDialog fd = new FileDialog(PlatformUI.getWorkbench()
					.getDisplay().getActiveShell(), SWT.OPEN);

			fd.setText("Open Configuration Script");
			fd.setFilterPath(txtProjectLocation.getText());

			String selected = fd.open();

			if (selected != null) {
				txtInit.setText(selected);
				updateModel();
			}
		}
	}
	public static final String PAGE_TITLE = "BitBake Commander Project";
	public static final String INIT_SCRIPT_KEY = "Init Script";
	public static final String INSTALL_DIRECTORY_KEY = "Install Directory";
	public static final String PROJECT_NAME_KEY = "Project Name";
	
	private Text txtProjectLocation;

	private Text txtInit;
	private ValidationListener validationListener;
	private Text txtProjectName;

	public BBCProjectPage(Map model) {
		super(PAGE_TITLE, model);
		setMessage("Enter information to create a BitBake Commander project.");
	}

	public void createControl(Composite parent) {
		GridData gdFillH = new GridData(GridData.FILL_HORIZONTAL);
		GridData gdVU = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
		
		Composite top = new Composite(parent, SWT.NONE);
		top.setLayoutData(new GridData(GridData.FILL_BOTH));
		top.setLayout(new GridLayout());

		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("N&ame:");

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

		Label lblProjectLocation = new Label(projectNameComp, SWT.None);
		lblProjectLocation.setText("&Location:");

		Composite locComposite = new Composite(projectNameComp, SWT.NONE);
		GridData gd = new GridData(GridData.VERTICAL_ALIGN_END
				| GridData.FILL_HORIZONTAL);
		gd.horizontalIndent = 0;
		locComposite.setLayoutData(gd);
		GridLayout gl = new GridLayout(2, false);
		gl.marginWidth = 0;
		locComposite.setLayout(gl);

		txtProjectLocation = new Text(locComposite, SWT.BORDER);
		txtProjectLocation.setLayoutData(gdFillH);
		txtProjectLocation.addModifyListener(validationListener);

		Button button = new Button(locComposite, SWT.PUSH);
		button.setText("Browse...");
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				handleBrowse();
			}
		});

		Label lblInit = new Label(projectNameComp, SWT.NONE);
		lblInit.setText("Init Script:");

		Composite initComposite = new Composite(projectNameComp, SWT.NONE);
		gd = new GridData(GridData.VERTICAL_ALIGN_END
				| GridData.FILL_HORIZONTAL);
		gd.horizontalIndent = 0;
		initComposite.setLayoutData(gd);
		gl = new GridLayout(2, false);
		gl.marginWidth = 0;
		initComposite.setLayout(gl);

		txtInit = new Text(initComposite, SWT.BORDER);
		GridData gdi = new GridData(GridData.FILL_HORIZONTAL);
		txtInit.setLayoutData(gdi);
		txtInit.addModifyListener(validationListener);

		Button btnLoadInit = new Button(initComposite, SWT.PUSH);
		btnLoadInit.setLayoutData(gdVU);
		btnLoadInit.setText("Choose...");
		btnLoadInit.addSelectionListener(new FileOpenSelectionAdapter());

		if (System.getenv("OEROOT") != null) {
			txtProjectLocation.setText(System.getenv("OEROOT"));
		}

		setControl(top);
	}

	private void handleBrowse() {
		DirectoryDialog dialog = new DirectoryDialog(getShell(), SWT.None);
		String dir = dialog.open();
		if (dir != null) {
			txtProjectLocation.setText(dir);
		}
	}

	private String getFileSegment(String initScriptPath) {
		//return the first segment of " " seperated array, or full string if no " " exists
		return initScriptPath.split(" ")[0];
	}

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

		return true;
	}


	@Override
	public void pageCleanup() {
		
	}

	@Override
	public void pageDisplay() {
		
	}

	@Override
	protected void updateModel() {
		Map props = (Map) model.get(OptionsPage.OPTION_MAP);
		
		if (props == null) {
			props = new Hashtable();
			model.put(OptionsPage.OPTION_MAP, props);
		}
		
		props.put(INIT_SCRIPT_KEY, txtInit.getText());
		props.put(INSTALL_DIRECTORY_KEY, txtProjectLocation.getText());
		props.put(PROJECT_NAME_KEY, txtProjectName.getText());
	}
	

	@Override
	protected boolean validatePage() {
		IWorkspaceRoot wsroot = ResourcesPlugin.getWorkspace().getRoot();

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

		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;
		}

		if (txtProjectLocation.getText().trim().length() == 0) {
			setErrorMessage("Set directory to an OpenEmbedded or Poky project root (OEROOT)");
			return false;
		}

		File f = new File(txtProjectLocation.getText());
		if (!f.exists() || !f.isDirectory()) {
			setErrorMessage("Invalid Directory");
			return false;
		}
		
		if (txtInit.getText().length() == 0) {
			setErrorMessage("Set configuration file before bitbake is launched.");
			return false;
		}
		
		File f2 = new File(getFileSegment(txtInit.getText()));
		if (!f2.exists() || f2.isDirectory()) {
			setErrorMessage("The configuration file is invalid.");
			return false;
		}

		setErrorMessage(null);

		return true;
	}
}