KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > text > AbstractEditingModel


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.core.text;
12
13 import java.io.BufferedInputStream JavaDoc;
14 import java.io.ByteArrayInputStream JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.PrintWriter JavaDoc;
17 import java.io.UnsupportedEncodingException JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.core.runtime.PlatformObject;
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.pde.core.IModelChangedEvent;
27 import org.eclipse.pde.core.IModelChangedListener;
28 import org.eclipse.pde.core.ModelChangedEvent;
29 import org.eclipse.pde.internal.core.IModelChangeProviderExtension;
30 import org.eclipse.pde.internal.core.IModelChangedListenerFilter;
31 import org.eclipse.pde.internal.core.NLResourceHelper;
32
33 public abstract class AbstractEditingModel extends PlatformObject implements IEditingModel, IModelChangeProviderExtension {
34     private ArrayList JavaDoc fListeners = new ArrayList JavaDoc();
35     protected boolean fReconciling;
36     protected boolean fInSync = true;
37     protected boolean fLoaded = false;
38     protected boolean fDisposed;
39     protected long fTimestamp;
40     private transient NLResourceHelper fNLResourceHelper;
41     private IDocument fDocument;
42     private boolean fDirty;
43     private String JavaDoc fCharset;
44     private IResource fUnderlyingResource;
45     private String JavaDoc fInstallLocation;
46     private boolean fStale;
47     
48     public AbstractEditingModel(IDocument document, boolean isReconciling) {
49         fDocument = document;
50         fReconciling = isReconciling;
51     }
52     
53     /* (non-Javadoc)
54      * @see org.eclipse.pde.core.IModel#dispose()
55      */

56     public void dispose() {
57         if (fNLResourceHelper != null) {
58             fNLResourceHelper.dispose();
59             fNLResourceHelper = null;
60         }
61         fDisposed = true;
62         fListeners.clear();
63     }
64     
65     /* (non-Javadoc)
66      * @see org.eclipse.pde.core.IModel#getResourceString(java.lang.String)
67      */

68     public String JavaDoc getResourceString(String JavaDoc key) {
69         if (key == null || key.length() == 0)
70             return ""; //$NON-NLS-1$
71

72         if (fNLResourceHelper == null)
73             fNLResourceHelper = createNLResourceHelper();
74         
75         return (fNLResourceHelper == null) ? key : fNLResourceHelper.getResourceString(key);
76     }
77
78     protected abstract NLResourceHelper createNLResourceHelper();
79
80     /* (non-Javadoc)
81      * @see org.eclipse.pde.core.IModel#isDisposed()
82      */

83     public boolean isDisposed() {
84         return fDisposed;
85     }
86     /* (non-Javadoc)
87      * @see org.eclipse.pde.core.IModel#isEditable()
88      */

89     public boolean isEditable() {
90         return fReconciling;
91     }
92     /* (non-Javadoc)
93      * @see org.eclipse.pde.core.IModel#isLoaded()
94      */

95     public boolean isLoaded() {
96         return fLoaded;
97     }
98     /* (non-Javadoc)
99      * @see org.eclipse.pde.core.IModel#isInSync()
100      */

101     public boolean isInSync() {
102         return fInSync;
103     }
104     /* (non-Javadoc)
105      * @see org.eclipse.pde.core.IModel#isValid()
106      */

107     public boolean isValid() {
108         return isLoaded();
109     }
110     /* (non-Javadoc)
111      * @see org.eclipse.pde.core.IModel#getTimeStamp()
112      */

113     public final long getTimeStamp() {
114         return fTimestamp;
115     }
116     /* (non-Javadoc)
117      * @see org.eclipse.pde.core.IModel#load()
118      */

119     public final void load() throws CoreException {
120         try {
121             load(getInputStream(getDocument()), false);
122         } catch (UnsupportedEncodingException JavaDoc e) {
123         }
124     }
125     /* (non-Javadoc)
126      * @see org.eclipse.pde.core.IModel#reload(java.io.InputStream, boolean)
127      */

128     public final void reload(InputStream JavaDoc source, boolean outOfSync)
129             throws CoreException {
130         load(source, outOfSync);
131         fireModelChanged(
132                 new ModelChangedEvent(this,
133                     IModelChangedEvent.WORLD_CHANGED,
134                     new Object JavaDoc[] {this},
135                     null));
136         
137     }
138     /* (non-Javadoc)
139      * @see org.eclipse.pde.core.IModel#isReconcilingModel()
140      */

141     public boolean isReconcilingModel() {
142         return fReconciling;
143     }
144     
145     public IDocument getDocument() {
146         return fDocument;
147     }
148     /* (non-Javadoc)
149      * @see org.eclipse.pde.internal.ui.neweditor.text.IReconcilingParticipant#reconciled(org.eclipse.jface.text.IDocument)
150      */

151     public final void reconciled(IDocument document) {
152         if (isReconcilingModel()) {
153             try {
154                 if (isStale()) {
155                     adjustOffsets(document);
156                     setStale(false);
157                 } else {
158                     reload(getInputStream(document), false);
159                 }
160             } catch (UnsupportedEncodingException JavaDoc e) {
161             } catch (CoreException e) {
162             }
163             if (isDirty())
164                 setDirty(false);
165         }
166     }
167     
168     public abstract void adjustOffsets(IDocument document) throws CoreException;
169     
170     protected InputStream JavaDoc getInputStream(IDocument document) throws UnsupportedEncodingException JavaDoc {
171         return new BufferedInputStream JavaDoc(new ByteArrayInputStream JavaDoc(document.get().getBytes(getCharset())));
172     }
173     
174     public String JavaDoc getCharset() {
175         return fCharset != null ? fCharset : "UTF-8"; //$NON-NLS-1$
176
}
177     
178     public void setCharset(String JavaDoc charset) {
179         fCharset = charset;
180     }
181     
182     /* (non-Javadoc)
183      * @see org.eclipse.pde.core.IModelChangeProvider#addModelChangedListener(org.eclipse.pde.core.IModelChangedListener)
184      */

185     public void addModelChangedListener(IModelChangedListener listener) {
186         if (!fListeners.contains(listener))
187             fListeners.add(listener);
188     }
189     public void transferListenersTo(IModelChangeProviderExtension target, IModelChangedListenerFilter filter) {
190         List JavaDoc oldList = (List JavaDoc)fListeners.clone();
191         for (int i=0; i<oldList.size(); i++) {
192             IModelChangedListener listener = (IModelChangedListener)oldList.get(i);
193             if (filter==null || filter.accept(listener)) {
194                 // add the listener to the target
195
target.addModelChangedListener(listener);
196                 // remove the listener from our list
197
fListeners.remove(listener);
198             }
199         }
200     }
201     
202     /* (non-Javadoc)
203      * @see org.eclipse.pde.core.IModelChangeProvider#fireModelChanged(org.eclipse.pde.core.IModelChangedEvent)
204      */

205     public void fireModelChanged(IModelChangedEvent event) {
206         if (event.getChangeType() == IModelChangedEvent.CHANGE &&
207                 event.getOldValue() != null &&
208                 event.getOldValue().equals(event.getNewValue()))
209             return;
210         setDirty(event.getChangeType() != IModelChangedEvent.WORLD_CHANGED);
211         for (int i = 0; i < fListeners.size(); i++) {
212             ((IModelChangedListener)fListeners.get(i)).modelChanged(event);
213         }
214     }
215     /* (non-Javadoc)
216      * @see org.eclipse.pde.core.IModelChangeProvider#fireModelObjectChanged(java.lang.Object, java.lang.String, java.lang.Object, java.lang.Object)
217      */

218     public void fireModelObjectChanged(Object JavaDoc object, String JavaDoc property, Object JavaDoc oldValue, Object JavaDoc newValue) {
219         fireModelChanged(new ModelChangedEvent(this, object, property, oldValue, newValue));
220     }
221     /* (non-Javadoc)
222      * @see org.eclipse.pde.core.IModelChangeProvider#removeModelChangedListener(org.eclipse.pde.core.IModelChangedListener)
223      */

224     public void removeModelChangedListener(IModelChangedListener listener) {
225         fListeners.remove(listener);
226     }
227     /* (non-Javadoc)
228      * @see org.eclipse.pde.core.IEditable#isDirty()
229      */

230     public boolean isDirty() {
231         return fDirty;
232     }
233     /* (non-Javadoc)
234      * @see org.eclipse.pde.core.IEditable#save(java.io.PrintWriter)
235      */

236     public void save(PrintWriter JavaDoc writer) {
237     }
238     /* (non-Javadoc)
239      * @see org.eclipse.pde.core.IEditable#setDirty(boolean)
240      */

241     public void setDirty(boolean dirty) {
242         this.fDirty = dirty;
243     }
244     
245     /* (non-Javadoc)
246      * @see org.eclipse.pde.internal.ui.model.IEditingModel#isStale()
247      */

248     public boolean isStale() {
249         return fStale;
250     }
251     
252     /* (non-Javadoc)
253      * @see org.eclipse.pde.internal.ui.model.IEditingModel#setStale(boolean)
254      */

255     public void setStale(boolean stale) {
256         fStale = stale;
257     }
258     /* (non-Javadoc)
259      * @see org.eclipse.pde.core.IModel#getUnderlyingResource()
260      */

261     public IResource getUnderlyingResource() {
262         return fUnderlyingResource;
263     }
264     
265     public void setUnderlyingResource(IResource resource) {
266         fUnderlyingResource = resource;
267     }
268
269     /* (non-Javadoc)
270      * @see org.eclipse.pde.core.plugin.ISharedPluginModel#getInstallLocation()
271      */

272     public String JavaDoc getInstallLocation() {
273         if (fInstallLocation == null && fUnderlyingResource != null) {
274             IPath path = fUnderlyingResource.getProject().getLocation();
275             return path != null ? path.addTrailingSeparator().toString() : null;
276         }
277         return fInstallLocation;
278     }
279     
280     public void setInstallLocation(String JavaDoc location) {
281         fInstallLocation = location;
282     }
283
284     public IModelTextChangeListener getLastTextChangeListener() {
285         for (int i = fListeners.size() - 1; i >= 0; i--) {
286             Object JavaDoc obj = fListeners.get(i);
287             if (obj instanceof IModelTextChangeListener)
288                 return (IModelTextChangeListener)obj;
289         }
290         return null;
291     }
292
293 }
Popular Tags