KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > model > AbstractEditingModel


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.model;
12
13 import java.io.*;
14 import java.util.*;
15
16 import org.eclipse.core.resources.*;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.jface.text.*;
19 import org.eclipse.pde.core.*;
20 import org.eclipse.pde.internal.core.*;
21
22 public abstract class AbstractEditingModel extends PlatformObject implements IEditingModel, IModelChangeProviderExtension {
23     private ArrayList fListeners = new ArrayList();
24     protected boolean fReconciling;
25     protected boolean fInSync = true;
26     protected boolean fLoaded = false;
27     protected boolean fDisposed;
28     protected long fTimestamp;
29     private transient NLResourceHelper fNLResourceHelper;
30     private IDocument fDocument;
31     private boolean fDirty;
32     private String JavaDoc fCharset;
33     private IResource fUnderlyingResource;
34     private String JavaDoc fInstallLocation;
35     private boolean fStale;
36     
37     public AbstractEditingModel(IDocument document, boolean isReconciling) {
38         fDocument = document;
39         fReconciling = isReconciling;
40     }
41     
42     /* (non-Javadoc)
43      * @see org.eclipse.pde.core.IModel#dispose()
44      */

45     public void dispose() {
46         if (fNLResourceHelper != null) {
47             fNLResourceHelper.dispose();
48             fNLResourceHelper = null;
49         }
50         fDisposed = true;
51         fListeners.clear();
52     }
53     
54     /* (non-Javadoc)
55      * @see org.eclipse.pde.core.IModel#getResourceString(java.lang.String)
56      */

57     public String JavaDoc getResourceString(String JavaDoc key) {
58         if (key == null || key.length() == 0)
59             return ""; //$NON-NLS-1$
60

61         if (fNLResourceHelper == null)
62             fNLResourceHelper = createNLResourceHelper();
63         
64         return (fNLResourceHelper == null) ? key : fNLResourceHelper.getResourceString(key);
65     }
66
67     protected abstract NLResourceHelper createNLResourceHelper();
68
69     /* (non-Javadoc)
70      * @see org.eclipse.pde.core.IModel#isDisposed()
71      */

72     public boolean isDisposed() {
73         return fDisposed;
74     }
75     /* (non-Javadoc)
76      * @see org.eclipse.pde.core.IModel#isEditable()
77      */

78     public boolean isEditable() {
79         return fReconciling;
80     }
81     /* (non-Javadoc)
82      * @see org.eclipse.pde.core.IModel#isLoaded()
83      */

84     public boolean isLoaded() {
85         return fLoaded;
86     }
87     /* (non-Javadoc)
88      * @see org.eclipse.pde.core.IModel#isInSync()
89      */

90     public boolean isInSync() {
91         return fInSync;
92     }
93     /* (non-Javadoc)
94      * @see org.eclipse.pde.core.IModel#isValid()
95      */

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

102     public final long getTimeStamp() {
103         return fTimestamp;
104     }
105     /* (non-Javadoc)
106      * @see org.eclipse.pde.core.IModel#load()
107      */

108     public final void load() throws CoreException {
109         try {
110             load(getInputStream(getDocument()), false);
111         } catch (UnsupportedEncodingException e) {
112         }
113     }
114     /* (non-Javadoc)
115      * @see org.eclipse.pde.core.IModel#reload(java.io.InputStream, boolean)
116      */

117     public final void reload(InputStream source, boolean outOfSync)
118             throws CoreException {
119         load(source, outOfSync);
120         fireModelChanged(
121                 new ModelChangedEvent(this,
122                     IModelChangedEvent.WORLD_CHANGED,
123                     new Object JavaDoc[] {this},
124                     null));
125         
126     }
127     /* (non-Javadoc)
128      * @see org.eclipse.pde.core.IModel#isReconcilingModel()
129      */

130     public boolean isReconcilingModel() {
131         return fReconciling;
132     }
133     
134     public IDocument getDocument() {
135         return fDocument;
136     }
137     /* (non-Javadoc)
138      * @see org.eclipse.pde.internal.ui.neweditor.text.IReconcilingParticipant#reconciled(org.eclipse.jface.text.IDocument)
139      */

140     public final void reconciled(IDocument document) {
141         if (isReconcilingModel()) {
142             try {
143                 if (isStale()) {
144                     adjustOffsets(document);
145                     setStale(false);
146                 } else {
147                     reload(getInputStream(document), false);
148                 }
149                 if (isDirty())
150                     setDirty(false);
151             } catch (Exception JavaDoc e) {
152             }
153         }
154     }
155     
156     protected abstract void adjustOffsets(IDocument document);
157     
158     protected InputStream getInputStream(IDocument document) throws UnsupportedEncodingException {
159         return new ByteArrayInputStream(document.get().getBytes(getCharset()));
160     }
161     
162     public String JavaDoc getCharset() {
163         return fCharset != null ? fCharset : "UTF-8"; //$NON-NLS-1$
164
}
165     
166     public void setCharset(String JavaDoc charset) {
167         fCharset = charset;
168     }
169     
170     /* (non-Javadoc)
171      * @see org.eclipse.pde.core.IModelChangeProvider#addModelChangedListener(org.eclipse.pde.core.IModelChangedListener)
172      */

173     public void addModelChangedListener(IModelChangedListener listener) {
174         if (!fListeners.contains(listener))
175             fListeners.add(listener);
176     }
177     public void transferListenersTo(IModelChangeProviderExtension target, IModelChangedListenerFilter filter) {
178         List oldList = (List)fListeners.clone();
179         for (int i=0; i<oldList.size(); i++) {
180             IModelChangedListener listener = (IModelChangedListener)oldList.get(i);
181             if (filter==null || filter.accept(listener)) {
182                 // add the listener to the target
183
target.addModelChangedListener(listener);
184                 // remove the listener from our list
185
fListeners.remove(listener);
186             }
187         }
188     }
189     
190     /* (non-Javadoc)
191      * @see org.eclipse.pde.core.IModelChangeProvider#fireModelChanged(org.eclipse.pde.core.IModelChangedEvent)
192      */

193     public void fireModelChanged(IModelChangedEvent event) {
194         setDirty(event.getChangeType() != IModelChangedEvent.WORLD_CHANGED);
195         for (int i = 0; i < fListeners.size(); i++) {
196             ((IModelChangedListener)fListeners.get(i)).modelChanged(event);
197         }
198     }
199     /* (non-Javadoc)
200      * @see org.eclipse.pde.core.IModelChangeProvider#fireModelObjectChanged(java.lang.Object, java.lang.String, java.lang.Object, java.lang.Object)
201      */

202     public void fireModelObjectChanged(Object JavaDoc object, String JavaDoc property, Object JavaDoc oldValue, Object JavaDoc newValue) {
203         fireModelChanged(new ModelChangedEvent(this, object, property, oldValue, newValue));
204     }
205     /* (non-Javadoc)
206      * @see org.eclipse.pde.core.IModelChangeProvider#removeModelChangedListener(org.eclipse.pde.core.IModelChangedListener)
207      */

208     public void removeModelChangedListener(IModelChangedListener listener) {
209         fListeners.remove(listener);
210     }
211     /* (non-Javadoc)
212      * @see org.eclipse.pde.core.IEditable#isDirty()
213      */

214     public boolean isDirty() {
215         return fDirty;
216     }
217     /* (non-Javadoc)
218      * @see org.eclipse.pde.core.IEditable#save(java.io.PrintWriter)
219      */

220     public void save(PrintWriter writer) {
221     }
222     /* (non-Javadoc)
223      * @see org.eclipse.pde.core.IEditable#setDirty(boolean)
224      */

225     public void setDirty(boolean dirty) {
226         this.fDirty = dirty;
227     }
228     
229     /* (non-Javadoc)
230      * @see org.eclipse.pde.internal.ui.model.IEditingModel#isStale()
231      */

232     public boolean isStale() {
233         return fStale;
234     }
235     
236     /* (non-Javadoc)
237      * @see org.eclipse.pde.internal.ui.model.IEditingModel#setStale(boolean)
238      */

239     public void setStale(boolean stale) {
240         fStale = stale;
241     }
242     /* (non-Javadoc)
243      * @see org.eclipse.pde.core.IModel#getUnderlyingResource()
244      */

245     public IResource getUnderlyingResource() {
246         return fUnderlyingResource;
247     }
248     
249     public void setUnderlyingResource(IResource resource) {
250         fUnderlyingResource = resource;
251     }
252
253     /* (non-Javadoc)
254      * @see org.eclipse.pde.core.plugin.ISharedPluginModel#getInstallLocation()
255      */

256     public String JavaDoc getInstallLocation() {
257         if (fUnderlyingResource != null) {
258             IPath path = fUnderlyingResource.getProject().getLocation();
259             return path != null ? path.addTrailingSeparator().toString() : null;
260         }
261         return fInstallLocation;
262     }
263     
264     public void setInstallLocation(String JavaDoc location) {
265         fInstallLocation = location;
266     }
267
268
269 }
270
Popular Tags