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