summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/actions/LaunchHobDialog.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/actions/LaunchHobDialog.java')
-rw-r--r--plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/actions/LaunchHobDialog.java328
1 files changed, 328 insertions, 0 deletions
diff --git a/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/actions/LaunchHobDialog.java b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/actions/LaunchHobDialog.java
new file mode 100644
index 0000000..861360d
--- /dev/null
+++ b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/actions/LaunchHobDialog.java
@@ -0,0 +1,328 @@
1/*******************************************************************************
2 * Copyright (c) 2011 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 *******************************************************************************/
11package org.yocto.bc.ui.actions;
12
13import java.util.Map;
14import java.util.Iterator;
15import java.util.Map.Entry;
16import java.util.ArrayList;
17import java.util.HashMap;
18import java.util.HashSet;
19import java.io.File;
20import java.io.IOException;
21
22import org.eclipse.jface.dialogs.Dialog;
23import org.eclipse.jface.dialogs.IDialogConstants;
24
25import org.eclipse.swt.SWT;
26import org.eclipse.swt.events.ModifyEvent;
27import org.eclipse.swt.events.ModifyListener;
28import org.eclipse.swt.events.SelectionAdapter;
29import org.eclipse.swt.events.SelectionEvent;
30import org.eclipse.swt.events.SelectionListener;
31import org.eclipse.swt.layout.GridData;
32import org.eclipse.swt.layout.GridLayout;
33import org.eclipse.swt.widgets.Button;
34import org.eclipse.swt.widgets.Combo;
35import org.eclipse.swt.widgets.Composite;
36import org.eclipse.swt.widgets.Control;
37import org.eclipse.swt.widgets.DirectoryDialog;
38import org.eclipse.swt.widgets.Display;
39import org.eclipse.swt.widgets.Label;
40import org.eclipse.swt.widgets.MessageBox;
41import org.eclipse.swt.widgets.Shell;
42import org.eclipse.swt.widgets.Widget;
43
44import org.eclipse.core.resources.IProject;
45import org.eclipse.core.resources.IProjectDescription;
46import org.eclipse.core.resources.ICommand;
47
48import org.yocto.bc.ui.builder.BitbakeBuilder;
49import org.yocto.bc.ui.builder.BitbakeCommanderNature;
50
51public class LaunchHobDialog extends Dialog {
52 private String title;
53 private Button buildButton;
54 private SelectionListener fSelectionListener;
55 private ModifyListener fModifyListener;
56 private Combo build_dir_combo;
57
58 private IProject project;
59 private Shell shell;
60 private String build_dir;
61
62 public LaunchHobDialog(Shell parentShell, String dialogTitle, IProject project) {
63 super(parentShell);
64 this.shell = parentShell;
65 this.project = project;
66 this.title = dialogTitle;
67 setShellStyle(getShellStyle() | SWT.RESIZE);
68
69 fSelectionListener= new SelectionListener() {
70 public void widgetDefaultSelected(SelectionEvent e) {}
71
72 public void widgetSelected(SelectionEvent e) {
73 controlChanged(e.widget);
74 }
75 };
76
77 fModifyListener= new ModifyListener() {
78 public void modifyText(ModifyEvent e) {
79 controlModified(e.widget);
80 }
81 };
82
83 }
84
85 public String getBuildDir() {
86 return build_dir;
87 }
88 @Override
89 protected Control createDialogArea(Composite parent) {
90 final Composite result = (Composite) super.createDialogArea(parent);
91
92 try {
93 createComposite(result);
94 } catch (Exception e) {
95 // TODO Auto-generated catch block
96 System.out.println("Have you ever set the project specific Yocto Settings?");
97 System.out.println(e.getMessage());
98 }
99
100 return result;
101 }
102
103 private void createComposite(Composite composite) throws Exception{
104 Label root_label, sysroot_label;
105
106 GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
107 GridLayout layout = new GridLayout(2, false);
108 composite.setLayout(layout);
109
110 gd= new GridData(SWT.FILL, SWT.CENTER, true, false);
111 gd.horizontalSpan= 2;
112 composite.setLayoutData(gd);
113
114 Label build_dir_label = new Label(composite, SWT.NONE);
115 build_dir_label.setText("Bitbake build directory: ");
116 Composite textContainer = new Composite(composite, SWT.NONE);
117 textContainer.setLayout(new GridLayout(2, false));
118 textContainer.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
119
120 build_dir_combo = new Combo(textContainer, SWT.DROP_DOWN);
121 build_dir_combo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
122 initializeBuildCombo();
123
124 Button buildButton = addDirSelectButton(textContainer, build_dir_combo);
125
126 //we add the listener at the end for avoiding the useless event trigger when control
127 //changed or modified.
128 buildButton.addSelectionListener(fSelectionListener);
129 build_dir_combo.addModifyListener(fModifyListener);
130 }
131
132 private Button addDirSelectButton(final Composite parent, final Combo combo) {
133 Button button = new Button(parent, SWT.PUSH | SWT.LEAD);
134 button.setText("Browse");
135 button.addSelectionListener(new SelectionAdapter() {
136 @Override
137 public void widgetSelected(SelectionEvent event) {
138 String dirName = new DirectoryDialog(parent.getShell()).open();
139
140 if (dirName != null) {
141 combo.add(dirName);
142 combo.setText(dirName);
143 }
144 }
145 });
146 return button;
147 }
148 @Override
149 protected void configureShell(Shell newShell) {
150 super.configureShell(newShell);
151 newShell.setText(title);
152 }
153
154 protected void buttonPressed(int buttonId) {
155 if (buttonId == IDialogConstants.OK_ID) {
156 try {
157 build_dir = build_dir_combo.getText().toString();
158 updateBuildSpec(build_dir);
159 super.buttonPressed(buttonId);
160 } catch (Exception e) {
161 // TODO Auto-generated catch block
162 System.out.println(e.getMessage());
163 }
164 }
165 else if (buttonId == IDialogConstants.CANCEL_ID)
166 {
167 super.buttonPressed(buttonId);
168 }
169 }
170
171 private boolean validateInput() {
172 boolean valid = false;
173 String build_dir = build_dir_combo.getText().toString();
174 if ((build_dir == null) || build_dir.isEmpty()) {
175 Display display = Display.getCurrent();
176 Shell shell = new Shell(display);
177 MessageBox msgBox = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK);
178 msgBox.setText("Yocto Configuration Error");
179 msgBox.setMessage("The specified build directory is empty!");
180 msgBox.open();
181 if (shell != null)
182 shell.dispose();
183 return valid;
184 }
185 String project_path = project.getLocation().toString();
186 File project_dir_file = new File(project_path);
187 File build_dir_file = new File(build_dir);
188 try {
189 if (isSubDirectory(project_dir_file, build_dir_file)) {
190 Display display = Display.getCurrent();
191 Shell shell = new Shell(display);
192 MessageBox msgBox = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK);
193 msgBox.setText("Yocto Configuration Error");
194 msgBox.setMessage("The specified build directory is a sub-dir of project path: " + project_path);
195 msgBox.open();
196 if (shell != null)
197 shell.dispose();
198 } else
199 valid = true;
200 } catch (IOException e) {
201 System.out.println(e.getMessage());
202 }
203 return valid;
204 }
205
206 private boolean isSubDirectory(File baseDir, File subDir) throws IOException {
207 baseDir = baseDir.getCanonicalFile();
208 subDir = subDir.getCanonicalFile();
209
210 File parentFile = subDir;
211 while (parentFile != null) {
212 if (baseDir.equals(parentFile)) {
213 return true;
214 }
215 parentFile = parentFile.getParentFile();
216 }
217 return false;
218 }
219
220 private void controlChanged(Widget widget) {
221
222 if (widget == buildButton)
223 {
224 }
225 }
226
227 private void controlModified(Widget widget) {
228 if (widget == build_dir_combo)
229 {
230
231 }
232 }
233
234 private void initializeBuildCombo()
235 {
236 ArrayList<String> items = new ArrayList<String> ();
237
238 try {
239 IProjectDescription desc = project.getDescription();
240
241 ICommand[] buildSpec = desc.getBuildSpec();
242 if ((buildSpec != null) && (buildSpec.length != 0))
243 {
244 for (int i = 0; i < buildSpec.length; i++) {
245 ICommand cmd = buildSpec[i];
246 if (cmd.getBuilderName().equalsIgnoreCase(BitbakeBuilder.HOB_BUILD_ID))
247 {
248 Map<String, String> args = cmd.getArguments();
249 if ((args != null) && !args.isEmpty())
250 {
251 Iterator entries = args.entrySet().iterator();
252 while (entries.hasNext()) {
253 Entry thisEntry = (Entry) entries.next();
254 String key = (String)thisEntry.getKey();
255 if (key.equalsIgnoreCase(BitbakeCommanderNature.BUILD_DIR_KEY)) {
256 build_dir_combo.removeAll();
257 build_dir_combo.setItems(getValues((String)thisEntry.getValue()));
258 }
259 }
260 }
261 }
262 }
263 }
264 } catch (Exception e) {
265 System.out.println(e.getMessage());
266 }
267 }
268
269 private String[] getValues(String value) {
270
271 if ((value != null) && !value.isEmpty())
272 {
273 String[] pieces = value.split(",");
274 for (int i = 0; i < pieces.length; i++)
275 {
276 int start = pieces[i].indexOf("[");
277 if (start >= 0)
278 pieces[i] = pieces[i].substring(start+1);
279 int end = pieces[i].indexOf("]");
280 if (end >= 0)
281 pieces[i] = pieces[i].substring(0, end);
282 pieces[i] = pieces[i].trim();
283 }
284 return pieces;
285 }
286 return null;
287 }
288
289 private void updateBuildSpec(String build_dir)
290 {
291 try {
292 String[] items = build_dir_combo.getItems();
293 HashSet values = new HashSet();
294 Map<String, String> args = new HashMap<String, String>();
295 values.add(build_dir);
296 for (int i = 0; i < items.length; i++) {
297 values.add(items[i]);
298 }
299 args.put(BitbakeCommanderNature.BUILD_DIR_KEY, values.toString());
300 IProjectDescription desc = project.getDescription();
301 ICommand[] buildSpec = desc.getBuildSpec();
302 boolean found = false;
303 if ((buildSpec != null) || (buildSpec.length != 0)) {
304 for (int i = 0; i < buildSpec.length; i++) {
305 ICommand cmd = buildSpec[i];
306 if (cmd.getBuilderName().equalsIgnoreCase(BitbakeBuilder.HOB_BUILD_ID)) {
307 cmd.setArguments(args);
308 desc.setBuildSpec(buildSpec);
309 found = true;
310 break;
311 }
312 }
313 }
314 if (!found) {
315 ICommand[] newBuildSpec = new ICommand[buildSpec.length + 1];
316 System.arraycopy(buildSpec, 0, newBuildSpec, 0, buildSpec.length);
317 ICommand cmd = desc.newCommand();
318 cmd.setBuilderName(BitbakeBuilder.HOB_BUILD_ID);
319 cmd.setArguments(args);
320 newBuildSpec[newBuildSpec.length - 1] = cmd;
321 desc.setBuildSpec(newBuildSpec);
322 }
323 project.setDescription(desc, null);
324 } catch (Exception e) {
325 System.out.println(e.getMessage());
326 }
327 }
328}