KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > site > SiteInputContext


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.site;
12 import java.io.BufferedInputStream JavaDoc;
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.PrintWriter JavaDoc;
17 import java.io.StringWriter JavaDoc;
18 import java.io.UnsupportedEncodingException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.pde.core.IBaseModel;
25 import org.eclipse.pde.core.IEditable;
26 import org.eclipse.pde.core.IModelChangedEvent;
27 import org.eclipse.pde.internal.core.isite.ISiteModel;
28 import org.eclipse.pde.internal.core.site.ExternalSiteModel;
29 import org.eclipse.pde.internal.core.site.WorkspaceSiteModel;
30 import org.eclipse.pde.internal.ui.PDEPlugin;
31 import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
32 import org.eclipse.pde.internal.ui.editor.context.XMLInputContext;
33 import org.eclipse.ui.IEditorInput;
34 import org.eclipse.ui.IFileEditorInput;
35 import org.eclipse.ui.IStorageEditorInput;
36
37 public class SiteInputContext extends XMLInputContext {
38     public static final String JavaDoc CONTEXT_ID = "site-context"; //$NON-NLS-1$
39
private boolean storageModel=false;
40     /**
41      * @param editor
42      * @param input
43      */

44     public SiteInputContext(PDEFormEditor editor, IEditorInput input,
45             boolean primary) {
46         super(editor, input, primary);
47         create();
48     }
49
50     protected IBaseModel createModel(IEditorInput input) {
51         IBaseModel model = null;
52         if (input instanceof IStorageEditorInput) {
53             InputStream JavaDoc is = null;
54             try {
55                 if (input instanceof IFileEditorInput) {
56                     IFile file = ((IFileEditorInput) input).getFile();
57                     is = new BufferedInputStream JavaDoc(file.getContents());
58                     model = createWorkspaceModel(file, is, true);
59                 } else if (input instanceof IStorageEditorInput) {
60                     is = new BufferedInputStream JavaDoc(((IStorageEditorInput) input).getStorage()
61                             .getContents());
62                     model = createStorageModel(is);
63                 }
64             } catch (CoreException e) {
65                 PDEPlugin.logException(e);
66                 return null;
67             }
68         }
69         return model;
70     }
71
72     private IBaseModel createWorkspaceModel(IFile file, InputStream JavaDoc stream,
73             boolean editable) {
74         WorkspaceSiteModel model = new WorkspaceSiteModel(file);
75         try {
76             model.setEditable(editable);
77             model.load(stream, false);
78         } catch (CoreException e) {
79         }
80         try {
81             stream.close();
82         } catch (IOException JavaDoc e) {
83             PDEPlugin.logException(e);
84         }
85         return model;
86     }
87     
88     private IBaseModel createStorageModel(InputStream JavaDoc stream) {
89         ExternalSiteModel model = new ExternalSiteModel();
90         try {
91             model.load(stream, true);
92         } catch (CoreException e) {
93         } finally {
94             try {
95                 stream.close();
96             } catch (IOException JavaDoc e1) {
97             }
98         }
99         return model;
100     }
101
102     public void dispose() {
103         ISiteModel model = (ISiteModel) getModel();
104         if (storageModel) {
105             model.dispose();
106         }
107         super.dispose();
108     }
109     protected void flushModel(IDocument doc) {
110         // if model is dirty, flush its content into
111
// the document so that the source editor will
112
// pick up the changes.
113
if (!(getModel() instanceof IEditable))
114             return;
115         IEditable editableModel = (IEditable) getModel();
116         if (editableModel.isEditable()==false) return;
117         if (editableModel.isDirty() == false) return;
118         try {
119             StringWriter JavaDoc swriter = new StringWriter JavaDoc();
120             PrintWriter JavaDoc writer = new PrintWriter JavaDoc(swriter);
121             editableModel.save(writer);
122             writer.flush();
123             swriter.close();
124             doc.set(swriter.toString());
125         } catch (IOException JavaDoc e) {
126             PDEPlugin.logException(e);
127         }
128     }
129     
130     protected boolean synchronizeModel(IDocument doc) {
131         ISiteModel model = (ISiteModel) getModel();
132         boolean cleanModel = true;
133         String JavaDoc text = doc.get();
134         try {
135             InputStream JavaDoc stream =
136                 new ByteArrayInputStream JavaDoc(text.getBytes("UTF8")); //$NON-NLS-1$
137
try {
138                 model.reload(stream, false);
139             } catch (CoreException e) {
140                 cleanModel = false;
141             }
142             try {
143                 stream.close();
144             } catch (IOException JavaDoc e) {
145             }
146         } catch (UnsupportedEncodingException JavaDoc e) {
147             PDEPlugin.logException(e);
148         }
149         return cleanModel;
150     }
151     /*
152      * (non-Javadoc)
153      *
154      * @see org.eclipse.pde.internal.ui.neweditor.InputContext#getId()
155      */

156     public String JavaDoc getId() {
157         return CONTEXT_ID;
158     }
159     /*
160      * (non-Javadoc)
161      *
162      * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#addTextEditOperation(java.util.ArrayList,
163      * org.eclipse.pde.core.IModelChangedEvent)
164      */

165     protected void addTextEditOperation(ArrayList JavaDoc ops, IModelChangedEvent event) {
166     }
167
168     /* (non-Javadoc)
169      * @see org.eclipse.pde.internal.ui.neweditor.context.XMLInputContext#reorderInsertEdits(java.util.ArrayList)
170      */

171     protected void reorderInsertEdits(ArrayList JavaDoc ops) {
172     }
173
174     protected String JavaDoc getPartitionName() {
175         return "___site_partition"; //$NON-NLS-1$
176
}
177 }
178
Popular Tags