001: /*******************************************************************************
002: * Copyright (c) 2003, 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.schema;
011:
012: import java.io.File;
013: import java.io.IOException;
014: import java.util.zip.ZipException;
015: import java.util.zip.ZipFile;
016:
017: import org.eclipse.core.resources.IFile;
018: import org.eclipse.core.resources.IProject;
019: import org.eclipse.core.resources.IStorage;
020: import org.eclipse.core.runtime.IPath;
021: import org.eclipse.jface.viewers.ISelection;
022: import org.eclipse.jface.viewers.IStructuredSelection;
023: import org.eclipse.pde.core.plugin.IPluginModelBase;
024: import org.eclipse.pde.core.plugin.PluginRegistry;
025: import org.eclipse.pde.internal.core.ischema.ISchema;
026: import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
027: import org.eclipse.pde.internal.core.ischema.ISchemaElement;
028: import org.eclipse.pde.internal.core.ischema.ISchemaObject;
029: import org.eclipse.pde.internal.core.ischema.ISchemaRootElement;
030: import org.eclipse.pde.internal.ui.IPDEUIConstants;
031: import org.eclipse.pde.internal.ui.PDEPlugin;
032: import org.eclipse.pde.internal.ui.editor.ISortableContentOutlinePage;
033: import org.eclipse.pde.internal.ui.editor.JarEntryEditorInput;
034: import org.eclipse.pde.internal.ui.editor.JarEntryFile;
035: import org.eclipse.pde.internal.ui.editor.MultiSourceEditor;
036: import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
037: import org.eclipse.pde.internal.ui.editor.PDESourcePage;
038: import org.eclipse.pde.internal.ui.editor.SystemFileEditorInput;
039: import org.eclipse.pde.internal.ui.editor.context.InputContext;
040: import org.eclipse.pde.internal.ui.editor.context.InputContextManager;
041: import org.eclipse.pde.internal.ui.search.ShowDescriptionAction;
042: import org.eclipse.swt.widgets.Display;
043: import org.eclipse.ui.IEditorInput;
044: import org.eclipse.ui.IEditorPart;
045: import org.eclipse.ui.IFileEditorInput;
046: import org.eclipse.ui.IStorageEditorInput;
047: import org.eclipse.ui.PartInitException;
048: import org.eclipse.ui.part.FileEditorInput;
049:
050: public class SchemaEditor extends MultiSourceEditor {
051: private ShowDescriptionAction fPreviewAction;
052:
053: /* (non-Javadoc)
054: * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#getEditorID()
055: */
056: protected String getEditorID() {
057: return IPDEUIConstants.SCHEMA_EDITOR_ID;
058: }
059:
060: /* (non-Javadoc)
061: * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#isSaveAsAllowed()
062: */
063: public boolean isSaveAsAllowed() {
064: return true;
065: }
066:
067: /* (non-Javadoc)
068: * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#getContextIDForSaveAs()
069: */
070: public String getContextIDForSaveAs() {
071: return SchemaInputContext.CONTEXT_ID;
072: }
073:
074: protected void createResourceContexts(InputContextManager manager,
075: IFileEditorInput input) {
076: IFile file = input.getFile();
077: IFileEditorInput in = new FileEditorInput(file);
078: manager.putContext(in, new SchemaInputContext(this , in, true));
079: manager.monitorFile(file);
080: }
081:
082: protected InputContextManager createInputContextManager() {
083: SchemaInputContextManager contextManager = new SchemaInputContextManager(
084: this );
085: //contextManager.setUndoManager(new SchemaUndoManager(this));
086: return contextManager;
087: }
088:
089: public void monitoredFileAdded(IFile file) {
090: /*
091: String name = file.getName();
092: if (name.equalsIgnoreCase("site.xml")) {
093: IEditorInput in = new FileEditorInput(file);
094: inputContextManager.putContext(in, new SiteInputContext(this, in, false));
095: }
096: */
097: }
098:
099: public boolean monitoredFileRemoved(IFile file) {
100: /*
101: //TODO may need to check with the user if there
102: //are unsaved changes in the model for the
103: //file that just got removed under us.
104: * */
105: return true;
106: }
107:
108: public void editorContextAdded(InputContext context) {
109: addSourcePage(context.getId());
110: }
111:
112: public void contextRemoved(InputContext context) {
113: close(false);
114: }
115:
116: protected void createSystemFileContexts(
117: InputContextManager manager, SystemFileEditorInput input) {
118: manager.putContext(input, new SchemaInputContext(this , input,
119: true));
120: }
121:
122: protected void createStorageContexts(InputContextManager manager,
123: IStorageEditorInput input) {
124: manager.putContext(input, new SchemaInputContext(this , input,
125: true));
126: }
127:
128: void previewReferenceDocument() {
129: ISchema schema = (ISchema) getAggregateModel();
130: if (fPreviewAction == null)
131: fPreviewAction = new ShowDescriptionAction(schema);
132: else
133: fPreviewAction.setSchema(schema);
134: fPreviewAction.run();
135: }
136:
137: protected void addEditorPages() {
138: try {
139: addPage(new SchemaOverviewPage(this ));
140: addPage(new SchemaFormPage(this ));
141: } catch (PartInitException e) {
142: PDEPlugin.logException(e);
143: }
144: addSourcePage(SchemaInputContext.CONTEXT_ID);
145: }
146:
147: protected String computeInitialPageId() {
148: String firstPageId = super .computeInitialPageId();
149: if (firstPageId == null) {
150: firstPageId = SchemaOverviewPage.PAGE_ID;
151: }
152: return firstPageId;
153: }
154:
155: /* (non-Javadoc)
156: * @see org.eclipse.pde.internal.ui.neweditor.MultiSourceEditor#createXMLSourcePage(org.eclipse.pde.internal.ui.neweditor.PDEFormEditor, java.lang.String, java.lang.String)
157: */
158: protected PDESourcePage createSourcePage(PDEFormEditor editor,
159: String title, String name, String contextId) {
160: return new SchemaSourcePage(editor, title, name);
161: }
162:
163: protected ISortableContentOutlinePage createContentOutline() {
164: return new SchemaFormOutlinePage(this );
165: }
166:
167: /* (non-Javadoc)
168: * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#getInputContext(java.lang.Object)
169: */
170: protected InputContext getInputContext(Object object) {
171: InputContext context = null;
172: if (object instanceof ISchemaObject) {
173: context = fInputContextManager
174: .findContext(SchemaInputContext.CONTEXT_ID);
175: }
176: return context;
177: }
178:
179: public static boolean openSchema(IFile file) {
180: if (file != null && file.exists()) {
181: IEditorInput input = new FileEditorInput(file);
182: try {
183: return PDEPlugin.getActivePage().openEditor(input,
184: IPDEUIConstants.SCHEMA_EDITOR_ID) != null;
185: } catch (PartInitException e) {
186: }
187: }
188: Display.getDefault().beep();
189: return false;
190: }
191:
192: public static boolean openSchema(IPath path) {
193: String pluginId = path.segment(0);
194: IPluginModelBase model = PluginRegistry.findModel(pluginId);
195: if (model != null && model.getUnderlyingResource() != null) {
196: IProject project = model.getUnderlyingResource()
197: .getProject();
198: IFile file = project.getFile(path.removeFirstSegments(1));
199: return openSchema(file);
200: }
201: Display.getDefault().beep();
202: return false;
203: }
204:
205: /**
206: * @param file
207: * @return
208: */
209: public static boolean openSchema(File file) {
210: // Ensure the file exists
211: if ((file == null) || (file.exists() == false)) {
212: Display.getDefault().beep();
213: return false;
214: }
215: // Create the editor input
216: IEditorInput input = new SystemFileEditorInput(file);
217: return openEditor(input);
218: }
219:
220: /**
221: * @param input
222: */
223: private static boolean openEditor(IEditorInput input) {
224: IEditorPart part = null;
225: try {
226: // Open the schema editor using the editor input
227: part = PDEPlugin.getActivePage().openEditor(input,
228: IPDEUIConstants.SCHEMA_EDITOR_ID);
229: } catch (PartInitException e) {
230: Display.getDefault().beep();
231: return false;
232: }
233: // Ensure the schema editor was opened properly
234: if (part == null) {
235: Display.getDefault().beep();
236: return false;
237: }
238: return true;
239: }
240:
241: /**
242: * @param jarFile
243: * @param schemaJarFileEntry
244: * @return
245: */
246: public static boolean openSchema(File jarFile,
247: String schemaJarFileEntry) {
248: // Ensure the file exists
249: if ((jarFile == null) || (jarFile.exists() == false)) {
250: Display.getDefault().beep();
251: return false;
252: }
253: // Open the jar archive
254: ZipFile zipFile;
255: try {
256: zipFile = new ZipFile(jarFile);
257: } catch (ZipException e) {
258: Display.getDefault().beep();
259: return false;
260: } catch (IOException e) {
261: Display.getDefault().beep();
262: return false;
263: }
264: // Ensure the schema file exists in the jar archive
265: if ((schemaJarFileEntry == null)
266: || zipFile.getEntry(schemaJarFileEntry) == null) {
267: Display.getDefault().beep();
268: return false;
269: }
270: // Create the editor input
271: IStorage storage = new JarEntryFile(zipFile, schemaJarFileEntry);
272: IEditorInput input = new JarEntryEditorInput(storage);
273: return openEditor(input);
274: }
275:
276: public static void openToElement(IPath path, ISchemaElement element) {
277: if (openSchema(path)) {
278: IEditorPart editorPart = PDEPlugin.getActivePage()
279: .getActiveEditor();
280: if (!(editorPart instanceof SchemaEditor))
281: return; // something messed up, schema editor should be open
282: SchemaEditor schemaEditor = (SchemaEditor) editorPart;
283: schemaEditor.selectReveal(element);
284: }
285: }
286:
287: public boolean canCut(ISelection selection) {
288: if (selection instanceof IStructuredSelection) {
289: Object selected = ((IStructuredSelection) selection)
290: .getFirstElement();
291: if (selected instanceof ISchemaRootElement) {
292: return false;
293: } else if (selected instanceof ISchemaAttribute) {
294: if (((ISchemaAttribute) selected).getParent() instanceof ISchemaRootElement) {
295: return false;
296: }
297: }
298: }
299:
300: return super.canCut(selection);
301: }
302:
303: }
|