KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > site > SynchronizePropertiesWizardPage


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.editor.site;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.jface.dialogs.Dialog;
18 import org.eclipse.jface.dialogs.IDialogSettings;
19 import org.eclipse.jface.operation.IRunnableWithProgress;
20 import org.eclipse.jface.wizard.WizardPage;
21 import org.eclipse.pde.internal.core.PDECore;
22 import org.eclipse.pde.internal.core.ifeature.IFeature;
23 import org.eclipse.pde.internal.core.ifeature.IFeatureImport;
24 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
25 import org.eclipse.pde.internal.core.isite.ISiteFeature;
26 import org.eclipse.pde.internal.core.isite.ISiteModel;
27 import org.eclipse.pde.internal.ui.IHelpContextIds;
28 import org.eclipse.pde.internal.ui.PDEPlugin;
29 import org.eclipse.pde.internal.ui.PDEUIMessages;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Group;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.actions.WorkspaceModifyOperation;
38
39 public class SynchronizePropertiesWizardPage extends WizardPage {
40
41     public static final int ALL_FEATURES = 2;
42
43     public static final int ONE_FEATURE = 1;
44
45     private static final String JavaDoc PREFIX = PDEPlugin.getPluginId()
46             + ".synchronizeFeatueEnvironment."; //$NON-NLS-1$
47

48     private static final String JavaDoc PROP_SYNCHRO_MODE = PREFIX + "mode"; //$NON-NLS-1$
49

50     private Button fAllFeaturesButton;
51
52     private ISiteModel fModel;
53
54     private Button fOneFeatureButton;
55
56     private ISiteFeature fSiteFeature;
57
58     /**
59      *
60      * @param siteFeature
61      * selected feature or null
62      */

63     public SynchronizePropertiesWizardPage(ISiteFeature siteFeature,
64             ISiteModel model) {
65         super("featureSelection"); //$NON-NLS-1$
66
setTitle(PDEUIMessages.SynchronizePropertiesWizardPage_title);
67         setDescription(PDEUIMessages.SynchronizePropertiesWizardPage_desc);
68         fSiteFeature = siteFeature != null && getFeature(siteFeature) != null ? siteFeature
69                 : null;
70         fModel = model;
71     }
72
73     public void createControl(Composite parent) {
74         Composite container = new Composite(parent, SWT.NULL);
75         GridLayout layout = new GridLayout();
76         container.setLayout(layout);
77
78         Group group = new Group(container, SWT.SHADOW_ETCHED_IN);
79         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
80         layout = new GridLayout();
81         group.setLayout(layout);
82         group.setLayoutData(gd);
83         group.setText(PDEUIMessages.SynchronizePropertiesWizardPage_group);
84
85         fOneFeatureButton = new Button(group, SWT.RADIO);
86         fOneFeatureButton.setText(PDEUIMessages.SynchronizePropertiesWizardPage_oneFeature);
87         gd = new GridData(GridData.FILL_HORIZONTAL);
88         fOneFeatureButton.setLayoutData(gd);
89
90         fAllFeaturesButton = new Button(group, SWT.RADIO);
91         fAllFeaturesButton.setText(PDEUIMessages.SynchronizePropertiesWizardPage_allFeatures);
92         gd = new GridData(GridData.FILL_HORIZONTAL);
93         fAllFeaturesButton.setLayoutData(gd);
94
95         setControl(container);
96         Dialog.applyDialogFont(container);
97         loadSettings();
98         // TODO add own F1 context
99
PlatformUI.getWorkbench().getHelpSystem().setHelp(container,
100                 IHelpContextIds.FEATURE_SYNCHRONIZE_VERSIONS);
101     }
102
103     public boolean finish() {
104         final int mode = saveSettings();
105
106         IRunnableWithProgress operation = new WorkspaceModifyOperation() {
107             public void execute(IProgressMonitor monitor) {
108                 try {
109                     runOperation(mode, monitor);
110                 } catch (CoreException e) {
111                     PDEPlugin.logException(e);
112                 } catch (InvocationTargetException JavaDoc e) {
113                     PDEPlugin.logException(e);
114                 } finally {
115                     monitor.done();
116                 }
117             }
118         };
119         try {
120             getContainer().run(true, true, operation);
121         } catch (InvocationTargetException JavaDoc e) {
122             PDEPlugin.logException(e);
123             return false;
124         } catch (InterruptedException JavaDoc e) {
125             return false;
126         }
127         return true;
128     }
129
130     /**
131      *
132      * @param siteFeature
133      * @return IFeatureModel or null
134      */

135     private IFeature getFeature(ISiteFeature siteFeature) {
136         IFeatureModel model = PDECore
137                 .getDefault()
138                 .getFeatureModelManager()
139                 .findFeatureModel(siteFeature.getId(), siteFeature.getVersion());
140         if (model != null)
141             return model.getFeature();
142         return null;
143     }
144
145     private void importEnvironment(final ISiteFeature siteFeature) {
146         final IFeature feature = getFeature(siteFeature);
147         if (feature == null) {
148             return;
149         }
150         boolean patch = false;
151         IFeatureImport[] imports = feature.getImports();
152         for (int i = 0; i < imports.length; i++) {
153             if (imports[i].isPatch()) {
154                 patch = true;
155                 break;
156             }
157         }
158         final boolean isPatch = patch;
159         getShell().getDisplay().syncExec(new Runnable JavaDoc() {
160             public void run() {
161                 try {
162                     siteFeature.setNL(feature.getNL());
163                     siteFeature.setOS(feature.getOS());
164                     siteFeature.setWS(feature.getWS());
165                     siteFeature.setArch(feature.getArch());
166                     siteFeature.setIsPatch(isPatch);
167                 } catch (CoreException ce) {
168                     PDEPlugin.log(ce);
169                 }
170             }
171         });
172     }
173
174     private void importEnvironment(ISiteFeature[] siteFeatures,
175             IProgressMonitor monitor) {
176         for (int i = 0; i < siteFeatures.length; i++) {
177             if (monitor.isCanceled()) {
178                 return;
179             }
180             monitor.subTask(siteFeatures[i].getId()
181                     + "_" + siteFeatures[i].getVersion()); //$NON-NLS-1$
182
importEnvironment(siteFeatures[i]);
183             monitor.worked(1);
184         }
185     }
186
187     private void loadSettings() {
188         if (fSiteFeature != null) {
189             IDialogSettings settings = getDialogSettings();
190             if (settings.get(PROP_SYNCHRO_MODE) != null) {
191                 int mode = settings.getInt(PROP_SYNCHRO_MODE);
192                 switch (mode) {
193                 case ONE_FEATURE:
194                     fOneFeatureButton.setSelection(true);
195                     break;
196                 case ALL_FEATURES:
197                     fAllFeaturesButton.setSelection(true);
198                     break;
199                 default:
200                     fOneFeatureButton.setSelection(true);
201                 }
202             } else
203                 fOneFeatureButton.setSelection(true);
204         } else {
205             fOneFeatureButton.setEnabled(false);
206             fAllFeaturesButton.setSelection(true);
207         }
208     }
209
210     private void runOperation(int mode, IProgressMonitor monitor)
211             throws CoreException, InvocationTargetException JavaDoc {
212         ISiteFeature[] siteFeatures;
213         if (mode == ONE_FEATURE) {
214             siteFeatures = new ISiteFeature[] { fSiteFeature };
215         } else {
216             siteFeatures = fModel.getSite().getFeatures();
217         }
218         int size = siteFeatures.length;
219         monitor.beginTask(PDEUIMessages.SynchronizePropertiesWizardPage_synchronizing, size);
220         importEnvironment(siteFeatures, monitor);
221     }
222
223     private int saveSettings() {
224         IDialogSettings settings = getDialogSettings();
225
226         int mode = ONE_FEATURE;
227
228         if (fAllFeaturesButton.getSelection())
229             mode = ALL_FEATURES;
230         settings.put(PROP_SYNCHRO_MODE, mode);
231         return mode;
232     }
233 }
234
Popular Tags