KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > templates > NewPluginTemplateChoiceWizard


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.pde.internal.ui.wizards.templates;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.core.runtime.IExtensionRegistry;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.jface.wizard.IWizardPage;
20 import org.eclipse.jface.wizard.WizardPage;
21 import org.eclipse.pde.internal.ui.PDEPlugin;
22 import org.eclipse.pde.ui.templates.AbstractNewPluginTemplateWizard;
23 import org.eclipse.pde.ui.templates.ITemplateSection;
24 import org.eclipse.swt.widgets.Composite;
25
26 public class NewPluginTemplateChoiceWizard
27     extends AbstractNewPluginTemplateWizard {
28     private TemplateSelectionPage fSelectionPage;
29     private ITemplateSection[] fCandiates;
30
31     public NewPluginTemplateChoiceWizard() {
32     }
33
34     public ITemplateSection[] getTemplateSections() {
35         if (fSelectionPage != null) {
36             return fSelectionPage.getSelectedTemplates();
37         }
38         return getCandidates();
39     }
40
41     public void addAdditionalPages() {
42         fSelectionPage = new TemplateSelectionPage(getCandidates());
43         addPage(fSelectionPage);
44     }
45     
46     public IWizardPage getNextPage(IWizardPage page) {
47         if (fSelectionPage == null)
48             return null;
49         return fSelectionPage.getNextVisiblePage(page);
50     }
51     public IWizardPage getPreviousPage(IWizardPage page) {
52         return null;
53     }
54     private ITemplateSection[] getCandidates() {
55         if (fCandiates == null) {
56             createCandidates();
57         }
58         return fCandiates;
59
60     }
61     
62     // calculate canFinish only on selected templateSections and the status of their pages
63
public boolean canFinish() {
64         ITemplateSection[] sections = fSelectionPage.getSelectedTemplates();
65         for (int i = 0; i < sections.length; i++) {
66             int pageCount = sections[i].getPageCount();
67             for (int j =0; j < pageCount; j++) {
68                 WizardPage page = sections[i].getPage(j);
69                 if (page != null && !page.isPageComplete())
70                     return false;
71             }
72         }
73         return true;
74     }
75
76     private void createCandidates() {
77         ArrayList JavaDoc candidates;
78         candidates = new ArrayList JavaDoc();
79         IExtensionRegistry registry = Platform.getExtensionRegistry();
80         IConfigurationElement[] elements = registry
81                 .getConfigurationElementsFor(PDEPlugin.getPluginId(),
82                         "templates"); //$NON-NLS-1$
83
for (int i = 0; i < elements.length; i++) {
84             IConfigurationElement element = elements[i];
85             addTemplate(element, candidates);
86         }
87         fCandiates = (ITemplateSection[]) candidates
88                 .toArray(new ITemplateSection[candidates.size()]);
89     }
90
91     private void addTemplate(IConfigurationElement config, ArrayList JavaDoc result) {
92         if (config.getName().equalsIgnoreCase("template") == false) //$NON-NLS-1$
93
return;
94
95         try {
96             Object JavaDoc template = config.createExecutableExtension("class"); //$NON-NLS-1$
97
if (template instanceof ITemplateSection) {
98                 result.add(template);
99             }
100         } catch (CoreException e) {
101             PDEPlugin.log(e);
102         }
103     }
104
105     // 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
106
// Therefore, the createPageControls only initializes the first page, allowing the other to be created as needed.
107
public void createPageControls(Composite pageContainer) {
108         fSelectionPage.createControl(pageContainer);
109     }
110 }
111
Popular Tags