1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.File ; 14 import java.io.Serializable ; 15 import java.util.ArrayList ; 16 import java.util.Collections ; 17 import java.util.List ; 18 19 import javax.xml.parsers.FactoryConfigurationError ; 20 import javax.xml.parsers.ParserConfigurationException ; 21 import javax.xml.parsers.SAXParser ; 22 import javax.xml.parsers.SAXParserFactory ; 23 24 import org.eclipse.core.resources.IResource; 25 import org.eclipse.core.runtime.CoreException; 26 import org.eclipse.core.runtime.IStatus; 27 import org.eclipse.core.runtime.PlatformObject; 28 import org.eclipse.core.runtime.Status; 29 import org.eclipse.pde.core.IModel; 30 import org.eclipse.pde.core.IModelChangedEvent; 31 import org.eclipse.pde.core.IModelChangedListener; 32 import org.eclipse.pde.core.ModelChangedEvent; 33 import org.xml.sax.SAXException ; 34 35 public abstract class AbstractModel extends PlatformObject implements IModel, 36 IModelChangeProviderExtension, Serializable { 37 private transient List fListeners; 38 39 private boolean fLoaded; 40 41 protected boolean fDisposed; 42 43 private long fTimestamp; 44 45 private Exception fException; 46 47 public AbstractModel() { 48 fListeners = Collections.synchronizedList(new ArrayList ()); 49 } 50 51 public void addModelChangedListener(IModelChangedListener listener) { 52 fListeners.add(listener); 53 } 54 55 public void transferListenersTo(IModelChangeProviderExtension target, 56 IModelChangedListenerFilter filter) { 57 ArrayList removed = new ArrayList (); 58 for (int i = 0; i < fListeners.size(); i++) { 59 IModelChangedListener listener = (IModelChangedListener) fListeners.get(i); 60 if (filter == null || filter.accept(listener)) { 61 target.addModelChangedListener(listener); 62 removed.add(listener); 63 } 64 } 65 fListeners.removeAll(removed); 66 } 67 68 public void dispose() { 69 fDisposed = true; 70 } 71 72 public void fireModelChanged(IModelChangedEvent event) { 73 IModelChangedListener[] list = (IModelChangedListener[]) fListeners 74 .toArray(new IModelChangedListener[fListeners.size()]); 75 for (int i = 0; i < list.length; i++) { 76 IModelChangedListener listener = list[i]; 77 listener.modelChanged(event); 78 } 79 } 80 81 public void fireModelObjectChanged(Object object, String property, Object oldValue, 82 Object newValue) { 83 fireModelChanged(new ModelChangedEvent(this, object, property, oldValue, newValue)); 84 } 85 86 public String getResourceString(String key) { 87 return key; 88 } 89 90 public IResource getUnderlyingResource() { 91 return null; 92 } 93 94 protected boolean isInSync(File localFile) { 95 return localFile.exists() && localFile.lastModified() == getTimeStamp(); 96 } 97 98 public boolean isValid() { 99 return !isDisposed() && isLoaded(); 100 } 101 102 public final long getTimeStamp() { 103 return fTimestamp; 104 } 105 106 protected abstract void updateTimeStamp(); 107 108 protected void updateTimeStamp(File localFile) { 109 if (localFile.exists()) 110 fTimestamp = localFile.lastModified(); 111 } 112 113 public boolean isDisposed() { 114 return fDisposed; 115 } 116 117 public boolean isLoaded() { 118 return fLoaded; 119 } 120 121 public void setLoaded(boolean loaded) { 122 fLoaded = loaded; 123 } 124 125 public void setException(Exception e) { 126 fException = e; 127 } 128 129 public Exception getException() { 130 return fException; 131 } 132 133 public void removeModelChangedListener(IModelChangedListener listener) { 134 fListeners.remove(listener); 135 } 136 137 public void throwParseErrorsException(Throwable e) throws CoreException { 138 Status status = new Status(IStatus.ERROR, PDECore.PLUGIN_ID, IStatus.OK, 139 "Error in the manifest file", e); 141 throw new CoreException(status); 142 } 143 144 protected SAXParser getSaxParser() throws ParserConfigurationException , SAXException , 145 FactoryConfigurationError { 146 return SAXParserFactory.newInstance().newSAXParser(); 147 } 148 149 154 public boolean isReconcilingModel() { 155 return false; 156 } 157 158 } 159 | Popular Tags |