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: * Bartosz Michalik <bartosz.michalik@gmail.com> - bug 181878
011: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.site;
012:
013: import java.lang.reflect.InvocationTargetException;
014:
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.wizard.WizardPage;
021: import org.eclipse.pde.internal.core.PDECore;
022: import org.eclipse.pde.internal.core.ifeature.IFeature;
023: import org.eclipse.pde.internal.core.ifeature.IFeatureImport;
024: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
025: import org.eclipse.pde.internal.core.isite.ISiteFeature;
026: import org.eclipse.pde.internal.core.isite.ISiteModel;
027: import org.eclipse.pde.internal.ui.IHelpContextIds;
028: import org.eclipse.pde.internal.ui.PDEPlugin;
029: import org.eclipse.pde.internal.ui.PDEUIMessages;
030: import org.eclipse.swt.SWT;
031: import org.eclipse.swt.layout.GridData;
032: import org.eclipse.swt.layout.GridLayout;
033: import org.eclipse.swt.widgets.Button;
034: import org.eclipse.swt.widgets.Composite;
035: import org.eclipse.swt.widgets.Group;
036: import org.eclipse.ui.PlatformUI;
037: import org.eclipse.ui.actions.WorkspaceModifyOperation;
038:
039: public class SynchronizePropertiesWizardPage extends WizardPage {
040:
041: public static final int ALL_FEATURES = 2;
042:
043: public static final int SELECTED_FEATURES = 1;
044:
045: private static final String PREFIX = PDEPlugin.getPluginId()
046: + ".synchronizeFeatueEnvironment."; //$NON-NLS-1$
047:
048: private static final String PROP_SYNCHRO_MODE = PREFIX + "mode"; //$NON-NLS-1$
049:
050: private Button fAllFeaturesButton;
051:
052: private ISiteModel fModel;
053:
054: private Button fSelectedFeaturesButton;
055:
056: private ISiteFeature[] fSiteFeatures;
057:
058: /**
059: *
060: * @param siteFeatures
061: * selected feature or null
062: */
063: public SynchronizePropertiesWizardPage(ISiteFeature[] siteFeatures,
064: ISiteModel model) {
065: super ("featureSelection"); //$NON-NLS-1$
066: setTitle(PDEUIMessages.SynchronizePropertiesWizardPage_title);
067: setDescription(PDEUIMessages.SynchronizePropertiesWizardPage_desc);
068: fSiteFeatures = siteFeatures != null ? siteFeatures
069: : new ISiteFeature[0];
070: fModel = model;
071: }
072:
073: public void createControl(Composite parent) {
074: Composite container = new Composite(parent, SWT.NULL);
075: GridLayout layout = new GridLayout();
076: container.setLayout(layout);
077:
078: Group group = new Group(container, SWT.SHADOW_ETCHED_IN);
079: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
080: layout = new GridLayout();
081: group.setLayout(layout);
082: group.setLayoutData(gd);
083: group
084: .setText(PDEUIMessages.SynchronizePropertiesWizardPage_group);
085:
086: fSelectedFeaturesButton = new Button(group, SWT.RADIO);
087: fSelectedFeaturesButton
088: .setText(PDEUIMessages.SynchronizePropertiesWizardPage_selectedFeatures);
089: gd = new GridData(GridData.FILL_HORIZONTAL);
090: fSelectedFeaturesButton.setLayoutData(gd);
091:
092: fAllFeaturesButton = new Button(group, SWT.RADIO);
093: fAllFeaturesButton
094: .setText(PDEUIMessages.SynchronizePropertiesWizardPage_allFeatures);
095: gd = new GridData(GridData.FILL_HORIZONTAL);
096: fAllFeaturesButton.setLayoutData(gd);
097:
098: setControl(container);
099: Dialog.applyDialogFont(container);
100: loadSettings();
101: // TODO add own F1 context
102: PlatformUI.getWorkbench().getHelpSystem().setHelp(container,
103: IHelpContextIds.FEATURE_SYNCHRONIZE_VERSIONS);
104: }
105:
106: public boolean finish() {
107: final int mode = saveSettings();
108:
109: IRunnableWithProgress operation = new WorkspaceModifyOperation() {
110: public void execute(IProgressMonitor monitor) {
111: try {
112: runOperation(mode, monitor);
113: } catch (CoreException e) {
114: PDEPlugin.logException(e);
115: } catch (InvocationTargetException e) {
116: PDEPlugin.logException(e);
117: } finally {
118: monitor.done();
119: }
120: }
121: };
122: try {
123: getContainer().run(true, true, operation);
124: } catch (InvocationTargetException e) {
125: PDEPlugin.logException(e);
126: return false;
127: } catch (InterruptedException e) {
128: return false;
129: }
130: return true;
131: }
132:
133: /**
134: *
135: * @param siteFeature
136: * @return IFeatureModel or null
137: */
138: private IFeature getFeature(ISiteFeature siteFeature) {
139: IFeatureModel model = PDECore.getDefault()
140: .getFeatureModelManager().findFeatureModel(
141: siteFeature.getId(), siteFeature.getVersion());
142: if (model != null)
143: return model.getFeature();
144: return null;
145: }
146:
147: private void importEnvironment(final ISiteFeature siteFeature) {
148: final IFeature feature = getFeature(siteFeature);
149: if (feature == null) {
150: return;
151: }
152: boolean patch = false;
153: IFeatureImport[] imports = feature.getImports();
154: for (int i = 0; i < imports.length; i++) {
155: if (imports[i].isPatch()) {
156: patch = true;
157: break;
158: }
159: }
160: final boolean isPatch = patch;
161: getShell().getDisplay().syncExec(new Runnable() {
162: public void run() {
163: try {
164: siteFeature.setNL(feature.getNL());
165: siteFeature.setOS(feature.getOS());
166: siteFeature.setWS(feature.getWS());
167: siteFeature.setArch(feature.getArch());
168: siteFeature.setIsPatch(isPatch);
169: } catch (CoreException ce) {
170: PDEPlugin.log(ce);
171: }
172: }
173: });
174: }
175:
176: private void importEnvironment(ISiteFeature[] siteFeatures,
177: IProgressMonitor monitor) {
178: for (int i = 0; i < siteFeatures.length; i++) {
179: if (monitor.isCanceled()) {
180: return;
181: }
182: monitor.subTask(siteFeatures[i].getId()
183: + "_" + siteFeatures[i].getVersion()); //$NON-NLS-1$
184: importEnvironment(siteFeatures[i]);
185: monitor.worked(1);
186: }
187: }
188:
189: private void loadSettings() {
190: if (fSiteFeatures != null) {
191: IDialogSettings settings = getDialogSettings();
192: if (settings.get(PROP_SYNCHRO_MODE) != null) {
193: int mode = settings.getInt(PROP_SYNCHRO_MODE);
194: switch (mode) {
195: case SELECTED_FEATURES:
196: fSelectedFeaturesButton.setSelection(true);
197: break;
198: case ALL_FEATURES:
199: fAllFeaturesButton.setSelection(true);
200: break;
201: default:
202: fSelectedFeaturesButton.setSelection(true);
203: }
204: } else
205: fSelectedFeaturesButton.setSelection(true);
206: } else {
207: fSelectedFeaturesButton.setEnabled(false);
208: fAllFeaturesButton.setSelection(true);
209: }
210: }
211:
212: private void runOperation(int mode, IProgressMonitor monitor)
213: throws CoreException, InvocationTargetException {
214: ISiteFeature[] siteFeatures;
215: if (mode == SELECTED_FEATURES) {
216: siteFeatures = fSiteFeatures;
217: } else {
218: siteFeatures = fModel.getSite().getFeatures();
219: }
220: int size = siteFeatures.length;
221: monitor
222: .beginTask(
223: PDEUIMessages.SynchronizePropertiesWizardPage_synchronizing,
224: size);
225: importEnvironment(siteFeatures, monitor);
226: }
227:
228: private int saveSettings() {
229: IDialogSettings settings = getDialogSettings();
230:
231: int mode = SELECTED_FEATURES;
232:
233: if (fAllFeaturesButton.getSelection())
234: mode = ALL_FEATURES;
235: settings.put(PROP_SYNCHRO_MODE, mode);
236: return mode;
237: }
238: }
|