1 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 fCharset; 33 private IResource fUnderlyingResource; 34 private String fInstallLocation; 35 private boolean fStale; 36 37 public AbstractEditingModel(IDocument document, boolean isReconciling) { 38 fDocument = document; 39 fReconciling = isReconciling; 40 } 41 42 45 public void dispose() { 46 if (fNLResourceHelper != null) { 47 fNLResourceHelper.dispose(); 48 fNLResourceHelper = null; 49 } 50 fDisposed = true; 51 fListeners.clear(); 52 } 53 54 57 public String getResourceString(String key) { 58 if (key == null || key.length() == 0) 59 return ""; 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 72 public boolean isDisposed() { 73 return fDisposed; 74 } 75 78 public boolean isEditable() { 79 return fReconciling; 80 } 81 84 public boolean isLoaded() { 85 return fLoaded; 86 } 87 90 public boolean isInSync() { 91 return fInSync; 92 } 93 96 public boolean isValid() { 97 return isLoaded(); 98 } 99 102 public final long getTimeStamp() { 103 return fTimestamp; 104 } 105 108 public final void load() throws CoreException { 109 try { 110 load(getInputStream(getDocument()), false); 111 } catch (UnsupportedEncodingException e) { 112 } 113 } 114 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 [] {this}, 124 null)); 125 126 } 127 130 public boolean isReconcilingModel() { 131 return fReconciling; 132 } 133 134 public IDocument getDocument() { 135 return fDocument; 136 } 137 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 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 getCharset() { 163 return fCharset != null ? fCharset : "UTF-8"; } 165 166 public void setCharset(String charset) { 167 fCharset = charset; 168 } 169 170 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 target.addModelChangedListener(listener); 184 fListeners.remove(listener); 186 } 187 } 188 } 189 190 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 202 public void fireModelObjectChanged(Object object, String property, Object oldValue, Object newValue) { 203 fireModelChanged(new ModelChangedEvent(this, object, property, oldValue, newValue)); 204 } 205 208 public void removeModelChangedListener(IModelChangedListener listener) { 209 fListeners.remove(listener); 210 } 211 214 public boolean isDirty() { 215 return fDirty; 216 } 217 220 public void save(PrintWriter writer) { 221 } 222 225 public void setDirty(boolean dirty) { 226 this.fDirty = dirty; 227 } 228 229 232 public boolean isStale() { 233 return fStale; 234 } 235 236 239 public void setStale(boolean stale) { 240 fStale = stale; 241 } 242 245 public IResource getUnderlyingResource() { 246 return fUnderlyingResource; 247 } 248 249 public void setUnderlyingResource(IResource resource) { 250 fUnderlyingResource = resource; 251 } 252 253 256 public String 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 location) { 265 fInstallLocation = location; 266 } 267 268 269 } 270 | Popular Tags |