001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.pde.internal.ui.wizards.templates;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IConfigurationElement;
016: import org.eclipse.core.runtime.IExtensionRegistry;
017: import org.eclipse.core.runtime.Platform;
018: import org.eclipse.jface.wizard.IWizardPage;
019: import org.eclipse.jface.wizard.WizardPage;
020: import org.eclipse.pde.internal.ui.PDEPlugin;
021: import org.eclipse.pde.ui.templates.AbstractNewPluginTemplateWizard;
022: import org.eclipse.pde.ui.templates.ITemplateSection;
023: import org.eclipse.swt.widgets.Composite;
024:
025: public class NewPluginTemplateChoiceWizard extends
026: AbstractNewPluginTemplateWizard {
027: private TemplateSelectionPage fSelectionPage;
028: private ITemplateSection[] fCandiates;
029:
030: public NewPluginTemplateChoiceWizard() {
031: }
032:
033: public ITemplateSection[] getTemplateSections() {
034: if (fSelectionPage != null) {
035: return fSelectionPage.getSelectedTemplates();
036: }
037: return getCandidates();
038: }
039:
040: public void addAdditionalPages() {
041: fSelectionPage = new TemplateSelectionPage(getCandidates());
042: addPage(fSelectionPage);
043: }
044:
045: public IWizardPage getNextPage(IWizardPage page) {
046: if (fSelectionPage == null)
047: return null;
048: return fSelectionPage.getNextVisiblePage(page);
049: }
050:
051: public IWizardPage getPreviousPage(IWizardPage page) {
052: return null;
053: }
054:
055: private ITemplateSection[] getCandidates() {
056: if (fCandiates == null) {
057: createCandidates();
058: }
059: return fCandiates;
060:
061: }
062:
063: // calculate canFinish only on selected templateSections and the status of their pages
064: public boolean canFinish() {
065: ITemplateSection[] sections = fSelectionPage
066: .getSelectedTemplates();
067: for (int i = 0; i < sections.length; i++) {
068: int pageCount = sections[i].getPageCount();
069: for (int j = 0; j < pageCount; j++) {
070: WizardPage page = sections[i].getPage(j);
071: if (page != null && !page.isPageComplete())
072: return false;
073: }
074: }
075: return true;
076: }
077:
078: private void createCandidates() {
079: ArrayList candidates;
080: candidates = new ArrayList();
081: IExtensionRegistry registry = Platform.getExtensionRegistry();
082: IConfigurationElement[] elements = registry
083: .getConfigurationElementsFor(PDEPlugin.getPluginId(),
084: "templates"); //$NON-NLS-1$
085: for (int i = 0; i < elements.length; i++) {
086: IConfigurationElement element = elements[i];
087: addTemplate(element, candidates);
088: }
089: fCandiates = (ITemplateSection[]) candidates
090: .toArray(new ITemplateSection[candidates.size()]);
091: }
092:
093: private void addTemplate(IConfigurationElement config,
094: ArrayList result) {
095: if (config.getName().equalsIgnoreCase("template") == false) //$NON-NLS-1$
096: return;
097:
098: try {
099: Object template = config.createExecutableExtension("class"); //$NON-NLS-1$
100: if (template instanceof ITemplateSection) {
101: result.add(template);
102: }
103: } catch (CoreException e) {
104: PDEPlugin.log(e);
105: }
106: }
107:
108: // by default, all pages in wizard get created. We add all the pages from the template sections and we don't want to initialize them yet
109: // Therefore, the createPageControls only initializes the first page, allowing the other to be created as needed.
110: public void createPageControls(Composite pageContainer) {
111: fSelectionPage.createControl(pageContainer);
112: }
113: }
|