KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > launcher > AdvancedLauncherTab


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.launcher;
12
13 import java.io.File JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.TreeMap JavaDoc;
20 import java.util.TreeSet JavaDoc;
21
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IAdaptable;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.debug.core.ILaunchConfiguration;
28 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
29 import org.eclipse.debug.ui.ILaunchConfigurationTab;
30 import org.eclipse.jdt.core.IJavaProject;
31 import org.eclipse.jface.dialogs.Dialog;
32 import org.eclipse.jface.dialogs.MessageDialog;
33 import org.eclipse.jface.viewers.CheckStateChangedEvent;
34 import org.eclipse.jface.viewers.CheckboxTreeViewer;
35 import org.eclipse.jface.viewers.ICheckStateListener;
36 import org.eclipse.jface.viewers.ITreeContentProvider;
37 import org.eclipse.jface.window.Window;
38 import org.eclipse.pde.core.plugin.IFragmentModel;
39 import org.eclipse.pde.core.plugin.IPlugin;
40 import org.eclipse.pde.core.plugin.IPluginBase;
41 import org.eclipse.pde.core.plugin.IPluginImport;
42 import org.eclipse.pde.core.plugin.IPluginLibrary;
43 import org.eclipse.pde.core.plugin.IPluginModelBase;
44 import org.eclipse.pde.internal.core.ClasspathUtilCore;
45 import org.eclipse.pde.internal.core.ModelEntry;
46 import org.eclipse.pde.internal.core.PDECore;
47 import org.eclipse.pde.internal.core.PluginModelManager;
48 import org.eclipse.pde.internal.core.plugin.ExternalPluginModelBase;
49 import org.eclipse.pde.internal.core.plugin.WorkspacePluginModelBase;
50 import org.eclipse.pde.internal.ui.IHelpContextIds;
51 import org.eclipse.pde.internal.ui.PDEPlugin;
52 import org.eclipse.pde.internal.ui.PDEPluginImages;
53 import org.eclipse.pde.internal.ui.PDEUIMessages;
54 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
55 import org.eclipse.pde.internal.ui.elements.NamedElement;
56 import org.eclipse.pde.internal.ui.util.PersistablePluginObject;
57 import org.eclipse.pde.internal.ui.util.SWTUtil;
58 import org.eclipse.pde.internal.ui.wizards.ListUtil;
59 import org.eclipse.swt.SWT;
60 import org.eclipse.swt.custom.BusyIndicator;
61 import org.eclipse.swt.events.SelectionAdapter;
62 import org.eclipse.swt.events.SelectionEvent;
63 import org.eclipse.swt.graphics.Image;
64 import org.eclipse.swt.layout.GridData;
65 import org.eclipse.swt.layout.GridLayout;
66 import org.eclipse.swt.widgets.Button;
67 import org.eclipse.swt.widgets.Composite;
68 import org.eclipse.ui.IWorkingSet;
69 import org.eclipse.ui.IWorkingSetManager;
70 import org.eclipse.ui.PlatformUI;
71 import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog;
72
73 public class AdvancedLauncherTab
74     extends AbstractLauncherTab
75     implements ILaunchConfigurationTab, ILauncherSettings {
76
77     private Button fUseDefaultRadio;
78     private Button fUseFeaturesRadio;
79     private Button fUseListRadio;
80     private Button fAddRequiredButton;
81     private CheckboxTreeViewer fPluginTreeViewer;
82     private NamedElement fWorkspacePlugins;
83     private NamedElement fExternalPlugins;
84     private IPluginModelBase[] fExternalModels;
85     private IPluginModelBase[] fWorkspaceModels;
86     private Button fDefaultsButton;
87     private int fNumExternalChecked = 0;
88     private int fNumWorkspaceChecked = 0;
89     private Image fImage;
90     private boolean fShowFeatures = true;
91     private Button fSelectAllButton;
92     private Button fDeselectButton;
93     private Button fWorkingSetButton;
94     private Button fIncludeFragmentsButton;
95     private Button fAddWorkspaceButton;
96     private String JavaDoc fProductID;
97     private String JavaDoc fApplicationID;
98     private Button fIncludeOptionalButton;
99
100     class PluginContentProvider
101         extends DefaultContentProvider
102         implements ITreeContentProvider {
103         public boolean hasChildren(Object JavaDoc parent) {
104             return !(parent instanceof IPluginModelBase);
105         }
106         public Object JavaDoc[] getChildren(Object JavaDoc parent) {
107             if (parent == fExternalPlugins)
108                 return fExternalModels;
109             if (parent == fWorkspacePlugins)
110                 return fWorkspaceModels;
111             return new Object JavaDoc[0];
112         }
113         public Object JavaDoc getParent(Object JavaDoc child) {
114             return null;
115         }
116         public Object JavaDoc[] getElements(Object JavaDoc input) {
117             return new Object JavaDoc[] { fWorkspacePlugins, fExternalPlugins };
118         }
119     }
120
121     public AdvancedLauncherTab() {
122         this(true);
123     }
124
125     public AdvancedLauncherTab(boolean showFeatures) {
126         this.fShowFeatures = showFeatures;
127         PDEPlugin.getDefault().getLabelProvider().connect(this);
128         fImage = PDEPluginImages.DESC_REQ_PLUGINS_OBJ.createImage();
129         fExternalModels = PDECore.getDefault().getModelManager().getExternalModels();
130         fWorkspaceModels = PDECore.getDefault().getModelManager().getWorkspaceModels();
131     }
132
133     public void dispose() {
134         PDEPlugin.getDefault().getLabelProvider().disconnect(this);
135         fImage.dispose();
136         super.dispose();
137     }
138
139     public void createControl(Composite parent) {
140         Composite composite = new Composite(parent, SWT.NONE);
141         composite.setLayout(new GridLayout());
142
143         fUseDefaultRadio = new Button(composite, SWT.RADIO);
144         fUseDefaultRadio.setText(
145             PDEUIMessages.AdvancedLauncherTab_useDefault); //$NON-NLS-1$
146

147         if (fShowFeatures) {
148             fUseFeaturesRadio = new Button(composite, SWT.RADIO);
149             fUseFeaturesRadio.setText(
150                 PDEUIMessages.AdvancedLauncherTab_useFeatures); //$NON-NLS-1$
151
}
152
153         fUseListRadio = new Button(composite, SWT.RADIO);
154         fUseListRadio.setText(PDEUIMessages.AdvancedLauncherTab_useList); //$NON-NLS-1$
155

156         createPluginList(composite);
157         
158         createSeparator(composite, 1);
159         
160         Button button = new Button(composite, SWT.PUSH);
161         button.setText(PDEUIMessages.AdvancedLauncherTab_validatePlugins); //$NON-NLS-1$
162
button.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
163         button.addSelectionListener(new SelectionAdapter() {
164         public void widgetSelected(SelectionEvent e) {
165             handleValidatePlugins();
166         }});
167         
168         SWTUtil.setButtonDimensionHint(button);
169         
170         hookListeners();
171         setControl(composite);
172
173         Dialog.applyDialogFont(composite);
174         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IHelpContextIds.LAUNCHER_ADVANCED);
175     }
176
177     private void hookListeners() {
178         SelectionAdapter adapter = new SelectionAdapter() {
179             public void widgetSelected(SelectionEvent e) {
180                 useDefaultChanged();
181             }
182         };
183         fUseDefaultRadio.addSelectionListener(adapter);
184
185         if (fShowFeatures)
186             fUseFeaturesRadio.addSelectionListener(adapter);
187
188         fDefaultsButton.addSelectionListener(new SelectionAdapter() {
189             public void widgetSelected(SelectionEvent e) {
190                 computeInitialCheckState();
191                 updateStatus();
192             }
193         });
194         
195         fWorkingSetButton.addSelectionListener(new SelectionAdapter() {
196             public void widgetSelected(SelectionEvent e) {
197                 handleWorkingSets();
198                 updateStatus();
199             }
200         });
201
202         fAddRequiredButton.addSelectionListener(new SelectionAdapter() {
203             public void widgetSelected(SelectionEvent e) {
204                 BusyIndicator.showWhile(getControl().getDisplay(), new Runnable JavaDoc() {
205                     public void run() {
206                         computeSubset();
207                         updateStatus();
208                     }
209                 });
210             }
211         });
212         
213         fSelectAllButton.addSelectionListener(new SelectionAdapter() {
214             public void widgetSelected(SelectionEvent e) {
215                 toggleGroups(true);
216                 updateStatus();
217             }}
218         );
219         
220         fDeselectButton.addSelectionListener(new SelectionAdapter() {
221             public void widgetSelected(SelectionEvent e) {
222                 toggleGroups(false);
223                 updateStatus();
224             }}
225         );
226         
227         fIncludeFragmentsButton.addSelectionListener(new SelectionAdapter() {
228             public void widgetSelected(SelectionEvent e) {
229                 updateStatus();
230             }
231         });
232         
233         fIncludeOptionalButton.addSelectionListener(new SelectionAdapter() {
234             public void widgetSelected(SelectionEvent e) {
235                 updateStatus();
236             }
237         });
238
239         fAddWorkspaceButton.addSelectionListener(new SelectionAdapter() {
240             public void widgetSelected(SelectionEvent e) {
241                 updateStatus();
242             }
243         });
244     }
245
246     private void handleWorkingSets() {
247         IWorkingSetManager workingSetManager = PlatformUI.getWorkbench().getWorkingSetManager();
248         IWorkingSetSelectionDialog dialog = workingSetManager.createWorkingSetSelectionDialog(getControl().getShell(), true);
249         if (dialog.open() == Window.OK) {
250             String JavaDoc[] ids = getPluginIDs(dialog.getSelection());
251             PluginModelManager manager = PDECore.getDefault().getModelManager();
252             for (int i = 0; i < ids.length; i++) {
253                 ModelEntry entry = manager.findEntry(ids[i]);
254                 if (entry != null) {
255                     IPluginModelBase model = entry.getActiveModel();
256                     if (!fPluginTreeViewer.getChecked(model)) {
257                         fPluginTreeViewer.setChecked(model, true);
258                         if (model.getUnderlyingResource() == null)
259                             fNumExternalChecked += 1;
260                         else
261                             fNumWorkspaceChecked += 1;
262                     }
263                 }
264             }
265             adjustGroupState();
266         }
267     }
268     
269     private String JavaDoc[] getPluginIDs(IWorkingSet[] workingSets) {
270         HashSet JavaDoc set = new HashSet JavaDoc();
271         for (int i = 0; i < workingSets.length; i++) {
272             IAdaptable[] elements = workingSets[i].getElements();
273             for (int j = 0; j < elements.length; j++) {
274                 Object JavaDoc element = elements[j];
275                 if (element instanceof PersistablePluginObject) {
276                     set.add(((PersistablePluginObject)element).getPluginID());
277                 } else {
278                     if (element instanceof IJavaProject)
279                         element = ((IJavaProject)element).getProject();
280                     if (element instanceof IProject) {
281                         IPluginModelBase model = PDECore.getDefault().getModelManager().findModel((IProject)element);
282                         if (model != null)
283                             set.add(model.getPluginBase().getId());
284                     }
285                 }
286             }
287         }
288         return (String JavaDoc[])set.toArray(new String JavaDoc[set.size()]);
289     }
290
291
292     protected void toggleGroups(boolean select) {
293         handleGroupStateChanged(fWorkspacePlugins, select);
294         handleGroupStateChanged(fExternalPlugins, select);
295     }
296
297     private void useDefaultChanged() {
298         adjustCustomControlEnableState(fUseListRadio.getSelection());
299         updateStatus();
300     }
301
302     private void adjustCustomControlEnableState(boolean enable) {
303         fPluginTreeViewer.getTree().setEnabled(enable);
304         fAddRequiredButton.setEnabled(enable);
305         fDefaultsButton.setEnabled(enable);
306         fWorkingSetButton.setEnabled(enable);
307         fSelectAllButton.setEnabled(enable);
308         fDeselectButton.setEnabled(enable);
309         fIncludeFragmentsButton.setEnabled(enable);
310         fIncludeOptionalButton.setEnabled(enable);
311         fAddWorkspaceButton.setEnabled(enable);
312     }
313
314     private void createPluginList(Composite parent) {
315         Composite composite = new Composite(parent, SWT.NONE);
316         GridLayout layout = new GridLayout();
317         layout.numColumns = 2;
318         composite.setLayout(layout);
319         GridData gd = new GridData(GridData.FILL_BOTH);
320         gd.horizontalIndent = 30;
321         composite.setLayoutData(gd);
322
323         createPluginViewer(composite);
324         createButtonContainer(composite);
325         
326         fIncludeFragmentsButton = new Button(composite, SWT.CHECK);
327         fIncludeFragmentsButton.setText(PDEUIMessages.AdvancedLauncherTab_includeFragments); //$NON-NLS-1$
328
gd = new GridData();
329         gd.horizontalSpan = 2;
330         fIncludeFragmentsButton.setLayoutData(gd);
331         
332         fIncludeOptionalButton = new Button(composite, SWT.CHECK);
333         fIncludeOptionalButton.setText(PDEUIMessages.AdvancedLauncherTab_includeOptional); //$NON-NLS-1$
334
gd = new GridData();
335         gd.horizontalSpan = 2;
336         fIncludeOptionalButton.setLayoutData(gd);
337         
338         fAddWorkspaceButton = new Button(composite, SWT.CHECK);
339         fAddWorkspaceButton.setText(PDEUIMessages.AdvancedLauncherTab_addNew); //$NON-NLS-1$
340
gd = new GridData();
341         gd.horizontalSpan = 2;
342         fAddWorkspaceButton.setLayoutData(gd);
343     }
344
345     private void computeSubset() {
346         Object JavaDoc[] checked = fPluginTreeViewer.getCheckedElements();
347         TreeMap JavaDoc map = new TreeMap JavaDoc();
348         for (int i = 0; i < checked.length; i++) {
349             if (checked[i] instanceof IPluginModelBase) {
350                 IPluginModelBase model = (IPluginModelBase) checked[i];
351                 addPluginAndDependencies(model, map);
352             }
353         }
354         if (!PDECore.getDefault().getModelManager().isOSGiRuntime()) {
355             addPluginAndDependencies(findPlugin("org.eclipse.core.runtime"), map); //$NON-NLS-1$
356
}
357
358         checked = map.values().toArray();
359
360         fPluginTreeViewer.setCheckedElements(map.values().toArray());
361         fNumExternalChecked = 0;
362         fNumWorkspaceChecked = 0;
363         for (int i = 0; i < checked.length; i++) {
364             if (checked[i] instanceof WorkspacePluginModelBase)
365                 fNumWorkspaceChecked += 1;
366             else
367                 fNumExternalChecked += 1;
368         }
369         adjustGroupState();
370     }
371
372     private void addPluginAndDependencies(IPluginModelBase model, TreeMap JavaDoc map) {
373         if (model == null)
374             return;
375
376         String JavaDoc id = model.getPluginBase().getId();
377         if (map.containsKey(id))
378             return;
379
380         map.put(id, model);
381
382         if (model instanceof IFragmentModel) {
383             IPluginModelBase parent =
384                 findPlugin(((IFragmentModel) model).getFragment().getPluginId());
385             addPluginAndDependencies(parent, map);
386         } else {
387             boolean addFragments = fIncludeFragmentsButton.getSelection()
388                         || ClasspathUtilCore.hasExtensibleAPI((IPlugin)model.getPluginBase());
389             if (!addFragments) {
390                 IPluginLibrary[] libs = model.getPluginBase().getLibraries();
391                 for (int i = 0; i < libs.length; i++) {
392                     if (ClasspathUtilCore.containsVariables(libs[i].getName())) {
393                         addFragments = true;
394                         break;
395                     }
396                 }
397             }
398             if (addFragments) {
399                 IFragmentModel[] fragments = findFragments(model.getPluginBase());
400                 for (int i = 0; i < fragments.length; i++) {
401                     addPluginAndDependencies(fragments[i], map);
402                 }
403             }
404         }
405
406         IPluginImport[] imports = model.getPluginBase().getImports();
407         for (int i = 0; i < imports.length; i++) {
408             if (imports[i].isOptional() && !fIncludeOptionalButton.getSelection())
409                 continue;
410             addPluginAndDependencies(findPlugin(imports[i].getId()), map);
411         }
412         
413     }
414
415     private IPluginModelBase findPlugin(String JavaDoc id) {
416         PluginModelManager manager = PDECore.getDefault().getModelManager();
417         ModelEntry entry = manager.findEntry(id);
418         if (entry != null) {
419             IPluginModelBase model = entry.getActiveModel();
420             if (fPluginTreeViewer.getChecked(model))
421                 return model;
422
423             model = entry.getExternalModel();
424             if (model != null && fPluginTreeViewer.getChecked(model)) {
425                 return model;
426             }
427             return entry.getActiveModel();
428         }
429         return null;
430     }
431
432     private IFragmentModel[] findFragments(IPluginBase plugin) {
433         ModelEntry[] entries = PDECore.getDefault().getModelManager().getEntries();
434         ArrayList JavaDoc result = new ArrayList JavaDoc();
435         for (int i = 0; i < entries.length; i++) {
436             ModelEntry entry = entries[i];
437             IPluginModelBase model = entry.getActiveModel();
438             if (model instanceof IFragmentModel) {
439                 String JavaDoc id = ((IFragmentModel) model).getFragment().getPluginId();
440                 if (id.equals(plugin.getId())) {
441                     if (fPluginTreeViewer.getChecked(model)) {
442                         result.add(model);
443                     } else {
444                         model = entry.getExternalModel();
445                         if (model != null && fPluginTreeViewer.getChecked(model)) {
446                             result.add(model);
447                         } else {
448                             result.add(entry.getActiveModel());
449                         }
450                     }
451                 }
452             }
453         }
454         return (IFragmentModel[]) result.toArray(new IFragmentModel[result.size()]);
455     }
456
457     private void adjustGroupState() {
458         fPluginTreeViewer.setChecked(fExternalPlugins, fNumExternalChecked > 0);
459         fPluginTreeViewer.setGrayed(
460             fExternalPlugins,
461             fNumExternalChecked > 0 && fNumExternalChecked < fExternalModels.length);
462         fPluginTreeViewer.setChecked(fWorkspacePlugins, fNumWorkspaceChecked > 0);
463         fPluginTreeViewer.setGrayed(
464             fWorkspacePlugins,
465             fNumWorkspaceChecked > 0 && fNumWorkspaceChecked < fWorkspaceModels.length);
466     }
467
468     private void createPluginViewer(Composite composite) {
469         fPluginTreeViewer = new CheckboxTreeViewer(composite, SWT.BORDER);
470         fPluginTreeViewer.setContentProvider(new PluginContentProvider());
471         fPluginTreeViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
472         fPluginTreeViewer.setAutoExpandLevel(2);
473         fPluginTreeViewer.addCheckStateListener(new ICheckStateListener() {
474             public void checkStateChanged(final CheckStateChangedEvent event) {
475                 Object JavaDoc element = event.getElement();
476                 if (element instanceof IPluginModelBase) {
477                     handleCheckStateChanged(
478                         (IPluginModelBase) element,
479                         event.getChecked());
480                 } else {
481                     handleGroupStateChanged(element, event.getChecked());
482                 }
483                 updateLaunchConfigurationDialog();
484             }
485         });
486         fPluginTreeViewer.setSorter(new ListUtil.PluginSorter() {
487             public int category(Object JavaDoc obj) {
488                 if (obj == fWorkspacePlugins)
489                     return -1;
490                 return 0;
491             }
492         });
493
494         fPluginTreeViewer.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
495
496         Image pluginsImage =
497             PDEPlugin.getDefault().getLabelProvider().get(
498                 PDEPluginImages.DESC_REQ_PLUGINS_OBJ);
499
500         fWorkspacePlugins =
501             new NamedElement(
502                 PDEUIMessages.AdvancedLauncherTab_workspacePlugins, //$NON-NLS-1$
503
pluginsImage);
504         fExternalPlugins =
505             new NamedElement(
506                 PDEUIMessages.AdvancedLauncherTab_externalPlugins, //$NON-NLS-1$
507
pluginsImage);
508     }
509
510     private void createButtonContainer(Composite parent) {
511         Composite composite = new Composite(parent, SWT.NONE);
512         GridLayout layout = new GridLayout();
513         layout.marginHeight = layout.marginWidth = 0;
514         composite.setLayout(layout);
515         composite.setLayoutData(new GridData(GridData.FILL_VERTICAL));
516
517         
518         fSelectAllButton = new Button(composite, SWT.PUSH);
519         fSelectAllButton.setText(PDEUIMessages.AdvancedLauncherTab_selectAll); //$NON-NLS-1$
520
fSelectAllButton.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.FILL_HORIZONTAL));
521         SWTUtil.setButtonDimensionHint(fSelectAllButton);
522         
523         fDeselectButton = new Button(composite, SWT.PUSH);
524         fDeselectButton.setText(PDEUIMessages.AdvancedLauncherTab_deselectAll); //$NON-NLS-1$
525
fDeselectButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
526         SWTUtil.setButtonDimensionHint(fDeselectButton);
527         
528         fWorkingSetButton = new Button(composite, SWT.PUSH);
529         fWorkingSetButton.setText(PDEUIMessages.AdvancedLauncherTab_workingSet); //$NON-NLS-1$
530
fWorkingSetButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
531         SWTUtil.setButtonDimensionHint(fWorkingSetButton);
532
533         fAddRequiredButton = new Button(composite, SWT.PUSH);
534         fAddRequiredButton.setText(PDEUIMessages.AdvancedLauncherTab_subset); //$NON-NLS-1$
535
fAddRequiredButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
536         SWTUtil.setButtonDimensionHint(fAddRequiredButton);
537
538         fDefaultsButton = new Button(composite, SWT.PUSH);
539         fDefaultsButton.setText(
540             PDEUIMessages.AdvancedLauncherTab_defaults); //$NON-NLS-1$
541
fDefaultsButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
542         SWTUtil.setButtonDimensionHint(fDefaultsButton);
543     }
544     
545     /*
546      * if the "automatic add" option is selected, then we save the ids of plugins
547      * that have been "deselected" by the user.
548      * When we initialize the tree, we first set the workspace plugins subtree to 'checked',
549      * then we check the plugins that had been deselected and saved in the config.
550      *
551      * If the "automatic add" option is not selected, then we save the ids of plugins
552      * that were "selected" by the user.
553      * When we initialize the tree, we first set the workspace plugins subtree to 'unchecked',
554      * then we check the plugins that had been selected and saved in the config.
555      */

556     private void initWorkspacePluginsState(ILaunchConfiguration config) throws CoreException {
557         boolean automaticAdd = config.getAttribute(AUTOMATIC_ADD, true);
558         fPluginTreeViewer.setSubtreeChecked(fWorkspacePlugins, automaticAdd);
559         fNumWorkspaceChecked = automaticAdd ? fWorkspaceModels.length : 0;
560         
561         TreeSet JavaDoc ids = LauncherUtils.parseWorkspacePluginIds(config);
562         for (int i = 0; i < fWorkspaceModels.length; i++) {
563             String JavaDoc id = fWorkspaceModels[i].getPluginBase().getId();
564             if (id == null)
565                 continue;
566             if (automaticAdd && ids.contains(id)) {
567                 if (fPluginTreeViewer.setChecked(fWorkspaceModels[i], false))
568                     fNumWorkspaceChecked -= 1;
569             } else if (!automaticAdd && ids.contains(id)) {
570                 if (fPluginTreeViewer.setChecked(fWorkspaceModels[i], true))
571                     fNumWorkspaceChecked += 1;
572             }
573         }
574
575         fPluginTreeViewer.setChecked(fWorkspacePlugins, fNumWorkspaceChecked > 0);
576         fPluginTreeViewer.setGrayed(
577             fWorkspacePlugins,
578             fNumWorkspaceChecked > 0 && fNumWorkspaceChecked < fWorkspaceModels.length);
579     }
580
581     private void initExternalPluginsState(ILaunchConfiguration config)
582         throws CoreException {
583         fNumExternalChecked = 0;
584
585         fPluginTreeViewer.setSubtreeChecked(fExternalPlugins, false);
586         TreeSet JavaDoc selected = LauncherUtils.parseExternalPluginIds(config);
587         for (int i = 0; i < fExternalModels.length; i++) {
588             if (selected.contains(fExternalModels[i].getPluginBase().getId())) {
589                 if (fPluginTreeViewer.setChecked(fExternalModels[i], true))
590                     fNumExternalChecked += 1;
591             }
592         }
593
594         fPluginTreeViewer.setChecked(fExternalPlugins, fNumExternalChecked > 0);
595         fPluginTreeViewer.setGrayed(
596             fExternalPlugins,
597             fNumExternalChecked > 0 && fNumExternalChecked < fExternalModels.length);
598     }
599
600     /*
601      * (non-Javadoc)
602      *
603      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
604      */

605     public void initializeFrom(ILaunchConfiguration config) {
606         initializeProductFrom(config);
607         try {
608             fUseDefaultRadio.setSelection(config.getAttribute(USE_DEFAULT, true));
609             if (fShowFeatures) {
610                 fUseFeaturesRadio.setSelection(config.getAttribute(USEFEATURES, false));
611                 fUseListRadio.setSelection(
612                     !fUseDefaultRadio.getSelection()
613                         && !fUseFeaturesRadio.getSelection());
614             } else {
615                 fUseListRadio.setSelection(!fUseDefaultRadio.getSelection());
616             }
617             fIncludeFragmentsButton.setSelection(config.getAttribute(INCLUDE_FRAGMENTS, false));
618             fIncludeOptionalButton.setSelection(config.getAttribute(INCLUDE_OPTIONAL, true));
619             fAddWorkspaceButton.setSelection(config.getAttribute(AUTOMATIC_ADD, true));
620
621             if (fPluginTreeViewer.getInput() == null) {
622                 fPluginTreeViewer.setUseHashlookup(true);
623                 fPluginTreeViewer.setInput(PDEPlugin.getDefault());
624                 fPluginTreeViewer.reveal(fWorkspacePlugins);
625             }
626
627             if (fUseDefaultRadio.getSelection()) {
628                 computeInitialCheckState();
629             } else if (fUseListRadio.getSelection()) {
630                 initWorkspacePluginsState(config);
631                 initExternalPluginsState(config);
632             }
633         } catch (CoreException e) {
634             PDEPlugin.logException(e);
635         }
636
637         adjustCustomControlEnableState(fUseListRadio.getSelection());
638         updateStatus();
639     }
640
641     private void computeInitialCheckState() {
642         TreeSet JavaDoc wtable = new TreeSet JavaDoc();
643         fNumWorkspaceChecked = 0;
644         fNumExternalChecked = 0;
645
646         for (int i = 0; i < fWorkspaceModels.length; i++) {
647             IPluginModelBase model = fWorkspaceModels[i];
648             fNumWorkspaceChecked += 1;
649             String JavaDoc id = model.getPluginBase().getId();
650             if (id != null)
651                 wtable.add(model.getPluginBase().getId());
652         }
653         fPluginTreeViewer.setSubtreeChecked(fWorkspacePlugins, true);
654
655         fNumExternalChecked = 0;
656         for (int i = 0; i < fExternalModels.length; i++) {
657             IPluginModelBase model = fExternalModels[i];
658             boolean masked = wtable.contains(model.getPluginBase().getId());
659             if (!masked && model.isEnabled()) {
660                 fPluginTreeViewer.setChecked(model, true);
661                 fNumExternalChecked += 1;
662             }
663         }
664         adjustGroupState();
665     }
666
667     private void handleCheckStateChanged(IPluginModelBase model, boolean checked) {
668         if (model.getUnderlyingResource() == null) {
669             if (checked) {
670                 fNumExternalChecked += 1;
671             } else {
672                 fNumExternalChecked -= 1;
673             }
674         } else {
675             if (checked) {
676                 fNumWorkspaceChecked += 1;
677             } else {
678                 fNumWorkspaceChecked -= 1;
679             }
680         }
681         adjustGroupState();
682     }
683
684     private void handleGroupStateChanged(Object JavaDoc group, boolean checked) {
685         fPluginTreeViewer.setSubtreeChecked(group, checked);
686         fPluginTreeViewer.setGrayed(group, false);
687
688         if (group == fWorkspacePlugins)
689             fNumWorkspaceChecked = checked ? fWorkspaceModels.length : 0;
690         else if (group == fExternalPlugins)
691             fNumExternalChecked = checked ? fExternalModels.length : 0;
692
693     }
694     
695     private void handleValidatePlugins() {
696         PluginValidationOperation op = new PluginValidationOperation(
697                 getPluginsToValidate(), fProductID, fApplicationID);
698         try {
699             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(op);
700         } catch (InvocationTargetException JavaDoc e) {
701         } catch (InterruptedException JavaDoc e) {
702         } finally {
703             if (op.hasErrors())
704                 new PluginStatusDialog(getControl().getShell(), op).open();
705             else
706                 MessageDialog.openInformation(getControl().getShell(), PDEUIMessages.AdvancedLauncherTab_pluginValidation, PDEUIMessages.AdvancedLauncherTab_noProblems); //$NON-NLS-1$ //$NON-NLS-2$
707
}
708     }
709     
710     private IPluginModelBase[] getPluginsToValidate() {
711         if (!fUseListRadio.getSelection())
712             return PDECore.getDefault().getModelManager().getPlugins();
713         
714         Map JavaDoc map = new HashMap JavaDoc();
715         Object JavaDoc[] objects = fPluginTreeViewer.getCheckedElements();
716         for (int i = 0; i < objects.length; i++) {
717             if (objects[i] instanceof IPluginModelBase) {
718                 IPluginModelBase model = (IPluginModelBase)objects[i];
719                 String JavaDoc id = model.getPluginBase().getId();
720                 if (id == null)
721                     continue;
722                 if (!map.containsKey(id) || model.getUnderlyingResource() != null)
723                     map.put(id, model);
724             }
725         }
726         return (IPluginModelBase[])map.values().toArray(new IPluginModelBase[map.size()]);
727     }
728
729     public void setDefaults(ILaunchConfigurationWorkingCopy config) {
730         if (fShowFeatures) {
731             config.setAttribute(USE_DEFAULT, true);
732             config.setAttribute(USEFEATURES, false);
733         } else {
734             config.setAttribute(USE_DEFAULT, true);
735         }
736         config.setAttribute(INCLUDE_FRAGMENTS, false);
737         config.setAttribute(INCLUDE_OPTIONAL, true);
738         config.setAttribute(AUTOMATIC_ADD, true);
739     }
740     
741     public void performApply(ILaunchConfigurationWorkingCopy config) {
742         config.setAttribute(USE_DEFAULT, fUseDefaultRadio.getSelection());
743         if (fShowFeatures)
744             config.setAttribute(USEFEATURES, fUseFeaturesRadio.getSelection());
745         config.setAttribute(INCLUDE_FRAGMENTS, fIncludeFragmentsButton.getSelection());
746         config.setAttribute(INCLUDE_OPTIONAL, fIncludeOptionalButton.getSelection());
747         config.setAttribute(AUTOMATIC_ADD, fAddWorkspaceButton.getSelection());
748         if (fUseListRadio.getSelection()) {
749             // store deselected projects
750
StringBuffer JavaDoc wbuf = new StringBuffer JavaDoc();
751             for (int i = 0; i < fWorkspaceModels.length; i++) {
752                 IPluginModelBase model = fWorkspaceModels[i];
753                 // if "automatic add" option is selected, save "deselected" workspace plugins
754
// Otherwise, save "selected" workspace plugins
755
if (fPluginTreeViewer.getChecked(model) != fAddWorkspaceButton.getSelection())
756                     wbuf.append(model.getPluginBase().getId() + File.pathSeparatorChar);
757             }
758             config.setAttribute(WSPROJECT, wbuf.toString());
759
760             // Store selected external models
761
StringBuffer JavaDoc exbuf = new StringBuffer JavaDoc();
762             Object JavaDoc[] checked = fPluginTreeViewer.getCheckedElements();
763             for (int i = 0; i < checked.length; i++) {
764                 if (checked[i] instanceof ExternalPluginModelBase) {
765                     IPluginModelBase model = (IPluginModelBase) checked[i];
766                     exbuf.append(model.getPluginBase().getId() + File.pathSeparatorChar);
767                 }
768             }
769             config.setAttribute(EXTPLUGINS, exbuf.toString());
770         } else {
771             config.setAttribute(WSPROJECT, (String JavaDoc) null);
772             config.setAttribute(EXTPLUGINS, (String JavaDoc) null);
773         }
774     }
775
776     private void updateStatus() {
777         updateStatus(validate());
778     }
779
780     private IStatus validate() {
781         if (fShowFeatures && fUseFeaturesRadio.getSelection()) {
782             IPath workspacePath = PDEPlugin.getWorkspace().getRoot().getLocation();
783             IPath featurePath = workspacePath.removeLastSegments(1).append("features"); //$NON-NLS-1$
784
if (!workspacePath.lastSegment().equalsIgnoreCase("plugins") //$NON-NLS-1$
785
|| !featurePath.toFile().exists())
786                 return createStatus(
787                     IStatus.ERROR,
788                     PDEUIMessages.AdvancedLauncherTab_error_featureSetup); //$NON-NLS-1$
789
}
790         return createStatus(IStatus.OK, ""); //$NON-NLS-1$
791
}
792
793     public String JavaDoc getName() {
794         return PDEUIMessages.AdvancedLauncherTab_name; //$NON-NLS-1$
795
}
796
797     public Image getImage() {
798         return fImage;
799     }
800     
801     /* (non-Javadoc)
802      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#activated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
803      */

804     public void activated(ILaunchConfigurationWorkingCopy config) {
805         initializeProductFrom(config);
806     }
807
808     /**
809      * @param config
810      */

811     private void initializeProductFrom(ILaunchConfiguration config) {
812         try {
813             if (config.getAttribute(USE_PRODUCT, false)) {
814                 fProductID = config.getAttribute(PRODUCT, (String JavaDoc)null);
815                 fApplicationID = null;
816             } else {
817                 String JavaDoc appToRun = config.getAttribute(APPLICATION, LauncherUtils.getDefaultApplicationName());
818                 if (fUseFeaturesRadio != null)
819                     fApplicationID = appToRun;
820                 else {
821                     if(JUnitLaunchConfiguration.CORE_APPLICATION.equals(appToRun)){
822                         fApplicationID = null;
823                     } else {
824                         fApplicationID = config.getAttribute(APP_TO_TEST, LauncherUtils.getDefaultApplicationName());
825                     }
826                 }
827                 fProductID = null;
828             }
829         } catch (CoreException e) {
830         }
831     }
832     
833     
834 }
835
Popular Tags