KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > plugin > TemplateListSelectionPage


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.pde.internal.ui.wizards.plugin;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IConfigurationElement;
15 import org.eclipse.core.runtime.IExecutableExtension;
16 import org.eclipse.jface.viewers.ISelectionChangedListener;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.jface.viewers.StructuredSelection;
19 import org.eclipse.jface.viewers.Viewer;
20 import org.eclipse.jface.viewers.ViewerFilter;
21 import org.eclipse.jface.wizard.IWizardNode;
22 import org.eclipse.pde.internal.ui.IHelpContextIds;
23 import org.eclipse.pde.internal.ui.PDEUIMessages;
24 import org.eclipse.pde.internal.ui.elements.ElementList;
25 import org.eclipse.pde.internal.ui.wizards.WizardElement;
26 import org.eclipse.pde.internal.ui.wizards.WizardListSelectionPage;
27 import org.eclipse.pde.internal.ui.wizards.WizardNode;
28 import org.eclipse.pde.ui.IBasePluginWizard;
29 import org.eclipse.pde.ui.IPluginContentWizard;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.ui.PlatformUI;
37
38
39 public class TemplateListSelectionPage extends WizardListSelectionPage
40         implements ISelectionChangedListener, IExecutableExtension {
41     private ContentPage fContentPage;
42     private Button fUseTemplate;
43     private String JavaDoc fInitialTemplateId;
44     
45     class WizardFilter extends ViewerFilter {
46         public boolean select(Viewer viewer, Object JavaDoc parentElement,
47                 Object JavaDoc element) {
48             PluginFieldData data = (PluginFieldData) fContentPage.getData();
49             boolean simple = data.isSimple();
50             boolean generate = data.doGenerateClass();
51             boolean ui = data.isUIPlugin();
52             boolean rcp = data.isRCPApplicationPlugin();
53             boolean osgi = data.getOSGiFramework() != null;
54             WizardElement welement = (WizardElement)element;
55             IConfigurationElement config = welement.getConfigurationElement();
56             boolean uiFlag = getFlag(config, "ui-content", true); //$NON-NLS-1$
57
boolean javaFlag = getFlag(config, "java", true); //$NON-NLS-1$
58
boolean rcpFlag = getFlag(config, "rcp", false); //$NON-NLS-1$
59
boolean osgiFlag = getFlag(config, "pureOSGi", false); //$NON-NLS-1$
60
boolean activatorFlag = getFlag(config, "requiresActivator", false); //$NON-NLS-1$
61

62             //osgi projects need java
63
if (osgi && simple) return false;
64             //filter out java wizards for simple projects
65
if (simple) return !javaFlag;
66             //filter out ui wizards for non-ui plug-ins
67
if (uiFlag && generate && !ui) return false;
68             //filter out wizards that require an activator when the user specifies not to generate a class
69
if (activatorFlag && !generate) return false;
70             //filter out non-RCP wizard if RCP option is selected
71
if (!osgi && (rcp != rcpFlag)) return false;
72             //filter out non-UI wizards if UI option is selected for rcp and osgi projects
73
return (osgi == osgiFlag && ((!osgiFlag && !rcpFlag) || ui == uiFlag));
74         }
75         private boolean getFlag(IConfigurationElement config, String JavaDoc name, boolean defaultValue) {
76             String JavaDoc value = config.getAttribute(name);
77             if (value==null) return defaultValue;
78             return value.equalsIgnoreCase("true"); //$NON-NLS-1$
79
}
80 }
81     
82     public TemplateListSelectionPage(ElementList wizardElements, ContentPage page, String JavaDoc message) {
83         super(wizardElements, message);
84         fContentPage = page;
85         setTitle(PDEUIMessages.WizardListSelectionPage_title);
86         setDescription(PDEUIMessages.WizardListSelectionPage_desc);
87     }
88     
89     /* (non-Javadoc)
90      * @see org.eclipse.pde.internal.ui.wizards.WizardListSelectionPage#createControl(org.eclipse.swt.widgets.Composite)
91      */

92     public void createControl(Composite parent) {
93         super.createControl(parent);
94         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IHelpContextIds.NEW_PROJECT_CODE_GEN_PAGE);
95     }
96     
97     public void createAbove(Composite container, int span) {
98         fUseTemplate = new Button(container, SWT.CHECK);
99         fUseTemplate.setText(PDEUIMessages.WizardListSelectionPage_label);
100         GridData gd = new GridData();
101         gd.horizontalSpan = span;
102         fUseTemplate.setLayoutData(gd);
103         fUseTemplate.addSelectionListener(new SelectionAdapter() {
104             public void widgetSelected(SelectionEvent e) {
105                 wizardSelectionViewer.getControl().setEnabled(fUseTemplate.getSelection());
106                 if (!fUseTemplate.getSelection())
107                     setDescription(""); //$NON-NLS-1$
108
setDescriptionEnabled(fUseTemplate.getSelection());
109                 getContainer().updateButtons();
110             }
111         });
112         fUseTemplate.setSelection(true);
113     }
114     
115     protected void initializeViewer() {
116         wizardSelectionViewer.addFilter(new WizardFilter());
117         if (getInitialTemplateId() != null)
118             selectInitialTemplate();
119     }
120     
121     private void selectInitialTemplate() {
122         Object JavaDoc [] children = wizardElements.getChildren();
123         for (int i=0; i<children.length; i++) {
124             WizardElement welement = (WizardElement)children[i];
125             if (welement.getID().equals(getInitialTemplateId())) {
126                 wizardSelectionViewer.setSelection(new StructuredSelection(welement), true);
127                 setSelectedNode(createWizardNode(welement));
128                 setDescriptionText(welement.getDescription());
129                 break;
130             }
131         }
132     }
133     
134     protected IWizardNode createWizardNode(WizardElement element) {
135         return new WizardNode(this, element) {
136             public IBasePluginWizard createWizard() throws CoreException {
137                 IPluginContentWizard wizard =
138                     (IPluginContentWizard) wizardElement.createExecutableExtension();
139                 wizard.init(fContentPage.getData());
140                 return wizard;
141             }
142         };
143     }
144     
145     public IPluginContentWizard getSelectedWizard() {
146         if (fUseTemplate.getSelection())
147             return super.getSelectedWizard();
148         return null;
149     }
150     
151     public boolean isPageComplete() {
152         PluginFieldData data = (PluginFieldData)fContentPage.getData();
153         boolean rcp = data.isRCPApplicationPlugin();
154         
155         return !rcp || (fUseTemplate.getSelection() && rcp && getSelectedNode() != null);
156     }
157     
158     
159     /* (non-Javadoc)
160      * @see org.eclipse.jface.wizard.WizardSelectionPage#canFlipToNextPage()
161      */

162     public boolean canFlipToNextPage() {
163         IStructuredSelection ssel = (IStructuredSelection)wizardSelectionViewer.getSelection();
164         return fUseTemplate.getSelection() && ssel != null && !ssel.isEmpty();
165     }
166     /**
167      * @return Returns the fInitialTemplateId.
168      */

169     public String JavaDoc getInitialTemplateId() {
170         return fInitialTemplateId;
171     }
172     /**
173      * @param initialTemplateId The fInitialTemplateId to set.
174      */

175     public void setInitialTemplateId(String JavaDoc initialTemplateId) {
176         fInitialTemplateId = initialTemplateId;
177     }
178     public void setVisible(boolean visible) {
179         if (visible) {
180             fContentPage.updateData();
181             wizardSelectionViewer.refresh();
182         }
183         super.setVisible(visible);
184     }
185 }
186
Popular Tags