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.samples;
011:
012: import java.lang.reflect.InvocationTargetException;
013: import java.util.Properties;
014:
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IConfigurationElement;
017: import org.eclipse.core.runtime.IProgressMonitor;
018: import org.eclipse.jface.action.Action;
019: import org.eclipse.jface.dialogs.MessageDialog;
020: import org.eclipse.jface.operation.IRunnableWithProgress;
021: import org.eclipse.jface.window.Window;
022: import org.eclipse.jface.wizard.WizardDialog;
023: import org.eclipse.pde.internal.core.util.VersionUtil;
024: import org.eclipse.pde.internal.ui.PDEPlugin;
025: import org.eclipse.pde.internal.ui.PDEUIMessages;
026: import org.eclipse.swt.widgets.Shell;
027: import org.eclipse.ui.PlatformUI;
028: import org.eclipse.ui.WorkbenchException;
029: import org.eclipse.ui.intro.IIntroSite;
030: import org.eclipse.ui.intro.config.IIntroAction;
031: import org.eclipse.ui.intro.config.IIntroURL;
032: import org.eclipse.ui.intro.config.IntroURLFactory;
033: import org.eclipse.update.configurator.ConfiguratorUtils;
034: import org.eclipse.update.configurator.IPlatformConfiguration;
035: import org.eclipse.update.standalone.InstallCommand;
036: import org.osgi.framework.Version;
037:
038: public class ShowSampleAction extends Action implements IIntroAction {
039: private static final String SAMPLE_FEATURE_ID = "org.eclipse.sdk.samples"; //$NON-NLS-1$
040: private static final String SAMPLE_FEATURE_VERSION = "3.3.0"; //$NON-NLS-1$
041: private static final String UPDATE_SITE = "http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/pde-ui-home/samples/"; //$NON-NLS-1$
042: private String sampleId;
043:
044: /**
045: *
046: */
047: public ShowSampleAction() {
048: }
049:
050: public void run(IIntroSite site, Properties params) {
051: sampleId = params.getProperty("id"); //$NON-NLS-1$
052: if (sampleId == null)
053: return;
054:
055: Runnable r = new Runnable() {
056: public void run() {
057: if (!ensureSampleFeaturePresent())
058: return;
059:
060: SampleWizard wizard = new SampleWizard();
061: try {
062: wizard.setInitializationData(null,
063: "class", sampleId); //$NON-NLS-1$
064: wizard.setSampleEditorNeeded(false);
065: wizard.setSwitchPerspective(false);
066: wizard.setSelectRevealEnabled(false);
067: wizard.setActivitiesEnabled(false);
068: WizardDialog dialog = new WizardDialog(PDEPlugin
069: .getActiveWorkbenchShell(), wizard);
070: dialog.create();
071: if (dialog.open() == Window.OK) {
072: switchToSampleStandby(wizard);
073: }
074: } catch (CoreException e) {
075: PDEPlugin.logException(e);
076: }
077: }
078: };
079:
080: Shell currentShell = PlatformUI.getWorkbench()
081: .getActiveWorkbenchWindow().getShell();
082: currentShell.getDisplay().asyncExec(r);
083: }
084:
085: private void switchToSampleStandby(SampleWizard wizard) {
086: StringBuffer url = new StringBuffer();
087: url.append("http://org.eclipse.ui.intro/showStandby?"); //$NON-NLS-1$
088: url.append("pluginId=org.eclipse.pde.ui"); //$NON-NLS-1$
089: url.append("&"); //$NON-NLS-1$
090: url.append("partId=org.eclipse.pde.ui.sampleStandbyPart"); //$NON-NLS-1$
091: url.append("&"); //$NON-NLS-1$
092: url.append("input="); //$NON-NLS-1$
093: url.append(sampleId);
094: IIntroURL introURL = IntroURLFactory.createIntroURL(url
095: .toString());
096: if (introURL != null) {
097: introURL.execute();
098: ensureProperContext(wizard);
099: }
100: }
101:
102: private void ensureProperContext(SampleWizard wizard) {
103: IConfigurationElement sample = wizard.getSelection();
104: String perspId = sample.getAttribute("perspectiveId"); //$NON-NLS-1$
105: if (perspId != null) {
106: try {
107: wizard.enableActivities();
108: PlatformUI.getWorkbench().showPerspective(perspId,
109: PDEPlugin.getActiveWorkbenchWindow());
110: wizard
111: .selectReveal(PDEPlugin
112: .getActiveWorkbenchShell());
113: } catch (WorkbenchException e) {
114: PDEPlugin.logException(e);
115: }
116: }
117: enableActivities(sample);
118: }
119:
120: private void enableActivities(IConfigurationElement sample) {
121: }
122:
123: private boolean ensureSampleFeaturePresent() {
124: if (checkFeature())
125: return true;
126: // the feature is not present - ask to download
127: if (MessageDialog.openQuestion(PDEPlugin
128: .getActiveWorkbenchShell(),
129: PDEUIMessages.ShowSampleAction_msgTitle,
130: PDEUIMessages.ShowSampleAction_msgDesc)) {
131: return downloadFeature();
132: }
133: return false;
134: }
135:
136: private boolean checkFeature() {
137: IPlatformConfiguration config = ConfiguratorUtils
138: .getCurrentPlatformConfiguration();
139: IPlatformConfiguration.IFeatureEntry[] features = config
140: .getConfiguredFeatureEntries();
141: Version sampleVersion = new Version(SAMPLE_FEATURE_VERSION);
142: for (int i = 0; i < features.length; i++) {
143: String id = features[i].getFeatureIdentifier();
144: if (SAMPLE_FEATURE_ID.equals(id)) {
145: String version = features[i].getFeatureVersion();
146: Version fversion = Version.parseVersion(version);
147: if (VersionUtil.isCompatibleWith(fversion,
148: sampleVersion))
149: return true;
150: }
151: }
152: return false;
153: }
154:
155: private boolean downloadFeature() {
156: IRunnableWithProgress op = new IRunnableWithProgress() {
157: public void run(IProgressMonitor monitor)
158: throws InvocationTargetException {
159: try {
160: InstallCommand command = new InstallCommand(
161: SAMPLE_FEATURE_ID, SAMPLE_FEATURE_VERSION,
162: UPDATE_SITE, null, "false"); //$NON-NLS-1$
163: command.run(monitor);
164: command.applyChangesNow();
165: } catch (Exception e) {
166: throw new InvocationTargetException(e);
167: }
168: }
169: };
170: try {
171: PlatformUI.getWorkbench().getProgressService()
172: .busyCursorWhile(op);
173: } catch (InvocationTargetException e) {
174: PDEPlugin.logException(e);
175: return false;
176: } catch (InterruptedException e) {
177: PDEPlugin.logException(e);
178: }
179: return true;
180: }
181: }
|