KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > actions > OpenSchemaAction


1 /*******************************************************************************
2  * Copyright (c) 2006, 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
12 package org.eclipse.pde.internal.ui.editor.actions;
13
14 import java.io.File JavaDoc;
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
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 /**
38  * OpenSchemaAction
39  *
40  */

41 public class OpenSchemaAction extends Action {
42
43     private ISchema fSchema;
44     
45     private String JavaDoc fFullPointID;
46     
47     /**
48      *
49      */

50     public OpenSchemaAction() {
51         fSchema = null;
52         fFullPointID = null;
53         
54         initialize();
55     }
56
57     /**
58      *
59      */

60     private void initialize() {
61         setImageDescriptor(PDEPluginImages.DESC_SCHEMA_OBJ);
62         setText(PDEUIMessages.HyperlinkActionOpenSchema);
63         setToolTipText(PDEUIMessages.HyperlinkActionOpenSchema);
64         setEnabled(false);
65     }
66     
67     /**
68      * @param schema
69      */

70     public void setInput(ISchema schema) {
71         // Ensure schema is defined
72
if (schema == null) {
73             fFullPointID = PDEUIMessages.OpenSchemaAction_msgUnknown;
74             return;
75         }
76         fFullPointID = schema.getQualifiedPointId();
77         fSchema = schema;
78     }
79
80     /**
81      * @param point
82      */

83     public void setInput(IPluginExtensionPoint point) {
84         // Ensure the point is defined
85
if (point == null) {
86             fSchema = null;
87             fFullPointID = PDEUIMessages.OpenSchemaAction_msgUnknown;
88             return;
89         }
90         fFullPointID = point.getFullId();
91         // Ensure the point is fully qualified
92
if (fFullPointID.indexOf('.') == -1) {
93             fSchema = null;
94             return;
95         }
96         // Find the schema
97
fSchema = findSchema(point);
98     }
99     
100     /**
101      * @param fullPointID
102      */

103     public void setInput(String JavaDoc fullPointID) {
104         // Ensure point ID is defined
105
if (fullPointID == null) {
106             fSchema = null;
107             fFullPointID = PDEUIMessages.OpenSchemaAction_msgUnknown;
108             return;
109         }
110         fFullPointID = fullPointID;
111         // Find the corresponding extension point
112
IPluginExtensionPoint point = PDEStateHelper.findExtensionPoint(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 = PDEStateHelper.findExtensionPoint(fFullPointID);
136         // Ensure the extension point is defined
137
if (point == null) {
138             fSchema = null;
139             return;
140         }
141         // Find the schema
142
fSchema = findSchema(point);
143     }
144
145     /**
146      * @param point
147      * @return
148      */

149     private ISchema findSchema(IPluginExtensionPoint point) {
150         // Find the corresponding schema URL for the extension point
151
URL JavaDoc url = SchemaRegistry.getSchemaURL(point);
152         // Ensure the URL is defined
153
if (url == null) {
154             return null;
155         }
156         // Create a schema descriptor
157
ISchemaDescriptor descriptor =
158             new SchemaDescriptor(fFullPointID, url);
159         // Get the schema
160
ISchema schema = descriptor.getSchema(false);
161         // Ensure schema is defined
162
if (schema == null) {
163             return null;
164         }
165         return schema;
166     }
167     
168     /**
169      * @param fullPointID
170      */

171     private void displayErrorDialog() {
172         String JavaDoc title = PDEUIMessages.OpenSchemaAction_titleExtensionPointSchema;
173         String JavaDoc message =
174             NLS.bind(PDEUIMessages.OpenSchemaAction_errorMsgSchemaNotFound, fFullPointID);
175         MessageDialog.openWarning(PDEPlugin.getActiveWorkbenchShell(),
176                 title, message);
177     }
178     
179     /* (non-Javadoc)
180      * @see org.eclipse.jface.action.Action#run()
181      */

182     public void run() {
183         // Ensure the schema is defined
184
if (fSchema == null) {
185             displayErrorDialog();
186             return;
187         }
188         // Retrieve the schema URL
189
URL JavaDoc schemaURL = fSchema.getURL();
190         // Ensure the URL is defined
191
if (schemaURL == null) {
192             displayErrorDialog();
193             return;
194         }
195         // Get the raw URL, determine if it is stored in a JAR, and handle
196
// accordingly
197
String JavaDoc rawURL = schemaURL.toString();
198         if (rawURL.startsWith("jar")) { //$NON-NLS-1$
199
// Call to getPath removes the 'jar:' qualifier
200
openSchemaJar(schemaURL.getPath());
201         } else {
202             openSchemaFile(schemaURL.getPath());
203         }
204
205     }
206
207     /**
208      * @param path
209      */

210     private void openSchemaFile(String JavaDoc path) {
211         // Open the schema in a new editor
212
try {
213             // see if schema URL is actually in workspace. If so, open it as we would if users opened file directly
214
IWorkspaceRoot root = PDEPlugin.getWorkspace().getRoot();
215             IPath workspacePath = root.getLocation();
216             String JavaDoc workspaceLoc = workspacePath.toFile().toURL().getPath();
217             if (path.startsWith(workspaceLoc)) {
218                 String JavaDoc 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 JavaDoc e) {
226         }
227         SchemaEditor.openSchema(new File JavaDoc(path));
228     }
229
230     /**
231      * @param path
232      */

233     private void openSchemaJar(String JavaDoc path) {
234         // Remove the 'file:' qualifier
235
if (path.startsWith("file:") == false) { //$NON-NLS-1$
236
displayErrorDialog();
237             return;
238         }
239         path = path.substring(5);
240         // An exclaimation point separates the jar filename from the
241
// schema file entry in the jar file
242
// Get the index of the '!'
243
int exclPointIndex = path.indexOf('!');
244         // Ensure there is an '!' and that the schema file entry is defined
245
// and the jar file name is defined
246
if ((exclPointIndex <= 0) ||
247                 ((exclPointIndex + 1) >= path.length())) {
248             displayErrorDialog();
249             return;
250         }
251         // Extract the jar file name - not including '!'
252
String JavaDoc jarFileName = path.substring(0, exclPointIndex);
253         // Extract the schema entry name - not including the '!'
254
String JavaDoc schemaEntryName = path.substring(exclPointIndex + 1);
255         // If the schema entry starts with a '/', remove it
256
if (schemaEntryName.startsWith("/")) { //$NON-NLS-1$
257
schemaEntryName = schemaEntryName.substring(1);
258         }
259         // Open the schema in a new editor
260
SchemaEditor.openSchema(new File JavaDoc(jarFileName), schemaEntryName);
261     }
262
263 }
264
Popular Tags