KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > MultiStepConfigureWizardPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
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  * IBM Corporation - initial API and implementation
10  * Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog font should be
11  * activated and used by other components.
12  *******************************************************************************/

13 package org.eclipse.ui.internal.ide.dialogs;
14
15 import java.lang.reflect.InvocationTargetException JavaDoc;
16
17 import org.eclipse.jface.operation.IRunnableWithProgress;
18 import org.eclipse.jface.wizard.IWizard;
19 import org.eclipse.jface.wizard.IWizardContainer;
20 import org.eclipse.jface.wizard.IWizardPage;
21 import org.eclipse.jface.wizard.WizardPage;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.custom.BusyIndicator;
24 import org.eclipse.swt.graphics.Point;
25 import org.eclipse.swt.graphics.Rectangle;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.ui.PlatformUI;
32 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
33 import org.eclipse.ui.internal.ide.misc.WizardStepGroup;
34
35 /**
36  * This page allows the user to go thru a series of steps. Each
37  * step that has a wizard will have its pages embedded into this
38  * page. At the end, the step will be asked to complete its work.
39  * <p>
40  * Example usage:
41  * <pre>
42  * mainPage = new MultiStepConfigureWizardPage("multiStepWizardPage");
43  * mainPage.setTitle("Project");
44  * mainPage.setDescription("Configure project capability.");
45  * </pre>
46  * </p>
47  */

48 public class MultiStepConfigureWizardPage extends WizardPage {
49     private MultiStepWizardDialog wizardDialog;
50
51     private Composite pageSite;
52
53     private WizardStepGroup stepGroup;
54
55     private WizardStepContainer stepContainer = new WizardStepContainer();
56
57     /**
58      * Creates a new multi-step wizard page.
59      *
60      * @param pageName the name of this page
61      */

62     public MultiStepConfigureWizardPage(String JavaDoc pageName) {
63         super(pageName);
64     }
65
66     /* (non-Javadoc)
67      * Method declared on IWizardPage
68      */

69     public boolean canFlipToNextPage() {
70         return stepContainer.canFlipToNextPage();
71     }
72
73     /* (non-Javadoc)
74      * Method declared on IDialogPage.
75      */

76     public void createControl(Composite parent) {
77         Composite composite = new Composite(parent, SWT.NULL);
78         GridLayout layout = new GridLayout();
79         layout.numColumns = 2;
80         composite.setLayout(layout);
81         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
82         composite.setFont(parent.getFont());
83
84         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite,
85                 IIDEHelpContextIds.NEW_PROJECT_CONFIGURE_WIZARD_PAGE);
86
87         createStepGroup(composite);
88         createEmbeddedPageSite(composite);
89
90         setControl(composite);
91     }
92
93     /**
94      * Creates the control where the step's wizard will
95      * display its pages.
96      */

97     private void createEmbeddedPageSite(Composite parent) {
98         pageSite = new Composite(parent, SWT.NONE);
99         pageSite.setLayout(new GridLayout());
100         pageSite.setLayoutData(new GridData(GridData.FILL_BOTH));
101     }
102
103     /**
104      * Creates the control for the step list
105      */

106     private void createStepGroup(Composite parent) {
107         stepGroup = new WizardStepGroup();
108         stepGroup.createContents(parent);
109     }
110
111     /**
112      * Returns the container handler for the pages
113      * of the step's wizard.
114      */

115     /* package */WizardStepContainer getStepContainer() {
116         return stepContainer;
117     }
118
119     /* (non-Javadoc)
120      * Method declared on IDialogPage.
121      */

122     public String JavaDoc getMessage() {
123         String JavaDoc msg = stepContainer.getMessage();
124         if (msg == null || msg.length() == 0) {
125             msg = super.getMessage();
126         }
127         return msg;
128     }
129
130     /* (non-Javadoc)
131      * Method declared on IWizardPage.
132      */

133     public IWizardPage getPreviousPage() {
134         return stepContainer.getPreviousPage();
135     }
136
137     /* (non-Javadoc)
138      * Method declared on IWizardPage.
139      */

140     public void setPreviousPage(IWizardPage page) {
141         // Do not allow to go back
142
super.setPreviousPage(null);
143     }
144
145     /**
146      * Sets the steps to be displayed. Ignored if the
147      * createControl has not been called yet.
148      *
149      * @param steps the collection of steps
150      */

151     /* package */void setSteps(WizardStep[] steps) {
152         if (stepGroup != null) {
153             stepGroup.setSteps(steps);
154         }
155     }
156
157     /**
158      * Sets the multi-step wizard dialog processing this
159      * page.
160      */

161     /* package */void setWizardDialog(MultiStepWizardDialog dialog) {
162         wizardDialog = dialog;
163     }
164
165     /* (non-Javadoc)
166      * Method declared on IDialogPage.
167      */

168     public void setVisible(boolean visible) {
169         super.setVisible(visible);
170         WizardStep[] steps = stepGroup.getSteps();
171         MultiStepWizard stepWizard = wizardDialog.getMultiStepWizard();
172         wizardDialog.setFinishLabel(stepWizard.getFinishStepLabel(steps));
173
174         getControl().getDisplay().asyncExec(new Runnable JavaDoc() {
175             public void run() {
176                 stepContainer.processCurrentStep();
177             }
178         });
179     }
180
181     /**
182      * Support for handling pages from the step's wizard
183      */

184     /* package */class WizardStepContainer implements IWizardContainer {
185         private int stepIndex = 0;
186
187         private IWizard wizard;
188
189         private IWizardPage currentPage;
190
191         /* (non-Javadoc)
192          * Method declared on IRunnableContext.
193          */

194         public void run(boolean fork, boolean cancelable,
195                 IRunnableWithProgress runnable)
196                 throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
197             getContainer().run(fork, cancelable, runnable);
198         }
199
200         /* (non-Javadoc)
201          * Method declared on IWizardContainer.
202          */

203         public IWizardPage getCurrentPage() {
204             return currentPage;
205         }
206
207         /* (non-Javadoc)
208          * Method declared on IWizardContainer.
209          */

210         public Shell getShell() {
211             return getContainer().getShell();
212         }
213
214         /* (non-Javadoc)
215          * Method declared on IWizardContainer.
216          */

217         public void showPage(IWizardPage page) {
218             showPage(page, true);
219         }
220
221         /* (non-Javadoc)
222          * Method declared on IWizardContainer.
223          */

224         public void updateButtons() {
225             getContainer().updateButtons();
226         }
227
228         /* (non-Javadoc)
229          * Method declared on IWizardContainer.
230          */

231         public void updateMessage() {
232             getContainer().updateMessage();
233         }
234
235         /* (non-Javadoc)
236          * Method declared on IWizardContainer.
237          */

238         public void updateTitleBar() {
239             getContainer().updateTitleBar();
240         }
241
242         /* (non-Javadoc)
243          * Method declared on IWizardContainer.
244          */

245         public void updateWindowTitle() {
246             getContainer().updateWindowTitle();
247         }
248
249         /**
250          * Handles the back button pressed
251          */

252         public void backPressed() {
253             showPage(currentPage.getPreviousPage(), false);
254         }
255
256         /**
257          * Handles the next button pressed
258          */

259         public void nextPressed() {
260             showPage(currentPage.getNextPage(), true);
261         }
262
263         /**
264          * Handles the help button pressed
265          */

266         public void helpPressed() {
267             if (currentPage != null) {
268                 currentPage.performHelp();
269             }
270         }
271
272         /**
273          * Handles close request.
274          *
275          * @return the result
276          */

277         public final boolean performCancel() {
278             if (wizard != null) {
279                 return wizard.performCancel();
280             }
281
282             return true;
283         }
284
285         /**
286          * Handles finish request.
287          *
288          * @return the result
289          */

290         public final boolean performFinish() {
291             if (wizard != null) {
292                 if (wizard.performFinish()) {
293                     wizard.dispose();
294                     wizard.setContainer(null);
295                     stepGroup.markStepAsDone();
296                     stepIndex++;
297                     return true;
298                 }
299                 return false;
300             }
301             return true;
302             
303         }
304
305         /**
306          * Calculates the difference in size between the given
307          * page and the page site. A larger page results
308          * in a positive delta.
309          *
310          * @param page the page
311          * @return the size difference encoded
312          * as a <code>new Point(deltaWidth,deltaHeight)</code>
313          */

314         private Point calculatePageSizeDelta(IWizardPage page) {
315             Control pageControl = page.getControl();
316
317             if (pageControl == null) {
318                 // control not created yet
319
return new Point(0, 0);
320             }
321
322             Point contentSize = pageControl.computeSize(SWT.DEFAULT,
323                     SWT.DEFAULT, true);
324             Rectangle rect = pageSite.getClientArea();
325             Point containerSize = new Point(rect.width, rect.height);
326
327             return new Point(Math.max(0, contentSize.x - containerSize.x), Math
328                     .max(0, contentSize.y - containerSize.y));
329         }
330
331         /**
332          * Computes the correct page site size for the given page
333          * and resizes the dialog if nessessary.
334          *
335          * @param page the wizard page
336          */

337         private void updateSizeForPage(IWizardPage page) {
338             // ensure the page container is large enough
339
Point delta = calculatePageSizeDelta(page);
340
341             if (delta.x > 0 || delta.y > 0) {
342                 Point siteSize = pageSite.getSize();
343                 GridData data = (GridData) pageSite.getLayoutData();
344                 data.heightHint = siteSize.y + delta.y;
345                 data.widthHint = siteSize.x + delta.x;
346             }
347         }
348
349         /**
350          * Computes the correct page site size for the given wizard
351          * and resizes the dialog if nessessary.
352          *
353          * @param wizard the wizard
354          */

355         private void updateSizeForWizard(IWizard wizard) {
356             Point delta = new Point(0, 0);
357             IWizardPage[] pages = wizard.getPages();
358             for (int i = 0; i < pages.length; i++) {
359                 // ensure the page site is large enough
360
Point pageDelta = calculatePageSizeDelta(pages[i]);
361
362                 delta.x = Math.max(delta.x, pageDelta.x);
363                 delta.y = Math.max(delta.y, pageDelta.y);
364             }
365
366             if (delta.x > 0 || delta.y > 0) {
367                 Point siteSize = pageSite.getSize();
368                 GridData data = (GridData) pageSite.getLayoutData();
369                 data.heightHint = siteSize.y + delta.y;
370                 data.widthHint = siteSize.x + delta.x;
371             }
372         }
373
374         /**
375          * Process the current step's wizard.
376          */

377         public void processCurrentStep() {
378             WizardStep[] steps = stepGroup.getSteps();
379             while (stepIndex < steps.length) {
380                 // adjust the finish button label
381
if (stepIndex == (steps.length - 1)) {
382                     wizardDialog.setFinishLabel(null);
383                 }
384
385                 final WizardStep step = steps[stepIndex];
386                 stepGroup.setCurrentStep(step);
387
388                 final IWizard[] stepWizard = new IWizard[1];
389                 BusyIndicator.showWhile(getShell().getDisplay(),
390                         new Runnable JavaDoc() {
391                             public void run() {
392                                 stepWizard[0] = step.getWizard();
393                                 int tries = 0;
394                                 while (stepWizard[0] == null && tries++ < 3) {
395                                     boolean tryAgain = wizardDialog
396                                             .getMultiStepWizard()
397                                             .handleMissingStepWizard(step);
398                                     if (!tryAgain) {
399                                         break;
400                                     }
401
402                                     stepWizard[0] = step.getWizard();
403                                 }
404                             }
405                         });
406
407                 if (stepWizard[0] == null) {
408                     break;
409                 }
410                 setWizard(stepWizard[0]);
411                 if (stepWizard[0].getPageCount() > 0) {
412                     return;
413                 }
414
415                 performFinish();
416             }
417
418             wizardDialog.forceClose();
419         }
420
421         /**
422          * Sets the current wizard.
423          *
424          * @param newWizard the current wizard
425          */

426         public void setWizard(IWizard newWizard) {
427             wizard = newWizard;
428
429             // Allow the wizard pages to precreate their page controls
430
// This allows the wizard to open to the correct size
431
wizard.createPageControls(pageSite);
432
433             // Ensure that all of the created pages are initially not visible
434
IWizardPage[] pages = wizard.getPages();
435             for (int i = 0; i < pages.length; i++) {
436                 IWizardPage page = pages[i];
437                 if (page.getControl() != null) {
438                     page.getControl().setVisible(false);
439                 }
440             }
441
442             // Ensure the dialog is large enough for the wizard
443
updateSizeForWizard(wizard);
444             wizardDialog.updateLayout();
445
446             wizard.setContainer(this);
447             showPage(wizard.getStartingPage(), false);
448         }
449
450         /**
451          * Show the requested page.
452          *
453          * @param page the page
454          * @param rememberPrevious whether hte previous page should be remembered
455          */

456         public void showPage(IWizardPage page, boolean rememberPrevious) {
457             if (page == null || page == currentPage) {
458                 return;
459             }
460
461             if (rememberPrevious && currentPage != null) {
462                 page.setPreviousPage(currentPage);
463             }
464
465             if (wizard != page.getWizard()) {
466                 throw new IllegalStateException JavaDoc();
467             }
468
469             // ensure that page control has been created
470
// (this allows lazy page control creation)
471
if (page.getControl() == null) {
472                 page.createControl(pageSite);
473                 // the page is responsible for ensuring the created control is accessable
474
// via getControl.
475
if (page.getControl() == null) {
476                     throw new IllegalArgumentException JavaDoc();
477                 }
478                 // ensure the dialog is large enough for this page
479
updateSizeForPage(page);
480                 wizardDialog.updateLayout();
481             }
482
483             // make the new page visible
484
IWizardPage oldPage = currentPage;
485             currentPage = page;
486             currentPage.setVisible(true);
487             if (oldPage != null) {
488                 oldPage.setVisible(false);
489             }
490             page.getControl().setBounds(pageSite.getClientArea());
491
492             // update the dialog controls
493
wizardDialog.updateAll();
494         }
495
496         /**
497          * Returns whether the current wizard can finish.
498          *
499          * @return whether the wizard can finish
500          */

501         public boolean canWizardFinish() {
502             if (wizard != null) {
503                 return wizard.canFinish();
504             }
505
506             return false;
507         }
508
509         /**
510          * Returns whether the current page can flip to
511          * the next page.
512          *
513          * @return can flip to next page
514          */

515         public boolean canFlipToNextPage() {
516             if (currentPage != null) {
517                 return currentPage.canFlipToNextPage();
518             }
519             return false;
520         }
521
522         /**
523          * Returns the current page's message.
524          *
525          * @return the message
526          */

527         public String JavaDoc getMessage() {
528             if (currentPage != null) {
529                 return currentPage.getMessage();
530             }
531
532             return null;
533         }
534
535         /**
536          * Returns the current page's previous page.
537          *
538          * @return the page
539          */

540         public IWizardPage getPreviousPage() {
541             if (currentPage != null) {
542                 return currentPage.getPreviousPage();
543             }
544
545             return null;
546         }
547     }
548 }
549
Popular Tags