KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > AbstractModel


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.core;
12
13 import java.io.File JavaDoc;
14 import java.io.Serializable JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.List JavaDoc;
18
19 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
20 import javax.xml.parsers.ParserConfigurationException JavaDoc;
21 import javax.xml.parsers.SAXParser JavaDoc;
22 import javax.xml.parsers.SAXParserFactory JavaDoc;
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 JavaDoc;
34
35 public abstract class AbstractModel extends PlatformObject implements IModel,
36         IModelChangeProviderExtension, Serializable JavaDoc {
37     private transient List JavaDoc fListeners;
38
39     private boolean fLoaded;
40
41     protected boolean fDisposed;
42
43     private long fTimestamp;
44
45     private Exception JavaDoc fException;
46
47     public AbstractModel() {
48         fListeners = Collections.synchronizedList(new ArrayList JavaDoc());
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 JavaDoc removed = new ArrayList JavaDoc();
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 JavaDoc object, String JavaDoc property, Object JavaDoc oldValue,
82             Object JavaDoc newValue) {
83         fireModelChanged(new ModelChangedEvent(this, object, property, oldValue, newValue));
84     }
85
86     public String JavaDoc getResourceString(String JavaDoc key) {
87         return key;
88     }
89
90     public IResource getUnderlyingResource() {
91         return null;
92     }
93
94     protected boolean isInSync(File JavaDoc 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 JavaDoc 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 JavaDoc e) {
126         fException = e;
127     }
128
129     public Exception JavaDoc getException() {
130         return fException;
131     }
132
133     public void removeModelChangedListener(IModelChangedListener listener) {
134         fListeners.remove(listener);
135     }
136
137     public void throwParseErrorsException(Throwable JavaDoc e) throws CoreException {
138         Status status = new Status(IStatus.ERROR, PDECore.PLUGIN_ID, IStatus.OK,
139                 "Error in the manifest file", //$NON-NLS-1$
140
e);
141         throw new CoreException(status);
142     }
143
144     protected SAXParser JavaDoc getSaxParser() throws ParserConfigurationException JavaDoc, SAXException JavaDoc,
145             FactoryConfigurationError JavaDoc {
146         return SAXParserFactory.newInstance().newSAXParser();
147     }
148
149     /*
150      * (non-Javadoc)
151      *
152      * @see org.eclipse.pde.core.IModel#isReconcilingModel()
153      */

154     public boolean isReconcilingModel() {
155         return false;
156     }
157
158 }
159
Popular Tags