KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > samples > ShowSampleAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.samples;
12 import java.lang.reflect.InvocationTargetException JavaDoc;
13 import java.util.Properties JavaDoc;
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 JavaDoc SAMPLE_FEATURE_ID = "org.eclipse.sdk.samples"; //$NON-NLS-1$
40
private static final String JavaDoc SAMPLE_FEATURE_VERSION = "3.3.0"; //$NON-NLS-1$
41
private static final String JavaDoc UPDATE_SITE = "http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/pde-ui-home/samples/"; //$NON-NLS-1$
42
private String JavaDoc sampleId;
43     /**
44      *
45      */

46     public ShowSampleAction() {
47     }
48
49     public void run(IIntroSite site, Properties JavaDoc params) {
50         sampleId = params.getProperty("id"); //$NON-NLS-1$
51
if (sampleId == null)
52             return;
53         
54          Runnable JavaDoc r= new Runnable JavaDoc() {
55                 public void run() {
56             if (!ensureSampleFeaturePresent())
57                 return;
58         
59                 SampleWizard wizard = new SampleWizard();
60                 try {
61                     wizard.setInitializationData(null, "class", sampleId); //$NON-NLS-1$
62
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 JavaDoc url = new StringBuffer JavaDoc();
85         url.append("http://org.eclipse.ui.intro/showStandby?"); //$NON-NLS-1$
86
url.append("pluginId=org.eclipse.pde.ui"); //$NON-NLS-1$
87
url.append("&"); //$NON-NLS-1$
88
url.append("partId=org.eclipse.pde.ui.sampleStandbyPart"); //$NON-NLS-1$
89
url.append("&"); //$NON-NLS-1$
90
url.append("input="); //$NON-NLS-1$
91
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 JavaDoc perspId = sample.getAttribute("perspectiveId"); //$NON-NLS-1$
101
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         // the feature is not present - ask to download
119
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 JavaDoc id = features[i].getFeatureIdentifier();
136             if (SAMPLE_FEATURE_ID.equals(id)) {
137                 String JavaDoc 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 JavaDoc {
149                 try {
150                     InstallCommand command = new InstallCommand(
151                             SAMPLE_FEATURE_ID, SAMPLE_FEATURE_VERSION,
152                             UPDATE_SITE, null, "false"); //$NON-NLS-1$
153
command.run(monitor);
154                     command.applyChangesNow();
155                 } catch (Exception JavaDoc e) {
156                     throw new InvocationTargetException JavaDoc(e);
157                 }
158             }
159         };
160         try {
161             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(op);
162         } catch (InvocationTargetException JavaDoc e) {
163             PDEPlugin.logException(e);
164             return false;
165         } catch (InterruptedException JavaDoc e) {
166             PDEPlugin.logException(e);
167         }
168         return true;
169     }
170 }
171
Popular Tags