KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > feature > SynchronizeVersionsWizardPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.editor.feature;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.dialogs.IDialogSettings;
20 import org.eclipse.jface.operation.IRunnableWithProgress;
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.wizard.WizardPage;
23 import org.eclipse.pde.core.IBaseModel;
24 import org.eclipse.pde.core.plugin.IPluginModelBase;
25 import org.eclipse.pde.core.plugin.PluginRegistry;
26 import org.eclipse.pde.internal.core.feature.WorkspaceFeatureModel;
27 import org.eclipse.pde.internal.core.ibundle.IBundleModel;
28 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
29 import org.eclipse.pde.internal.core.ifeature.IFeature;
30 import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin;
31 import org.eclipse.pde.internal.ui.IHelpContextIds;
32 import org.eclipse.pde.internal.ui.PDEPlugin;
33 import org.eclipse.pde.internal.ui.PDEUIMessages;
34 import org.eclipse.pde.internal.ui.util.ModelModification;
35 import org.eclipse.pde.internal.ui.util.PDEModelUtility;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.layout.GridLayout;
39 import org.eclipse.swt.widgets.Button;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Group;
42 import org.eclipse.ui.PlatformUI;
43 import org.eclipse.ui.actions.WorkspaceModifyOperation;
44 import org.osgi.framework.Constants;
45
46 public class SynchronizeVersionsWizardPage extends WizardPage {
47     public static final int USE_PLUGINS_AT_BUILD = 0;
48     public static final int USE_FEATURE = 1;
49     public static final int USE_PLUGINS = 2;
50     private FeatureEditor fFeatureEditor;
51     private Button fUsePluginsAtBuildButton;
52     private Button fUseComponentButton;
53     private Button fUsePluginsButton;
54
55     private static final String JavaDoc PREFIX = PDEPlugin.getPluginId() + ".synchronizeVersions."; //$NON-NLS-1$
56
private static final String JavaDoc PROP_SYNCHRO_MODE = PREFIX + "mode"; //$NON-NLS-1$
57

58     public SynchronizeVersionsWizardPage(FeatureEditor featureEditor) {
59         super("featureJar"); //$NON-NLS-1$
60
setTitle(PDEUIMessages.VersionSyncWizard_title);
61         setDescription(PDEUIMessages.VersionSyncWizard_desc);
62         this.fFeatureEditor = featureEditor;
63     }
64
65     public void createControl(Composite parent) {
66         Composite container = new Composite(parent, SWT.NULL);
67         GridLayout layout = new GridLayout();
68         container.setLayout(layout);
69
70         Group group = new Group(container, SWT.SHADOW_ETCHED_IN);
71         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
72         layout = new GridLayout();
73         group.setLayout(layout);
74         group.setLayoutData(gd);
75         group.setText(PDEUIMessages.VersionSyncWizard_group);
76
77         fUsePluginsAtBuildButton = new Button(group, SWT.RADIO);
78         fUsePluginsAtBuildButton.setText(PDEUIMessages.VersionSyncWizard_usePluginsAtBuild);
79         gd = new GridData(GridData.FILL_HORIZONTAL);
80         fUsePluginsAtBuildButton.setLayoutData(gd);
81
82         fUsePluginsButton = new Button(group, SWT.RADIO);
83         fUsePluginsButton.setText(PDEUIMessages.VersionSyncWizard_usePlugins);
84         gd = new GridData(GridData.FILL_HORIZONTAL);
85         fUsePluginsButton.setLayoutData(gd);
86
87         fUseComponentButton = new Button(group, SWT.RADIO);
88         fUseComponentButton.setText(PDEUIMessages.VersionSyncWizard_useComponent);
89         gd = new GridData(GridData.FILL_HORIZONTAL);
90         fUseComponentButton.setLayoutData(gd);
91
92         setControl(container);
93         Dialog.applyDialogFont(container);
94         loadSettings();
95         PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.FEATURE_SYNCHRONIZE_VERSIONS);
96     }
97
98     private IPluginModelBase findModel(String JavaDoc id) {
99         IPluginModelBase[] models = PluginRegistry.getWorkspaceModels();
100         for (int i = 0; i < models.length; i++) {
101             IPluginModelBase modelBase = models[i];
102             if (modelBase != null && id.equals(modelBase.getPluginBase().getId()))
103                 return modelBase;
104         }
105         return null;
106     }
107
108     public boolean finish() {
109         final int mode = saveSettings();
110
111         IRunnableWithProgress operation = new WorkspaceModifyOperation() {
112             public void execute(IProgressMonitor monitor) {
113                 try {
114                     runOperation(mode, monitor);
115                 } catch (CoreException e) {
116                     PDEPlugin.logException(e);
117                 } catch (BadLocationException e) {
118                     PDEPlugin.logException(e);
119                 } finally {
120                     monitor.done();
121                 }
122             }
123         };
124         try {
125             PlatformUI.getWorkbench().getProgressService().runInUI(
126                     PDEPlugin.getActiveWorkbenchWindow(), operation,
127                     PDEPlugin.getWorkspace().getRoot());
128         } catch (InvocationTargetException JavaDoc e) {
129             PDEPlugin.logException(e);
130             return false;
131         } catch (InterruptedException JavaDoc e) {
132             return false;
133         }
134         return true;
135     }
136
137     /**
138      * Forces a version into plugin/fragment .xml
139      *
140      * @param targetVersion
141      * @param modelBase
142      * @throws CoreException
143      */

144     private void forceVersion(final String JavaDoc targetVersion, IPluginModelBase modelBase, IProgressMonitor monitor) {
145         IFile file = (IFile) modelBase.getUnderlyingResource();
146         if (file == null)
147             return;
148
149         PDEModelUtility.modifyModel(new ModelModification(file) {
150             protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException {
151                 if (model instanceof IBundlePluginModelBase) {
152                     modifyVersion(((IBundlePluginModelBase) model).getBundleModel(), targetVersion);
153                 } else if (model instanceof IPluginModelBase) {
154                     modifyVersion((IPluginModelBase) model, targetVersion);
155                 }
156             }
157         }, monitor);
158     }
159
160     private void modifyVersion(IBundleModel model, String JavaDoc targetVersion) {
161         model.getBundle().setHeader(Constants.BUNDLE_VERSION, targetVersion);
162     }
163
164     private void modifyVersion(IPluginModelBase model, String JavaDoc version) throws CoreException {
165         model.getPluginBase().setVersion(version);
166     }
167
168     private void loadSettings() {
169         IDialogSettings settings = getDialogSettings();
170         if (settings.get(PROP_SYNCHRO_MODE) != null) {
171             int mode = settings.getInt(PROP_SYNCHRO_MODE);
172             switch (mode) {
173             case USE_FEATURE:
174                 fUseComponentButton.setSelection(true);
175                 break;
176             case USE_PLUGINS:
177                 fUsePluginsButton.setSelection(true);
178                 break;
179             default: // USE_PLUGINS_AT_BUILD
180
fUsePluginsAtBuildButton.setSelection(true);
181             break;
182             }
183         } else
184             fUsePluginsAtBuildButton.setSelection(true);
185     }
186
187     private void runOperation(int mode, IProgressMonitor monitor) throws CoreException, BadLocationException {
188         WorkspaceFeatureModel model = (WorkspaceFeatureModel) fFeatureEditor.getAggregateModel();
189         IFeature feature = model.getFeature();
190         IFeaturePlugin[] plugins = feature.getPlugins();
191         int size = plugins.length;
192         monitor.beginTask(PDEUIMessages.VersionSyncWizard_synchronizing, size);
193         for (int i = 0; i < plugins.length; i++)
194             synchronizeVersion(mode, feature.getVersion(), plugins[i], monitor);
195     }
196
197     private int saveSettings() {
198         IDialogSettings settings = getDialogSettings();
199         int mode = USE_PLUGINS_AT_BUILD;
200         if (fUseComponentButton.getSelection())
201             mode = USE_FEATURE;
202         else if (fUsePluginsButton.getSelection())
203             mode = USE_PLUGINS;
204         settings.put(PROP_SYNCHRO_MODE, mode);
205         return mode;
206     }
207
208     private void synchronizeVersion(int mode, String JavaDoc featureVersion,
209             IFeaturePlugin ref, IProgressMonitor monitor) throws CoreException,
210             BadLocationException {
211         String JavaDoc id = ref.getId();
212
213         if (mode == USE_PLUGINS_AT_BUILD) {
214             if (!"0.0.0".equals(ref.getVersion())) //$NON-NLS-1$
215
ref.setVersion("0.0.0"); //$NON-NLS-1$
216
} else if (mode == USE_PLUGINS) {
217             IPluginModelBase modelBase = PluginRegistry.findModel(id);
218             if (modelBase == null)
219                 return;
220             String JavaDoc baseVersion = modelBase.getPluginBase().getVersion();
221             if (!ref.getVersion().equals(baseVersion))
222                 ref.setVersion(baseVersion);
223         } else /* mode == USE_FEATURE */{
224             IPluginModelBase modelBase = findModel(id);
225             if (modelBase == null)
226                 return;
227             ref.setVersion(featureVersion);
228             String JavaDoc baseVersion = modelBase.getPluginBase().getVersion();
229             if (!featureVersion.equals(baseVersion))
230                 forceVersion(featureVersion, modelBase, monitor);
231         }
232         monitor.worked(1);
233     }
234 }
235
Popular Tags