001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core;
011:
012: import java.io.File;
013: import java.io.Serializable;
014: import java.util.ArrayList;
015: import java.util.Collections;
016: import java.util.List;
017:
018: import javax.xml.parsers.FactoryConfigurationError;
019: import javax.xml.parsers.ParserConfigurationException;
020: import javax.xml.parsers.SAXParser;
021: import javax.xml.parsers.SAXParserFactory;
022:
023: import org.eclipse.core.resources.IResource;
024: import org.eclipse.core.runtime.CoreException;
025: import org.eclipse.core.runtime.IStatus;
026: import org.eclipse.core.runtime.PlatformObject;
027: import org.eclipse.core.runtime.Status;
028: import org.eclipse.pde.core.IModel;
029: import org.eclipse.pde.core.IModelChangedEvent;
030: import org.eclipse.pde.core.IModelChangedListener;
031: import org.eclipse.pde.core.ModelChangedEvent;
032: import org.xml.sax.SAXException;
033:
034: public abstract class AbstractModel extends PlatformObject implements
035: IModel, IModelChangeProviderExtension, Serializable {
036: private transient List fListeners;
037:
038: private boolean fLoaded;
039:
040: protected boolean fDisposed;
041:
042: private long fTimestamp;
043:
044: private Exception fException;
045:
046: public AbstractModel() {
047: fListeners = Collections.synchronizedList(new ArrayList());
048: }
049:
050: public void addModelChangedListener(IModelChangedListener listener) {
051: fListeners.add(listener);
052: }
053:
054: public void transferListenersTo(
055: IModelChangeProviderExtension target,
056: IModelChangedListenerFilter filter) {
057: ArrayList removed = new ArrayList();
058: for (int i = 0; i < fListeners.size(); i++) {
059: IModelChangedListener listener = (IModelChangedListener) fListeners
060: .get(i);
061: if (filter == null || filter.accept(listener)) {
062: target.addModelChangedListener(listener);
063: removed.add(listener);
064: }
065: }
066: fListeners.removeAll(removed);
067: }
068:
069: public void dispose() {
070: fDisposed = true;
071: }
072:
073: public void fireModelChanged(IModelChangedEvent event) {
074: IModelChangedListener[] list = (IModelChangedListener[]) fListeners
075: .toArray(new IModelChangedListener[fListeners.size()]);
076: for (int i = 0; i < list.length; i++) {
077: IModelChangedListener listener = list[i];
078: listener.modelChanged(event);
079: }
080: }
081:
082: public void fireModelObjectChanged(Object object, String property,
083: Object oldValue, Object newValue) {
084: fireModelChanged(new ModelChangedEvent(this , object, property,
085: oldValue, newValue));
086: }
087:
088: public String getResourceString(String key) {
089: return key;
090: }
091:
092: public IResource getUnderlyingResource() {
093: return null;
094: }
095:
096: protected boolean isInSync(File localFile) {
097: return localFile.exists()
098: && localFile.lastModified() == getTimeStamp();
099: }
100:
101: public boolean isValid() {
102: return !isDisposed() && isLoaded();
103: }
104:
105: public final long getTimeStamp() {
106: return fTimestamp;
107: }
108:
109: protected abstract void updateTimeStamp();
110:
111: protected void updateTimeStamp(File localFile) {
112: if (localFile.exists())
113: fTimestamp = localFile.lastModified();
114: }
115:
116: public boolean isDisposed() {
117: return fDisposed;
118: }
119:
120: public boolean isLoaded() {
121: return fLoaded;
122: }
123:
124: public void setLoaded(boolean loaded) {
125: fLoaded = loaded;
126: }
127:
128: public void setException(Exception e) {
129: fException = e;
130: }
131:
132: public Exception getException() {
133: return fException;
134: }
135:
136: public void removeModelChangedListener(
137: IModelChangedListener listener) {
138: fListeners.remove(listener);
139: }
140:
141: public void throwParseErrorsException(Throwable e)
142: throws CoreException {
143: Status status = new Status(IStatus.ERROR, PDECore.PLUGIN_ID,
144: IStatus.OK, "Error in the manifest file", //$NON-NLS-1$
145: e);
146: throw new CoreException(status);
147: }
148:
149: protected SAXParser getSaxParser()
150: throws ParserConfigurationException, SAXException,
151: FactoryConfigurationError {
152: return SAXParserFactory.newInstance().newSAXParser();
153: }
154:
155: /*
156: * (non-Javadoc)
157: *
158: * @see org.eclipse.pde.core.IModel#isReconcilingModel()
159: */
160: public boolean isReconcilingModel() {
161: return false;
162: }
163:
164: }
|