KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > search > ShowDescriptionAction


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.search;
12
13 import java.io.File JavaDoc;
14 import java.io.FileOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.OutputStream JavaDoc;
17 import java.io.PrintWriter JavaDoc;
18 import java.net.MalformedURLException JavaDoc;
19 import java.net.URL JavaDoc;
20
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.ResourcesPlugin;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.jface.action.Action;
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.osgi.util.NLS;
27 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
28 import org.eclipse.pde.internal.core.PDECore;
29 import org.eclipse.pde.internal.core.PDEStateHelper;
30 import org.eclipse.pde.internal.core.builders.SchemaTransformer;
31 import org.eclipse.pde.internal.core.ischema.ISchema;
32 import org.eclipse.pde.internal.core.ischema.ISchemaDescriptor;
33 import org.eclipse.pde.internal.core.schema.SchemaDescriptor;
34 import org.eclipse.pde.internal.core.schema.SchemaRegistry;
35 import org.eclipse.pde.internal.ui.PDEPlugin;
36 import org.eclipse.pde.internal.ui.PDEPluginImages;
37 import org.eclipse.pde.internal.ui.PDEUIMessages;
38 import org.eclipse.ui.PartInitException;
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.ui.browser.IWebBrowser;
41 import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
42 import org.eclipse.update.core.Utilities;
43
44
45 public class ShowDescriptionAction extends Action {
46     private String JavaDoc fPointID;
47     private ISchema fSchema;
48     private File JavaDoc fPreviewFile;
49     private boolean fForceExternal;
50     
51     private static File JavaDoc fTempWorkingDir;
52     
53     public ShowDescriptionAction(String JavaDoc pointID) {
54         fPointID = pointID;
55         initialize();
56     }
57
58     public ShowDescriptionAction(IPluginExtensionPoint point) {
59         this(point, false);
60     }
61     
62     public ShowDescriptionAction(IPluginExtensionPoint point, boolean forceExternal) {
63         setExtensionPoint(point, point.getFullId());
64         fForceExternal = forceExternal;
65         initialize();
66     }
67     
68     public ShowDescriptionAction(IPluginExtensionPoint point, String JavaDoc pointID) {
69         setExtensionPoint(point, pointID);
70         fForceExternal = false;
71         initialize();
72     }
73     
74     public ShowDescriptionAction(ISchema schema) {
75         setSchema(schema);
76         initialize();
77     }
78     
79     private void initialize() {
80         setImageDescriptor(PDEPluginImages.DESC_DOC_SECTION_OBJ);
81     }
82     
83     public void setSchema(ISchema schema) {
84         fSchema = schema;
85         fPointID = schema.getQualifiedPointId();
86     }
87     
88     public void setExtensionPoint(IPluginExtensionPoint point, String JavaDoc pointID) {
89         fPointID = pointID;
90         setText(PDEUIMessages.ShowDescriptionAction_label);
91         fSchema = null;
92     }
93     
94     public void run() {
95         if (fSchema == null) {
96             IPluginExtensionPoint point = PDEStateHelper.findExtensionPoint(fPointID);
97             URL JavaDoc url = null;
98             if (point != null) {
99                 url = SchemaRegistry.getSchemaURL(point);
100                 if (url != null) {
101                     ISchemaDescriptor desc = new SchemaDescriptor(fPointID, url);
102                     fSchema = desc.getSchema(false);
103                 }
104             }
105             if (point == null|| url == null || fSchema == null) {
106                 showNoSchemaMessage();
107                 return;
108             }
109         }
110         showSchemaDocument();
111     }
112     
113     private void showNoSchemaMessage() {
114         String JavaDoc title = PDEUIMessages.ShowDescriptionAction_title;
115         String JavaDoc message;
116         if (fPointID == null || fPointID.startsWith("null")) //$NON-NLS-1$
117
message = PDEUIMessages.ShowDescriptionAction_schemaNotAvail;
118         else
119             message = NLS.bind(PDEUIMessages.ShowDescriptionAction_noPoint_desc, fPointID);
120         MessageDialog.openWarning(PDEPlugin.getActiveWorkbenchShell(), title, message);
121     }
122
123     private void showSchemaDocument() {
124         try {
125             fPreviewFile = getTempPreviewFile();
126             if (fPreviewFile == null)
127                 return;
128
129             SchemaTransformer transformer = new SchemaTransformer();
130             OutputStream JavaDoc os = new FileOutputStream JavaDoc(fPreviewFile);
131             PrintWriter JavaDoc printWriter = new PrintWriter JavaDoc(os, true);
132             transformer.transform(fSchema, printWriter);
133             os.flush();
134             os.close();
135             showURL(fPreviewFile, fForceExternal);
136             // Associate the generated preview file with the schema file
137
// to enable automatic preview file updates on schema file changes
138
linkPreviewFileToSchemaFile();
139         } catch (IOException JavaDoc e) {
140             PDEPlugin.logException(e);
141         }
142     }
143     
144     /**
145      * @return
146      * @throws IOException
147      */

148     private File JavaDoc getTempWorkingDir() throws IOException JavaDoc {
149         if (fTempWorkingDir == null) {
150             fTempWorkingDir = Utilities.createWorkingDirectory();
151         }
152         return fTempWorkingDir;
153     }
154     
155     /**
156      * @return
157      */

158     private File JavaDoc getTempPreviewFile(){
159         // Get the temporary working directory
160
File JavaDoc tempWorkingDir = null;
161         try {
162             tempWorkingDir = getTempWorkingDir();
163         } catch (IOException JavaDoc e) {
164             return null;
165         }
166         // Generate a consistent unique preview file name for this schema
167
StringBuffer JavaDoc previewFileName = new StringBuffer JavaDoc();
168         previewFileName.append("pde_schema_"); //$NON-NLS-1$
169
previewFileName.append(fSchema.getQualifiedPointId().replace('.', '-'));
170         previewFileName.append("_preview.html"); //$NON-NLS-1$
171
File JavaDoc previewFile = new File JavaDoc(
172             tempWorkingDir.getPath() +
173             File.separatorChar +
174             previewFileName.toString());
175         // If the file does not exist yet, create it within the temporary
176
// working diretory
177
if (previewFile.exists() == false) {
178             try {
179                 previewFile.createNewFile();
180             } catch (IOException JavaDoc e) {
181                 return null;
182             }
183             // Mark file for deletion on VM exit
184
previewFile.deleteOnExit();
185         }
186         
187         return previewFile;
188     }
189     
190     /**
191      *
192      */

193     private void linkPreviewFileToSchemaFile() {
194         // Ensure the preview file is defined
195
if (fPreviewFile == null) {
196             return;
197         }
198         // Get the schema file
199
IFile schemaFile = getSchemaFile();
200         // Ensure we found the workspace Eclipse schema file
201
if (schemaFile == null) {
202             return;
203         }
204         // Set the preview file on the Eclipse schema file resource.
205
// Later on, content changes to the Eclipse schema file in the workspace
206
// will result in an automatic regeneration of the schema preview
207
// contents
208
// This is handled in
209
// org.eclipse.pde.internal.core.WorkspacePluginModelManager.handleEclipseSchemaDelta(IFile, IResourceDelta)
210
try {
211             schemaFile.setSessionProperty(
212                     PDECore.SCHEMA_PREVIEW_FILE, fPreviewFile);
213         } catch (CoreException e) {
214             // Ignore
215
}
216     }
217     
218     /**
219      * @return
220      */

221     private IFile getSchemaFile() {
222         // Ensure the schema is defined
223
if (fSchema == null) {
224             return null;
225         }
226         // Get the Java schema file
227
File JavaDoc javaSchemaFile = new File JavaDoc(fSchema.getURL().getFile());
228         // Get the Eclipse schema file
229
IFile[] eclipseSchemaFiles =
230             ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(
231                 javaSchemaFile.toURI());
232         // Ensure the file was found in the workspace
233
if (eclipseSchemaFiles.length == 0) {
234             return null;
235         }
236         return eclipseSchemaFiles[0];
237     }
238     
239     private void showURL(File JavaDoc file, boolean forceExternal) {
240         try {
241             IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport();
242             URL JavaDoc url = file.toURL();
243             
244             if (forceExternal) {
245                 IWebBrowser browser = support.getExternalBrowser();
246                 browser.openURL(url);
247             } else {
248                 IWebBrowser browser = support.createBrowser(
249                         IWorkbenchBrowserSupport.AS_EDITOR
250                                 | IWorkbenchBrowserSupport.STATUS,
251                         "org.eclipse.pde", fPointID, fPointID); //$NON-NLS-1$
252
browser.openURL(url);
253             }
254         } catch (MalformedURLException JavaDoc e) {
255             PDEPlugin.logException(e);
256         }
257         catch (PartInitException e) {
258             PDEPlugin.logException(e);
259         }
260     }
261 }
262
Popular Tags