KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > wizards > OptionalFeaturesPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.update.internal.ui.wizards;
12 import java.util.ArrayList JavaDoc;
13 import java.util.HashSet JavaDoc;
14
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.viewers.AbstractTreeViewer;
17 import org.eclipse.jface.viewers.CheckStateChangedEvent;
18 import org.eclipse.jface.viewers.CheckboxTreeViewer;
19 import org.eclipse.jface.viewers.ICheckStateListener;
20 import org.eclipse.jface.viewers.ITreeContentProvider;
21 import org.eclipse.jface.viewers.LabelProvider;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.ui.PlatformUI;
33 import org.eclipse.update.configuration.IConfiguredSite;
34 import org.eclipse.update.configuration.IInstallConfiguration;
35 import org.eclipse.update.core.IFeature;
36 import org.eclipse.update.core.IFeatureReference;
37 import org.eclipse.update.internal.operations.FeatureHierarchyElement;
38 import org.eclipse.update.internal.operations.JobRoot;
39 import org.eclipse.update.internal.operations.UpdateUtils;
40 import org.eclipse.update.internal.ui.UpdateUI;
41 import org.eclipse.update.internal.ui.UpdateUIImages;
42 import org.eclipse.update.internal.ui.UpdateUIMessages;
43 import org.eclipse.update.internal.ui.parts.DefaultContentProvider;
44 import org.eclipse.update.internal.ui.parts.SWTUtil;
45 import org.eclipse.update.operations.IInstallFeatureOperation;
46
47 public class OptionalFeaturesPage extends BannerPage implements IDynamicPage {
48     private CheckboxTreeViewer treeViewer;
49     private Button selectAllButton;
50     private Button deselectAllButton;
51     private IInstallConfiguration config;
52     private JobRoot[] jobRoots;
53
54     class TreeContentProvider
55         extends DefaultContentProvider
56         implements ITreeContentProvider {
57
58         public Object JavaDoc[] getChildren(Object JavaDoc parent) {
59             if (parent instanceof JobRoot) {
60                 return ((JobRoot) parent).getElements();
61             }
62             if (parent instanceof FeatureHierarchyElement) {
63                 FeatureHierarchyElement fe = (FeatureHierarchyElement) parent;
64                 Object JavaDoc root = fe.getRoot();
65                 boolean oldFeature = false;
66                 if (root instanceof JobRoot) {
67                     IInstallFeatureOperation job = ((JobRoot)root).getJob();
68                     boolean patch = UpdateUtils.isPatch(job.getFeature());
69                     oldFeature = job.getOldFeature() != null;
70                     return fe.getChildren(oldFeature, patch, config);
71                 }
72             }
73             return new Object JavaDoc[0];
74         }
75
76         public Object JavaDoc getParent(Object JavaDoc child) {
77             return null;
78         }
79
80         public boolean hasChildren(Object JavaDoc parent) {
81             return getChildren(parent).length > 0;
82         }
83
84         public Object JavaDoc[] getElements(Object JavaDoc input) {
85             if (jobRoots == null)
86                 return new Object JavaDoc[0];
87             return jobRoots;
88         }
89     }
90
91     class TreeLabelProvider extends LabelProvider {
92         public String JavaDoc getText(Object JavaDoc obj) {
93             if (obj instanceof JobRoot) {
94                 IFeature feature = ((JobRoot) obj).getJob().getFeature();
95                 return feature.getLabel()
96                     + " " //$NON-NLS-1$
97
+ feature.getVersionedIdentifier().getVersion().toString();
98             }
99             if (obj instanceof FeatureHierarchyElement) {
100                 String JavaDoc name = ((FeatureHierarchyElement) obj).getLabel();
101                 if (name != null)
102                     return name;
103             }
104
105             return super.getText(obj);
106         }
107         public Image getImage(Object JavaDoc obj) {
108             return UpdateUI.getDefault().getLabelProvider().get(
109                 UpdateUIImages.DESC_FEATURE_OBJ);
110         }
111     }
112
113     /**
114      * Constructor for ReviewPage2
115      */

116     public OptionalFeaturesPage(IInstallConfiguration config) {
117         super("OptionalFeatures"); //$NON-NLS-1$
118
setTitle(UpdateUIMessages.InstallWizard_OptionalFeaturesPage_title);
119         setDescription(UpdateUIMessages.InstallWizard_OptionalFeaturesPage_desc);
120         this.config = config;
121         UpdateUI.getDefault().getLabelProvider().connect(this);
122     }
123
124     public void setJobs(IInstallFeatureOperation[] jobs) {
125         jobRoots = new JobRoot[jobs.length];
126         for (int i = 0; i < jobs.length; i++) {
127             jobRoots[i] = new JobRoot(jobs[i]);
128         }
129     }
130
131     public void dispose() {
132         UpdateUI.getDefault().getLabelProvider().disconnect(this);
133         super.dispose();
134     }
135
136
137     public Control createContents(Composite parent) {
138         Composite client = new Composite(parent, SWT.NULL);
139         GridLayout layout = new GridLayout();
140         layout.numColumns = 2;
141         layout.marginWidth = layout.marginHeight = 0;
142         client.setLayout(layout);
143
144         createCheckboxTreeViewer(client);
145
146         selectAllButton = new Button(client, SWT.PUSH);
147         selectAllButton.addSelectionListener(new SelectionAdapter() {
148             public void widgetSelected(SelectionEvent e) {
149                 selectAll(true);
150             }
151         });
152         selectAllButton.setText(UpdateUIMessages.InstallWizard_OptionalFeaturesPage_selectAll);
153         GridData gd =
154             new GridData(
155                 GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
156         selectAllButton.setLayoutData(gd);
157         SWTUtil.setButtonDimensionHint(selectAllButton);
158
159         deselectAllButton = new Button(client, SWT.PUSH);
160         deselectAllButton.addSelectionListener(new SelectionAdapter() {
161             public void widgetSelected(SelectionEvent e) {
162                 selectAll(false);
163             }
164         });
165         deselectAllButton.setText(UpdateUIMessages.InstallWizard_OptionalFeaturesPage_deselectAll);
166         gd =
167             new GridData(
168                 GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
169         deselectAllButton.setLayoutData(gd);
170         SWTUtil.setButtonDimensionHint(deselectAllButton);
171         PlatformUI.getWorkbench().getHelpSystem().setHelp(client, "org.eclipse.update.ui.MultiOptionalFeaturesPage2"); //$NON-NLS-1$
172

173         Dialog.applyDialogFont(parent);
174         
175         return client;
176     }
177
178     private void createCheckboxTreeViewer(Composite parent) {
179         Label label = new Label(parent, SWT.NULL);
180         label.setText(UpdateUIMessages.InstallWizard_OptionalFeaturesPage_treeLabel);
181         GridData gd = new GridData();
182         gd.horizontalSpan = 2;
183         label.setLayoutData(gd);
184         treeViewer =
185             new CheckboxTreeViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
186         gd = new GridData(GridData.FILL_BOTH);
187         gd.verticalSpan = 2;
188         treeViewer.getTree().setLayoutData(gd);
189         treeViewer.setContentProvider(new TreeContentProvider());
190         treeViewer.setLabelProvider(new TreeLabelProvider());
191         treeViewer.addCheckStateListener(new ICheckStateListener() {
192             public void checkStateChanged(CheckStateChangedEvent e) {
193                 handleChecked(e.getElement(), e.getChecked());
194             }
195         });
196         treeViewer.setAutoExpandLevel(AbstractTreeViewer.ALL_LEVELS);
197         treeViewer.setInput(this);
198     }
199
200     public void setVisible(boolean visible) {
201         if (visible) {
202             treeViewer.setInput(jobRoots);
203             initializeStates();
204         }
205         super.setVisible(visible);
206         if (visible) {
207             treeViewer.getTree().setFocus();
208         }
209     }
210
211     private void initializeStates() {
212         ArrayList JavaDoc checked = new ArrayList JavaDoc();
213         ArrayList JavaDoc grayed = new ArrayList JavaDoc();
214         ArrayList JavaDoc editable = new ArrayList JavaDoc();
215
216         for (int i = 0; i < jobRoots.length; i++) {
217             checked.add(jobRoots[i]);
218             grayed.add(jobRoots[i]);
219             boolean update = jobRoots[i].getJob().getOldFeature() != null;
220             initializeStates(update, jobRoots[i].getElements(), checked, grayed, editable);
221         }
222         treeViewer.setCheckedElements(checked.toArray());
223         treeViewer.setGrayedElements(grayed.toArray());
224         selectAllButton.setEnabled(editable.size()>0);
225         deselectAllButton.setEnabled(editable.size()>0);
226     }
227
228     private void initializeStates(
229         boolean update,
230         Object JavaDoc[] elements,
231         ArrayList JavaDoc checked,
232         ArrayList JavaDoc grayed,
233         ArrayList JavaDoc editable) {
234
235         for (int i = 0; i < elements.length; i++) {
236             FeatureHierarchyElement element =
237                 (FeatureHierarchyElement) elements[i];
238             if (element.isChecked())
239                 checked.add(element);
240             if (!element.isEditable())
241                 grayed.add(element);
242             else
243                 editable.add(element);
244             initializeStates(update, element.getChildren(), checked, grayed, editable);
245         }
246     }
247
248     private void selectAll(boolean value) {
249         ArrayList JavaDoc selected = new ArrayList JavaDoc();
250         for (int i = 0; i < jobRoots.length; i++) {
251             IInstallFeatureOperation job = jobRoots[i].getJob();
252             selected.add(job);
253             Object JavaDoc[] elements = jobRoots[i].getElements();
254             for (int j = 0; j < elements.length; j++) {
255                 FeatureHierarchyElement element = (FeatureHierarchyElement) elements[j];
256                 selectAll(job.getOldFeature() != null, element, selected, value);
257             }
258         }
259         treeViewer.setCheckedElements(selected.toArray());
260     }
261
262     private void selectAll(
263         boolean update,
264         FeatureHierarchyElement ref,
265         ArrayList JavaDoc selected,
266         boolean value) {
267
268         if (!ref.isOptional()) {
269             selected.add(ref);
270         } else {
271             if (ref.isEditable()) {
272                 ref.setChecked(value);
273                 if (value)
274                     selected.add(ref);
275             } else if (ref.isChecked()) {
276                 selected.add(ref);
277             }
278         }
279         Object JavaDoc[] included = ref.getChildren();
280         for (int i = 0; i < included.length; i++) {
281             selectAll(update, (FeatureHierarchyElement) included[i], selected, value);
282         }
283     }
284
285     private void handleChecked(Object JavaDoc element, boolean checked) {
286         if (element instanceof JobRoot) {
287             treeViewer.setChecked(element, !checked);
288             return;
289         }
290         FeatureHierarchyElement fe = (FeatureHierarchyElement) element;
291
292         if (!fe.isEditable())
293             treeViewer.setChecked(element, !checked);
294         else {
295             // update the result
296
fe.setChecked(checked);
297         }
298     }
299     
300     public IFeature[] getUnconfiguredOptionalFeatures(IInstallFeatureOperation job, IConfiguredSite targetSite) {
301         for (int i = 0; i < jobRoots.length; i++) {
302             if (job.equals(jobRoots[i].getJob())) {
303                 return jobRoots[i].getUnconfiguredOptionalFeatures(config, targetSite);
304             }
305         }
306         return new IFeature[0];
307     }
308     
309     public IFeatureReference[] getCheckedOptionalFeatures(IInstallFeatureOperation currentJob) {
310         HashSet JavaDoc set = new HashSet JavaDoc();
311         JobRoot jobRoot = null;
312
313         for (int i = 0; i < jobRoots.length; i++) {
314             if (currentJob.equals(jobRoots[i].getJob())) {
315                 jobRoot = jobRoots[i];
316                 break;
317             }
318         }
319         if (jobRoot == null)
320             return new IFeatureReference[0];
321
322         IInstallFeatureOperation job = jobRoot.getJob();
323         boolean update = job.getOldFeature() != null;
324         boolean patch = UpdateUtils.isPatch(job.getFeature());
325         FeatureHierarchyElement[] elements = jobRoot.getElements();
326         for (int i = 0; i < elements.length; i++) {
327             elements[i].addCheckedOptionalFeatures(update, patch, config, set);
328         }
329         return (IFeatureReference[]) set.toArray(new IFeatureReference[set.size()]);
330     }
331 }
332
Popular Tags