1 11 package org.eclipse.pde.internal.ui.editor.schema; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.File ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.io.PrintWriter ; 18 import java.io.StringWriter ; 19 import java.io.UnsupportedEncodingException ; 20 import java.util.ArrayList ; 21 22 import org.eclipse.core.resources.IFile; 23 import org.eclipse.core.resources.IStorage; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.jface.text.IDocument; 26 import org.eclipse.pde.core.IBaseModel; 27 import org.eclipse.pde.core.IEditable; 28 import org.eclipse.pde.core.IModelChangedEvent; 29 import org.eclipse.pde.internal.core.ischema.ISchema; 30 import org.eclipse.pde.internal.core.schema.EditableSchema; 31 import org.eclipse.pde.internal.core.schema.Schema; 32 import org.eclipse.pde.internal.core.schema.SchemaDescriptor; 33 import org.eclipse.pde.internal.core.schema.StorageSchemaDescriptor; 34 import org.eclipse.pde.internal.ui.PDEPlugin; 35 import org.eclipse.pde.internal.ui.editor.PDEFormEditor; 36 import org.eclipse.pde.internal.ui.editor.SystemFileEditorInput; 37 import org.eclipse.pde.internal.ui.editor.context.XMLInputContext; 38 import org.eclipse.ui.IEditorInput; 39 import org.eclipse.ui.IFileEditorInput; 40 import org.eclipse.ui.IStorageEditorInput; 41 import org.eclipse.ui.texteditor.IDocumentProvider; 42 43 46 public class SchemaInputContext extends XMLInputContext { 47 public static final String CONTEXT_ID="schema-context"; 53 public SchemaInputContext(PDEFormEditor editor, IEditorInput input, 54 boolean primary) { 55 super(editor, input, primary); 56 create(); 57 } 58 61 public String getId() { 62 return CONTEXT_ID; 63 } 64 67 protected IBaseModel createModel(IEditorInput input) throws CoreException { 68 if (input instanceof SystemFileEditorInput) 69 return createExternalModel((SystemFileEditorInput)input); 70 71 if (!(input instanceof IFileEditorInput)) { 72 if (input instanceof IStorageEditorInput) 73 return createStorageModel((IStorageEditorInput)input); 74 return null; 75 } 76 77 IFile file = ((IFileEditorInput)input).getFile(); 78 SchemaDescriptor sd = new SchemaDescriptor(file, true); 79 ISchema schema = sd.getSchema(false); 80 if (schema instanceof EditableSchema) { 81 ((EditableSchema) schema).setNotificationEnabled(true); 82 } 83 return schema; 84 } 85 86 private IBaseModel createExternalModel(SystemFileEditorInput input) { 87 File file = (File )input.getAdapter(File .class); 88 SchemaDescriptor sd = new SchemaDescriptor(file); 89 90 ISchema schema = sd.getSchema(false); 91 if (schema instanceof EditableSchema) { 92 ((EditableSchema) schema).setNotificationEnabled(true); 93 } 94 return schema; 95 } 96 97 private IBaseModel createStorageModel(IStorageEditorInput input) { 98 try { 99 IStorage storage = input.getStorage(); 100 StorageSchemaDescriptor sd = new StorageSchemaDescriptor(storage); 101 ISchema schema = sd.getSchema(false); 102 return schema; 103 } 104 catch (CoreException e) { 105 PDEPlugin.logException(e); 106 return null; 107 } 108 } 109 112 protected void addTextEditOperation(ArrayList ops, IModelChangedEvent event) { 113 } 114 protected void flushModel(IDocument doc) { 115 if (!(getModel() instanceof IEditable)) 119 return; 120 IEditable editableModel = (IEditable) getModel(); 121 if (editableModel.isDirty() == false) 122 return; 123 try { 124 StringWriter swriter = new StringWriter (); 125 PrintWriter writer = new PrintWriter (swriter); 126 editableModel.save(writer); 127 writer.flush(); 128 swriter.close(); 129 doc.set(swriter.toString()); 130 } catch (IOException e) { 131 PDEPlugin.logException(e); 132 } 133 } 134 135 138 public void flushEditorInput() { 139 IDocumentProvider provider = getDocumentProvider(); 141 IEditorInput input = getInput(); 142 IDocument doc = provider.getDocument(input); 143 provider.aboutToChange(input); 144 flushModel(doc); 145 provider.changed(input); 146 setValidated(false); 147 } 148 149 protected boolean synchronizeModel(IDocument doc) { 150 Schema schema = (Schema) getModel(); 151 if (schema == null) { 152 create(); 154 return getModel() == null; 155 } 156 String text = doc.get(); 157 try { 158 InputStream stream = 159 new ByteArrayInputStream (text.getBytes("UTF8")); schema.reload(stream); 161 if (schema instanceof IEditable) 162 ((IEditable)schema).setDirty(false); 163 try { 164 stream.close(); 165 } catch (IOException e) { 166 } 167 } catch (UnsupportedEncodingException e) { 168 PDEPlugin.logException(e); 169 return false; 170 } 171 return true; 172 } 173 176 protected void reorderInsertEdits(ArrayList ops) { 177 } 178 protected String getPartitionName() { 179 return "___schema_partition"; } 181 } 182 | Popular Tags |