001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.launcher;
011:
012: import java.util.TreeSet;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.debug.core.ILaunchConfiguration;
016: import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
017: import org.eclipse.pde.core.plugin.IPluginAttribute;
018: import org.eclipse.pde.core.plugin.IPluginElement;
019: import org.eclipse.pde.core.plugin.IPluginExtension;
020: import org.eclipse.pde.core.plugin.IPluginModelBase;
021: import org.eclipse.pde.core.plugin.PluginRegistry;
022: import org.eclipse.pde.core.plugin.TargetPlatform;
023: import org.eclipse.pde.internal.core.util.IdUtil;
024: import org.eclipse.pde.internal.ui.IPDEUIConstants;
025: import org.eclipse.pde.ui.launcher.AbstractLauncherTab;
026: import org.eclipse.pde.ui.launcher.EclipseLaunchShortcut;
027: import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
028:
029: public class PluginBlock extends AbstractPluginBlock {
030:
031: protected ILaunchConfiguration fLaunchConfig;
032:
033: public PluginBlock(AbstractLauncherTab tab) {
034: super (tab);
035: }
036:
037: public void initializeFrom(ILaunchConfiguration config,
038: boolean customSelection) throws CoreException {
039: super .initializeFrom(config);
040: if (customSelection) {
041: initWorkspacePluginsState(config);
042: initExternalPluginsState(config);
043: } else {
044: handleRestoreDefaults();
045: }
046: enableViewer(customSelection);
047: updateCounter();
048: fTab.updateLaunchConfigurationDialog();
049: fLaunchConfig = config;
050: }
051:
052: /*
053: * if the "automatic add" option is selected, then we save the ids of plugins
054: * that have been "deselected" by the user.
055: * When we initialize the tree, we first set the workspace plugins subtree to 'checked',
056: * then we check the plugins that had been deselected and saved in the config.
057: *
058: * If the "automatic add" option is not selected, then we save the ids of plugins
059: * that were "selected" by the user.
060: * When we initialize the tree, we first set the workspace plugins subtree to 'unchecked',
061: * then we check the plugins that had been selected and saved in the config.
062: */
063: protected void initWorkspacePluginsState(
064: ILaunchConfiguration configuration) throws CoreException {
065: boolean automaticAdd = configuration.getAttribute(
066: IPDELauncherConstants.AUTOMATIC_ADD, true);
067: fPluginTreeViewer.setSubtreeChecked(fWorkspacePlugins,
068: automaticAdd);
069: fNumWorkspaceChecked = automaticAdd ? fWorkspaceModels.length
070: : 0;
071:
072: String attribute = automaticAdd ? IPDELauncherConstants.DESELECTED_WORKSPACE_PLUGINS
073: : IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS;
074: TreeSet ids = LaunchPluginValidator.parsePlugins(configuration,
075: attribute);
076: for (int i = 0; i < fWorkspaceModels.length; i++) {
077: String id = fWorkspaceModels[i].getPluginBase().getId();
078: if (id == null)
079: continue;
080: if (automaticAdd && ids.contains(id)) {
081: if (fPluginTreeViewer.setChecked(fWorkspaceModels[i],
082: false))
083: fNumWorkspaceChecked -= 1;
084: } else if (!automaticAdd && ids.contains(id)) {
085: if (fPluginTreeViewer.setChecked(fWorkspaceModels[i],
086: true))
087: fNumWorkspaceChecked += 1;
088: }
089: }
090:
091: fPluginTreeViewer.setChecked(fWorkspacePlugins,
092: fNumWorkspaceChecked > 0);
093: fPluginTreeViewer
094: .setGrayed(
095: fWorkspacePlugins,
096: fNumWorkspaceChecked > 0
097: && fNumWorkspaceChecked < fWorkspaceModels.length);
098: }
099:
100: protected void initExternalPluginsState(ILaunchConfiguration config)
101: throws CoreException {
102: fNumExternalChecked = 0;
103:
104: fPluginTreeViewer.setSubtreeChecked(fExternalPlugins, false);
105: TreeSet selected = LaunchPluginValidator.parsePlugins(config,
106: IPDELauncherConstants.SELECTED_TARGET_PLUGINS);
107: for (int i = 0; i < fExternalModels.length; i++) {
108: if (selected.contains(fExternalModels[i].getPluginBase()
109: .getId())) {
110: if (fPluginTreeViewer.setChecked(fExternalModels[i],
111: true))
112: fNumExternalChecked += 1;
113: }
114: }
115:
116: fPluginTreeViewer.setChecked(fExternalPlugins,
117: fNumExternalChecked > 0);
118: fPluginTreeViewer
119: .setGrayed(fExternalPlugins, fNumExternalChecked > 0
120: && fNumExternalChecked < fExternalModels.length);
121: }
122:
123: protected void savePluginState(
124: ILaunchConfigurationWorkingCopy config) {
125: if (isEnabled()) {
126: // store deselected projects
127: StringBuffer wbuf = new StringBuffer();
128: for (int i = 0; i < fWorkspaceModels.length; i++) {
129: IPluginModelBase model = fWorkspaceModels[i];
130: // if "automatic add" option is selected, save "deselected" workspace plugins
131: // Otherwise, save "selected" workspace plugins
132: if (fPluginTreeViewer.getChecked(model) != fAddWorkspaceButton
133: .getSelection()) {
134: if (wbuf.length() > 0)
135: wbuf.append(","); //$NON-NLS-1$
136: wbuf.append(model.getPluginBase().getId());
137: }
138: }
139:
140: String value = wbuf.length() > 0 ? wbuf.toString() : null;
141: if (fAddWorkspaceButton.getSelection()) {
142: config
143: .setAttribute(
144: IPDELauncherConstants.DESELECTED_WORKSPACE_PLUGINS,
145: value);
146: config
147: .setAttribute(
148: IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS,
149: (String) null);
150: } else {
151: config
152: .setAttribute(
153: IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS,
154: value);
155: }
156: // Store selected external models
157: StringBuffer exbuf = new StringBuffer();
158: Object[] checked = fPluginTreeViewer.getCheckedElements();
159: for (int i = 0; i < checked.length; i++) {
160: if (checked[i] instanceof IPluginModelBase) {
161: IPluginModelBase model = (IPluginModelBase) checked[i];
162: if (model.getUnderlyingResource() == null) {
163: if (exbuf.length() > 0)
164: exbuf.append(","); //$NON-NLS-1$
165: exbuf.append(model.getPluginBase().getId());
166: }
167: }
168: }
169: value = exbuf.length() > 0 ? exbuf.toString() : null;
170: config.setAttribute(
171: IPDELauncherConstants.SELECTED_TARGET_PLUGINS,
172: value);
173: } else {
174: config.setAttribute(
175: IPDELauncherConstants.SELECTED_TARGET_PLUGINS,
176: (String) null);
177: config.setAttribute(
178: IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS,
179: (String) null);
180: config.setAttribute(
181: IPDELauncherConstants.DESELECTED_WORKSPACE_PLUGINS,
182: (String) null);
183: }
184: }
185:
186: protected void computeSubset() {
187: validateExtensions();
188: super .computeSubset();
189: }
190:
191: private void validateExtensions() {
192: try {
193: if (fLaunchConfig.getAttribute(
194: IPDELauncherConstants.USE_PRODUCT, true)) {
195: String product = fLaunchConfig.getAttribute(
196: IPDELauncherConstants.PRODUCT, (String) null);
197: if (product != null) {
198: validateLaunchId(product);
199: String application = getApplication(product);
200: if (application != null)
201: validateLaunchId(application);
202: }
203: } else {
204: String configType = fLaunchConfig.getType()
205: .getIdentifier();
206: String attribute = configType
207: .equals(EclipseLaunchShortcut.CONFIGURATION_TYPE) ? IPDELauncherConstants.APPLICATION
208: : IPDELauncherConstants.APP_TO_TEST;
209: String application = fLaunchConfig.getAttribute(
210: attribute, TargetPlatform
211: .getDefaultApplication());
212: if (!IPDEUIConstants.CORE_TEST_APPLICATION
213: .equals(application))
214: validateLaunchId(application);
215: }
216: } catch (CoreException e) {
217: }
218: }
219:
220: private void validateLaunchId(String launchId) {
221: if (launchId != null) {
222: int index = launchId.lastIndexOf('.');
223: if (index > 0) {
224: String pluginId = launchId.substring(0, index);
225: // see if launcher plugin is already included
226: IPluginModelBase base = findPlugin(pluginId);
227: if (base == null) {
228: base = PluginRegistry.findModel(pluginId);
229: if (base != null) {
230: fPluginTreeViewer.setChecked(base, true);
231: }
232: }
233: }
234: }
235: }
236:
237: private String getApplication(String product) {
238: String bundleID = product
239: .substring(0, product.lastIndexOf('.'));
240: IPluginModelBase model = findPlugin(bundleID);
241:
242: if (model != null) {
243: IPluginExtension[] extensions = model.getPluginBase()
244: .getExtensions();
245: for (int i = 0; i < extensions.length; i++) {
246: IPluginExtension ext = extensions[i];
247: String point = ext.getPoint();
248: if ("org.eclipse.core.runtime.products".equals(point) //$NON-NLS-1$
249: && product.equals(IdUtil.getFullId(ext))) {
250: if (ext.getChildCount() == 1) {
251: IPluginElement prod = (IPluginElement) ext
252: .getChildren()[0];
253: if (prod.getName().equals("product")) { //$NON-NLS-1$
254: IPluginAttribute attr = prod
255: .getAttribute("application"); //$NON-NLS-1$
256: return attr != null ? attr.getValue()
257: : null;
258: }
259: }
260: }
261: }
262: }
263: return null;
264: }
265:
266: protected LaunchValidationOperation createValidationOperation() {
267: return new EclipsePluginValidationOperation(fLaunchConfig);
268: }
269: }
|