1 11 package org.eclipse.pde.internal.ui.search; 12 13 import java.io.File ; 14 import java.io.FileOutputStream ; 15 import java.io.IOException ; 16 import java.io.OutputStream ; 17 import java.io.PrintWriter ; 18 import java.net.MalformedURLException ; 19 import java.net.URL ; 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 fPointID; 47 private ISchema fSchema; 48 private File fPreviewFile; 49 private boolean fForceExternal; 50 51 private static File fTempWorkingDir; 52 53 public ShowDescriptionAction(String 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 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 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 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 title = PDEUIMessages.ShowDescriptionAction_title; 115 String message; 116 if (fPointID == null || fPointID.startsWith("null")) 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 os = new FileOutputStream (fPreviewFile); 131 PrintWriter printWriter = new PrintWriter (os, true); 132 transformer.transform(fSchema, printWriter); 133 os.flush(); 134 os.close(); 135 showURL(fPreviewFile, fForceExternal); 136 linkPreviewFileToSchemaFile(); 139 } catch (IOException e) { 140 PDEPlugin.logException(e); 141 } 142 } 143 144 148 private File getTempWorkingDir() throws IOException { 149 if (fTempWorkingDir == null) { 150 fTempWorkingDir = Utilities.createWorkingDirectory(); 151 } 152 return fTempWorkingDir; 153 } 154 155 158 private File getTempPreviewFile(){ 159 File tempWorkingDir = null; 161 try { 162 tempWorkingDir = getTempWorkingDir(); 163 } catch (IOException e) { 164 return null; 165 } 166 StringBuffer previewFileName = new StringBuffer (); 168 previewFileName.append("pde_schema_"); previewFileName.append(fSchema.getQualifiedPointId().replace('.', '-')); 170 previewFileName.append("_preview.html"); File previewFile = new File ( 172 tempWorkingDir.getPath() + 173 File.separatorChar + 174 previewFileName.toString()); 175 if (previewFile.exists() == false) { 178 try { 179 previewFile.createNewFile(); 180 } catch (IOException e) { 181 return null; 182 } 183 previewFile.deleteOnExit(); 185 } 186 187 return previewFile; 188 } 189 190 193 private void linkPreviewFileToSchemaFile() { 194 if (fPreviewFile == null) { 196 return; 197 } 198 IFile schemaFile = getSchemaFile(); 200 if (schemaFile == null) { 202 return; 203 } 204 try { 211 schemaFile.setSessionProperty( 212 PDECore.SCHEMA_PREVIEW_FILE, fPreviewFile); 213 } catch (CoreException e) { 214 } 216 } 217 218 221 private IFile getSchemaFile() { 222 if (fSchema == null) { 224 return null; 225 } 226 File javaSchemaFile = new File (fSchema.getURL().getFile()); 228 IFile[] eclipseSchemaFiles = 230 ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI( 231 javaSchemaFile.toURI()); 232 if (eclipseSchemaFiles.length == 0) { 234 return null; 235 } 236 return eclipseSchemaFiles[0]; 237 } 238 239 private void showURL(File file, boolean forceExternal) { 240 try { 241 IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport(); 242 URL 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); browser.openURL(url); 253 } 254 } catch (MalformedURLException e) { 255 PDEPlugin.logException(e); 256 } 257 catch (PartInitException e) { 258 PDEPlugin.logException(e); 259 } 260 } 261 } 262 | Popular Tags |