KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ui.internal.samples;
12 import java.lang.reflect.InvocationTargetException JavaDoc;
13 import java.util.Properties JavaDoc;
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 JavaDoc SAMPLE_FEATURE_ID = "org.eclipse.sdk.samples"; //$NON-NLS-1$
31
private static final String JavaDoc SAMPLE_FEATURE_VERSION = "3.1.0"; //$NON-NLS-1$
32
private static final String JavaDoc UPDATE_SITE = "http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/pde-ui-home/samples/"; //$NON-NLS-1$
33
private String JavaDoc sampleId;
34     /**
35      *
36      */

37     public ShowSampleAction() {
38     }
39
40     public void run(IIntroSite site, Properties JavaDoc params) {
41         sampleId = params.getProperty("id"); //$NON-NLS-1$
42
if (sampleId == null)
43             return;
44         
45          Runnable JavaDoc r= new Runnable JavaDoc() {
46                 public void run() {
47             if (!ensureSampleFeaturePresent())
48                 return;
49         
50                 SampleWizard wizard = new SampleWizard();
51                 try {
52                     wizard.setInitializationData(null, "class", sampleId); //$NON-NLS-1$
53
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 JavaDoc url = new StringBuffer JavaDoc();
77         url.append("http://org.eclipse.ui.intro/showStandby?"); //$NON-NLS-1$
78
url.append("pluginId=org.eclipse.pde.ui"); //$NON-NLS-1$
79
url.append("&"); //$NON-NLS-1$
80
url.append("partId=org.eclipse.pde.ui.sampleStandbyPart"); //$NON-NLS-1$
81
url.append("&"); //$NON-NLS-1$
82
url.append("input="); //$NON-NLS-1$
83
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 JavaDoc perspId = sample.getAttribute("perspectiveId"); //$NON-NLS-1$
93
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         // the feature is not present - ask to download
111
if (MessageDialog
112                 .openQuestion(
113                         PDEPlugin.getActiveWorkbenchShell(),
114                         PDEUIMessages.ShowSampleAction_msgTitle, //$NON-NLS-1$
115
PDEUIMessages.ShowSampleAction_msgDesc)) { //$NON-NLS-1$
116
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 JavaDoc id = features[i].getFeatureIdentifier();
129             if (SAMPLE_FEATURE_ID.equals(id)) {
130                 String JavaDoc 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 JavaDoc {
143                 try {
144                     InstallCommand command = new InstallCommand(
145                             SAMPLE_FEATURE_ID, SAMPLE_FEATURE_VERSION,
146                             UPDATE_SITE, null, "false"); //$NON-NLS-1$
147
command.run(monitor);
148                     command.applyChangesNow();
149                 } catch (Exception JavaDoc e) {
150                     throw new InvocationTargetException JavaDoc(e);
151                 }
152             }
153         };
154         try {
155             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(op);
156         } catch (InvocationTargetException JavaDoc e) {
157             PDEPlugin.logException(e);
158             return false;
159         } catch (InterruptedException JavaDoc e) {
160             PDEPlugin.logException(e);
161         }
162         return true;
163     }
164 }
165
Popular Tags