001: /*******************************************************************************
002: * Copyright (c) 2000, 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.ByteArrayInputStream;
013: import java.io.File;
014: import java.io.IOException;
015: import java.io.InputStream;
016: import java.io.PrintWriter;
017: import java.io.StringWriter;
018: import java.io.UnsupportedEncodingException;
019: import java.util.ArrayList;
020:
021: import org.eclipse.core.resources.IFile;
022: import org.eclipse.core.resources.IStorage;
023: import org.eclipse.core.runtime.CoreException;
024: import org.eclipse.jface.text.IDocument;
025: import org.eclipse.pde.core.IBaseModel;
026: import org.eclipse.pde.core.IEditable;
027: import org.eclipse.pde.core.IModelChangedEvent;
028: import org.eclipse.pde.internal.core.ischema.ISchema;
029: import org.eclipse.pde.internal.core.schema.EditableSchema;
030: import org.eclipse.pde.internal.core.schema.Schema;
031: import org.eclipse.pde.internal.core.schema.SchemaDescriptor;
032: import org.eclipse.pde.internal.core.schema.StorageSchemaDescriptor;
033: import org.eclipse.pde.internal.ui.PDEPlugin;
034: import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
035: import org.eclipse.pde.internal.ui.editor.SystemFileEditorInput;
036: import org.eclipse.pde.internal.ui.editor.context.XMLInputContext;
037: import org.eclipse.ui.IEditorInput;
038: import org.eclipse.ui.IFileEditorInput;
039: import org.eclipse.ui.IStorageEditorInput;
040: import org.eclipse.ui.texteditor.IDocumentProvider;
041:
042: /**
043: *
044: */
045: public class SchemaInputContext extends XMLInputContext {
046: public static final String CONTEXT_ID = "schema-context"; //$NON-NLS-1$
047:
048: /**
049: * @param editor
050: * @param input
051: * @param primary
052: */
053: public SchemaInputContext(PDEFormEditor editor, IEditorInput input,
054: boolean primary) {
055: super (editor, input, primary);
056: create();
057: }
058:
059: /* (non-Javadoc)
060: * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#getId()
061: */
062: public String getId() {
063: return CONTEXT_ID;
064: }
065:
066: /* (non-Javadoc)
067: * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#createModel(org.eclipse.ui.IEditorInput)
068: */
069: protected IBaseModel createModel(IEditorInput input)
070: throws CoreException {
071: if (input instanceof SystemFileEditorInput)
072: return createExternalModel((SystemFileEditorInput) input);
073:
074: if (!(input instanceof IFileEditorInput)) {
075: if (input instanceof IStorageEditorInput)
076: return createStorageModel((IStorageEditorInput) input);
077: return null;
078: }
079:
080: IFile file = ((IFileEditorInput) input).getFile();
081: SchemaDescriptor sd = new SchemaDescriptor(file, true);
082: ISchema schema = sd.getSchema(false);
083: if (schema instanceof EditableSchema) {
084: ((EditableSchema) schema).setNotificationEnabled(true);
085: }
086: return schema;
087: }
088:
089: private IBaseModel createExternalModel(SystemFileEditorInput input) {
090: File file = (File) input.getAdapter(File.class);
091: SchemaDescriptor sd = new SchemaDescriptor(file);
092:
093: ISchema schema = sd.getSchema(false);
094: if (schema instanceof EditableSchema) {
095: ((EditableSchema) schema).setNotificationEnabled(true);
096: }
097: return schema;
098: }
099:
100: private IBaseModel createStorageModel(IStorageEditorInput input) {
101: try {
102: IStorage storage = input.getStorage();
103: StorageSchemaDescriptor sd = new StorageSchemaDescriptor(
104: storage);
105: ISchema schema = sd.getSchema(false);
106: return schema;
107: } catch (CoreException e) {
108: PDEPlugin.logException(e);
109: return null;
110: }
111: }
112:
113: /* (non-Javadoc)
114: * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#addTextEditOperation(java.util.ArrayList, org.eclipse.pde.core.IModelChangedEvent)
115: */
116: protected void addTextEditOperation(ArrayList ops,
117: IModelChangedEvent event) {
118: }
119:
120: protected void flushModel(IDocument doc) {
121: // if model is dirty, flush its content into
122: // the document so that the source editor will
123: // pick up the changes.
124: if (!(getModel() instanceof IEditable))
125: return;
126: IEditable editableModel = (IEditable) getModel();
127: if (editableModel.isDirty() == false)
128: return;
129: try {
130: StringWriter swriter = new StringWriter();
131: PrintWriter writer = new PrintWriter(swriter);
132: editableModel.save(writer);
133: writer.flush();
134: swriter.close();
135: doc.set(swriter.toString());
136: } catch (IOException e) {
137: PDEPlugin.logException(e);
138: }
139: }
140:
141: /* (non-Javadoc)
142: * @see org.eclipse.pde.internal.ui.editor.context.InputContext#flushEditorInput()
143: */
144: public void flushEditorInput() {
145: // Override parent, since this editor does not utilize edit operations
146: IDocumentProvider provider = getDocumentProvider();
147: IEditorInput input = getInput();
148: IDocument doc = provider.getDocument(input);
149: provider.aboutToChange(input);
150: flushModel(doc);
151: provider.changed(input);
152: setValidated(false);
153: }
154:
155: protected boolean synchronizeModel(IDocument doc) {
156: Schema schema = (Schema) getModel();
157: if (schema == null) {
158: // if model is null try to recreate it
159: create();
160: return getModel() == null;
161: }
162: String text = doc.get();
163: try {
164: InputStream stream = new ByteArrayInputStream(text
165: .getBytes("UTF8")); //$NON-NLS-1$
166: schema.reload(stream);
167: if (schema instanceof IEditable)
168: ((IEditable) schema).setDirty(false);
169: try {
170: stream.close();
171: } catch (IOException e) {
172: }
173: } catch (UnsupportedEncodingException e) {
174: PDEPlugin.logException(e);
175: return false;
176: }
177: return true;
178: }
179:
180: /* (non-Javadoc)
181: * @see org.eclipse.pde.internal.ui.neweditor.context.XMLInputContext#reorderInsertEdits(java.util.ArrayList)
182: */
183: protected void reorderInsertEdits(ArrayList ops) {
184: }
185:
186: protected String getPartitionName() {
187: return "___schema_partition"; //$NON-NLS-1$
188: }
189: }
|