KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > feature > IncludeFeaturesWizardPage


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