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


import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;

import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.IJobChangeListener;
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.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.openembedded.bc.ui.wizards.FiniteStateWizardPage;


/**
 * Select which flavor of OE is to be installed.
 * @author kgilmer
 *
 */
public class ProgressPage extends FiniteStateWizardPage  {

	private static final String STARTED_INSTALL = "STARTED_INSTALL";
	private Text txtConsole;
	private StringBuffer consoleBuffer;
	private ProgressBar pbProgress;
	private String lastError;

	protected static final int PRINT_CMD = 1;
	protected static final int PRINT_OUT = 2;
	protected static final int PRINT_ERR = 3;
	
	protected ProgressPage(Map model) {
		super("Progress", model);
		setTitle("Installing OpenEmbedded");
		setMessage("");
	}

	public void createControl(Composite parent) {
		Composite top = new Composite(parent, SWT.None);
		top.setLayout(new GridLayout());
		top.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		txtConsole = new Text(top, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
		txtConsole.setLayoutData(new GridData(GridData.FILL_BOTH));
		txtConsole.setFont(JFaceResources.getFont(JFaceResources.TEXT_FONT));
		txtConsole.setEditable(false);
		
		Label s = new Label(top, SWT.SEPARATOR | SWT.HORIZONTAL);
		s.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		pbProgress = new ProgressBar(top, SWT.HORIZONTAL | SWT.BORDER);
		
		setControl(top);
	}
	
	protected void printLine(String line, final int type) {
		if (consoleBuffer == null) {
			consoleBuffer = new StringBuffer();
		}
		
		if (type == PRINT_CMD) {
			consoleBuffer.append("$ ");
		} else if (type == PRINT_ERR) {
			consoleBuffer.append("ERROR: ");
			lastError = line;
		}
		
		consoleBuffer.append(line);
		consoleBuffer.append('\n');
		
		PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {

			
			public void run() {
				txtConsole.setText(consoleBuffer.toString());
				txtConsole.setSelection(txtConsole.getText().length() - 1);
				
				if (type == PRINT_CMD) {
					pbProgress.setSelection(pbProgress.getSelection() + 1);
				}
			}
			
		});
	}

	public void pageCleanup() {
		
	}

	public void pageDisplay() {
		if (!model.containsKey(STARTED_INSTALL)) {
			model.put(STARTED_INSTALL, new Boolean(true));
	
			try {
				pbProgress.setMaximum(getLineCount((String) model.get(FlavorPage.INSTALL_SCRIPT)));
			} catch (IOException e) {
				//TODO add logging here.
				e.printStackTrace();
				return;
			}
			
			executeInstall();
		}
	}

	private int getLineCount(String str) throws IOException {
		int count = 0;
		
		BufferedReader br = new BufferedReader(new StringReader(str));
		
		while (br.readLine() != null) {
			count++;
		}
		
		return count;
	}

	private void executeInstall() {
		InstallJob j = new InstallJob(model, this);
		j.addJobChangeListener(new IJobChangeListener() {
			
			public void aboutToRun(IJobChangeEvent event) {
			}

			
			public void awake(IJobChangeEvent event) {
			}

			
			public void done(final IJobChangeEvent event) {
				PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {

					
					public void run() {
						if (event.getResult().isOK()) {
							setMessage("Installation complete, next is to load into workspace.");
							pbProgress.setVisible(false);
							setPageComplete(true);
						} else {
							setErrorMessage("An error occurred while installing OpenEmbedded:\n" + lastError);
						}
					}
					
				});				
			}

			public void running(IJobChangeEvent event) {
			}

			
			public void scheduled(IJobChangeEvent event) {				
			}

			
			public void sleeping(IJobChangeEvent event) {
			}
			
		});
		setMessage("Installing OpenEmbedded...");
		j.schedule();
	}

	
	protected void updateModel() {
	}

	
	protected boolean validatePage() {		
		
		return true;
	}

	public void printDialog(final String msg) {
		PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {

			
			public void run() {
				setMessage(msg);
			}
		});
	}

}