001: /*******************************************************************************
002: * Copyright (c) 2006, 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.editor.actions;
011:
012: import java.io.File;
013: import java.net.MalformedURLException;
014: import java.net.URL;
015:
016: import org.eclipse.core.resources.IFile;
017: import org.eclipse.core.resources.IResource;
018: import org.eclipse.core.resources.IWorkspaceRoot;
019: import org.eclipse.core.runtime.IPath;
020: import org.eclipse.jface.action.Action;
021: import org.eclipse.jface.dialogs.MessageDialog;
022: import org.eclipse.osgi.util.NLS;
023: import org.eclipse.pde.core.plugin.IPluginExtension;
024: import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
025: import org.eclipse.pde.internal.core.PDECore;
026: import org.eclipse.pde.internal.core.ischema.ISchema;
027: import org.eclipse.pde.internal.core.ischema.ISchemaDescriptor;
028: import org.eclipse.pde.internal.core.schema.SchemaDescriptor;
029: import org.eclipse.pde.internal.core.schema.SchemaRegistry;
030: import org.eclipse.pde.internal.ui.PDEPlugin;
031: import org.eclipse.pde.internal.ui.PDEPluginImages;
032: import org.eclipse.pde.internal.ui.PDEUIMessages;
033: import org.eclipse.pde.internal.ui.editor.schema.SchemaEditor;
034:
035: /**
036: * OpenSchemaAction
037: *
038: */
039: public class OpenSchemaAction extends Action {
040:
041: private ISchema fSchema;
042:
043: private String fFullPointID;
044:
045: /**
046: *
047: */
048: public OpenSchemaAction() {
049: fSchema = null;
050: fFullPointID = null;
051:
052: initialize();
053: }
054:
055: /**
056: *
057: */
058: private void initialize() {
059: setImageDescriptor(PDEPluginImages.DESC_SCHEMA_OBJ);
060: setText(PDEUIMessages.HyperlinkActionOpenSchema);
061: setToolTipText(PDEUIMessages.HyperlinkActionOpenSchema);
062: setEnabled(false);
063: }
064:
065: /**
066: * @param schema
067: */
068: public void setInput(ISchema schema) {
069: // Ensure schema is defined
070: if (schema == null) {
071: fFullPointID = PDEUIMessages.OpenSchemaAction_msgUnknown;
072: return;
073: }
074: fFullPointID = schema.getQualifiedPointId();
075: fSchema = schema;
076: }
077:
078: /**
079: * @param point
080: */
081: public void setInput(IPluginExtensionPoint point) {
082: // Ensure the point is defined
083: if (point == null) {
084: fSchema = null;
085: fFullPointID = PDEUIMessages.OpenSchemaAction_msgUnknown;
086: return;
087: }
088: fFullPointID = point.getFullId();
089: // Ensure the point is fully qualified
090: if (fFullPointID.indexOf('.') == -1) {
091: fSchema = null;
092: return;
093: }
094: // Find the schema
095: fSchema = findSchema(point);
096: }
097:
098: /**
099: * @param fullPointID
100: */
101: public void setInput(String fullPointID) {
102: // Ensure point ID is defined
103: if (fullPointID == null) {
104: fSchema = null;
105: fFullPointID = PDEUIMessages.OpenSchemaAction_msgUnknown;
106: return;
107: }
108: fFullPointID = fullPointID;
109: // Find the corresponding extension point
110: IPluginExtensionPoint point = PDECore.getDefault()
111: .getExtensionsRegistry().findExtensionPoint(
112: fFullPointID);
113: // Ensure the extension point is defined
114: if (point == null) {
115: fSchema = null;
116: return;
117: }
118: // Find the schema
119: fSchema = findSchema(point);
120: }
121:
122: /**
123: * @param extension
124: */
125: public void setInput(IPluginExtension extension) {
126: // Ensure the extension is defined
127: if (extension == null) {
128: fSchema = null;
129: fFullPointID = PDEUIMessages.OpenSchemaAction_msgUnknown;
130: return;
131: }
132: // Get the full extension point ID
133: fFullPointID = extension.getPoint();
134: // Find the corresponding extension point
135: IPluginExtensionPoint point = PDECore.getDefault()
136: .getExtensionsRegistry().findExtensionPoint(
137: fFullPointID);
138: // Ensure the extension point is defined
139: if (point == null) {
140: fSchema = null;
141: return;
142: }
143: // Find the schema
144: fSchema = findSchema(point);
145: }
146:
147: /**
148: * @param point
149: * @return
150: */
151: private ISchema findSchema(IPluginExtensionPoint point) {
152: // Find the corresponding schema URL for the extension point
153: URL url = SchemaRegistry.getSchemaURL(point);
154: // Ensure the URL is defined
155: if (url == null) {
156: return null;
157: }
158: // Create a schema descriptor
159: ISchemaDescriptor descriptor = new SchemaDescriptor(
160: fFullPointID, url);
161: // Get the schema
162: ISchema schema = descriptor.getSchema(false);
163: // Ensure schema is defined
164: if (schema == null) {
165: return null;
166: }
167: return schema;
168: }
169:
170: /**
171: * @param fullPointID
172: */
173: private void displayErrorDialog() {
174: String title = PDEUIMessages.OpenSchemaAction_titleExtensionPointSchema;
175: String message = NLS.bind(
176: PDEUIMessages.OpenSchemaAction_errorMsgSchemaNotFound,
177: fFullPointID);
178: MessageDialog.openWarning(PDEPlugin.getActiveWorkbenchShell(),
179: title, message);
180: }
181:
182: /* (non-Javadoc)
183: * @see org.eclipse.jface.action.Action#run()
184: */
185: public void run() {
186: // Ensure the schema is defined
187: if (fSchema == null) {
188: displayErrorDialog();
189: return;
190: }
191: // Retrieve the schema URL
192: URL schemaURL = fSchema.getURL();
193: // Ensure the URL is defined
194: if (schemaURL == null) {
195: displayErrorDialog();
196: return;
197: }
198: // Get the raw URL, determine if it is stored in a JAR, and handle
199: // accordingly
200: String rawURL = schemaURL.toString();
201: if (rawURL.startsWith("jar")) { //$NON-NLS-1$
202: // Call to getPath removes the 'jar:' qualifier
203: openSchemaJar(schemaURL.getPath());
204: } else {
205: openSchemaFile(schemaURL.getPath());
206: }
207:
208: }
209:
210: /**
211: * @param path
212: */
213: private void openSchemaFile(String path) {
214: // Open the schema in a new editor
215: try {
216: // see if schema URL is actually in workspace. If so, open it as we would if users opened file directly
217: IWorkspaceRoot root = PDEPlugin.getWorkspace().getRoot();
218: IPath workspacePath = root.getLocation();
219: String workspaceLoc = workspacePath.toFile().toURL()
220: .getPath();
221: if (path.startsWith(workspaceLoc)) {
222: String relativeLocation = path.substring(workspaceLoc
223: .length());
224: IResource res = root.findMember(relativeLocation);
225: if (res != null && res instanceof IFile
226: && res.getProject().isOpen()) {
227: SchemaEditor.openSchema((IFile) res);
228: return;
229: }
230: }
231: } catch (MalformedURLException e) {
232: }
233: SchemaEditor.openSchema(new File(path));
234: }
235:
236: /**
237: * @param path
238: */
239: private void openSchemaJar(String path) {
240: // Remove the 'file:' qualifier
241: if (path.startsWith("file:") == false) { //$NON-NLS-1$
242: displayErrorDialog();
243: return;
244: }
245: path = path.substring(5);
246: // An exclaimation point separates the jar filename from the
247: // schema file entry in the jar file
248: // Get the index of the '!'
249: int exclPointIndex = path.indexOf('!');
250: // Ensure there is an '!' and that the schema file entry is defined
251: // and the jar file name is defined
252: if ((exclPointIndex <= 0)
253: || ((exclPointIndex + 1) >= path.length())) {
254: displayErrorDialog();
255: return;
256: }
257: // Extract the jar file name - not including '!'
258: String jarFileName = path.substring(0, exclPointIndex);
259: // Extract the schema entry name - not including the '!'
260: String schemaEntryName = path.substring(exclPointIndex + 1);
261: // If the schema entry starts with a '/', remove it
262: if (schemaEntryName.startsWith("/")) { //$NON-NLS-1$
263: schemaEntryName = schemaEntryName.substring(1);
264: }
265: // Open the schema in a new editor
266: SchemaEditor.openSchema(new File(jarFileName), schemaEntryName);
267: }
268:
269: }
|