1 11 package org.eclipse.pde.internal.ui.samples; 12 import java.lang.reflect.InvocationTargetException ; 13 import java.util.Properties ; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IConfigurationElement; 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.jface.action.Action; 19 import org.eclipse.jface.dialogs.MessageDialog; 20 import org.eclipse.jface.operation.IRunnableWithProgress; 21 import org.eclipse.jface.window.Window; 22 import org.eclipse.jface.wizard.WizardDialog; 23 import org.eclipse.pde.internal.core.util.VersionUtil; 24 import org.eclipse.pde.internal.ui.PDEPlugin; 25 import org.eclipse.pde.internal.ui.PDEUIMessages; 26 import org.eclipse.swt.widgets.Shell; 27 import org.eclipse.ui.PlatformUI; 28 import org.eclipse.ui.WorkbenchException; 29 import org.eclipse.ui.intro.IIntroSite; 30 import org.eclipse.ui.intro.config.IIntroAction; 31 import org.eclipse.ui.intro.config.IIntroURL; 32 import org.eclipse.ui.intro.config.IntroURLFactory; 33 import org.eclipse.update.configurator.ConfiguratorUtils; 34 import org.eclipse.update.configurator.IPlatformConfiguration; 35 import org.eclipse.update.standalone.InstallCommand; 36 import org.osgi.framework.Version; 37 38 public class ShowSampleAction extends Action implements IIntroAction { 39 private static final String SAMPLE_FEATURE_ID = "org.eclipse.sdk.samples"; private static final String SAMPLE_FEATURE_VERSION = "3.3.0"; private static final String UPDATE_SITE = "http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/pde-ui-home/samples/"; private String sampleId; 43 46 public ShowSampleAction() { 47 } 48 49 public void run(IIntroSite site, Properties params) { 50 sampleId = params.getProperty("id"); if (sampleId == null) 52 return; 53 54 Runnable r= new Runnable () { 55 public void run() { 56 if (!ensureSampleFeaturePresent()) 57 return; 58 59 SampleWizard wizard = new SampleWizard(); 60 try { 61 wizard.setInitializationData(null, "class", sampleId); wizard.setSampleEditorNeeded(false); 63 wizard.setSwitchPerspective(false); 64 wizard.setSelectRevealEnabled(false); 65 wizard.setActivitiesEnabled(false); 66 WizardDialog dialog = new WizardDialog(PDEPlugin 67 .getActiveWorkbenchShell(), wizard); 68 dialog.create(); 69 if (dialog.open() == Window.OK) { 70 switchToSampleStandby(wizard); 71 } 72 } catch (CoreException e) { 73 PDEPlugin.logException(e); 74 } 75 } 76 }; 77 78 Shell currentShell = PlatformUI.getWorkbench() 79 .getActiveWorkbenchWindow().getShell(); 80 currentShell.getDisplay().asyncExec(r); 81 } 82 83 private void switchToSampleStandby(SampleWizard wizard) { 84 StringBuffer url = new StringBuffer (); 85 url.append("http://org.eclipse.ui.intro/showStandby?"); url.append("pluginId=org.eclipse.pde.ui"); url.append("&"); url.append("partId=org.eclipse.pde.ui.sampleStandbyPart"); url.append("&"); url.append("input="); url.append(sampleId); 92 IIntroURL introURL = IntroURLFactory.createIntroURL(url.toString()); 93 if (introURL != null) { 94 introURL.execute(); 95 ensureProperContext(wizard); 96 } 97 } 98 private void ensureProperContext(SampleWizard wizard) { 99 IConfigurationElement sample = wizard.getSelection(); 100 String perspId = sample.getAttribute("perspectiveId"); if (perspId!=null) { 102 try { 103 wizard.enableActivities(); 104 PlatformUI.getWorkbench().showPerspective(perspId, PDEPlugin.getActiveWorkbenchWindow()); 105 wizard.selectReveal(PDEPlugin.getActiveWorkbenchShell()); 106 } 107 catch (WorkbenchException e) { 108 PDEPlugin.logException(e); 109 } 110 } 111 enableActivities(sample); 112 } 113 private void enableActivities(IConfigurationElement sample) { 114 } 115 private boolean ensureSampleFeaturePresent() { 116 if (checkFeature()) 117 return true; 118 if (MessageDialog 120 .openQuestion( 121 PDEPlugin.getActiveWorkbenchShell(), 122 PDEUIMessages.ShowSampleAction_msgTitle, 123 PDEUIMessages.ShowSampleAction_msgDesc)) { 124 return downloadFeature(); 125 } 126 return false; 127 } 128 private boolean checkFeature() { 129 IPlatformConfiguration config = ConfiguratorUtils 130 .getCurrentPlatformConfiguration(); 131 IPlatformConfiguration.IFeatureEntry [] features = config 132 .getConfiguredFeatureEntries(); 133 Version sampleVersion = new Version(SAMPLE_FEATURE_VERSION); 134 for (int i = 0; i < features.length; i++) { 135 String id = features[i].getFeatureIdentifier(); 136 if (SAMPLE_FEATURE_ID.equals(id)) { 137 String version = features[i].getFeatureVersion(); 138 Version fversion = Version.parseVersion(version); 139 if (VersionUtil.isCompatibleWith(fversion, sampleVersion)) 140 return true; 141 } 142 } 143 return false; 144 } 145 private boolean downloadFeature() { 146 IRunnableWithProgress op = new IRunnableWithProgress() { 147 public void run(IProgressMonitor monitor) 148 throws InvocationTargetException { 149 try { 150 InstallCommand command = new InstallCommand( 151 SAMPLE_FEATURE_ID, SAMPLE_FEATURE_VERSION, 152 UPDATE_SITE, null, "false"); command.run(monitor); 154 command.applyChangesNow(); 155 } catch (Exception e) { 156 throw new InvocationTargetException (e); 157 } 158 } 159 }; 160 try { 161 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(op); 162 } catch (InvocationTargetException e) { 163 PDEPlugin.logException(e); 164 return false; 165 } catch (InterruptedException e) { 166 PDEPlugin.logException(e); 167 } 168 return true; 169 } 170 } 171 | Popular Tags |