001: /*******************************************************************************
002: * Copyright (c) 2000, 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.editor.feature;
011:
012: import java.lang.reflect.InvocationTargetException;
013:
014: import org.eclipse.core.resources.IFile;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IProgressMonitor;
017: import org.eclipse.jface.dialogs.Dialog;
018: import org.eclipse.jface.dialogs.IDialogSettings;
019: import org.eclipse.jface.operation.IRunnableWithProgress;
020: import org.eclipse.jface.text.BadLocationException;
021: import org.eclipse.jface.wizard.WizardPage;
022: import org.eclipse.pde.core.IBaseModel;
023: import org.eclipse.pde.core.plugin.IPluginModelBase;
024: import org.eclipse.pde.core.plugin.PluginRegistry;
025: import org.eclipse.pde.internal.core.feature.WorkspaceFeatureModel;
026: import org.eclipse.pde.internal.core.ibundle.IBundleModel;
027: import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
028: import org.eclipse.pde.internal.core.ifeature.IFeature;
029: import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin;
030: import org.eclipse.pde.internal.ui.IHelpContextIds;
031: import org.eclipse.pde.internal.ui.PDEPlugin;
032: import org.eclipse.pde.internal.ui.PDEUIMessages;
033: import org.eclipse.pde.internal.ui.util.ModelModification;
034: import org.eclipse.pde.internal.ui.util.PDEModelUtility;
035: import org.eclipse.swt.SWT;
036: import org.eclipse.swt.layout.GridData;
037: import org.eclipse.swt.layout.GridLayout;
038: import org.eclipse.swt.widgets.Button;
039: import org.eclipse.swt.widgets.Composite;
040: import org.eclipse.swt.widgets.Group;
041: import org.eclipse.ui.PlatformUI;
042: import org.eclipse.ui.actions.WorkspaceModifyOperation;
043: import org.osgi.framework.Constants;
044:
045: public class SynchronizeVersionsWizardPage extends WizardPage {
046: public static final int USE_PLUGINS_AT_BUILD = 0;
047: public static final int USE_FEATURE = 1;
048: public static final int USE_PLUGINS = 2;
049: private FeatureEditor fFeatureEditor;
050: private Button fUsePluginsAtBuildButton;
051: private Button fUseComponentButton;
052: private Button fUsePluginsButton;
053:
054: private static final String PREFIX = PDEPlugin.getPluginId()
055: + ".synchronizeVersions."; //$NON-NLS-1$
056: private static final String PROP_SYNCHRO_MODE = PREFIX + "mode"; //$NON-NLS-1$
057:
058: public SynchronizeVersionsWizardPage(FeatureEditor featureEditor) {
059: super ("featureJar"); //$NON-NLS-1$
060: setTitle(PDEUIMessages.VersionSyncWizard_title);
061: setDescription(PDEUIMessages.VersionSyncWizard_desc);
062: this .fFeatureEditor = featureEditor;
063: }
064:
065: public void createControl(Composite parent) {
066: Composite container = new Composite(parent, SWT.NULL);
067: GridLayout layout = new GridLayout();
068: container.setLayout(layout);
069:
070: Group group = new Group(container, SWT.SHADOW_ETCHED_IN);
071: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
072: layout = new GridLayout();
073: group.setLayout(layout);
074: group.setLayoutData(gd);
075: group.setText(PDEUIMessages.VersionSyncWizard_group);
076:
077: fUsePluginsAtBuildButton = new Button(group, SWT.RADIO);
078: fUsePluginsAtBuildButton
079: .setText(PDEUIMessages.VersionSyncWizard_usePluginsAtBuild);
080: gd = new GridData(GridData.FILL_HORIZONTAL);
081: fUsePluginsAtBuildButton.setLayoutData(gd);
082:
083: fUsePluginsButton = new Button(group, SWT.RADIO);
084: fUsePluginsButton
085: .setText(PDEUIMessages.VersionSyncWizard_usePlugins);
086: gd = new GridData(GridData.FILL_HORIZONTAL);
087: fUsePluginsButton.setLayoutData(gd);
088:
089: fUseComponentButton = new Button(group, SWT.RADIO);
090: fUseComponentButton
091: .setText(PDEUIMessages.VersionSyncWizard_useComponent);
092: gd = new GridData(GridData.FILL_HORIZONTAL);
093: fUseComponentButton.setLayoutData(gd);
094:
095: setControl(container);
096: Dialog.applyDialogFont(container);
097: loadSettings();
098: PlatformUI.getWorkbench().getHelpSystem().setHelp(container,
099: IHelpContextIds.FEATURE_SYNCHRONIZE_VERSIONS);
100: }
101:
102: private IPluginModelBase findModel(String id) {
103: IPluginModelBase[] models = PluginRegistry.getWorkspaceModels();
104: for (int i = 0; i < models.length; i++) {
105: IPluginModelBase modelBase = models[i];
106: if (modelBase != null
107: && id.equals(modelBase.getPluginBase().getId()))
108: return modelBase;
109: }
110: return null;
111: }
112:
113: public boolean finish() {
114: final int mode = saveSettings();
115:
116: IRunnableWithProgress operation = new WorkspaceModifyOperation() {
117: public void execute(IProgressMonitor monitor) {
118: try {
119: runOperation(mode, monitor);
120: } catch (CoreException e) {
121: PDEPlugin.logException(e);
122: } catch (BadLocationException e) {
123: PDEPlugin.logException(e);
124: } finally {
125: monitor.done();
126: }
127: }
128: };
129: try {
130: PlatformUI.getWorkbench().getProgressService().runInUI(
131: PDEPlugin.getActiveWorkbenchWindow(), operation,
132: PDEPlugin.getWorkspace().getRoot());
133: } catch (InvocationTargetException e) {
134: PDEPlugin.logException(e);
135: return false;
136: } catch (InterruptedException e) {
137: return false;
138: }
139: return true;
140: }
141:
142: /**
143: * Forces a version into plugin/fragment .xml
144: *
145: * @param targetVersion
146: * @param modelBase
147: * @throws CoreException
148: */
149: private void forceVersion(final String targetVersion,
150: IPluginModelBase modelBase, IProgressMonitor monitor) {
151: IFile file = (IFile) modelBase.getUnderlyingResource();
152: if (file == null)
153: return;
154:
155: PDEModelUtility.modifyModel(new ModelModification(file) {
156: protected void modifyModel(IBaseModel model,
157: IProgressMonitor monitor) throws CoreException {
158: if (model instanceof IBundlePluginModelBase) {
159: modifyVersion(((IBundlePluginModelBase) model)
160: .getBundleModel(), targetVersion);
161: } else if (model instanceof IPluginModelBase) {
162: modifyVersion((IPluginModelBase) model,
163: targetVersion);
164: }
165: }
166: }, monitor);
167: }
168:
169: private void modifyVersion(IBundleModel model, String targetVersion) {
170: model.getBundle().setHeader(Constants.BUNDLE_VERSION,
171: targetVersion);
172: }
173:
174: private void modifyVersion(IPluginModelBase model, String version)
175: throws CoreException {
176: model.getPluginBase().setVersion(version);
177: }
178:
179: private void loadSettings() {
180: IDialogSettings settings = getDialogSettings();
181: if (settings.get(PROP_SYNCHRO_MODE) != null) {
182: int mode = settings.getInt(PROP_SYNCHRO_MODE);
183: switch (mode) {
184: case USE_FEATURE:
185: fUseComponentButton.setSelection(true);
186: break;
187: case USE_PLUGINS:
188: fUsePluginsButton.setSelection(true);
189: break;
190: default: // USE_PLUGINS_AT_BUILD
191: fUsePluginsAtBuildButton.setSelection(true);
192: break;
193: }
194: } else
195: fUsePluginsAtBuildButton.setSelection(true);
196: }
197:
198: private void runOperation(int mode, IProgressMonitor monitor)
199: throws CoreException, BadLocationException {
200: WorkspaceFeatureModel model = (WorkspaceFeatureModel) fFeatureEditor
201: .getAggregateModel();
202: IFeature feature = model.getFeature();
203: IFeaturePlugin[] plugins = feature.getPlugins();
204: int size = plugins.length;
205: monitor.beginTask(
206: PDEUIMessages.VersionSyncWizard_synchronizing, size);
207: for (int i = 0; i < plugins.length; i++)
208: synchronizeVersion(mode, feature.getVersion(), plugins[i],
209: monitor);
210: }
211:
212: private int saveSettings() {
213: IDialogSettings settings = getDialogSettings();
214: int mode = USE_PLUGINS_AT_BUILD;
215: if (fUseComponentButton.getSelection())
216: mode = USE_FEATURE;
217: else if (fUsePluginsButton.getSelection())
218: mode = USE_PLUGINS;
219: settings.put(PROP_SYNCHRO_MODE, mode);
220: return mode;
221: }
222:
223: private void synchronizeVersion(int mode, String featureVersion,
224: IFeaturePlugin ref, IProgressMonitor monitor)
225: throws CoreException, BadLocationException {
226: String id = ref.getId();
227:
228: if (mode == USE_PLUGINS_AT_BUILD) {
229: if (!"0.0.0".equals(ref.getVersion())) //$NON-NLS-1$
230: ref.setVersion("0.0.0"); //$NON-NLS-1$
231: } else if (mode == USE_PLUGINS) {
232: IPluginModelBase modelBase = PluginRegistry.findModel(id);
233: if (modelBase == null)
234: return;
235: String baseVersion = modelBase.getPluginBase().getVersion();
236: if (!ref.getVersion().equals(baseVersion))
237: ref.setVersion(baseVersion);
238: } else /* mode == USE_FEATURE */{
239: IPluginModelBase modelBase = findModel(id);
240: if (modelBase == null)
241: return;
242: ref.setVersion(featureVersion);
243: String baseVersion = modelBase.getPluginBase().getVersion();
244: if (!featureVersion.equals(baseVersion))
245: forceVersion(featureVersion, modelBase, monitor);
246: }
247: monitor.worked(1);
248: }
249: }
|