1 11 package org.eclipse.pde.internal.ui.editor.schema; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.util.zip.ZipException ; 16 import java.util.zip.ZipFile ; 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 53 protected String getEditorID() { 54 return IPDEUIConstants.SCHEMA_EDITOR_ID; 55 } 56 57 60 public boolean isSaveAsAllowed() { 61 return true; 62 } 63 64 67 public String 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 return contextManager; 82 } 83 84 85 public void monitoredFileAdded(IFile file) { 86 93 } 94 95 public boolean monitoredFileRemoved(IFile file) { 96 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 computeInitialPageId() { 140 String firstPageId = super.computeInitialPageId(); 141 if (firstPageId == null) { 142 firstPageId = SchemaOverviewPage.PAGE_ID; 143 } 144 return firstPageId; 145 } 146 147 150 protected PDESourcePage createSourcePage(PDEFormEditor editor, String title, String name, String contextId) { 151 return new SchemaSourcePage(editor, title, name); 152 } 153 154 protected ISortableContentOutlinePage createContentOutline() { 155 return new SchemaFormOutlinePage(this); 156 } 157 158 161 protected InputContext getInputContext(Object 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 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 197 public static boolean openSchema(File file) { 198 if ((file == null) || 200 (file.exists() == false)) { 201 Display.getDefault().beep(); 202 return false; 203 } 204 IEditorInput input = new SystemFileEditorInput(file); 206 return openEditor(input); 207 } 208 209 212 private static boolean openEditor(IEditorInput input) { 213 IEditorPart part = null; 214 try { 215 part = PDEPlugin.getActivePage().openEditor(input, 217 IPDEUIConstants.SCHEMA_EDITOR_ID); 218 } catch (PartInitException e) { 219 Display.getDefault().beep(); 220 return false; 221 } 222 if (part == null) { 224 Display.getDefault().beep(); 225 return false; 226 } 227 return true; 228 } 229 230 235 public static boolean openSchema(File jarFile, String schemaJarFileEntry) { 236 if ((jarFile == null) || 238 (jarFile.exists() == false)) { 239 Display.getDefault().beep(); 240 return false; 241 } 242 ZipFile zipFile; 244 try { 245 zipFile = new ZipFile (jarFile); 246 } catch (ZipException e) { 247 Display.getDefault().beep(); 248 return false; 249 } catch (IOException e) { 250 Display.getDefault().beep(); 251 return false; 252 } 253 if ((schemaJarFileEntry == null) || 255 zipFile.getEntry(schemaJarFileEntry) == null) { 256 Display.getDefault().beep(); 257 return false; 258 } 259 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; SchemaEditor schemaEditor = (SchemaEditor)editorPart; 272 schemaEditor.selectReveal(element); 273 } 274 } 275 276 } 277 | Popular Tags |