KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > schema > SchemaEditor


1 /*******************************************************************************
2  * Copyright (c) 2003, 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 package org.eclipse.pde.internal.ui.editor.schema;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.zip.ZipException JavaDoc;
16 import java.util.zip.ZipFile JavaDoc;
17
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.IStorage;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.pde.core.plugin.IPluginModelBase;
23 import org.eclipse.pde.core.plugin.PluginRegistry;
24 import org.eclipse.pde.internal.core.ischema.ISchema;
25 import org.eclipse.pde.internal.core.ischema.ISchemaElement;
26 import org.eclipse.pde.internal.core.ischema.ISchemaObject;
27 import org.eclipse.pde.internal.ui.IPDEUIConstants;
28 import org.eclipse.pde.internal.ui.PDEPlugin;
29 import org.eclipse.pde.internal.ui.editor.ISortableContentOutlinePage;
30 import org.eclipse.pde.internal.ui.editor.JarEntryEditorInput;
31 import org.eclipse.pde.internal.ui.editor.JarEntryFile;
32 import org.eclipse.pde.internal.ui.editor.MultiSourceEditor;
33 import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
34 import org.eclipse.pde.internal.ui.editor.PDESourcePage;
35 import org.eclipse.pde.internal.ui.editor.SystemFileEditorInput;
36 import org.eclipse.pde.internal.ui.editor.context.InputContext;
37 import org.eclipse.pde.internal.ui.editor.context.InputContextManager;
38 import org.eclipse.pde.internal.ui.search.ShowDescriptionAction;
39 import org.eclipse.swt.widgets.Display;
40 import org.eclipse.ui.IEditorInput;
41 import org.eclipse.ui.IEditorPart;
42 import org.eclipse.ui.IFileEditorInput;
43 import org.eclipse.ui.IStorageEditorInput;
44 import org.eclipse.ui.PartInitException;
45 import org.eclipse.ui.part.FileEditorInput;
46
47 public class SchemaEditor extends MultiSourceEditor {
48     private ShowDescriptionAction fPreviewAction;
49
50     /* (non-Javadoc)
51      * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#getEditorID()
52      */

53     protected String JavaDoc getEditorID() {
54         return IPDEUIConstants.SCHEMA_EDITOR_ID;
55     }
56     
57     /* (non-Javadoc)
58      * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#isSaveAsAllowed()
59      */

60     public boolean isSaveAsAllowed() {
61         return true;
62     }
63     
64     /* (non-Javadoc)
65      * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#getContextIDForSaveAs()
66      */

67     public String JavaDoc getContextIDForSaveAs() {
68         return SchemaInputContext.CONTEXT_ID;
69     }
70     
71     protected void createResourceContexts(InputContextManager manager, IFileEditorInput input) {
72         IFile file = input.getFile();
73         IFileEditorInput in = new FileEditorInput(file);
74         manager.putContext(in, new SchemaInputContext(this, in, true));
75         manager.monitorFile(file);
76     }
77     
78     protected InputContextManager createInputContextManager() {
79         SchemaInputContextManager contextManager = new SchemaInputContextManager(this);
80         //contextManager.setUndoManager(new SchemaUndoManager(this));
81
return contextManager;
82     }
83     
84
85     public void monitoredFileAdded(IFile file) {
86         /*
87         String name = file.getName();
88         if (name.equalsIgnoreCase("site.xml")) {
89             IEditorInput in = new FileEditorInput(file);
90             inputContextManager.putContext(in, new SiteInputContext(this, in, false));
91         }
92         */

93     }
94
95     public boolean monitoredFileRemoved(IFile file) {
96         /*
97         //TODO may need to check with the user if there
98         //are unsaved changes in the model for the
99         //file that just got removed under us.
100          * */

101         return true;
102     }
103
104     public void editorContextAdded(InputContext context) {
105         addSourcePage(context.getId());
106     }
107     public void contextRemoved(InputContext context) {
108         close(false);
109     }
110
111     protected void createSystemFileContexts(InputContextManager manager, SystemFileEditorInput input) {
112         manager.putContext(input, new SchemaInputContext(this, input, true));
113     }
114
115     protected void createStorageContexts(InputContextManager manager, IStorageEditorInput input) {
116         manager.putContext(input, new SchemaInputContext(this, input, true));
117     }
118     
119     void previewReferenceDocument() {
120         ISchema schema = (ISchema) getAggregateModel();
121         if (fPreviewAction == null)
122             fPreviewAction = new ShowDescriptionAction(schema);
123         else
124             fPreviewAction.setSchema(schema);
125         fPreviewAction.run();
126     }
127
128     protected void addEditorPages() {
129         try {
130             addPage(new SchemaOverviewPage(this));
131             addPage(new SchemaFormPage(this));
132         } catch (PartInitException e) {
133             PDEPlugin.logException(e);
134         }
135         addSourcePage(SchemaInputContext.CONTEXT_ID);
136     }
137
138
139     protected String JavaDoc computeInitialPageId() {
140         String JavaDoc firstPageId = super.computeInitialPageId();
141         if (firstPageId == null) {
142             firstPageId = SchemaOverviewPage.PAGE_ID;
143         }
144         return firstPageId;
145     }
146     
147     /* (non-Javadoc)
148      * @see org.eclipse.pde.internal.ui.neweditor.MultiSourceEditor#createXMLSourcePage(org.eclipse.pde.internal.ui.neweditor.PDEFormEditor, java.lang.String, java.lang.String)
149      */

150     protected PDESourcePage createSourcePage(PDEFormEditor editor, String JavaDoc title, String JavaDoc name, String JavaDoc contextId) {
151         return new SchemaSourcePage(editor, title, name);
152     }
153     
154     protected ISortableContentOutlinePage createContentOutline() {
155         return new SchemaFormOutlinePage(this);
156     }
157     
158     /* (non-Javadoc)
159      * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#getInputContext(java.lang.Object)
160      */

161     protected InputContext getInputContext(Object JavaDoc object) {
162         InputContext context = null;
163         if (object instanceof ISchemaObject) {
164             context = fInputContextManager.findContext(SchemaInputContext.CONTEXT_ID);
165         }
166         return context;
167     }
168     
169     public static boolean openSchema(IFile file) {
170         if (file != null && file.exists()) {
171             IEditorInput input = new FileEditorInput(file);
172             try {
173                 return PDEPlugin.getActivePage().openEditor(input, IPDEUIConstants.SCHEMA_EDITOR_ID) != null;
174             } catch (PartInitException e) {
175             }
176         }
177         Display.getDefault().beep();
178         return false;
179     }
180
181     public static boolean openSchema(IPath path) {
182         String JavaDoc pluginId = path.segment(0);
183         IPluginModelBase model = PluginRegistry.findModel(pluginId);
184         if (model != null && model.getUnderlyingResource() != null) {
185             IProject project = model.getUnderlyingResource().getProject();
186             IFile file = project.getFile(path.removeFirstSegments(1));
187             return openSchema(file);
188         }
189         Display.getDefault().beep();
190         return false;
191     }
192     
193     /**
194      * @param file
195      * @return
196      */

197     public static boolean openSchema(File JavaDoc file) {
198         // Ensure the file exists
199
if ((file == null) ||
200                 (file.exists() == false)) {
201             Display.getDefault().beep();
202             return false;
203         }
204         // Create the editor input
205
IEditorInput input = new SystemFileEditorInput(file);
206         return openEditor(input);
207     }
208
209     /**
210      * @param input
211      */

212     private static boolean openEditor(IEditorInput input) {
213         IEditorPart part = null;
214         try {
215             // Open the schema editor using the editor input
216
part = PDEPlugin.getActivePage().openEditor(input,
217                     IPDEUIConstants.SCHEMA_EDITOR_ID);
218         } catch (PartInitException e) {
219             Display.getDefault().beep();
220             return false;
221         }
222         // Ensure the schema editor was opened properly
223
if (part == null) {
224             Display.getDefault().beep();
225             return false;
226         }
227         return true;
228     }
229     
230     /**
231      * @param jarFile
232      * @param schemaJarFileEntry
233      * @return
234      */

235     public static boolean openSchema(File JavaDoc jarFile, String JavaDoc schemaJarFileEntry) {
236         // Ensure the file exists
237
if ((jarFile == null) ||
238                 (jarFile.exists() == false)) {
239             Display.getDefault().beep();
240             return false;
241         }
242         // Open the jar archive
243
ZipFile JavaDoc zipFile;
244         try {
245             zipFile = new ZipFile JavaDoc(jarFile);
246         } catch (ZipException JavaDoc e) {
247             Display.getDefault().beep();
248             return false;
249         } catch (IOException JavaDoc e) {
250             Display.getDefault().beep();
251             return false;
252         }
253         // Ensure the schema file exists in the jar archive
254
if ((schemaJarFileEntry == null) ||
255                 zipFile.getEntry(schemaJarFileEntry) == null) {
256             Display.getDefault().beep();
257             return false;
258         }
259         // Create the editor input
260
IStorage storage = new JarEntryFile(zipFile, schemaJarFileEntry);
261         IEditorInput input = new JarEntryEditorInput(storage);
262         return openEditor(input);
263     }
264     
265     
266     public static void openToElement(IPath path, ISchemaElement element) {
267         if (openSchema(path)) {
268             IEditorPart editorPart = PDEPlugin.getActivePage().getActiveEditor();
269             if (!(editorPart instanceof SchemaEditor))
270                 return; // something messed up, schema editor should be open
271
SchemaEditor schemaEditor = (SchemaEditor)editorPart;
272             schemaEditor.selectReveal(element);
273         }
274     }
275
276 }
277
Popular Tags