KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.ByteArrayInputStream JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.io.PrintWriter JavaDoc;
18 import java.io.StringWriter JavaDoc;
19 import java.io.UnsupportedEncodingException JavaDoc;
20 import java.util.ArrayList JavaDoc;
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 /**
44  *
45  */

46 public class SchemaInputContext extends XMLInputContext {
47     public static final String JavaDoc CONTEXT_ID="schema-context"; //$NON-NLS-1$
48
/**
49      * @param editor
50      * @param input
51      * @param primary
52      */

53     public SchemaInputContext(PDEFormEditor editor, IEditorInput input,
54             boolean primary) {
55         super(editor, input, primary);
56         create();
57     }
58     /* (non-Javadoc)
59      * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#getId()
60      */

61     public String JavaDoc getId() {
62         return CONTEXT_ID;
63     }
64     /* (non-Javadoc)
65      * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#createModel(org.eclipse.ui.IEditorInput)
66      */

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 JavaDoc file = (File JavaDoc)input.getAdapter(File JavaDoc.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     /* (non-Javadoc)
110      * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#addTextEditOperation(java.util.ArrayList, org.eclipse.pde.core.IModelChangedEvent)
111      */

112     protected void addTextEditOperation(ArrayList JavaDoc ops, IModelChangedEvent event) {
113     }
114     protected void flushModel(IDocument doc) {
115         // if model is dirty, flush its content into
116
// the document so that the source editor will
117
// pick up the changes.
118
if (!(getModel() instanceof IEditable))
119             return;
120         IEditable editableModel = (IEditable) getModel();
121         if (editableModel.isDirty() == false)
122             return;
123         try {
124             StringWriter JavaDoc swriter = new StringWriter JavaDoc();
125             PrintWriter JavaDoc writer = new PrintWriter JavaDoc(swriter);
126             editableModel.save(writer);
127             writer.flush();
128             swriter.close();
129             doc.set(swriter.toString());
130         } catch (IOException JavaDoc e) {
131             PDEPlugin.logException(e);
132         }
133     }
134     
135     /* (non-Javadoc)
136      * @see org.eclipse.pde.internal.ui.editor.context.InputContext#flushEditorInput()
137      */

138     public void flushEditorInput() {
139         // Override parent, since this editor does not utilize edit operations
140
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             // if model is null try to recreate it
153
create();
154             return getModel() == null;
155         }
156         String JavaDoc text = doc.get();
157         try {
158             InputStream JavaDoc stream =
159                 new ByteArrayInputStream JavaDoc(text.getBytes("UTF8")); //$NON-NLS-1$
160
schema.reload(stream);
161             if (schema instanceof IEditable)
162                ((IEditable)schema).setDirty(false);
163             try {
164                 stream.close();
165             } catch (IOException JavaDoc e) {
166             }
167         } catch (UnsupportedEncodingException JavaDoc e) {
168             PDEPlugin.logException(e);
169             return false;
170         }
171         return true;
172     }
173     /* (non-Javadoc)
174      * @see org.eclipse.pde.internal.ui.neweditor.context.XMLInputContext#reorderInsertEdits(java.util.ArrayList)
175      */

176     protected void reorderInsertEdits(ArrayList JavaDoc ops) {
177     }
178     protected String JavaDoc getPartitionName() {
179         return "___schema_partition"; //$NON-NLS-1$
180
}
181 }
182
Popular Tags