1 11 package org.eclipse.pde.internal.core.text; 12 13 import java.io.BufferedInputStream ; 14 import java.io.ByteArrayInputStream ; 15 import java.io.InputStream ; 16 import java.io.PrintWriter ; 17 import java.io.UnsupportedEncodingException ; 18 import java.util.ArrayList ; 19 import java.util.List ; 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 fListeners = new ArrayList (); 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 fCharset; 44 private IResource fUnderlyingResource; 45 private String fInstallLocation; 46 private boolean fStale; 47 48 public AbstractEditingModel(IDocument document, boolean isReconciling) { 49 fDocument = document; 50 fReconciling = isReconciling; 51 } 52 53 56 public void dispose() { 57 if (fNLResourceHelper != null) { 58 fNLResourceHelper.dispose(); 59 fNLResourceHelper = null; 60 } 61 fDisposed = true; 62 fListeners.clear(); 63 } 64 65 68 public String getResourceString(String key) { 69 if (key == null || key.length() == 0) 70 return ""; 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 83 public boolean isDisposed() { 84 return fDisposed; 85 } 86 89 public boolean isEditable() { 90 return fReconciling; 91 } 92 95 public boolean isLoaded() { 96 return fLoaded; 97 } 98 101 public boolean isInSync() { 102 return fInSync; 103 } 104 107 public boolean isValid() { 108 return isLoaded(); 109 } 110 113 public final long getTimeStamp() { 114 return fTimestamp; 115 } 116 119 public final void load() throws CoreException { 120 try { 121 load(getInputStream(getDocument()), false); 122 } catch (UnsupportedEncodingException e) { 123 } 124 } 125 128 public final void reload(InputStream source, boolean outOfSync) 129 throws CoreException { 130 load(source, outOfSync); 131 fireModelChanged( 132 new ModelChangedEvent(this, 133 IModelChangedEvent.WORLD_CHANGED, 134 new Object [] {this}, 135 null)); 136 137 } 138 141 public boolean isReconcilingModel() { 142 return fReconciling; 143 } 144 145 public IDocument getDocument() { 146 return fDocument; 147 } 148 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 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 getInputStream(IDocument document) throws UnsupportedEncodingException { 171 return new BufferedInputStream (new ByteArrayInputStream (document.get().getBytes(getCharset()))); 172 } 173 174 public String getCharset() { 175 return fCharset != null ? fCharset : "UTF-8"; } 177 178 public void setCharset(String charset) { 179 fCharset = charset; 180 } 181 182 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 oldList = (List )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 target.addModelChangedListener(listener); 196 fListeners.remove(listener); 198 } 199 } 200 } 201 202 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 218 public void fireModelObjectChanged(Object object, String property, Object oldValue, Object newValue) { 219 fireModelChanged(new ModelChangedEvent(this, object, property, oldValue, newValue)); 220 } 221 224 public void removeModelChangedListener(IModelChangedListener listener) { 225 fListeners.remove(listener); 226 } 227 230 public boolean isDirty() { 231 return fDirty; 232 } 233 236 public void save(PrintWriter writer) { 237 } 238 241 public void setDirty(boolean dirty) { 242 this.fDirty = dirty; 243 } 244 245 248 public boolean isStale() { 249 return fStale; 250 } 251 252 255 public void setStale(boolean stale) { 256 fStale = stale; 257 } 258 261 public IResource getUnderlyingResource() { 262 return fUnderlyingResource; 263 } 264 265 public void setUnderlyingResource(IResource resource) { 266 fUnderlyingResource = resource; 267 } 268 269 272 public String 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 location) { 281 fInstallLocation = location; 282 } 283 284 public IModelTextChangeListener getLastTextChangeListener() { 285 for (int i = fListeners.size() - 1; i >= 0; i--) { 286 Object obj = fListeners.get(i); 287 if (obj instanceof IModelTextChangeListener) 288 return (IModelTextChangeListener)obj; 289 } 290 return null; 291 } 292 293 } | Popular Tags |