001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.search;
011:
012: import java.io.File;
013: import java.io.FileOutputStream;
014: import java.io.IOException;
015: import java.io.OutputStream;
016: import java.io.PrintWriter;
017: import java.net.MalformedURLException;
018: import java.net.URL;
019:
020: import org.eclipse.core.resources.IFile;
021: import org.eclipse.core.resources.ResourcesPlugin;
022: import org.eclipse.core.runtime.CoreException;
023: import org.eclipse.jface.action.Action;
024: import org.eclipse.jface.dialogs.MessageDialog;
025: import org.eclipse.osgi.util.NLS;
026: import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
027: import org.eclipse.pde.internal.core.PDECore;
028: import org.eclipse.pde.internal.core.builders.SchemaTransformer;
029: import org.eclipse.pde.internal.core.ischema.ISchema;
030: import org.eclipse.pde.internal.core.ischema.ISchemaDescriptor;
031: import org.eclipse.pde.internal.core.schema.SchemaDescriptor;
032: import org.eclipse.pde.internal.core.schema.SchemaRegistry;
033: import org.eclipse.pde.internal.ui.PDEPlugin;
034: import org.eclipse.pde.internal.ui.PDEPluginImages;
035: import org.eclipse.pde.internal.ui.PDEUIMessages;
036: import org.eclipse.ui.PartInitException;
037: import org.eclipse.ui.PlatformUI;
038: import org.eclipse.ui.browser.IWebBrowser;
039: import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
040: import org.eclipse.update.core.Utilities;
041:
042: public class ShowDescriptionAction extends Action {
043: private String fPointID;
044: private ISchema fSchema;
045: private File fPreviewFile;
046: private boolean fForceExternal;
047:
048: private static File fTempWorkingDir;
049:
050: public ShowDescriptionAction(String pointID) {
051: fPointID = pointID;
052: initialize();
053: }
054:
055: public ShowDescriptionAction(IPluginExtensionPoint point) {
056: this (point, false);
057: }
058:
059: public ShowDescriptionAction(IPluginExtensionPoint point,
060: boolean forceExternal) {
061: setExtensionPoint(point, point.getFullId());
062: fForceExternal = forceExternal;
063: initialize();
064: }
065:
066: public ShowDescriptionAction(IPluginExtensionPoint point,
067: String pointID) {
068: setExtensionPoint(point, pointID);
069: fForceExternal = false;
070: initialize();
071: }
072:
073: public ShowDescriptionAction(ISchema schema) {
074: setSchema(schema);
075: initialize();
076: }
077:
078: private void initialize() {
079: setImageDescriptor(PDEPluginImages.DESC_DOC_SECTION_OBJ);
080: }
081:
082: public void setSchema(ISchema schema) {
083: fSchema = schema;
084: fPointID = schema.getQualifiedPointId();
085: }
086:
087: public void setExtensionPoint(IPluginExtensionPoint point,
088: String pointID) {
089: fPointID = pointID;
090: setText(PDEUIMessages.ShowDescriptionAction_label);
091: fSchema = null;
092: }
093:
094: public void run() {
095: if (fSchema == null) {
096: IPluginExtensionPoint point = PDECore.getDefault()
097: .getExtensionsRegistry().findExtensionPoint(
098: fPointID);
099: URL url = null;
100: if (point != null) {
101: url = SchemaRegistry.getSchemaURL(point);
102: if (url != null) {
103: ISchemaDescriptor desc = new SchemaDescriptor(
104: fPointID, url);
105: fSchema = desc.getSchema(false);
106: }
107: }
108: if (point == null || url == null || fSchema == null) {
109: showNoSchemaMessage();
110: return;
111: }
112: }
113: showSchemaDocument();
114: }
115:
116: private void showNoSchemaMessage() {
117: String title = PDEUIMessages.ShowDescriptionAction_title;
118: String message;
119: if (fPointID == null || fPointID.startsWith("null")) //$NON-NLS-1$
120: message = PDEUIMessages.ShowDescriptionAction_schemaNotAvail;
121: else
122: message = NLS.bind(
123: PDEUIMessages.ShowDescriptionAction_noPoint_desc,
124: fPointID);
125: MessageDialog.openWarning(PDEPlugin.getActiveWorkbenchShell(),
126: title, message);
127: }
128:
129: private void showSchemaDocument() {
130: try {
131: fPreviewFile = getTempPreviewFile();
132: if (fPreviewFile == null)
133: return;
134:
135: SchemaTransformer transformer = new SchemaTransformer();
136: OutputStream os = new FileOutputStream(fPreviewFile);
137: PrintWriter printWriter = new PrintWriter(os, true);
138: transformer.transform(fSchema, printWriter);
139: os.flush();
140: os.close();
141: showURL(fPreviewFile, fForceExternal);
142: // Associate the generated preview file with the schema file
143: // to enable automatic preview file updates on schema file changes
144: linkPreviewFileToSchemaFile();
145: } catch (IOException e) {
146: PDEPlugin.logException(e);
147: }
148: }
149:
150: /**
151: * @return
152: * @throws IOException
153: */
154: private File getTempWorkingDir() throws IOException {
155: if (fTempWorkingDir == null) {
156: fTempWorkingDir = Utilities.createWorkingDirectory();
157: }
158: return fTempWorkingDir;
159: }
160:
161: /**
162: * @return
163: */
164: private File getTempPreviewFile() {
165: // Get the temporary working directory
166: File tempWorkingDir = null;
167: try {
168: tempWorkingDir = getTempWorkingDir();
169: } catch (IOException e) {
170: return null;
171: }
172: // Generate a consistent unique preview file name for this schema
173: StringBuffer previewFileName = new StringBuffer();
174: previewFileName.append("pde_schema_"); //$NON-NLS-1$
175: previewFileName.append(fSchema.getQualifiedPointId().replace(
176: '.', '-'));
177: previewFileName.append("_preview.html"); //$NON-NLS-1$
178: File previewFile = new File(tempWorkingDir.getPath()
179: + File.separatorChar + previewFileName.toString());
180: // If the file does not exist yet, create it within the temporary
181: // working diretory
182: if (previewFile.exists() == false) {
183: try {
184: previewFile.createNewFile();
185: } catch (IOException e) {
186: return null;
187: }
188: // Mark file for deletion on VM exit
189: previewFile.deleteOnExit();
190: }
191:
192: return previewFile;
193: }
194:
195: /**
196: *
197: */
198: private void linkPreviewFileToSchemaFile() {
199: // Ensure the preview file is defined
200: if (fPreviewFile == null) {
201: return;
202: }
203: // Get the schema file
204: IFile schemaFile = getSchemaFile();
205: // Ensure we found the workspace Eclipse schema file
206: if (schemaFile == null) {
207: return;
208: }
209: // Set the preview file on the Eclipse schema file resource.
210: // Later on, content changes to the Eclipse schema file in the workspace
211: // will result in an automatic regeneration of the schema preview
212: // contents
213: // This is handled in
214: // org.eclipse.pde.internal.core.WorkspacePluginModelManager.handleEclipseSchemaDelta(IFile, IResourceDelta)
215: try {
216: schemaFile.setSessionProperty(PDECore.SCHEMA_PREVIEW_FILE,
217: fPreviewFile);
218: } catch (CoreException e) {
219: // Ignore
220: }
221: }
222:
223: /**
224: * @return
225: */
226: private IFile getSchemaFile() {
227: // Ensure the schema is defined
228: if (fSchema == null) {
229: return null;
230: }
231: // Get the Java schema file
232: File javaSchemaFile = new File(fSchema.getURL().getFile());
233: // Get the Eclipse schema file
234: IFile[] eclipseSchemaFiles = ResourcesPlugin.getWorkspace()
235: .getRoot().findFilesForLocationURI(
236: javaSchemaFile.toURI());
237: // Ensure the file was found in the workspace
238: if (eclipseSchemaFiles.length == 0) {
239: return null;
240: }
241: return eclipseSchemaFiles[0];
242: }
243:
244: private void showURL(File file, boolean forceExternal) {
245: try {
246: IWorkbenchBrowserSupport support = PlatformUI
247: .getWorkbench().getBrowserSupport();
248: URL url = file.toURL();
249:
250: if (forceExternal) {
251: IWebBrowser browser = support.getExternalBrowser();
252: browser.openURL(url);
253: } else {
254: IWebBrowser browser = support.createBrowser(
255: IWorkbenchBrowserSupport.AS_EDITOR
256: | IWorkbenchBrowserSupport.STATUS,
257: "org.eclipse.pde", fPointID, fPointID); //$NON-NLS-1$
258: browser.openURL(url);
259: }
260: } catch (MalformedURLException e) {
261: PDEPlugin.logException(e);
262: } catch (PartInitException e) {
263: PDEPlugin.logException(e);
264: }
265: }
266: }
|