KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.launcher;
12
13 import java.util.TreeSet JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.debug.core.ILaunchConfiguration;
17 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
18 import org.eclipse.pde.core.plugin.IPluginAttribute;
19 import org.eclipse.pde.core.plugin.IPluginElement;
20 import org.eclipse.pde.core.plugin.IPluginExtension;
21 import org.eclipse.pde.core.plugin.IPluginModelBase;
22 import org.eclipse.pde.core.plugin.PluginRegistry;
23 import org.eclipse.pde.core.plugin.TargetPlatform;
24 import org.eclipse.pde.internal.core.util.IdUtil;
25 import org.eclipse.pde.internal.ui.IPDEUIConstants;
26 import org.eclipse.pde.ui.launcher.AbstractLauncherTab;
27 import org.eclipse.pde.ui.launcher.EclipseLaunchShortcut;
28 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
29
30 public class PluginBlock extends AbstractPluginBlock {
31     
32     protected ILaunchConfiguration fLaunchConfig;
33
34     public PluginBlock(AbstractLauncherTab tab) {
35         super(tab);
36     }
37     
38     public void initializeFrom(ILaunchConfiguration config, boolean customSelection) throws CoreException {
39         super.initializeFrom(config);
40         if (customSelection) {
41             initWorkspacePluginsState(config);
42             initExternalPluginsState(config);
43         } else {
44             handleRestoreDefaults();
45         }
46         enableViewer(customSelection);
47         updateCounter();
48         fTab.updateLaunchConfigurationDialog();
49         fLaunchConfig = config;
50     }
51     
52     /*
53      * if the "automatic add" option is selected, then we save the ids of plugins
54      * that have been "deselected" by the user.
55      * When we initialize the tree, we first set the workspace plugins subtree to 'checked',
56      * then we check the plugins that had been deselected and saved in the config.
57      *
58      * If the "automatic add" option is not selected, then we save the ids of plugins
59      * that were "selected" by the user.
60      * When we initialize the tree, we first set the workspace plugins subtree to 'unchecked',
61      * then we check the plugins that had been selected and saved in the config.
62      */

63     protected void initWorkspacePluginsState(ILaunchConfiguration configuration) throws CoreException {
64         boolean automaticAdd = configuration.getAttribute(IPDELauncherConstants.AUTOMATIC_ADD, true);
65         fPluginTreeViewer.setSubtreeChecked(fWorkspacePlugins, automaticAdd);
66         fNumWorkspaceChecked = automaticAdd ? fWorkspaceModels.length : 0;
67         
68         String JavaDoc attribute = automaticAdd
69                             ? IPDELauncherConstants.DESELECTED_WORKSPACE_PLUGINS
70                             : IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS;
71         TreeSet JavaDoc ids = LaunchPluginValidator.parsePlugins(configuration, attribute);
72         for (int i = 0; i < fWorkspaceModels.length; i++) {
73             String JavaDoc id = fWorkspaceModels[i].getPluginBase().getId();
74             if (id == null)
75                 continue;
76             if (automaticAdd && ids.contains(id)) {
77                 if (fPluginTreeViewer.setChecked(fWorkspaceModels[i], false))
78                     fNumWorkspaceChecked -= 1;
79             } else if (!automaticAdd && ids.contains(id)) {
80                 if (fPluginTreeViewer.setChecked(fWorkspaceModels[i], true))
81                     fNumWorkspaceChecked += 1;
82             }
83         }
84
85         fPluginTreeViewer.setChecked(fWorkspacePlugins, fNumWorkspaceChecked > 0);
86         fPluginTreeViewer.setGrayed(
87             fWorkspacePlugins,
88             fNumWorkspaceChecked > 0 && fNumWorkspaceChecked < fWorkspaceModels.length);
89     }
90     
91     protected void initExternalPluginsState(ILaunchConfiguration config)
92             throws CoreException {
93         fNumExternalChecked = 0;
94
95         fPluginTreeViewer.setSubtreeChecked(fExternalPlugins, false);
96         TreeSet JavaDoc selected = LaunchPluginValidator.parsePlugins(config,
97                                 IPDELauncherConstants.SELECTED_TARGET_PLUGINS);
98         for (int i = 0; i < fExternalModels.length; i++) {
99             if (selected.contains(fExternalModels[i].getPluginBase().getId())) {
100                 if (fPluginTreeViewer.setChecked(fExternalModels[i], true))
101                     fNumExternalChecked += 1;
102             }
103         }
104
105         fPluginTreeViewer.setChecked(fExternalPlugins, fNumExternalChecked > 0);
106         fPluginTreeViewer.setGrayed(fExternalPlugins, fNumExternalChecked > 0
107                 && fNumExternalChecked < fExternalModels.length);
108     }
109
110     protected void savePluginState(ILaunchConfigurationWorkingCopy config) {
111         if (isEnabled()) {
112             // store deselected projects
113
StringBuffer JavaDoc wbuf = new StringBuffer JavaDoc();
114             for (int i = 0; i < fWorkspaceModels.length; i++) {
115                 IPluginModelBase model = fWorkspaceModels[i];
116                 // if "automatic add" option is selected, save "deselected" workspace plugins
117
// Otherwise, save "selected" workspace plugins
118
if (fPluginTreeViewer.getChecked(model) != fAddWorkspaceButton.getSelection()) {
119                     if (wbuf.length() > 0)
120                         wbuf.append(","); //$NON-NLS-1$
121
wbuf.append(model.getPluginBase().getId());
122                 }
123             }
124             
125             String JavaDoc value = wbuf.length() > 0 ? wbuf.toString() : null;
126             if (fAddWorkspaceButton.getSelection()) {
127                 config.setAttribute(IPDELauncherConstants.DESELECTED_WORKSPACE_PLUGINS, value);
128                 config.setAttribute(IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS, (String JavaDoc)null);
129             } else {
130                 config.setAttribute(IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS, value);
131             }
132             // Store selected external models
133
StringBuffer JavaDoc exbuf = new StringBuffer JavaDoc();
134             Object JavaDoc[] checked = fPluginTreeViewer.getCheckedElements();
135             for (int i = 0; i < checked.length; i++) {
136                 if (checked[i] instanceof IPluginModelBase) {
137                     IPluginModelBase model = (IPluginModelBase) checked[i];
138                     if (model.getUnderlyingResource() == null) {
139                         if (exbuf.length() > 0)
140                             exbuf.append(","); //$NON-NLS-1$
141
exbuf.append(model.getPluginBase().getId());
142                     }
143                 }
144             }
145             value = exbuf.length() > 0 ? exbuf.toString() : null;
146             config.setAttribute(IPDELauncherConstants.SELECTED_TARGET_PLUGINS, value);
147         } else {
148             config.setAttribute(IPDELauncherConstants.SELECTED_TARGET_PLUGINS, (String JavaDoc) null);
149             config.setAttribute(IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS, (String JavaDoc) null);
150             config.setAttribute(IPDELauncherConstants.DESELECTED_WORKSPACE_PLUGINS, (String JavaDoc)null);
151         }
152     }
153
154     protected void computeSubset() {
155         validateExtensions();
156         super.computeSubset();
157     }
158     
159     private void validateExtensions() {
160         try {
161             if (fLaunchConfig.getAttribute(IPDELauncherConstants.USE_PRODUCT, true)) {
162                 String JavaDoc product = fLaunchConfig.getAttribute(IPDELauncherConstants.PRODUCT, (String JavaDoc)null);
163                 if (product != null) {
164                     validateLaunchId(product);
165                     String JavaDoc application = getApplication(product);
166                     if (application != null)
167                         validateLaunchId(application);
168                 }
169             }
170             else {
171                 String JavaDoc configType = fLaunchConfig.getType().getIdentifier();
172                 String JavaDoc attribute = configType.equals(EclipseLaunchShortcut.CONFIGURATION_TYPE)
173                 ? IPDELauncherConstants.APPLICATION : IPDELauncherConstants.APP_TO_TEST;
174                 String JavaDoc application = fLaunchConfig.getAttribute(attribute, TargetPlatform.getDefaultApplication());
175                 if (!IPDEUIConstants.CORE_TEST_APPLICATION.equals(application))
176                     validateLaunchId(application);
177             }
178         } catch (CoreException e) {
179         }
180     }
181     
182     private void validateLaunchId(String JavaDoc launchId) {
183         if (launchId != null) {
184             int index = launchId.lastIndexOf('.');
185             if (index > 0) {
186                 String JavaDoc pluginId = launchId.substring(0, index);
187                 // see if launcher plugin is already included
188
IPluginModelBase base = findPlugin(pluginId);
189                 if (base == null) {
190                     base = PluginRegistry.findModel(pluginId);
191                     if (base != null) {
192                         fPluginTreeViewer.setChecked(base, true);
193                     }
194                 }
195             }
196         }
197     }
198     
199     private String JavaDoc getApplication(String JavaDoc product) {
200         String JavaDoc bundleID = product.substring(0, product.lastIndexOf('.'));
201         IPluginModelBase model = findPlugin(bundleID);
202         
203         if (model != null) {
204             IPluginExtension[] extensions = model.getPluginBase().getExtensions();
205             for (int i = 0; i < extensions.length; i++) {
206                 IPluginExtension ext = extensions[i];
207                 String JavaDoc point = ext.getPoint();
208                 if ("org.eclipse.core.runtime.products".equals(point) //$NON-NLS-1$
209
&& product.equals(IdUtil.getFullId(ext))) {
210                     if (ext.getChildCount() == 1) {
211                         IPluginElement prod = (IPluginElement)ext.getChildren()[0];
212                         if (prod.getName().equals("product")) { //$NON-NLS-1$
213
IPluginAttribute attr = prod.getAttribute("application"); //$NON-NLS-1$
214
return attr != null ? attr.getValue() : null;
215                         }
216                     }
217                 }
218             }
219         }
220         return null;
221     }
222     
223     protected LaunchValidationOperation createValidationOperation() {
224         return new EclipsePluginValidationOperation(fLaunchConfig);
225     }
226 }
227
Popular Tags