KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > NewWizard


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  *******************************************************************************/

11 package org.eclipse.ui.internal.dialogs;
12
13 import java.util.StringTokenizer JavaDoc;
14
15 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.jface.wizard.IWizard;
17 import org.eclipse.jface.wizard.Wizard;
18 import org.eclipse.ui.IWorkbench;
19 import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
20 import org.eclipse.ui.internal.WorkbenchImages;
21 import org.eclipse.ui.internal.WorkbenchMessages;
22 import org.eclipse.ui.internal.WorkbenchPlugin;
23 import org.eclipse.ui.wizards.IWizardCategory;
24 import org.eclipse.ui.wizards.IWizardDescriptor;
25
26 /**
27  * The new wizard is responsible for allowing the user to choose which new
28  * (nested) wizard to run. The set of available new wizards comes from the new
29  * extension point.
30  */

31 public class NewWizard extends Wizard {
32     private static final String JavaDoc CATEGORY_SEPARATOR = "/"; //$NON-NLS-1$
33

34     private String JavaDoc categoryId = null;
35
36     private NewWizardSelectionPage mainPage;
37
38     private boolean projectsOnly = false;
39
40     private IStructuredSelection selection;
41
42     private IWorkbench workbench;
43
44     /**
45      * Create the wizard pages
46      */

47     public void addPages() {
48         IWizardCategory root = WorkbenchPlugin.getDefault().getNewWizardRegistry().getRootCategory();
49         IWizardDescriptor [] primary = WorkbenchPlugin.getDefault().getNewWizardRegistry().getPrimaryWizards();
50
51         if (categoryId != null) {
52             IWizardCategory categories = root;
53             StringTokenizer JavaDoc familyTokenizer = new StringTokenizer JavaDoc(categoryId,
54                     CATEGORY_SEPARATOR);
55             while (familyTokenizer.hasMoreElements()) {
56                 categories = getChildWithID(categories, familyTokenizer
57                         .nextToken());
58                 if (categories == null) {
59                     break;
60                 }
61             }
62             if (categories != null) {
63                 root = categories;
64             }
65         }
66
67         mainPage = new NewWizardSelectionPage(workbench, selection, root,
68                 primary, projectsOnly);
69         addPage(mainPage);
70     }
71
72     /**
73      * Returns the id of the category of wizards to show or <code>null</code>
74      * to show all categories. If no entries can be found with this id then all
75      * categories are shown.
76      *
77      * @return String or <code>null</code>.
78      */

79     public String JavaDoc getCategoryId() {
80         return categoryId;
81     }
82
83     /**
84      * Returns the child collection element for the given id
85      */

86     private IWizardCategory getChildWithID(
87             IWizardCategory parent, String JavaDoc id) {
88         IWizardCategory [] children = parent.getCategories();
89         for (int i = 0; i < children.length; ++i) {
90             IWizardCategory currentChild = children[i];
91             if (currentChild.getId().equals(id)) {
92                 return currentChild;
93             }
94         }
95         return null;
96     }
97
98     /**
99      * Lazily create the wizards pages
100      * @param aWorkbench the workbench
101      * @param currentSelection the current selection
102      */

103     public void init(IWorkbench aWorkbench,
104             IStructuredSelection currentSelection) {
105         this.workbench = aWorkbench;
106         this.selection = currentSelection;
107
108         if (projectsOnly) {
109             setWindowTitle(WorkbenchMessages.NewProject_title);
110         } else {
111             setWindowTitle(WorkbenchMessages.NewWizard_title);
112         }
113         setDefaultPageImageDescriptor(WorkbenchImages
114                 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_WIZBAN_NEW_WIZ));
115         setNeedsProgressMonitor(true);
116     }
117
118     /**
119      * The user has pressed Finish. Instruct self's pages to finish, and answer
120      * a boolean indicating success.
121      *
122      * @return boolean
123      */

124     public boolean performFinish() {
125         //save our selection state
126
mainPage.saveWidgetValues();
127         // if we're finishing from the main page then perform finish on the selected wizard.
128
if (getContainer().getCurrentPage() == mainPage) {
129             if (mainPage.canFinishEarly()) {
130                 IWizard wizard = mainPage.getSelectedNode().getWizard();
131                 wizard.setContainer(getContainer());
132                 return wizard.performFinish();
133             }
134         }
135         return true;
136     }
137
138     /**
139      * Sets the id of the category of wizards to show or <code>null</code> to
140      * show all categories. If no entries can be found with this id then all
141      * categories are shown.
142      *
143      * @param id may be <code>null</code>.
144      */

145     public void setCategoryId(String JavaDoc id) {
146         categoryId = id;
147     }
148
149     /**
150      * Sets the projects only flag. If <code>true</code> only projects will
151      * be shown in this wizard.
152      * @param b if only projects should be shown
153      */

154     public void setProjectsOnly(boolean b) {
155         projectsOnly = b;
156     }
157     
158     /* (non-Javadoc)
159      * @see org.eclipse.jface.wizard.IWizard#canFinish()
160      */

161     public boolean canFinish() {
162          // we can finish if the first page is current and the the page can finish early.
163
if (getContainer().getCurrentPage() == mainPage) {
164                 if (mainPage.canFinishEarly()) {
165                     return true;
166                 }
167             }
168             return super.canFinish();
169     }
170 }
171
Popular Tags