aboutsummaryrefslogtreecommitdiffstats
path: root/org.openembedded.bc.ui/src/org/openembedded/bc/ui/wizards/install/BitbakePage.java
blob: 09e58b9b18b1b8abe43eca242042193abb02d403 (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
package org.openembedded.bc.ui.wizards.install;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import org.eclipse.core.commands.ParameterValueConversionException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.openembedded.bc.ui.Activator;
import org.openembedded.bc.ui.model.ProjectInfo;
import org.openembedded.bc.ui.wizards.FiniteStateWizard;
import org.openembedded.bc.ui.wizards.FiniteStateWizardPage;
import org.openembedded.bc.ui.wizards.importProject.ImportOEProjectWizard;
import org.openembedded.bc.ui.wizards.newproject.BBConfigurationInitializeOperation;


/**
 * Bitbake console view.
 * 
 * @author kgilmer
 * 
 */
public class BitbakePage extends FiniteStateWizardPage {

	private boolean valid = false;

	public BitbakePage(Map model) {
		super("Bitbake Console Page", model);
		setTitle("Extracting BitBake Environment");
		setMessage("Output of 'bitbake -e' command");
	}

	private Text txtConsole;

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

		txtConsole = new Text(top, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		txtConsole.setLayoutData(new GridData(GridData.FILL_BOTH));
		txtConsole.setEditable(false);
		txtConsole.setFont(JFaceResources.getFont(JFaceResources.TEXT_FONT));

		setControl(top);
	}

	@Override
	public void pageCleanup() {
		Activator.resetBBSession((String) model.get(ImportOEProjectWizard.KEY_LOCATION));
	}

	@Override
	public void pageDisplay() {
		Map props = (Map) model.get(OptionsPage.OPTION_MAP);

		try {
			String initPath = InstallJob.substitute((String) props.get(BBCProjectPage.INIT_SCRIPT_KEY), model);
			String location = InstallJob.substitute((String) props.get(BBCProjectPage.INSTALL_DIRECTORY_KEY), model);
			String name = null;
			if (props.containsKey(BBCProjectPage.PROJECT_NAME_KEY)) {
				name = InstallJob.substitute((String) props.get(BBCProjectPage.PROJECT_NAME_KEY), model);
			} else {
				name = parseName((String) props.get("Install Directory"));
			}

			ProjectInfo pinfo = new ProjectInfo();
			pinfo.setInitScriptPath(initPath);
			pinfo.setLocation(location);
			pinfo.setName(name);

			ConsoleWriter cw = new ConsoleWriter();
			this.getContainer().run(false, false, new BBConfigurationInitializeOperation(pinfo, cw));
			txtConsole.setText(cw.getContents());
			valid = true;
			model.put(InstallWizard.KEY_PINFO, pinfo);
		} catch (InvocationTargetException e) {
			Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, e.getTargetException().getMessage(), e));
			setErrorMessage("Failed to create project.");
			txtConsole.setText(e.getTargetException().getMessage());
			valid = false;
			setPageComplete(valid);
			return;
		} catch (InterruptedException e) {
		} catch (ParameterValueConversionException e) {
			Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e));
			setErrorMessage("Failed to create project: " + e.getMessage());
			valid = false;
			setPageComplete(valid);
			return;
		}

		setPageComplete(valid);
		((FiniteStateWizard) this.getWizard()).setCanFinish(true);
	}

	private String parseName(String name) {
		String[] e = name.split(File.separator);
		return e[e.length - 1];
	}

	@Override
	protected void updateModel() {

	}

	@Override
	protected boolean validatePage() {
		return valid;
	}

	private class ConsoleWriter extends Writer {

		private StringBuffer sb;

		public ConsoleWriter() {
			sb = new StringBuffer();
		}

		@Override
		public void close() throws IOException {
		}

		public String getContents() {
			return sb.toString();
		}

		@Override
		public void flush() throws IOException {
		}

		@Override
		public void write(char[] cbuf, int off, int len) throws IOException {
			txtConsole.getText().concat(new String(cbuf));
		}

		@Override
		public void write(String str) throws IOException {
			sb.append(str);
		}

	}

}