|
1 /* |
|
2 * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 package com.nokia.mj.impl.installer.ui.eswt2; |
|
20 |
|
21 import org.eclipse.swt.SWT; |
|
22 import org.eclipse.swt.events.KeyEvent; |
|
23 import org.eclipse.swt.events.KeyListener; |
|
24 import org.eclipse.swt.events.SelectionEvent; |
|
25 import org.eclipse.swt.events.SelectionListener; |
|
26 import org.eclipse.swt.graphics.Point; |
|
27 import org.eclipse.swt.graphics.Rectangle; |
|
28 import org.eclipse.swt.layout.GridData; |
|
29 import org.eclipse.swt.widgets.Button; |
|
30 import org.eclipse.swt.widgets.Composite; |
|
31 import org.eclipse.swt.widgets.Control; |
|
32 import org.eclipse.swt.widgets.Label; |
|
33 import org.eclipse.swt.widgets.ProgressBar; |
|
34 import org.eclipse.swt.widgets.Shell; |
|
35 |
|
36 /** |
|
37 * ProgressView displays a progress bar. |
|
38 * Value range for the progress bar is between 0 and 100, |
|
39 * unless the progress bar is created as indeterminate |
|
40 * in which case the value is ignored. |
|
41 */ |
|
42 public class ProgressView extends ViewBase |
|
43 { |
|
44 private KeyListener iViewKeyListener = null; |
|
45 private KeyListener iSoftKeyListener = null; |
|
46 private Label iLabel = null; |
|
47 private String iMsg = null; |
|
48 private ProgressBar iProgressBar = null; |
|
49 private boolean iIndeterminate = false; |
|
50 private int iValue = 0; |
|
51 protected Button iHideCommand = null; |
|
52 protected Button iCancelCommand = null; |
|
53 |
|
54 /** Constructor */ |
|
55 protected ProgressView() |
|
56 { |
|
57 super(); |
|
58 } |
|
59 |
|
60 /** Constructor */ |
|
61 protected ProgressView(InstallerUiEswt aInstaller, Composite aParent) |
|
62 { |
|
63 this(aInstaller, aParent, ""); |
|
64 } |
|
65 |
|
66 /** Constructor */ |
|
67 protected ProgressView(InstallerUiEswt aInstaller, Composite aParent, String aMsg) |
|
68 { |
|
69 this(aInstaller, aParent, aMsg, false); |
|
70 } |
|
71 |
|
72 /** Constructor */ |
|
73 protected ProgressView(InstallerUiEswt aInstaller, Composite aParent, String aMsg, |
|
74 boolean aIndeterminate) |
|
75 { |
|
76 super(aInstaller, aParent, 8); |
|
77 iMsg = aMsg; |
|
78 |
|
79 // Add header. |
|
80 if (iInstallerUi != null && iInstallerUi.getInstallInfo() != null) |
|
81 { |
|
82 // Add header. |
|
83 addHeader(aMsg, iInstallerUi.getInstallInfo(), null); |
|
84 } |
|
85 else |
|
86 { |
|
87 setTitle(iMsg); |
|
88 iLabel = createLabel(iMsg, SWT.WRAP); |
|
89 } |
|
90 |
|
91 iIndeterminate = aIndeterminate; |
|
92 if (iIndeterminate) |
|
93 { |
|
94 iProgressBar = new ProgressBar |
|
95 (getComposite(), SWT.INDETERMINATE | SWT.HORIZONTAL); |
|
96 } |
|
97 else |
|
98 { |
|
99 iProgressBar = new ProgressBar |
|
100 (getComposite(), SWT.SMOOTH | SWT.HORIZONTAL); |
|
101 iProgressBar.setMinimum(0); |
|
102 iProgressBar.setMaximum(100); |
|
103 iProgressBar.setSelection(iValue); |
|
104 } |
|
105 GridData gridData = new GridData(GridData.FILL_HORIZONTAL); |
|
106 gridData.horizontalSpan = getColumns(); |
|
107 iProgressBar.setLayoutData(gridData); |
|
108 |
|
109 // After other widgets have been added, add content to |
|
110 // application info Composite. |
|
111 if (iInstallerUi != null) |
|
112 { |
|
113 addAppInfo(iInstallerUi.getInstallInfo(), false); |
|
114 } |
|
115 |
|
116 // By default add cancel command to all progress bars. |
|
117 addCancelCommand(); |
|
118 } |
|
119 |
|
120 /** Update text for this progress bar. */ |
|
121 public void setText(String aText) |
|
122 { |
|
123 iMsg = aText; |
|
124 // UI updates must be executed in UI thread. |
|
125 getComposite().getDisplay().syncExec |
|
126 (new Runnable() |
|
127 { |
|
128 public void run() |
|
129 { |
|
130 setTitle(iMsg); |
|
131 iLabel.setText(iMsg); |
|
132 updateSize(); |
|
133 } |
|
134 }); |
|
135 } |
|
136 |
|
137 /** Update value for this progress bar. */ |
|
138 public void updateProgress(int aValue) |
|
139 { |
|
140 iValue = aValue; |
|
141 if (iIndeterminate) |
|
142 { |
|
143 // No need to update an indeterminate progress bar. |
|
144 return; |
|
145 } |
|
146 // UI updates must be executed in UI thread. |
|
147 getComposite().getDisplay().syncExec |
|
148 (new Runnable() |
|
149 { |
|
150 public void run() |
|
151 { |
|
152 iProgressBar.setSelection(iValue); |
|
153 } |
|
154 }); |
|
155 } |
|
156 |
|
157 /** |
|
158 * Adds a cancel command for this progress view. |
|
159 */ |
|
160 protected void addCancelCommand() |
|
161 { |
|
162 if (iCancelCommand != null || isDisposed()) |
|
163 { |
|
164 // Cancel command has already been added, do not add it anymore. |
|
165 return; |
|
166 } |
|
167 // UI updates must be executed in UI thread. |
|
168 getDisplay().syncExec(new Runnable() |
|
169 { |
|
170 public void run() |
|
171 { |
|
172 iHideCommand = new Button(getCommandComposite(), SWT.PUSH); |
|
173 GridData gridData = new GridData(GridData.FILL_HORIZONTAL); |
|
174 gridData.horizontalSpan = 1; |
|
175 iHideCommand.setLayoutData(gridData); |
|
176 iHideCommand.setText("Hide"); //InstallerUiTexts.get(InstallerUiTexts.HIDE)); |
|
177 iHideCommand.addSelectionListener |
|
178 (new SelectionListener() |
|
179 { |
|
180 public void widgetDefaultSelected(SelectionEvent aEvent) |
|
181 { |
|
182 widgetSelected(aEvent); |
|
183 } |
|
184 public void widgetSelected(SelectionEvent aEvent) |
|
185 { |
|
186 confirmHide(); |
|
187 } |
|
188 }); |
|
189 addSoftKeyListenerFor(iHideCommand); |
|
190 |
|
191 iCancelCommand = new Button(getCommandComposite(), SWT.PUSH); |
|
192 gridData = new GridData(GridData.FILL_HORIZONTAL); |
|
193 gridData.horizontalSpan = 1; |
|
194 iCancelCommand.setLayoutData(gridData); |
|
195 iCancelCommand.setText( |
|
196 InstallerUiTexts.get(InstallerUiTexts.CANCEL)); |
|
197 iCancelCommand.addSelectionListener |
|
198 (new SelectionListener() |
|
199 { |
|
200 public void widgetDefaultSelected(SelectionEvent aEvent) |
|
201 { |
|
202 widgetSelected(aEvent); |
|
203 } |
|
204 public void widgetSelected(SelectionEvent aEvent) |
|
205 { |
|
206 confirmCancel(); |
|
207 } |
|
208 }); |
|
209 addSoftKeyListenerFor(iCancelCommand); |
|
210 // Set the Cancel command to be the default command. |
|
211 iCancelCommand.setFocus(); |
|
212 getShell().setDefaultButton(iCancelCommand); |
|
213 |
|
214 // Add a KeyListener to handle selection key, LSK and RSK events. |
|
215 addViewKeyListenerFor(getContainer()); |
|
216 } |
|
217 }); |
|
218 } |
|
219 |
|
220 /** |
|
221 * Removes cancel command for this progress view. |
|
222 */ |
|
223 protected void removeCancelCommand() |
|
224 { |
|
225 if (iCancelCommand == null || isDisposed()) |
|
226 { |
|
227 // Cancel command does not exist, do nothing. |
|
228 return; |
|
229 } |
|
230 // UI updates must be executed in UI thread. |
|
231 getDisplay().syncExec |
|
232 (new Runnable() |
|
233 { |
|
234 public void run() |
|
235 { |
|
236 getShell().setDefaultButton(null); |
|
237 iHideCommand.dispose(); |
|
238 iHideCommand = null; |
|
239 iCancelCommand.dispose(); |
|
240 iCancelCommand = null; |
|
241 } |
|
242 }); |
|
243 } |
|
244 |
|
245 /** |
|
246 * This method is called when user cancels the dialog. |
|
247 */ |
|
248 protected void confirmCancel() |
|
249 { |
|
250 if (iCancelCommand == null) |
|
251 { |
|
252 // Ignore selection of a non-existing command. |
|
253 return; |
|
254 } |
|
255 if (iInstallerUi != null) |
|
256 { |
|
257 iInstallerUi.cancel(); |
|
258 } |
|
259 } |
|
260 |
|
261 /** |
|
262 * This method is called when user hides the dialog. |
|
263 */ |
|
264 protected void confirmHide() |
|
265 { |
|
266 if (iHideCommand == null) |
|
267 { |
|
268 // Ignore selection of a non-existing command. |
|
269 return; |
|
270 } |
|
271 // Hide the InstallerUi. |
|
272 if (iInstallerUi != null) |
|
273 { |
|
274 log(this + " hide"); |
|
275 iInstallerUi.hide(true); |
|
276 } |
|
277 } |
|
278 |
|
279 /** |
|
280 * Adds KeyListener for given control. This KeyListener handles |
|
281 * selection key, LSK and RSK. |
|
282 */ |
|
283 protected void addViewKeyListenerFor(Control aControl) |
|
284 { |
|
285 if (aControl == null) |
|
286 { |
|
287 return; |
|
288 } |
|
289 if (iViewKeyListener == null) |
|
290 { |
|
291 iViewKeyListener = new KeyListener() |
|
292 { |
|
293 public void keyPressed(KeyEvent aEvent) |
|
294 { |
|
295 switch (aEvent.keyCode) |
|
296 { |
|
297 case SWT.ARROW_UP: // up |
|
298 case SWT.ARROW_DOWN: // down |
|
299 case SWT.ARROW_LEFT: // left |
|
300 case SWT.ARROW_RIGHT: // right |
|
301 // When View has focus and user presses arrow |
|
302 // keys, set focus to the default command button. |
|
303 iCancelCommand.setFocus(); |
|
304 break; |
|
305 case -5: // selection key |
|
306 // When View has focus the selection key |
|
307 // does nothing in progress bar view. |
|
308 break; |
|
309 } |
|
310 } |
|
311 public void keyReleased(KeyEvent aEvent) |
|
312 { |
|
313 } |
|
314 }; |
|
315 } |
|
316 aControl.addKeyListener(iViewKeyListener); |
|
317 addSoftKeyListenerFor(aControl); |
|
318 } |
|
319 |
|
320 /** |
|
321 * Adds KeyListener for given control. This KeyListener handles |
|
322 * LSK and RSK. |
|
323 */ |
|
324 protected void addSoftKeyListenerFor(Control aControl) |
|
325 { |
|
326 if (aControl == null) |
|
327 { |
|
328 return; |
|
329 } |
|
330 if (iSoftKeyListener == null) |
|
331 { |
|
332 iSoftKeyListener = new KeyListener() |
|
333 { |
|
334 public void keyPressed(KeyEvent aEvent) |
|
335 { |
|
336 switch (aEvent.keyCode) |
|
337 { |
|
338 case -6: // LSK |
|
339 // Empty command, do nothing. |
|
340 break; |
|
341 case -7: // RSK |
|
342 confirmCancel(); |
|
343 break; |
|
344 } |
|
345 } |
|
346 public void keyReleased(KeyEvent aEvent) |
|
347 { |
|
348 } |
|
349 }; |
|
350 } |
|
351 aControl.addKeyListener(iSoftKeyListener); |
|
352 } |
|
353 } |