KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > site > BuiltFeaturesWizardPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.site;
12
13 import java.lang.reflect.*;
14
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.jface.dialogs.Dialog;
17 import org.eclipse.jface.operation.*;
18 import org.eclipse.jface.viewers.*;
19 import org.eclipse.jface.wizard.*;
20 import org.eclipse.pde.internal.core.*;
21 import org.eclipse.pde.internal.core.ifeature.*;
22 import org.eclipse.pde.internal.core.isite.*;
23 import org.eclipse.pde.internal.core.site.*;
24 import org.eclipse.pde.internal.ui.*;
25 import org.eclipse.pde.internal.ui.elements.*;
26 import org.eclipse.pde.internal.ui.parts.*;
27 import org.eclipse.pde.internal.ui.wizards.*;
28 import org.eclipse.swt.*;
29 import org.eclipse.swt.layout.*;
30 import org.eclipse.swt.widgets.*;
31 import org.eclipse.ui.*;
32 import org.eclipse.ui.forms.widgets.*;
33 import org.eclipse.ui.help.*;
34
35 public class BuiltFeaturesWizardPage extends WizardPage {
36     private ISiteBuildModel model;
37     private TablePart checkboxTablePart;
38     private CheckboxTableViewer featureViewer;
39
40     class PluginContentProvider
41         extends DefaultContentProvider
42         implements IStructuredContentProvider {
43         public Object JavaDoc[] getElements(Object JavaDoc parent) {
44             return getChoices();
45         }
46     }
47     
48     class TablePart extends WizardCheckboxTablePart {
49         public TablePart() {
50             super(PDEPlugin.getResourceString("BuildFeatureWizardPage.label")); //$NON-NLS-1$
51
}
52         public void updateCounter(int count) {
53             super.updateCounter(count);
54             setPageComplete(count>0);
55         }
56         protected StructuredViewer createStructuredViewer(
57             Composite parent,
58             int style,
59             FormToolkit toolkit) {
60             StructuredViewer viewer =
61                 super.createStructuredViewer(parent, style, toolkit);
62             viewer.setSorter(ListUtil.FEATURE_SORTER);
63             return viewer;
64         }
65     }
66
67     public BuiltFeaturesWizardPage(ISiteBuildModel model) {
68         super("BuiltFeaturesWizardPage"); //$NON-NLS-1$
69
this.model = model;
70         setTitle(PDEPlugin.getResourceString("BuildFeatureWizardPage.title")); //$NON-NLS-1$
71
setDescription(PDEPlugin.getResourceString("BuildFeatureWizardPage.desc")); //$NON-NLS-1$
72
setPageComplete(false);
73         
74         checkboxTablePart = new TablePart();
75         PDEPlugin.getDefault().getLabelProvider().connect(this);
76     }
77     
78     public void dispose() {
79         PDEPlugin.getDefault().getLabelProvider().disconnect(this);
80         super.dispose();
81     }
82
83     public void createControl(Composite parent) {
84         Composite container = new Composite(parent, SWT.NULL);
85         GridLayout layout = new GridLayout();
86         layout.numColumns = 2;
87         container.setLayout(layout);
88         
89         createPluginList(container);
90         initialize();
91         setControl(container);
92         Dialog.applyDialogFont(container);
93         WorkbenchHelp.setHelp(container, IHelpContextIds.FEATURE_INCLUDED_FEATURES_WIZARD);
94     }
95
96     protected void createPluginList(Composite parent) {
97         checkboxTablePart.createControl(parent);
98         featureViewer = checkboxTablePart.getTableViewer();
99         featureViewer.setContentProvider(new PluginContentProvider());
100         featureViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
101         featureViewer.addFilter(new ViewerFilter() {
102             public boolean select(Viewer v, Object JavaDoc parent, Object JavaDoc object) {
103                 if (object instanceof IFeatureModel) {
104                     IFeatureModel model = (IFeatureModel) object;
105                     return !isOnTheList(model);
106                 }
107                 return true;
108             }
109         });
110         GridData gd = (GridData)checkboxTablePart.getControl().getLayoutData();
111         gd.heightHint = 300;
112     }
113
114     private boolean isOnTheList(IFeatureModel candidate) {
115         ISiteBuildFeature[] features = model.getSiteBuild().getFeatures();
116         IFeature cfeature = candidate.getFeature();
117         
118         for (int i = 0; i < features.length; i++) {
119             ISiteBuildFeature bfeature = features[i];
120             if (bfeature.getId().equals(cfeature.getId()) &&
121                 bfeature.getVersion().equals(cfeature.getVersion())) return true;
122         }
123         return false;
124     }
125     
126     public void init(IWorkbench workbench) {
127     }
128
129     private void initialize() {
130         featureViewer.setInput(model.getSiteBuild());
131         checkboxTablePart.setSelection(new Object JavaDoc[0]);
132     }
133
134     private Object JavaDoc[] getChoices() {
135         WorkspaceModelManager mng = PDECore.getDefault().getWorkspaceModelManager();
136         return mng.getFeatureModels();
137     }
138
139     public boolean finish() {
140         final Object JavaDoc [] candidates = checkboxTablePart.getSelection();
141         IRunnableWithProgress op = new IRunnableWithProgress() {
142             public void run(IProgressMonitor monitor) throws InvocationTargetException {
143                 try {
144                     doAdd(candidates, monitor);
145                 } catch (CoreException e) {
146                     throw new InvocationTargetException(e);
147                 }
148             }
149         };
150         try {
151             getContainer().run(false, false, op);
152         } catch (InterruptedException JavaDoc e) {
153             return false;
154         } catch (InvocationTargetException e) {
155             PDEPlugin.logException(e);
156             return false;
157         }
158         return true;
159     }
160
161     private void doAdd(Object JavaDoc [] candidates, IProgressMonitor monitor) throws CoreException {
162         monitor.beginTask(
163             PDEPlugin.getResourceString("BuildFeatureWizardPage.adding"), //$NON-NLS-1$
164
candidates.length + 1);
165         ISiteBuild siteBuild = model.getSiteBuild();
166         ISiteBuildFeature[] added = new ISiteBuildFeature[candidates.length];
167         for (int i = 0; i < candidates.length; i++) {
168             IFeatureModel candidate = (IFeatureModel) candidates[i];
169             String JavaDoc name = candidate.getFeature().getLabel();
170             monitor.subTask(candidate.getResourceString(name));
171             SiteBuildFeature child = (SiteBuildFeature) model.createFeature();
172             child.setReferencedFeature(candidate.getFeature());
173             added[i] = child;
174             monitor.worked(1);
175         }
176         monitor.subTask(""); //$NON-NLS-1$
177
monitor.setTaskName(PDEPlugin.getResourceString("BuildFeatureWizardPage.updating")); //$NON-NLS-1$
178
siteBuild.addFeatures(added);
179         monitor.worked(1);
180     }
181 }
182
Popular Tags