001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.dialogs;
011:
012: import java.util.StringTokenizer;
013:
014: import org.eclipse.jface.viewers.IStructuredSelection;
015: import org.eclipse.jface.wizard.IWizard;
016: import org.eclipse.jface.wizard.Wizard;
017: import org.eclipse.ui.IWorkbench;
018: import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
019: import org.eclipse.ui.internal.WorkbenchImages;
020: import org.eclipse.ui.internal.WorkbenchMessages;
021: import org.eclipse.ui.internal.WorkbenchPlugin;
022: import org.eclipse.ui.wizards.IWizardCategory;
023: import org.eclipse.ui.wizards.IWizardDescriptor;
024:
025: /**
026: * The new wizard is responsible for allowing the user to choose which new
027: * (nested) wizard to run. The set of available new wizards comes from the new
028: * extension point.
029: */
030: public class NewWizard extends Wizard {
031: private static final String CATEGORY_SEPARATOR = "/"; //$NON-NLS-1$
032:
033: private String categoryId = null;
034:
035: private NewWizardSelectionPage mainPage;
036:
037: private boolean projectsOnly = false;
038:
039: private IStructuredSelection selection;
040:
041: private IWorkbench workbench;
042:
043: /**
044: * Create the wizard pages
045: */
046: public void addPages() {
047: IWizardCategory root = WorkbenchPlugin.getDefault()
048: .getNewWizardRegistry().getRootCategory();
049: IWizardDescriptor[] primary = WorkbenchPlugin.getDefault()
050: .getNewWizardRegistry().getPrimaryWizards();
051:
052: if (categoryId != null) {
053: IWizardCategory categories = root;
054: StringTokenizer familyTokenizer = new StringTokenizer(
055: categoryId, CATEGORY_SEPARATOR);
056: while (familyTokenizer.hasMoreElements()) {
057: categories = getChildWithID(categories, familyTokenizer
058: .nextToken());
059: if (categories == null) {
060: break;
061: }
062: }
063: if (categories != null) {
064: root = categories;
065: }
066: }
067:
068: mainPage = new NewWizardSelectionPage(workbench, selection,
069: root, primary, projectsOnly);
070: addPage(mainPage);
071: }
072:
073: /**
074: * Returns the id of the category of wizards to show or <code>null</code>
075: * to show all categories. If no entries can be found with this id then all
076: * categories are shown.
077: *
078: * @return String or <code>null</code>.
079: */
080: public String getCategoryId() {
081: return categoryId;
082: }
083:
084: /**
085: * Returns the child collection element for the given id
086: */
087: private IWizardCategory getChildWithID(IWizardCategory parent,
088: String id) {
089: IWizardCategory[] children = parent.getCategories();
090: for (int i = 0; i < children.length; ++i) {
091: IWizardCategory currentChild = children[i];
092: if (currentChild.getId().equals(id)) {
093: return currentChild;
094: }
095: }
096: return null;
097: }
098:
099: /**
100: * Lazily create the wizards pages
101: * @param aWorkbench the workbench
102: * @param currentSelection the current selection
103: */
104: public void init(IWorkbench aWorkbench,
105: IStructuredSelection currentSelection) {
106: this .workbench = aWorkbench;
107: this .selection = currentSelection;
108:
109: if (projectsOnly) {
110: setWindowTitle(WorkbenchMessages.NewProject_title);
111: } else {
112: setWindowTitle(WorkbenchMessages.NewWizard_title);
113: }
114: setDefaultPageImageDescriptor(WorkbenchImages
115: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_WIZBAN_NEW_WIZ));
116: setNeedsProgressMonitor(true);
117: }
118:
119: /**
120: * The user has pressed Finish. Instruct self's pages to finish, and answer
121: * a boolean indicating success.
122: *
123: * @return boolean
124: */
125: public boolean performFinish() {
126: //save our selection state
127: mainPage.saveWidgetValues();
128: // if we're finishing from the main page then perform finish on the selected wizard.
129: if (getContainer().getCurrentPage() == mainPage) {
130: if (mainPage.canFinishEarly()) {
131: IWizard wizard = mainPage.getSelectedNode().getWizard();
132: wizard.setContainer(getContainer());
133: return wizard.performFinish();
134: }
135: }
136: return true;
137: }
138:
139: /**
140: * Sets the id of the category of wizards to show or <code>null</code> to
141: * show all categories. If no entries can be found with this id then all
142: * categories are shown.
143: *
144: * @param id may be <code>null</code>.
145: */
146: public void setCategoryId(String id) {
147: categoryId = id;
148: }
149:
150: /**
151: * Sets the projects only flag. If <code>true</code> only projects will
152: * be shown in this wizard.
153: * @param b if only projects should be shown
154: */
155: public void setProjectsOnly(boolean b) {
156: projectsOnly = b;
157: }
158:
159: /* (non-Javadoc)
160: * @see org.eclipse.jface.wizard.IWizard#canFinish()
161: */
162: public boolean canFinish() {
163: // we can finish if the first page is current and the the page can finish early.
164: if (getContainer().getCurrentPage() == mainPage) {
165: if (mainPage.canFinishEarly()) {
166: return true;
167: }
168: }
169: return super.canFinish();
170: }
171: }
|