KickJava   Java API By Example, From Geeks To Geeks.

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


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.pde.internal.ui.preferences;
12
13 import java.lang.reflect.*;
14 import java.net.*;
15 import java.util.*;
16
17 import org.eclipse.core.resources.*;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.jdt.core.*;
20 import org.eclipse.jface.operation.*;
21 import org.eclipse.jface.viewers.*;
22 import org.eclipse.jface.window.*;
23 import org.eclipse.osgi.service.resolver.BundleDescription;
24 import org.eclipse.pde.core.*;
25 import org.eclipse.pde.core.plugin.*;
26 import org.eclipse.pde.internal.core.*;
27 import org.eclipse.pde.internal.ui.*;
28 import org.eclipse.pde.internal.ui.elements.*;
29 import org.eclipse.pde.internal.ui.parts.*;
30 import org.eclipse.pde.internal.ui.util.*;
31 import org.eclipse.pde.internal.ui.wizards.*;
32 import org.eclipse.swt.*;
33 import org.eclipse.swt.custom.*;
34 import org.eclipse.swt.layout.*;
35 import org.eclipse.swt.widgets.*;
36 import org.eclipse.ui.*;
37 import org.eclipse.ui.dialogs.*;
38 import org.eclipse.ui.forms.widgets.*;
39
40
41 public class ExternalPluginsBlock {
42     private CheckboxTableViewer fPluginListViewer;
43     private TargetPlatformPreferencePage fPage;
44     private boolean fReloaded;
45     private TablePart fTablePart;
46     private HashSet fChangedModels = new HashSet();
47     private IPluginModelBase[] fInitialModels;
48     private IPluginModelBase[] fModels;
49     private PDEState fCurrentState;
50     private Button fIncludeFragments;
51
52     
53     class ReloadOperation implements IRunnableWithProgress {
54         private String JavaDoc location;
55         
56         public ReloadOperation(String JavaDoc platformPath) {
57             this.location = platformPath;
58         }
59             
60         public void run(IProgressMonitor monitor)
61             throws InvocationTargetException, InterruptedException JavaDoc {
62             URL[] pluginPaths = PluginPathFinder.getPluginPaths(location);
63             fCurrentState = new PDEState(pluginPaths, true, monitor);
64             fModels = fCurrentState.getModels();
65         }
66         
67     }
68     
69     public class PluginContentProvider
70         extends DefaultContentProvider
71         implements IStructuredContentProvider {
72         public Object JavaDoc[] getElements(Object JavaDoc parent) {
73             return getAllModels();
74         }
75     }
76
77     class TablePart extends WizardCheckboxTablePart {
78         public TablePart(String JavaDoc[] buttonLabels) {
79             super(null, buttonLabels);
80         }
81
82         protected void buttonSelected(Button button, int index) {
83             switch (index) {
84                 case 0:
85                     handleReload();
86                     break;
87                 case 5:
88                     handleWorkingSets();
89                     break;
90                 case 6:
91                     handleAddRequired();
92                     break;
93                 case 7:
94                     selectNotInWorkspace();
95                     break;
96                 default:
97                     super.buttonSelected(button, index);
98             }
99         }
100
101         protected StructuredViewer createStructuredViewer(
102             Composite parent,
103             int style,
104             FormToolkit toolkit) {
105             StructuredViewer viewer =
106                 super.createStructuredViewer(parent, style, toolkit);
107             viewer.setSorter(ListUtil.PLUGIN_SORTER);
108             return viewer;
109         }
110
111         protected void elementChecked(Object JavaDoc element, boolean checked) {
112             IPluginModelBase model = (IPluginModelBase) element;
113             if (fChangedModels.contains(model) && model.isEnabled() == checked) {
114                 fChangedModels.remove(model);
115             } else if (model.isEnabled() != checked) {
116                 fChangedModels.add(model);
117             }
118             super.elementChecked(element, checked);
119         }
120         
121         protected void handleSelectAll(boolean select) {
122             super.handleSelectAll(select);
123             IPluginModelBase[] allModels = getAllModels();
124             for (int i = 0; i < allModels.length; i++) {
125                 IPluginModelBase model = allModels[i];
126                 if (model.isEnabled() != select) {
127                     fChangedModels.add(model);
128                 } else if (fChangedModels.contains(model) && model.isEnabled() == select) {
129                     fChangedModels.remove(model);
130                 }
131             }
132         }
133         
134         public void incrementCounter(int increment) {
135             updateCounter(getSelectionCount() + increment);
136         }
137     }
138
139     public ExternalPluginsBlock(TargetPlatformPreferencePage page) {
140         this.fPage = page;
141         String JavaDoc[] buttonLabels =
142             {
143                 PDEUIMessages.ExternalPluginsBlock_reload,
144                 null,
145                 null,
146                 PDEUIMessages.WizardCheckboxTablePart_selectAll,
147                 PDEUIMessages.WizardCheckboxTablePart_deselectAll,
148                 PDEUIMessages.ExternalPluginsBlock_workingSet, //$NON-NLS-1$
149
PDEUIMessages.ExternalPluginsBlock_addRequired, //$NON-NLS-1$
150
PDEUIMessages.ExternalPluginsBlock_workspace};
151         fTablePart = new TablePart(buttonLabels);
152         fTablePart.setSelectAllIndex(3);
153         fTablePart.setDeselectAllIndex(4);
154         PDEPlugin.getDefault().getLabelProvider().connect(this);
155     }
156
157     void computeDelta() {
158         int type = 0;
159         IModel[] addedArray = null;
160         IModel[] removedArray = null;
161         IModel[] changedArray = null;
162         if (fReloaded) {
163             type =
164                 IModelProviderEvent.MODELS_REMOVED
165                     | IModelProviderEvent.MODELS_ADDED;
166             removedArray = fInitialModels;
167             addedArray = getAllModels();
168         } else if (fChangedModels.size() > 0) {
169             type |= IModelProviderEvent.MODELS_CHANGED;
170             changedArray = (IModel[]) fChangedModels.toArray(new IModel[fChangedModels.size()]);
171         }
172         fChangedModels.clear();
173         if (type != 0) {
174             ExternalModelManager registry =
175                 PDECore.getDefault().getExternalModelManager();
176             ModelProviderEvent event =
177                 new ModelProviderEvent(
178                     registry,
179                     type,
180                     addedArray,
181                     removedArray,
182                     changedArray);
183             registry.fireModelProviderEvent(event);
184         }
185     }
186
187     public Control createContents(Composite parent) {
188         Composite container = new Composite(parent, SWT.NONE);
189         GridLayout layout = new GridLayout();
190         layout.numColumns = 2;
191         layout.marginHeight = 0;
192         layout.marginWidth = 0;
193         container.setLayout(layout);
194
195         fTablePart.createControl(container);
196
197         fPluginListViewer = fTablePart.getTableViewer();
198         fPluginListViewer.setContentProvider(new PluginContentProvider());
199         fPluginListViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
200
201         GridData gd = (GridData) fTablePart.getControl().getLayoutData();
202         gd.heightHint = 100;
203                 
204         fIncludeFragments = new Button(container, SWT.CHECK);
205         fIncludeFragments.setText(PDEUIMessages.ExternalPluginsBlock_includeFragments); //$NON-NLS-1$
206
gd = new GridData();
207         gd.horizontalSpan = 2;
208         gd.verticalIndent = 5;
209         gd.horizontalIndent = 5;
210         fIncludeFragments.setLayoutData(gd);
211         fIncludeFragments.setSelection(PDECore.getDefault().getPluginPreferences().getBoolean(ICoreConstants.INCLUDE_FRAGMENTS));
212         return container;
213     }
214
215
216     public void dispose() {
217         PDEPlugin.getDefault().getLabelProvider().disconnect(this);
218     }
219
220     public IPluginModelBase[] getAllModels() {
221         if (fModels == null) {
222             fInitialModels =
223                 PDECore.getDefault().getModelManager().getExternalModels();
224             return fInitialModels;
225         }
226         return fModels;
227     }
228
229     protected void handleReload() {
230         String JavaDoc platformPath = fPage.getPlatformPath();
231         if (platformPath != null && platformPath.length() > 0) {
232             ReloadOperation op = new ReloadOperation(platformPath);
233             try {
234                 PlatformUI.getWorkbench().getProgressService().run(true, false, op);
235             } catch (InvocationTargetException e) {
236             } catch (InterruptedException JavaDoc e) {
237             }
238             fPluginListViewer.setInput(PDECore.getDefault().getExternalModelManager());
239             fChangedModels.clear();
240             handleSelectAll(true);
241             fReloaded = true;
242             fPage.getSourceBlock().resetExtensionLocations(getAllModels());
243         }
244         fPage.resetNeedsReload();
245     }
246
247     public void initialize() {
248         String JavaDoc platformPath = fPage.getPlatformPath();
249         if (platformPath != null && platformPath.length() == 0)
250             return;
251
252         fPluginListViewer.setInput(PDECore.getDefault().getExternalModelManager());
253         IPluginModelBase[] allModels = getAllModels();
254
255         Vector selection = new Vector();
256         for (int i = 0; i < allModels.length; i++) {
257             IPluginModelBase model = allModels[i];
258             if (model.isEnabled()) {
259                 selection.add(model);
260             }
261         }
262         fTablePart.setSelection(selection.toArray());
263     }
264
265     public void performOk() {
266         BusyIndicator.showWhile(fPage.getShell().getDisplay(), new Runnable JavaDoc() {
267             public void run() {
268                 savePreferences();
269                 if (fReloaded)
270                     EclipseHomeInitializer.resetEclipseHomeVariable();
271                 if (fReloaded) {
272                     IPluginModelBase[] models = PDECore.getDefault().getModelManager().getWorkspaceModels();
273                     for (int i = 0; i < models.length; i++) {
274                         BundleDescription bundle = models[i].getBundleDescription();
275                         if (bundle == null)
276                             continue;
277                         BundleDescription[] conflicts = fCurrentState.getState().getBundles(bundle.getSymbolicName());
278                         for (int j = 0; j < conflicts.length; j++)
279                             fCurrentState.getState().removeBundle(conflicts[j]);
280                         fCurrentState.addBundle(models[i], false);
281                     }
282                     if (models.length > 0)
283                         fCurrentState.resolveState(true);
284                     PDECore.getDefault().getExternalModelManager().setModels(fCurrentState.getTargetModels());
285                     PDECore.getDefault().getModelManager().setState(fCurrentState);
286                     PDECore.getDefault().getFeatureModelManager().targetReloaded();
287                 }
288                 updateModels();
289                 computeDelta();
290             }
291         });
292     }
293     
294     private void savePreferences() {
295         Preferences preferences = PDECore.getDefault().getPluginPreferences();
296         IPath newPath = new Path(fPage.getPlatformPath());
297         IPath defaultPath = new Path(ExternalModelManager.computeDefaultPlatformPath());
298         String JavaDoc mode =
299             ExternalModelManager.arePathsEqual(newPath, defaultPath)
300                 ? ICoreConstants.VALUE_USE_THIS
301                 : ICoreConstants.VALUE_USE_OTHER;
302         preferences.setValue(ICoreConstants.TARGET_MODE, mode);
303         preferences.setValue(ICoreConstants.PLATFORM_PATH, fPage.getPlatformPath());
304         String JavaDoc[] locations = fPage.getPlatformLocations();
305         for (int i = 0; i < locations.length && i < 5; i++) {
306             preferences.setValue(ICoreConstants.SAVED_PLATFORM + i, locations[i]);
307         }
308         preferences.setValue(ICoreConstants.INCLUDE_FRAGMENTS, fIncludeFragments.getSelection());
309         PDECore.getDefault().savePluginPreferences();
310     }
311     
312     private void updateModels() {
313         Iterator iter = fChangedModels.iterator();
314         while (iter.hasNext()) {
315             IPluginModelBase model = (IPluginModelBase) iter.next();
316             model.setEnabled(fTablePart.getTableViewer().getChecked(model));
317         }
318     }
319     
320     private void selectNotInWorkspace() {
321         IPluginModelBase[] wsModels = PDECore.getDefault().getModelManager().getWorkspaceModels();
322         IPluginModelBase[] exModels = getAllModels();
323         Vector selected = new Vector();
324         for (int i = 0; i < exModels.length; i++) {
325             IPluginModelBase exModel = exModels[i];
326             boolean inWorkspace = false;
327             for (int j = 0; j < wsModels.length; j++) {
328                 IPluginModelBase wsModel = wsModels[j];
329                 String JavaDoc extId = exModel.getPluginBase().getId();
330                 String JavaDoc wsId = wsModel.getPluginBase().getId();
331                 if (extId != null && wsId != null && extId.equals(wsId)) {
332                     inWorkspace = true;
333                     break;
334                 }
335             }
336             if (!inWorkspace) {
337                 selected.add(exModel);
338             }
339             if (exModel.isEnabled() == inWorkspace)
340                 fChangedModels.add(exModel);
341             else if (fChangedModels.contains(exModel))
342                 fChangedModels.remove(exModel);
343         }
344         fTablePart.setSelection(selected.toArray());
345     }
346     
347     public void handleSelectAll(boolean selected) {
348         fTablePart.selectAll(selected);
349     }
350     
351     private void handleWorkingSets() {
352         IWorkingSetManager manager = PlatformUI.getWorkbench().getWorkingSetManager();
353         IWorkingSetSelectionDialog dialog = manager.createWorkingSetSelectionDialog(fTablePart.getControl().getShell(), true);
354         if (dialog.open() == Window.OK) {
355             HashSet set = getPluginIDs(dialog.getSelection());
356             IPluginModelBase[] models = getAllModels();
357             int counter = 0;
358             for (int i = 0; i < models.length; i++) {
359                 String JavaDoc id = models[i].getPluginBase().getId();
360                 if (id == null)
361                     continue;
362                 if (set.contains(id)) {
363                     if (!fPluginListViewer.getChecked(models[i])) {
364                         fPluginListViewer.setChecked(models[i], true);
365                         counter += 1;
366                         if (!models[i].isEnabled())
367                             fChangedModels.add(models[i]);
368                     }
369                     set.remove(id);
370                 }
371                 if (set.isEmpty())
372                     break;
373             }
374             fTablePart.incrementCounter(counter);
375         }
376     }
377     
378     private HashSet getPluginIDs(IWorkingSet[] workingSets) {
379         HashSet set = new HashSet();
380         for (int i = 0; i < workingSets.length; i++) {
381             IAdaptable[] elements = workingSets[i].getElements();
382             for (int j = 0; j < elements.length; j++) {
383                 Object JavaDoc element = elements[j];
384                 if (element instanceof PersistablePluginObject) {
385                     set.add(((PersistablePluginObject)element).getPluginID());
386                 } else {
387                     if (element instanceof IJavaProject)
388                         element = ((IJavaProject)element).getProject();
389                     if (element instanceof IProject) {
390                         IPluginModelBase model = PDECore.getDefault().getModelManager().findModel((IProject)element);
391                         if (model != null)
392                             set.add(model.getPluginBase().getId());
393                     }
394                 }
395             }
396         }
397         return set;
398     }
399     
400     private void handleAddRequired() {
401         TableItem[] items = fTablePart.getTableViewer().getTable().getItems();
402         
403         if (items.length == 0)
404             return;
405         
406         ArrayList result = new ArrayList();
407         for (int i = 0; i < items.length; i++) {
408             IPluginModelBase model = (IPluginModelBase)items[i].getData();
409             if (fTablePart.getTableViewer().getChecked(model))
410                 addPluginAndDependencies((IPluginModelBase) items[i].getData(), result);
411         }
412         fTablePart.setSelection(result.toArray());
413     }
414     
415     protected void addPluginAndDependencies(
416             IPluginModelBase model,
417             ArrayList selected) {
418                 
419             if (!selected.contains(model)) {
420                 selected.add(model);
421                 if (!model.isEnabled())
422                     fChangedModels.add(model);
423                 addDependencies(getAllModels(), model, selected);
424             }
425         }
426         
427     protected void addDependencies(
428         IPluginModelBase[] models,
429         IPluginModelBase model,
430         ArrayList selected) {
431         
432         IPluginImport[] required = model.getPluginBase().getImports();
433         if (required.length > 0) {
434             for (int i = 0; i < required.length; i++) {
435                 IPluginModelBase found = findModel(models, required[i].getId());
436                 if (found != null) {
437                     addPluginAndDependencies(found, selected);
438                 }
439             }
440         }
441         
442         if (model instanceof IPluginModel) {
443             boolean addFragments = fIncludeFragments.getSelection()
444                             || ClasspathUtilCore.hasExtensibleAPI((IPlugin)model.getPluginBase());
445             if (!addFragments) {
446                 IPluginLibrary[] libraries = model.getPluginBase().getLibraries();
447                 for (int i = 0; i < libraries.length; i++) {
448                     if (ClasspathUtilCore.containsVariables(libraries[i].getName())) {
449                         addFragments = true;
450                         break;
451                     }
452                 }
453             }
454             if (addFragments) {
455                 IFragmentModel[] fragments = findFragments(models, ((IPluginModel)model).getPlugin());
456                 for (int i = 0; i < fragments.length; i++) {
457                     addPluginAndDependencies(fragments[i], selected);
458                 }
459             }
460         } else {
461             IFragment fragment = ((IFragmentModel) model).getFragment();
462             IPluginModelBase found = findModel(models, fragment.getPluginId());
463             if (found != null) {
464                 addPluginAndDependencies(found, selected);
465             }
466         }
467     }
468
469     private IPluginModelBase findModel(IPluginModelBase[] models, String JavaDoc id) {
470         for (int i = 0; i < models.length; i++) {
471             String JavaDoc modelId = models[i].getPluginBase().getId();
472             if (modelId != null && modelId.equals(id))
473                 return models[i];
474         }
475         return null;
476     }
477
478     private IFragmentModel[] findFragments(IPluginModelBase[] models, IPlugin plugin) {
479         ArrayList result = new ArrayList();
480         for (int i = 0; i < models.length; i++) {
481             if (models[i] instanceof IFragmentModel) {
482                 IFragment fragment = ((IFragmentModel) models[i]).getFragment();
483                 if (plugin.getId().equalsIgnoreCase(fragment.getPluginId())) {
484                     result.add(models[i]);
485                 }
486             }
487         }
488         return (IFragmentModel[]) result.toArray(new IFragmentModel[result.size()]);
489     }
490
491 }
492
Popular Tags