1 11 12 package org.eclipse.pde.internal.ui.editor.actions; 13 14 import java.io.File ; 15 import java.net.MalformedURLException ; 16 import java.net.URL ; 17 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.resources.IWorkspaceRoot; 21 import org.eclipse.core.runtime.IPath; 22 import org.eclipse.jface.action.Action; 23 import org.eclipse.jface.dialogs.MessageDialog; 24 import org.eclipse.osgi.util.NLS; 25 import org.eclipse.pde.core.plugin.IPluginExtension; 26 import org.eclipse.pde.core.plugin.IPluginExtensionPoint; 27 import org.eclipse.pde.internal.core.PDEStateHelper; 28 import org.eclipse.pde.internal.core.ischema.ISchema; 29 import org.eclipse.pde.internal.core.ischema.ISchemaDescriptor; 30 import org.eclipse.pde.internal.core.schema.SchemaDescriptor; 31 import org.eclipse.pde.internal.core.schema.SchemaRegistry; 32 import org.eclipse.pde.internal.ui.PDEPlugin; 33 import org.eclipse.pde.internal.ui.PDEPluginImages; 34 import org.eclipse.pde.internal.ui.PDEUIMessages; 35 import org.eclipse.pde.internal.ui.editor.schema.SchemaEditor; 36 37 41 public class OpenSchemaAction extends Action { 42 43 private ISchema fSchema; 44 45 private String fFullPointID; 46 47 50 public OpenSchemaAction() { 51 fSchema = null; 52 fFullPointID = null; 53 54 initialize(); 55 } 56 57 60 private void initialize() { 61 setImageDescriptor(PDEPluginImages.DESC_SCHEMA_OBJ); 62 setText(PDEUIMessages.HyperlinkActionOpenSchema); 63 setToolTipText(PDEUIMessages.HyperlinkActionOpenSchema); 64 setEnabled(false); 65 } 66 67 70 public void setInput(ISchema schema) { 71 if (schema == null) { 73 fFullPointID = PDEUIMessages.OpenSchemaAction_msgUnknown; 74 return; 75 } 76 fFullPointID = schema.getQualifiedPointId(); 77 fSchema = schema; 78 } 79 80 83 public void setInput(IPluginExtensionPoint point) { 84 if (point == null) { 86 fSchema = null; 87 fFullPointID = PDEUIMessages.OpenSchemaAction_msgUnknown; 88 return; 89 } 90 fFullPointID = point.getFullId(); 91 if (fFullPointID.indexOf('.') == -1) { 93 fSchema = null; 94 return; 95 } 96 fSchema = findSchema(point); 98 } 99 100 103 public void setInput(String fullPointID) { 104 if (fullPointID == null) { 106 fSchema = null; 107 fFullPointID = PDEUIMessages.OpenSchemaAction_msgUnknown; 108 return; 109 } 110 fFullPointID = fullPointID; 111 IPluginExtensionPoint point = PDEStateHelper.findExtensionPoint(fFullPointID); 113 if (point == null) { 115 fSchema = null; 116 return; 117 } 118 fSchema = findSchema(point); 120 } 121 122 125 public void setInput(IPluginExtension extension) { 126 if (extension == null) { 128 fSchema = null; 129 fFullPointID = PDEUIMessages.OpenSchemaAction_msgUnknown; 130 return; 131 } 132 fFullPointID = extension.getPoint(); 134 IPluginExtensionPoint point = PDEStateHelper.findExtensionPoint(fFullPointID); 136 if (point == null) { 138 fSchema = null; 139 return; 140 } 141 fSchema = findSchema(point); 143 } 144 145 149 private ISchema findSchema(IPluginExtensionPoint point) { 150 URL url = SchemaRegistry.getSchemaURL(point); 152 if (url == null) { 154 return null; 155 } 156 ISchemaDescriptor descriptor = 158 new SchemaDescriptor(fFullPointID, url); 159 ISchema schema = descriptor.getSchema(false); 161 if (schema == null) { 163 return null; 164 } 165 return schema; 166 } 167 168 171 private void displayErrorDialog() { 172 String title = PDEUIMessages.OpenSchemaAction_titleExtensionPointSchema; 173 String message = 174 NLS.bind(PDEUIMessages.OpenSchemaAction_errorMsgSchemaNotFound, fFullPointID); 175 MessageDialog.openWarning(PDEPlugin.getActiveWorkbenchShell(), 176 title, message); 177 } 178 179 182 public void run() { 183 if (fSchema == null) { 185 displayErrorDialog(); 186 return; 187 } 188 URL schemaURL = fSchema.getURL(); 190 if (schemaURL == null) { 192 displayErrorDialog(); 193 return; 194 } 195 String rawURL = schemaURL.toString(); 198 if (rawURL.startsWith("jar")) { openSchemaJar(schemaURL.getPath()); 201 } else { 202 openSchemaFile(schemaURL.getPath()); 203 } 204 205 } 206 207 210 private void openSchemaFile(String path) { 211 try { 213 IWorkspaceRoot root = PDEPlugin.getWorkspace().getRoot(); 215 IPath workspacePath = root.getLocation(); 216 String workspaceLoc = workspacePath.toFile().toURL().getPath(); 217 if (path.startsWith(workspaceLoc)) { 218 String relativeLocation = path.substring(workspaceLoc.length()); 219 IResource res = root.findMember(relativeLocation); 220 if (res != null && res instanceof IFile && res.getProject().isOpen()) { 221 SchemaEditor.openSchema((IFile)res); 222 return; 223 } 224 } 225 } catch (MalformedURLException e) { 226 } 227 SchemaEditor.openSchema(new File (path)); 228 } 229 230 233 private void openSchemaJar(String path) { 234 if (path.startsWith("file:") == false) { displayErrorDialog(); 237 return; 238 } 239 path = path.substring(5); 240 int exclPointIndex = path.indexOf('!'); 244 if ((exclPointIndex <= 0) || 247 ((exclPointIndex + 1) >= path.length())) { 248 displayErrorDialog(); 249 return; 250 } 251 String jarFileName = path.substring(0, exclPointIndex); 253 String schemaEntryName = path.substring(exclPointIndex + 1); 255 if (schemaEntryName.startsWith("/")) { schemaEntryName = schemaEntryName.substring(1); 258 } 259 SchemaEditor.openSchema(new File (jarFileName), schemaEntryName); 261 } 262 263 } 264 | Popular Tags |