KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > preferences > TargetPluginsTab


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.preferences;
12
13 import java.io.File JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.Stack JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import org.eclipse.core.resources.IProject;
27 import org.eclipse.core.runtime.IAdaptable;
28 import org.eclipse.core.runtime.IPath;
29 import org.eclipse.core.runtime.IProgressMonitor;
30 import org.eclipse.core.runtime.Path;
31 import org.eclipse.core.runtime.Preferences;
32 import org.eclipse.core.runtime.SubProgressMonitor;
33 import org.eclipse.core.runtime.jobs.Job;
34 import org.eclipse.jdt.core.IJavaProject;
35 import org.eclipse.jface.operation.IRunnableWithProgress;
36 import org.eclipse.jface.viewers.CheckStateChangedEvent;
37 import org.eclipse.jface.viewers.CheckboxTableViewer;
38 import org.eclipse.jface.viewers.CheckboxTreeViewer;
39 import org.eclipse.jface.viewers.ICheckStateListener;
40 import org.eclipse.jface.viewers.IStructuredContentProvider;
41 import org.eclipse.jface.viewers.ITreeContentProvider;
42 import org.eclipse.jface.window.Window;
43 import org.eclipse.jface.wizard.WizardDialog;
44 import org.eclipse.osgi.service.resolver.BundleDescription;
45 import org.eclipse.osgi.service.resolver.State;
46 import org.eclipse.osgi.util.NLS;
47 import org.eclipse.pde.core.IModel;
48 import org.eclipse.pde.core.IModelProviderEvent;
49 import org.eclipse.pde.core.plugin.IPluginModelBase;
50 import org.eclipse.pde.core.plugin.PluginRegistry;
51 import org.eclipse.pde.internal.core.DependencyManager;
52 import org.eclipse.pde.internal.core.ExternalFeatureModelManager;
53 import org.eclipse.pde.internal.core.ExternalModelManager;
54 import org.eclipse.pde.internal.core.FeatureModelManager;
55 import org.eclipse.pde.internal.core.ICoreConstants;
56 import org.eclipse.pde.internal.core.ModelProviderEvent;
57 import org.eclipse.pde.internal.core.PDECore;
58 import org.eclipse.pde.internal.core.PDEState;
59 import org.eclipse.pde.internal.core.PluginPathFinder;
60 import org.eclipse.pde.internal.core.TargetPlatformHelper;
61 import org.eclipse.pde.internal.core.TargetPlatformResetJob;
62 import org.eclipse.pde.internal.core.ifeature.IFeature;
63 import org.eclipse.pde.internal.core.ifeature.IFeatureChild;
64 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
65 import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin;
66 import org.eclipse.pde.internal.core.itarget.ITarget;
67 import org.eclipse.pde.internal.core.itarget.ITargetFeature;
68 import org.eclipse.pde.internal.core.itarget.ITargetPlugin;
69 import org.eclipse.pde.internal.ui.IHelpContextIds;
70 import org.eclipse.pde.internal.ui.PDELabelProvider;
71 import org.eclipse.pde.internal.ui.PDEPlugin;
72 import org.eclipse.pde.internal.ui.PDEPluginImages;
73 import org.eclipse.pde.internal.ui.PDEUIMessages;
74 import org.eclipse.pde.internal.ui.editor.target.TargetErrorDialog;
75 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
76 import org.eclipse.pde.internal.ui.parts.SharedPartWithButtons;
77 import org.eclipse.pde.internal.ui.util.PersistablePluginObject;
78 import org.eclipse.pde.internal.ui.util.SWTUtil;
79 import org.eclipse.pde.internal.ui.wizards.ListUtil;
80 import org.eclipse.pde.internal.ui.wizards.provisioner.AddTargetPluginsWizard;
81 import org.eclipse.swt.SWT;
82 import org.eclipse.swt.events.SelectionAdapter;
83 import org.eclipse.swt.events.SelectionEvent;
84 import org.eclipse.swt.graphics.Image;
85 import org.eclipse.swt.layout.GridData;
86 import org.eclipse.swt.layout.GridLayout;
87 import org.eclipse.swt.widgets.Button;
88 import org.eclipse.swt.widgets.Composite;
89 import org.eclipse.swt.widgets.Control;
90 import org.eclipse.swt.widgets.Label;
91 import org.eclipse.swt.widgets.TableItem;
92 import org.eclipse.ui.IWorkingSet;
93 import org.eclipse.ui.IWorkingSetManager;
94 import org.eclipse.ui.PlatformUI;
95 import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog;
96 import org.eclipse.ui.forms.widgets.FormToolkit;
97 import org.eclipse.ui.part.PageBook;
98 import org.eclipse.ui.progress.IProgressConstants;
99
100 public class TargetPluginsTab extends SharedPartWithButtons{
101     private CheckboxTableViewer fPluginListViewer;
102     private TargetPlatformPreferencePage fPage;
103     private boolean fReloaded;
104     private CheckboxTreeViewer fPluginTreeViewer;
105     private HashSet JavaDoc fChangedModels = new HashSet JavaDoc();
106     private PDEState fCurrentState;
107     private PageBook fBook;
108     private Button fGroupPlugins;
109     private HashMap JavaDoc fTreeViewerContents;
110     private Label fCounterLabel;
111     private int fCounter;
112     private Map JavaDoc fCurrentFeatures;
113     private ArrayList JavaDoc fAdditionalLocations = new ArrayList JavaDoc();
114     
115     class ReloadOperation implements IRunnableWithProgress {
116         private String JavaDoc location;
117         
118         public ReloadOperation(String JavaDoc platformPath) {
119             this.location = platformPath;
120         }
121         
122         private URL JavaDoc[] computePluginURLs() {
123             URL JavaDoc[] base = PluginPathFinder.getPluginPaths(location);
124             if (fAdditionalLocations.size() == 0)
125                 return base;
126             
127             File JavaDoc[] extraLocations = new File JavaDoc[fAdditionalLocations.size() * 2];
128             for (int i = 0; i < extraLocations.length; i++) {
129                 String JavaDoc location = fAdditionalLocations.get(i/2).toString();
130                 File JavaDoc dir = new File JavaDoc(location);
131                 extraLocations[i] = dir;
132                 dir = new File JavaDoc(dir, "plugins"); //$NON-NLS-1$
133
extraLocations[++i] = dir;
134             }
135
136             URL JavaDoc[] additional = PluginPathFinder.scanLocations(extraLocations);
137             if (additional.length == 0)
138                 return base;
139             
140             URL JavaDoc[] result = new URL JavaDoc[base.length + additional.length];
141             System.arraycopy(base, 0, result, 0, base.length);
142             System.arraycopy(additional, 0, result, base.length, additional.length);
143             return result;
144         }
145             
146         public void run(IProgressMonitor monitor)
147             throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
148             monitor.beginTask(PDEUIMessages.TargetPluginsTab_readingPlatform, 10);
149             SubProgressMonitor parsePluginMonitor = new SubProgressMonitor(monitor, 9);
150             fCurrentState = new PDEState(computePluginURLs(), true, parsePluginMonitor);
151             loadFeatures(new SubProgressMonitor(monitor, 1));
152             monitor.done();
153             fTreeViewerContents.clear();
154             initializeTreeContents(getCurrentModels());
155         }
156         
157         private void loadFeatures(IProgressMonitor monitor) {
158             IFeatureModel[] externalModels = ExternalFeatureModelManager.createModels(location, fAdditionalLocations, monitor);
159             IFeatureModel[] workspaceModels = PDECore.getDefault().getFeatureModelManager().getWorkspaceModels();
160             int numFeatures = externalModels.length + workspaceModels.length;
161             fCurrentFeatures = new HashMap JavaDoc((4/3) * (numFeatures) + 1);
162             for (int i = 0; i < externalModels.length; i++) {
163                 String JavaDoc id = externalModels[i].getFeature().getId();
164                 if (id != null)
165                     fCurrentFeatures.put(id, externalModels[i]);
166             }
167             for (int i = 0; i < workspaceModels.length; i++) {
168                 String JavaDoc id = workspaceModels[i].getFeature().getId();
169                 if (id != null)
170                     fCurrentFeatures.put(id, workspaceModels[i]);
171             }
172             monitor.done();
173         }
174         
175     }
176     
177     public class PluginContentProvider
178         extends DefaultContentProvider
179         implements IStructuredContentProvider {
180         public Object JavaDoc[] getElements(Object JavaDoc parent) {
181             return getCurrentModels();
182         }
183     }
184     
185     public class TreePluginContentProvider extends DefaultContentProvider
186         implements ITreeContentProvider{
187         
188         public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
189             if (parentElement instanceof File JavaDoc) {
190                 Set JavaDoc files = (Set JavaDoc)fTreeViewerContents.get(parentElement);
191                 if (files != null) {
192                     Object JavaDoc[] result = files.toArray();
193                     return result;
194                 }
195             }
196             return new Object JavaDoc[0];
197         }
198
199         public Object JavaDoc getParent(Object JavaDoc element) {
200             if (element instanceof IPluginModelBase) {
201                 IPluginModelBase model = (IPluginModelBase) element;
202                 String JavaDoc installPath = model.getInstallLocation();
203                 if (installPath != null)
204                     return new File JavaDoc(installPath).getParentFile();
205             }
206             return null;
207         }
208
209         public boolean hasChildren(Object JavaDoc element) {
210             if (element instanceof File JavaDoc)
211                 return fTreeViewerContents.containsKey(element);
212             return false;
213         }
214
215         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
216             if (fTreeViewerContents == null)
217                 return initializeTreeContents(getCurrentModels()).toArray();
218             return fTreeViewerContents.keySet().toArray();
219         }
220
221     }
222     
223     public TargetPluginsTab(TargetPlatformPreferencePage page) {
224         super(new String JavaDoc[]{ PDEUIMessages.ExternalPluginsBlock_reload,
225               PDEUIMessages.TargetPluginsTab_add,
226               null,
227               null,
228               PDEUIMessages.WizardCheckboxTablePart_selectAll,
229               PDEUIMessages.WizardCheckboxTablePart_deselectAll,
230               PDEUIMessages.ExternalPluginsBlock_workingSet,
231               PDEUIMessages.ExternalPluginsBlock_addRequired});
232         this.fPage = page;
233         Preferences preferences = PDECore.getDefault().getPluginPreferences();
234         String JavaDoc additional = preferences.getString(ICoreConstants.ADDITIONAL_LOCATIONS);
235         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(additional, ","); //$NON-NLS-1$
236
while (tokenizer.hasMoreTokens()) {
237             fAdditionalLocations.add(tokenizer.nextToken().trim());
238         }
239         PDEPlugin.getDefault().getLabelProvider().connect(this);
240     }
241
242     void computeDelta() {
243         int type = 0;
244         IModel[] changedArray = null;
245         if (fChangedModels.size() > 0) {
246             type |= IModelProviderEvent.MODELS_CHANGED;
247             changedArray = (IModel[]) fChangedModels.toArray(new IModel[fChangedModels.size()]);
248         }
249         fChangedModels.clear();
250         if (type != 0) {
251             ExternalModelManager registry =
252                 PDECore.getDefault().getModelManager().getExternalModelManager();
253             ModelProviderEvent event =
254                 new ModelProviderEvent(
255                     registry,
256                     type,
257                     null,
258                     null,
259                     changedArray);
260             registry.fireModelProviderEvent(event);
261         }
262     }
263
264     public Control createContents(Composite parent) {
265         Composite container = new Composite(parent, SWT.NONE);
266         GridLayout layout = new GridLayout();
267         layout.numColumns = 2;
268         layout.marginHeight = 0;
269         layout.marginWidth = 0;
270         layout.verticalSpacing = 5;
271         container.setLayout(layout);
272         
273         super.createControl(container, SWT.NONE, 2, null);
274                 
275         fCounterLabel = new Label (container, SWT.NONE);
276         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
277         gd.horizontalSpan = 2;
278         fCounterLabel.setLayoutData(gd);
279         
280         fGroupPlugins = new Button (container, SWT.CHECK);
281         gd = new GridData();
282         gd.horizontalSpan = 2;
283         fGroupPlugins.setLayoutData(gd);
284         fGroupPlugins.setText(PDEUIMessages.TargetPluginsTab_groupPlugins);
285         fGroupPlugins.addSelectionListener(new SelectionAdapter() {
286             public void widgetSelected(SelectionEvent e) {
287                 handleSwitchView();
288             }
289         });
290         
291         initializeView();
292         PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.TARGET_PLUGINS_PREFERENCE_PAGE);
293         return container;
294     }
295     
296     protected void initializeView() {
297         Preferences preferences = PDECore.getDefault().getPluginPreferences();
298         boolean groupPlugins = preferences.getBoolean(ICoreConstants.GROUP_PLUGINS_VIEW);
299         fGroupPlugins.setSelection(groupPlugins);
300         if (groupPlugins) {
301             fBook.showPage(fPluginTreeViewer.getControl());
302         }
303         else {
304             fBook.showPage(fPluginListViewer.getControl());
305         }
306     }
307     
308     protected void createMainControl(Composite parent, int style, int span, FormToolkit toolkit) {
309         fBook = new PageBook(parent, SWT.NONE);
310         GridData gd = new GridData(GridData.FILL_BOTH);
311         gd.heightHint = 100;
312         gd.widthHint = 250;
313         fBook.setLayoutData(gd);
314         createTableViewer(fBook);
315         createTreeViewer(fBook);
316     }
317     
318     private void createTableViewer(Composite container) {
319         fPluginListViewer = CheckboxTableViewer.newCheckList(container, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
320         GridData gd = new GridData(GridData.FILL_BOTH);
321         gd.heightHint = 100;
322         gd.widthHint = 250;
323         fPluginListViewer.getControl().setLayoutData(gd);
324         fPluginListViewer.setContentProvider(new PluginContentProvider());
325         fPluginListViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
326         fPluginListViewer.setComparator(ListUtil.PLUGIN_COMPARATOR);
327         fPluginListViewer.addCheckStateListener(new ICheckStateListener() {
328
329             public void checkStateChanged(CheckStateChangedEvent event) {
330                 IPluginModelBase model = (IPluginModelBase) event.getElement();
331                 boolean checked = event.getChecked();
332                 if (fChangedModels.contains(model) && model.isEnabled() == checked) {
333                     fChangedModels.remove(model);
334                 } else if (model.isEnabled() != checked) {
335                     fChangedModels.add(model);
336                 }
337                 
338                 // handle checking the TreeViewer
339
fPluginTreeViewer.setChecked(model, checked);
340                 String JavaDoc path = model.getInstallLocation();
341                 if (path != null) {
342                     File JavaDoc parent = new File JavaDoc(path).getParentFile();
343                     if (checked) {
344                         fPluginTreeViewer.setChecked(parent, true);
345                         handleGrayChecked(parent, false);
346                     } else
347                         handleGrayChecked(parent, true);
348                 }
349                 
350                 // update the counter
351
if (checked)
352                     setCounter(fCounter + 1);
353                 else
354                     setCounter(fCounter - 1);
355             }
356             
357         });
358     }
359     
360     private void createTreeViewer(Composite container) {
361         fPluginTreeViewer = new CheckboxTreeViewer(container, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER );
362         GridData gd = new GridData(GridData.FILL_BOTH);
363         gd.heightHint = 100;
364         gd.widthHint = 250;
365         fPluginTreeViewer.getControl().setLayoutData(gd);
366         fPluginTreeViewer.setContentProvider(new TreePluginContentProvider());
367         fPluginTreeViewer.setComparator(ListUtil.PLUGIN_COMPARATOR);
368         fPluginTreeViewer.setLabelProvider(new PDELabelProvider() {
369             
370             public Image getImage(Object JavaDoc obj) {
371                 if (obj instanceof File JavaDoc) {
372                     return get(PDEPluginImages.DESC_SITE_OBJ);
373                 }
374                 return super.getImage(obj);
375             }
376         });
377         fPluginTreeViewer.addCheckStateListener(new ICheckStateListener() {
378
379             public void checkStateChanged(CheckStateChangedEvent event) {
380                 Object JavaDoc element = event.getElement();
381                 boolean checked = event.getChecked();
382                 
383                 // if user selected a plugin in the TreeViewer
384
if (element instanceof IPluginModelBase) {
385                     IPluginModelBase model = (IPluginModelBase) event.getElement();
386                     if (fChangedModels.contains(model) && model.isEnabled() == checked) {
387                         fChangedModels.remove(model);
388                     } else if (model.isEnabled() != checked) {
389                         fChangedModels.add(model);
390                     }
391                     // update Table Viewer
392
fPluginListViewer.setChecked(model, checked);
393                     
394                     // update parent in tree
395
String JavaDoc path = model.getInstallLocation();
396                     if (path != null) {
397                         File JavaDoc parent = new File JavaDoc(path).getParentFile();
398                         if (checked) {
399                             fPluginTreeViewer.setChecked(parent, true);
400                             handleGrayChecked(parent, false);
401                         }
402                         else
403                             handleGrayChecked(parent, true);
404                     }
405                     
406                     // update table
407
if (checked)
408                         setCounter(fCounter + 1);
409                     else
410                         setCounter(fCounter - 1);
411                     
412                 // else if the user selected and eclipse directory in the TreeViewer
413
} else if (element instanceof File JavaDoc) {
414                     int changedCount = 0;
415                     Set JavaDoc plugins = (Set JavaDoc) fTreeViewerContents.get(element);
416                     // iterator through plugins from selected eclipse directory
417
IPluginModelBase [] models = (IPluginModelBase[]) plugins.toArray( new IPluginModelBase[ plugins.size()]);
418                     for (int i = 0 ; i < models.length; i++) {
419                         if (fChangedModels.contains(models[i]) && models[i].isEnabled() == checked) {
420                             fChangedModels.remove(models[i]);
421                         } else if (models[i].isEnabled() != checked) {
422                             fChangedModels.add(models[i]);
423                         }
424                         if (checked && !fPluginTreeViewer.getChecked(models[i]))
425                             ++changedCount;
426                         else if (!checked && fPluginTreeViewer.getChecked(models[i]))
427                             --changedCount;
428                         fPluginListViewer.setChecked(models[i], checked);
429                     }
430                     // update the element in the TreeViewer
431
fPluginTreeViewer.setSubtreeChecked(element, checked);
432                     fPluginTreeViewer.setGrayed(element, false);
433                     fPluginTreeViewer.setChecked(element, checked);
434                     setCounter(fCounter + changedCount);
435                 }
436             }
437             
438         });
439         fPluginTreeViewer.setAutoExpandLevel(2);
440     }
441     
442     public void dispose() {
443         PDEPlugin.getDefault().getLabelProvider().disconnect(this);
444     }
445
446     protected void loadTargetProfile(ITarget target) {
447         if (target.useAllPlugins()) {
448             handleSelectAll(true);
449             return;
450         }
451         Map JavaDoc required = new HashMap JavaDoc(), missingFeatures = new HashMap JavaDoc();
452         Set JavaDoc optional = new HashSet JavaDoc();
453         ITargetFeature[] targetFeatures = target.getFeatures();
454         Stack JavaDoc features = new Stack JavaDoc();
455         
456         FeatureModelManager featureManager = null;
457         if (fCurrentFeatures == null)
458             featureManager = PDECore.getDefault().getFeatureModelManager();
459         for (int i = 0 ; i < targetFeatures.length; i++) {
460             IFeatureModel model = (featureManager != null) ? featureManager.findFeatureModel(targetFeatures[i].getId()) :
461                 (IFeatureModel)fCurrentFeatures.get(targetFeatures[i].getId());
462             if (model != null)
463                 features.push(model.getFeature());
464             else if (!targetFeatures[i].isOptional()) {
465                 missingFeatures.put(targetFeatures[i].getId(), targetFeatures[i]);
466                 break;
467             }
468             while (!features.isEmpty()) {
469                 IFeature feature = (IFeature) features.pop();
470                 IFeaturePlugin [] plugins = feature.getPlugins();
471                 for (int j = 0; j < plugins.length; j++) {
472                     if (target.isValidFeatureObject(plugins[j])) {
473                         if (targetFeatures[i].isOptional() || plugins[j].isFragment())
474                             optional.add(plugins[j].getId());
475                         else
476                             required.put(plugins[j].getId(), plugins[j]);
477                     }
478                 }
479                 IFeatureChild[] children = feature.getIncludedFeatures();
480                 for (int j = 0; j < children.length; j++) {
481                     if (!target.isValidFeatureObject(children[j]))
482                         continue;
483                     model = (featureManager != null) ? featureManager.findFeatureModel(children[j].getId()) :
484                         (IFeatureModel)fCurrentFeatures.get(children[j].getId());
485                     if (model != null)
486                         features.push(model.getFeature());
487                     else if (!targetFeatures[i].isOptional() && !missingFeatures.containsKey(children[j].getId())){
488                         // create dummy feature to display feature is missing
489
ITargetFeature missingFeature = target.getModel().getFactory().createFeature();
490                         missingFeature.setId(children[j].getId());
491                         missingFeatures.put(children[j].getId(), missingFeature);
492                     }
493                 }
494             }
495         }
496
497         ITargetPlugin[] plugins = target.getPlugins();
498         for (int i = 0 ; i < plugins.length; i++) {
499             if (plugins[i].isOptional())
500                 optional.add(plugins[i].getId());
501             else
502                 required.put(plugins[i].getId(), plugins[i]);
503         }
504         
505         IPluginModelBase workspacePlugins[] = PDECore.getDefault().getModelManager().getWorkspaceModels();
506         for (int i = 0; i < workspacePlugins.length; i++) {
507             if (workspacePlugins[i].isEnabled())
508                 required.remove(workspacePlugins[i].getBundleDescription().getSymbolicName());
509         }
510         
511         IPluginModelBase[] models = getCurrentModels();
512         int counter = 0;
513         for (int i = 0; i < models.length; i++) {
514             String JavaDoc id = models[i].getPluginBase().getId();
515             if (id == null)
516                 continue;
517             if (required.containsKey(id) || optional.contains(id)) {
518                 ++counter;
519                 if (!fPluginListViewer.getChecked(models[i])) {
520                     fPluginTreeViewer.setChecked(models[i], true);
521                     fPluginListViewer.setChecked(models[i], true);
522                     if (!models[i].isEnabled())
523                         fChangedModels.add(models[i]);
524                     // handle checking the parent
525
String JavaDoc path = models[i].getInstallLocation();
526                     if (path != null) {
527                         File JavaDoc parent = new File JavaDoc(path).getParentFile();
528                         fPluginTreeViewer.setChecked(parent, true);
529                         handleGrayChecked(parent, false);
530                     }
531                     
532                 }
533                 required.remove(id);
534             } else {
535                 if (fPluginListViewer.getChecked(models[i])) {
536                     fPluginTreeViewer.setChecked(models[i], false);
537                     fPluginListViewer.setChecked(models[i], false);
538                     if (models[i].isEnabled())
539                         fChangedModels.add(models[i]);
540                     // handle updating parent
541
String JavaDoc path = models[i].getInstallLocation();
542                     if (path != null) {
543                         File JavaDoc parent = new File JavaDoc(path).getParentFile();
544                         handleGrayChecked(parent, true);
545                     }
546                 }
547             }
548         }
549         setCounter(counter);
550         if (!required.isEmpty() || !missingFeatures.isEmpty())
551             TargetErrorDialog.showDialog(fPage.getShell(), missingFeatures.values().toArray(),
552                     required.values().toArray());
553     }
554     
555     protected void handleReload() {
556         String JavaDoc platformPath = fPage.getPlatformPath();
557         if (platformPath != null && platformPath.length() > 0) {
558             ReloadOperation op = new ReloadOperation(platformPath);
559             try {
560                 PlatformUI.getWorkbench().getProgressService().run(true, false, op);
561             } catch (InvocationTargetException JavaDoc e) {
562             } catch (InterruptedException JavaDoc e) {
563             }
564             fPluginListViewer.setInput(PDECore.getDefault().getModelManager().getExternalModelManager());
565             fPluginTreeViewer.setInput(PDECore.getDefault().getModelManager().getExternalModelManager());
566             fChangedModels.clear();
567             handleSelectAll(true);
568             if (fTreeViewerContents.size() > 1)
569                 fPluginTreeViewer.collapseAll();
570             fReloaded = true;
571             fPage.getSourceBlock().resetExtensionLocations(getCurrentModels());
572         }
573         fPage.resetNeedsReload();
574     }
575
576     protected void handleReload(ArrayList JavaDoc additionalLocations) {
577         fAdditionalLocations = additionalLocations;
578         handleReload();
579     }
580
581     public void initialize() {
582         String JavaDoc platformPath = fPage.getPlatformPath();
583         if (platformPath != null && platformPath.length() == 0)
584             return;
585         
586         fPluginTreeViewer.setUseHashlookup(true);
587         ExternalModelManager manager = PDECore.getDefault().getModelManager().getExternalModelManager();
588         fPluginListViewer.setInput(manager);
589         fPluginTreeViewer.setInput(manager);
590         IPluginModelBase[] allModels = getCurrentModels();
591
592         Vector JavaDoc selection = new Vector JavaDoc();
593         Set JavaDoc parentsToCheck = new HashSet JavaDoc();
594         for (int i = 0; i < allModels.length; i++) {
595             IPluginModelBase model = allModels[i];
596             if (model.isEnabled()) {
597                 selection.add(model);
598             }
599             // if model is to be selected, add parent to list of parents to be updated
600
String JavaDoc path = model.getInstallLocation();
601             if (path != null ) {
602                 File JavaDoc installFile = new File JavaDoc(path);
603                 File JavaDoc parentFile = installFile.getParentFile();
604                 if (model.isEnabled())
605                     parentsToCheck.add(parentFile);
606             }
607         }
608         Object JavaDoc[] elements = selection.toArray();
609         fPluginListViewer.setCheckedElements(elements);
610         
611         // handle checking for the TreeViewer
612
Object JavaDoc[] parents = parentsToCheck.toArray();
613         Object JavaDoc[] checkedValues= new Object JavaDoc[parents.length + elements.length];
614         System.arraycopy(parents, 0, checkedValues, 0, parents.length);
615         System.arraycopy(elements, 0, checkedValues, parents.length, elements.length);
616         fPluginTreeViewer.setCheckedElements(checkedValues);
617         for (int i = 0; i < parents.length; i++) {
618             handleGrayChecked((File JavaDoc)parents[i], false);
619         }
620         setCounter(elements.length);
621     }
622     
623     // returns a Set which contains all the new File objects representing a new location
624
public Set JavaDoc initializeTreeContents(IPluginModelBase[] allModels) {
625         HashSet JavaDoc parents = new HashSet JavaDoc();
626         if (fTreeViewerContents == null)
627             fTreeViewerContents = new HashMap JavaDoc();
628         for (int i = 0; i < allModels.length; i++) {
629             IPluginModelBase model = allModels[i];
630             String JavaDoc path = model.getInstallLocation();
631             if (path != null ) {
632                 File JavaDoc installFile = new File JavaDoc(path);
633                 File JavaDoc parentFile = installFile.getParentFile();
634                 Set JavaDoc models = (Set JavaDoc)fTreeViewerContents.get(parentFile);
635                 if (models == null) {
636                     models = new HashSet JavaDoc();
637                     models.add(model);
638                     fTreeViewerContents.put(parentFile, models);
639                     parents.add(parentFile);
640                 } else {
641                     models.add(model);
642                 }
643             }
644         }
645         return parents;
646     }
647     
648     public void performOk() {
649         savePreferences();
650         if (fReloaded) {
651             updateModels();
652             Job job = new TargetPlatformResetJob(fCurrentState);
653             job.setProperty(IProgressConstants.ICON_PROPERTY, PDEPluginImages.DESC_PLUGIN_OBJ);
654             job.schedule();
655             fReloaded = false;
656             fChangedModels.clear();
657         } else {
658             updateModels();
659             computeDelta();
660         }
661     }
662     
663     private void savePreferences() {
664         Preferences preferences = PDECore.getDefault().getPluginPreferences();
665         IPath newPath = new Path(fPage.getPlatformPath());
666         IPath defaultPath = new Path(org.eclipse.pde.core.plugin.TargetPlatform.getDefaultLocation());
667         String JavaDoc mode =
668             newPath.equals(defaultPath)
669                 ? ICoreConstants.VALUE_USE_THIS
670                 : ICoreConstants.VALUE_USE_OTHER;
671         preferences.setValue(ICoreConstants.TARGET_MODE, mode);
672         preferences.setValue(ICoreConstants.PLATFORM_PATH, fPage.getPlatformPath());
673
674         if (fCounter == 0) {
675             preferences.setValue(ICoreConstants.CHECKED_PLUGINS, ICoreConstants.VALUE_SAVED_NONE);
676         } else if (fCounter == fPluginListViewer.getTable().getItemCount()) {
677             preferences.setValue(ICoreConstants.CHECKED_PLUGINS, ICoreConstants.VALUE_SAVED_ALL);
678         } else {
679             StringBuffer JavaDoc saved = new StringBuffer JavaDoc();
680             TableItem[] models = fPluginListViewer.getTable().getItems();
681             for (int i = 0; i < models.length; i++) {
682                 if (models[i].getChecked())
683                     continue;
684                 IPluginModelBase model = (IPluginModelBase)models[i].getData();
685                 if (saved.length() > 0)
686                     saved.append(" "); //$NON-NLS-1$
687
saved.append(model.getPluginBase().getId());
688             }
689             preferences.setValue(ICoreConstants.CHECKED_PLUGINS, saved.toString());
690         }
691         
692         String JavaDoc[] locations = fPage.getPlatformLocations();
693         for (int i = 0; i < locations.length && i < 5; i++) {
694             preferences.setValue(ICoreConstants.SAVED_PLATFORM + i, locations[i]);
695         }
696         preferences.setValue(ICoreConstants.GROUP_PLUGINS_VIEW, fGroupPlugins.getSelection());
697         
698         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
699         for (int i = 0; i < fAdditionalLocations.size(); i++) {
700             if (buffer.length() > 0)
701                 buffer.append(","); //$NON-NLS-1$
702
buffer.append(fAdditionalLocations.get(i).toString());
703         }
704         preferences.setValue(ICoreConstants.ADDITIONAL_LOCATIONS, buffer.toString());
705         PDECore.getDefault().savePluginPreferences();
706     }
707     
708     private void updateModels() {
709         Iterator JavaDoc iter = fChangedModels.iterator();
710         while (iter.hasNext()) {
711             IPluginModelBase model = (IPluginModelBase) iter.next();
712             model.setEnabled(fPluginListViewer.getChecked(model));
713         }
714     }
715     
716     public void handleSelectAll(boolean selected) {
717         fPluginListViewer.setAllChecked(selected);
718         fPluginTreeViewer.setAllChecked(selected);
719         
720         IPluginModelBase[] allModels = getCurrentModels();
721         for (int i = 0; i < allModels.length; i++) {
722             IPluginModelBase model = allModels[i];
723             if (model.isEnabled() != selected) {
724                 fChangedModels.add(model);
725             } else if (fChangedModels.contains(model) && model.isEnabled() == selected) {
726                 fChangedModels.remove(model);
727             }
728         }
729         if (selected) {
730             setCounter(fPluginListViewer.getTable().getItemCount());
731         } else
732             setCounter(0);
733     }
734     
735     private void handleWorkingSets() {
736         IWorkingSetManager manager = PlatformUI.getWorkbench().getWorkingSetManager();
737         IWorkingSetSelectionDialog dialog = manager.createWorkingSetSelectionDialog(fPluginListViewer.getControl().getShell(), true);
738         if (dialog.open() == Window.OK) {
739             HashSet JavaDoc set = getPluginIDs(dialog.getSelection());
740             IPluginModelBase[] models = getCurrentModels();
741             int counter = 0;
742             for (int i = 0; i < models.length; i++) {
743                 String JavaDoc id = models[i].getPluginBase().getId();
744                 if (id == null)
745                     continue;
746                 if (set.contains(id)) {
747                     if (!fPluginListViewer.getChecked(models[i])) {
748                         fPluginListViewer.setChecked(models[i], true);
749                         counter += 1;
750                         if (!models[i].isEnabled())
751                             fChangedModels.add(models[i]);
752                     }
753                     if (!fPluginTreeViewer.getChecked(models[i])) {
754                         fPluginTreeViewer.setChecked(models[i], true);
755                         String JavaDoc path = models[i].getInstallLocation();
756                         if (path != null) {
757                             File JavaDoc parent = new File JavaDoc(path).getParentFile();
758                             fPluginTreeViewer.setChecked(parent, true);
759                             handleGrayChecked(parent, false);
760                         }
761                     }
762                     set.remove(id);
763                 }
764                 if (set.isEmpty())
765                     break;
766             }
767             setCounter(fCounter + counter);
768         }
769     }
770     
771     /*
772      * Sets the parent object to the right state. This includes the checked state
773      * and/or the gray state.
774      *
775      * @param parent The parent object to check the current state of.
776      * @param handleCheck True if you wish the set the checked value of the object, False otherwise.
777      */

778     // added the second boolean because many time I know the parent will be checked.
779
// Therefore I didn't want to waste time calling .setChecked() again.
780
protected void handleGrayChecked(File JavaDoc parent, boolean handleCheck) {
781         boolean gray = false, check = false, allChecked = true;
782         Set JavaDoc models = (Set JavaDoc)fTreeViewerContents.get(parent);
783         Iterator JavaDoc it = models.iterator();
784         while (it.hasNext()) {
785             Object JavaDoc model = it.next();
786             boolean checked = fPluginTreeViewer.getChecked(model);
787             check = check || checked;
788             allChecked = allChecked && checked;
789             if (!gray && checked) {
790                 gray = true;
791             } else if (gray && !checked) {
792                 allChecked = false;
793                 break;
794             }
795         }
796         if (!allChecked && gray)
797             fPluginTreeViewer.setGrayed(parent, true);
798         else
799             fPluginTreeViewer.setGrayed(parent, false);
800         if (handleCheck) {
801             if (check)
802                 fPluginTreeViewer.setChecked(parent, true);
803             else
804                 fPluginTreeViewer.setChecked(parent, false);
805         }
806     }
807     
808     private HashSet JavaDoc getPluginIDs(IWorkingSet[] workingSets) {
809         HashSet JavaDoc set = new HashSet JavaDoc();
810         for (int i = 0; i < workingSets.length; i++) {
811             IAdaptable[] elements = workingSets[i].getElements();
812             for (int j = 0; j < elements.length; j++) {
813                 Object JavaDoc element = elements[j];
814                 if (element instanceof PersistablePluginObject) {
815                     set.add(((PersistablePluginObject)element).getPluginID());
816                 } else {
817                     if (element instanceof IJavaProject)
818                         element = ((IJavaProject)element).getProject();
819                     if (element instanceof IProject) {
820                         IPluginModelBase model = PluginRegistry.findModel((IProject)element);
821                         if (model != null)
822                             set.add(model.getPluginBase().getId());
823                     }
824                 }
825             }
826         }
827         return set;
828     }
829     
830     private void handleAddRequired() {
831         Object JavaDoc[] checked = fPluginListViewer.getCheckedElements();
832         if (checked.length == 0)
833             return;
834         String JavaDoc[] implicit = fPage.getImplicitPlugins();
835         State state = getCurrentState().getState();
836         Set JavaDoc set = DependencyManager.getDependencies(checked, implicit, state);
837         Set JavaDoc parents = new HashSet JavaDoc();
838         IPluginModelBase[] models = getCurrentModels();
839         int counter = 0;
840         for (int i = 0; i < models.length; i++) {
841             if (set.contains(models[i].getPluginBase().getId())) {
842                 fPluginListViewer.setChecked(models[i], true);
843                 fPluginTreeViewer.setChecked(models[i], true);
844                 fChangedModels.add(models[i]);
845                 counter += 1;
846                 String JavaDoc path = models[i].getInstallLocation();
847                 if (path != null) {
848                     parents.add(new File JavaDoc(path).getParentFile());
849                 }
850             }
851         }
852         Iterator JavaDoc it = parents.iterator();
853         while (it.hasNext())
854             handleGrayChecked((File JavaDoc)it.next(), true);
855         
856         setCounter(fCounter + counter);
857     }
858     
859     protected IPluginModelBase[] getCurrentModels() {
860         if (fCurrentState != null)
861             return fCurrentState.getTargetModels();
862         return PDECore.getDefault().getModelManager().getExternalModels();
863     }
864     
865     protected PDEState getCurrentState() {
866         return (fCurrentState != null) ? fCurrentState : TargetPlatformHelper.getPDEState();
867     }
868     
869     protected void handleSwitchView() {
870         if (fGroupPlugins.getSelection())
871             fBook.showPage(fPluginTreeViewer.getControl());
872         else
873             fBook.showPage(fPluginListViewer.getControl());
874     
875     }
876     
877     private void setCounter(int value) {
878         fCounter = value;
879         int total = fPluginListViewer.getTable().getItemCount();
880         String JavaDoc message =
881             NLS.bind(PDEUIMessages.WizardCheckboxTablePart_counter, (new String JavaDoc[] { Integer.toString(fCounter), Integer.toString(total) }));
882         fCounterLabel.setText(message);
883     }
884
885     protected void buttonSelected(Button button, int index) {
886         switch (index) {
887         case 0:
888             handleReload();
889             fPage.resetTargetProfile();
890             break;
891         case 1:
892             handleAdd();
893             break;
894         case 4:
895             handleSelectAll(true);
896             break;
897         case 5:
898             handleSelectAll(false);
899             break;
900         case 6:
901             handleWorkingSets();
902             break;
903         case 7:
904             handleAddRequired();
905             break;
906         }
907     }
908     
909     private void handleAdd() {
910         AddTargetPluginsWizard wizard = new AddTargetPluginsWizard();
911         WizardDialog dialog = new WizardDialog(fPage.getShell(), wizard);
912         dialog.create();
913         SWTUtil.setDialogSize(dialog, 400, 450);
914         dialog.open();
915         
916         File JavaDoc[] dirs = wizard.getDirectories();
917         if (dirs.length == 0) {
918             // no new URLs found/to add
919
return;
920         }
921         for (int i = 0; i < dirs.length; i++) {
922             fAdditionalLocations.add(dirs[i].getPath());
923             File JavaDoc temp = new File JavaDoc(dirs[i], "plugins"); //$NON-NLS-1$
924
if (temp.exists())
925                 dirs[i] = temp;
926         }
927         
928         URL JavaDoc[] pluginLocs = PluginPathFinder.scanLocations(dirs);
929         Object JavaDoc[] checkedPlugins = null;
930         if (fCurrentState == null) {
931             checkedPlugins = fPluginListViewer.getCheckedElements();
932             createCopyState();
933         }
934         BundleDescription[] descriptions = fCurrentState.addAdditionalBundles(pluginLocs);
935         addNewBundles(descriptions, checkedPlugins);
936     }
937     
938     private void createCopyState() {
939         fCurrentState = new PDEState(TargetPlatformHelper.getPDEState());
940         IPluginModelBase[] bases = fCurrentState.getTargetModels();
941         for (int j = 0; j < bases.length; j++) {
942             long bundleId = bases[j].getBundleDescription().getBundleId();
943             BundleDescription newDesc = fCurrentState.getState().getBundle(bundleId);
944             bases[j].setBundleDescription(newDesc);
945         }
946     }
947     
948     private void addNewBundles(BundleDescription[] descriptions, Object JavaDoc[] checkedPlugins) {
949         if (descriptions.length > 0) {
950             IPluginModelBase[] models = fCurrentState.createTargetModels(descriptions);
951             // add new models to tree viewer
952
Set JavaDoc parents = initializeTreeContents(models);
953             
954             fPluginListViewer.setInput(PDECore.getDefault().getModelManager().getExternalModelManager());
955             fPluginTreeViewer.setInput(PDECore.getDefault().getModelManager().getExternalModelManager());
956             
957             if (checkedPlugins == null) {
958                 for (int i = 0; i < models.length; i++) {
959                     fPluginListViewer.setChecked(models[i], true);
960                     fPluginTreeViewer.setChecked(models[i], true);
961                 }
962             } else {
963                 Object JavaDoc[] newCheckedPlugins = new Object JavaDoc[checkedPlugins.length + models.length];
964                 System.arraycopy(checkedPlugins, 0, newCheckedPlugins, 0, checkedPlugins.length);
965                 System.arraycopy(models, 0, newCheckedPlugins, checkedPlugins.length, models.length);
966                 fPluginListViewer.setCheckedElements(newCheckedPlugins);
967                 fPluginTreeViewer.setCheckedElements(newCheckedPlugins);
968             }
969             for (int i = 0; i < models.length; i++) {
970                 fChangedModels.add(models[i]);
971             }
972             Iterator JavaDoc it = parents.iterator();
973             while (it.hasNext())
974                 fPluginTreeViewer.setChecked(it.next(), true);
975             // have to use getCheckedElements() instead of newCheckedPlugins because a new plug-in might have replaced an plug-in model in the original checked list.
976
setCounter(fPluginListViewer.getCheckedElements().length);
977             fPage.getSourceBlock().resetExtensionLocations(getCurrentModels());
978             fReloaded = true;
979         }
980     }
981
982 }
983
Popular Tags