Revision 23063

View differences:

trunk/libraries/libTools/build.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<!-- ======================================================================
3
     02/09/2008 09:29
4

  
5
     libTools
6
     Build library libTols
7

  
8
     jmvivo
9
     ====================================================================== -->
10
<project name="libFMap_data" default="batch-build">
11
	<description>
12
            Build del libDataStore
13
    </description>
14
	<dirname file="${ant.file.libTools}" property="proyectDir"/>
15
	<import file="${proyectDir}/../binaries/ant/utilities.xml"/>
16

  
17
	<property name="src" location="${proyectDir}/src"/>
18
	<property name="src-test" location="${proyectDir}/src-test"/>
19
	<property name="build" location="${proyectDir}/bin"/>
20
	<property name="build-test" location="${proyectDir}/bin-test"/>
21
	<property name="dist" location="${proyectDir}/dist"/>
22
	<property name="jarName" value="org.gvsig.tools.jar"/>
23

  
24

  
25
	<!-- =================================
26
          target: generate-source
27
         ================================= -->
28
	<target name="generate-source" description="--> genera el tar de fuentes">
29
		<!-- TODO -->
30
		<echo level="warning">TODO!!!!</echo>
31
	</target>
32

  
33

  
34

  
35
	<!-- =================================
36
          target: compile
37
         ================================= -->
38
	<target name="compile" depends="" description="--> Compile sources">
39
		<mkdir dir="${build}"/>
40
		<mkdir dir="${build-test}"/>
41
		<!-- Compile the Java code from ${src} to ${build} -->
42
		<loadEclipseClasspath project="${basedir}"/>
43
		<gvSIG-javac
44
			classpath="${eclipseClasspath}"/>
45
		<gvSIG-javac
46
			classpath="${eclipseClasspath}"
47
			srcdir="${src-test}"
48
			destdir="${build-test}"/>
49

  
50
	</target>
51

  
52
	<!-- =================================
53
          target: batch-build
54
         ================================= -->
55
	<target name="batch-build" depends="compile" description="--> Prepare library">
56
		<mkdir dir="${dist}"/>
57
		<jar destfile="${dist}/${jarName}" basedir="${build}"/>
58
	</target>
59

  
60
	<!-- =================================
61
          target: clean
62
         ================================= -->
63
	<target name="clean" depends="" description="--> Clean proyect">
64
		<delete>
65
			<fileset dir="${build}">
66
				<include name="***"/>
67
				<exclude name=".svn"/>
68
			</fileset>
69
			<fileset dir="${build-test}">
70
				<include name="***"/>
71
				<exclude name=".svn"/>
72
			</fileset>
73

  
74
		</delete>
75
		<delete file="${dist}/${jarName}"/>
76

  
77
	</target>
78

  
79
</project>
0 80

  
trunk/libraries/libTools/.classpath
1
<?xml version="1.0" encoding="UTF-8"?>
2
<classpath>
3
	<classpathentry kind="src" path="src"/>
4
	<classpathentry kind="src" output="bin-test" path="src-test"/>
5
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
6
	<classpathentry combineaccessrules="false" kind="src" path="/libExceptions"/>
7
	<classpathentry kind="output" path="bin"/>
8
</classpath>
0 9

  
trunk/libraries/libTools/.project
1
<?xml version="1.0" encoding="UTF-8"?>
2
<projectDescription>
3
	<name>libTools</name>
4
	<comment></comment>
5
	<projects>
6
	</projects>
7
	<buildSpec>
8
		<buildCommand>
9
			<name>org.eclipse.jdt.core.javabuilder</name>
10
			<arguments>
11
			</arguments>
12
		</buildCommand>
13
	</buildSpec>
14
	<natures>
15
		<nature>org.eclipse.jdt.core.javanature</nature>
16
	</natures>
17
</projectDescription>
0 18

  
trunk/libraries/libTools/src/org/gvsig/tools/observer/Observable.java
1
package org.gvsig.tools.observer;
2

  
3

  
4
public interface Observable {
5
	public void addObserver(Observer o);
6
	public void deleteObserver(Observer o);
7
	public void deleteObservers();
8
}
0 9

  
trunk/libraries/libTools/src/org/gvsig/tools/observer/Observer.java
1
package org.gvsig.tools.observer;
2

  
3

  
4
public interface Observer {
5

  
6
	public void update(Observable observable,Object notification);
7

  
8
}
0 9

  
trunk/libraries/libTools/src/org/gvsig/tools/observer/DefaultObservable.java
1
package org.gvsig.tools.observer;
2

  
3
import java.lang.ref.Reference;
4
import java.lang.ref.WeakReference;
5
import java.util.ArrayList;
6
import java.util.Iterator;
7
import java.util.Vector;
8

  
9
//TODO Actualizar Javadoc... sobretodo que se almacenan WeakRef en vez de referencias
10

  
11
public class DefaultObservable implements Observable{
12

  
13
	private boolean propageNotifications=true;
14
	private boolean inComplex=false;
15
	private ComplexNotification complexNotification= null;
16

  
17
	private boolean changed = false;
18
    private Vector obs;
19

  
20
    /** Construct an DefaultObservable with zero Observers. */
21

  
22
    public DefaultObservable() {
23
		obs = new Vector();
24
		this.inComplex=false;
25
    }
26

  
27
    /**
28
     * Adds an observer to the set of observers for this object, provided
29
     * that it is not the same as some observer already in the set.
30
     * The order in which notifications will be delivered to multiple
31
     * observers is not specified. See the class comment.
32
     *
33
     * @param   o   an observer to be added.
34
     * @throws NullPointerException   if the parameter o is null.
35
     */
36
    public synchronized void addObserver(Observer o) {
37
        this.addObserver(new WeakReference(o));
38

  
39
    }
40

  
41
    public synchronized void addObserver(Reference ref) {
42
        if (ref == null || ref.get() == null)
43
            throw new NullPointerException();
44
        Observer o = (Observer)ref.get();
45
		if (!contains(o)) {
46
	    	obs.addElement(ref);
47
		}
48
    }
49

  
50
    public synchronized void addObservers(DefaultObservable o){
51
    	Iterator iter = o.obs.iterator();
52
    	o.clearDeadReferences();
53
    	while (iter.hasNext()){
54
    		this.addObserver((Reference) iter.next());
55
    	}
56

  
57
    }
58

  
59
    private boolean contains(Observer o){
60
    	if (obs.contains(o)){
61
    		return true;
62
    	}
63
        Iterator iter = obs.iterator();
64
        Object obj;
65
        Reference ref1;
66
        while (iter.hasNext()){
67
        	obj = iter.next();
68
        	if (obj instanceof Reference){
69
        		ref1 = (Reference)obj;
70
        		if (o.equals(ref1.get())){
71
        			return true;
72
        		}
73
        	}
74
        }
75
        return false;
76
    }
77

  
78
    /**
79
     * Deletes an observer from the set of observers of this object.
80
     * Passing <CODE>null</CODE> to this method will have no effect.
81
     * @param   o   the observer to be deleted.
82
     */
83
    public synchronized void deleteObserver(Observer o) {
84
        if (!obs.removeElement(o)){
85
        	this.deleteObserverReferenced(o);
86
        }
87
    }
88

  
89
    private void deleteObserverReferenced(Observer o){
90
        Iterator iter = obs.iterator();
91
        Object obj;
92
        ArrayList toRemove = new ArrayList();
93
        Reference ref1;
94
        while (iter.hasNext()){
95
        	obj = iter.next();
96
        	if (obj instanceof Reference){
97
        		ref1 = (Reference)obj;
98
        		if (ref1.get() == null || o.equals(ref1.get())){
99
        			toRemove.add(obj);
100
        		}
101
        	}
102
        }
103
        iter = toRemove.iterator();
104
        while (iter.hasNext()){
105
        	obs.remove(iter.next());
106
        }
107
    }
108

  
109
    public synchronized void deleteObserver(Reference ref) {
110
    	Observer o = (Observer)ref.get();
111
        obs.removeElement(ref);
112
        if (o == null)
113
        	return;
114
        deleteObserverReferenced(o);
115
    }
116

  
117
    /**
118
     * If this object has changed, as indicated by the
119
     * <code>hasChanged</code> method, then notify all of its observers
120
     * and then call the <code>clearChanged</code> method to
121
     * indicate that this object has no longer changed.
122
     * <p>
123
     * Each observer has its <code>update</code> method called with two
124
     * arguments: this observable object and <code>null</code>. In other
125
     * words, this method is equivalent to:
126
     * <blockquote><tt>
127
     * notifyObservers(null)</tt></blockquote>
128
     *
129
     * @see     java.util.Observable#clearChanged()
130
     * @see     java.util.Observable#hasChanged()
131
     * @see     java.util.Observer#update(java.util.Observable, java.lang.Object)
132
     */
133
    public void notifyObservers() {
134
    	throw new UnsupportedOperationException("Notify requires an notification Object");
135
    }
136

  
137
    /**
138
     * If this object has changed, as indicated by the
139
     * <code>hasChanged</code> method, then notify all of its observers
140
     * and then call the <code>clearChanged</code> method to indicate
141
     * that this object has no longer changed.
142
     * <p>
143
     * Each observer has its <code>update</code> method called with two
144
     * arguments: this observable object and the <code>arg</code> argument.
145
     *
146
     * @param   arg   any object.
147
     * @see     java.util.Observable#clearChanged()
148
     * @see     java.util.Observable#hasChanged()
149
     * @see     java.util.Observer#update(java.util.Observable, java.lang.Object)
150
     */
151
    public void notifyObservers(Observable observable,Object arg) {
152
    	if (!this.inComplex){
153
			this.setChanged();
154
			notify(observable,arg);
155
		}else{
156
			complexNotification.addNotification(arg);
157
		}
158

  
159
    }
160

  
161
    /**
162
     * Clears the observer list so that this object no longer has any observers.
163
     */
164
    public synchronized void deleteObservers() {
165
    	obs.removeAllElements();
166
    }
167

  
168
    /**
169
     * Marks this <tt>DefaultObservable</tt> object as having been changed; the
170
     * <tt>hasChanged</tt> method will now return <tt>true</tt>.
171
     */
172
    protected synchronized void setChanged() {
173
    	changed = true;
174
    }
175

  
176
    /**
177
     * Indicates that this object has no longer changed, or that it has
178
     * already notified all of its observers of its most recent change,
179
     * so that the <tt>hasChanged</tt> method will now return <tt>false</tt>.
180
     * This method is called automatically by the
181
     * <code>notifyObservers</code> methods.
182
     *
183
     * @see     java.util.Observable#notifyObservers()
184
     * @see     java.util.Observable#notifyObservers(java.lang.Object)
185
     */
186
    protected synchronized void clearChanged() {
187
    	changed = false;
188
    }
189

  
190
    /**
191
     * Tests if this object has changed.
192
     *
193
     * @return  <code>true</code> if and only if the <code>setChanged</code>
194
     *          method has been called more recently than the
195
     *          <code>clearChanged</code> method on this object;
196
     *          <code>false</code> otherwise.
197
     * @see     java.util.Observable#clearChanged()
198
     * @see     java.util.Observable#setChanged()
199
     */
200
    public synchronized boolean hasChanged() {
201
    	return changed;
202
    }
203

  
204
    /**
205
     * Returns the number of observers of this <tt>DefaultObservable</tt> object.
206
     *
207
     * @return  the number of observers of this object.
208
     */
209
    public synchronized int countObservers() {
210
    	clearDeadReferences();
211
    	return obs.size();
212
    }
213

  
214
	public void enableNotifications(){
215
		clearDeadReferences();
216
		this.propageNotifications =true;
217
	}
218

  
219
	public void diableNotifications(){
220
		this.propageNotifications =false;
221
	}
222

  
223
	public boolean isEnabledNotifications(){
224
		return this.propageNotifications;
225
	}
226

  
227
	public boolean inComplex(){
228
		return this.inComplex;
229
	}
230

  
231
	public void beginComplexNotification(ComplexNotification complex){
232
		clearDeadReferences();
233
		this.clearChanged();
234
		inComplex=true;
235
		complexNotification=complex;
236
		complexNotification.clear();
237
	}
238

  
239
	public void endComplexNotification(Observable observable){
240
		inComplex=false;
241
		this.setChanged();
242

  
243
		Iterator iter=complexNotification.getIterator();
244
		while(iter.hasNext()){
245
			notify(observable,iter.next());
246
		}
247
		complexNotification = null;
248
	}
249

  
250
	protected synchronized void clearDeadReferences(){
251
        Iterator iter = obs.iterator();
252
        Object obj;
253
        ArrayList toRemove = new ArrayList();
254
        Reference ref1;
255
        while (iter.hasNext()){
256
        	obj = iter.next();
257
        	if (obj instanceof Reference){
258
        		ref1 = (Reference)obj;
259
        		if (ref1.get() == null){
260
        			toRemove.add(obj);
261
        		}
262
        	}
263
        }
264
        iter = toRemove.iterator();
265
        while (iter.hasNext()){
266
        	obs.remove(iter.next());
267
        }
268

  
269
	}
270

  
271
	private void notify(Observable observable, Object object) {
272
		/*
273
         * a temporary array buffer, used as a snapshot of the state of
274
         * current Observers.
275
         */
276
        Object[] arrLocal;
277

  
278
	synchronized (this) {
279
	    /* We don't want the Observer doing callbacks into
280
	     * arbitrary code while holding its own Monitor.
281
	     * The code where we extract each DefaultObservable from
282
	     * the Vector and store the state of the Observer
283
	     * needs synchronization, but notifying observers
284
	     * does not (should not).  The worst result of any
285
	     * potential race-condition here is that:
286
	     * 1) a newly-added Observer will miss a
287
	     *   notification in progress
288
	     * 2) a recently unregistered Observer will be
289
	     *   wrongly notified when it doesn't care
290
	     */
291
	    if (!changed)
292
                return;
293
            arrLocal = obs.toArray();
294
            clearChanged();
295
        }
296

  
297
		Object obj;
298
		Observer observer;
299
		ArrayList toRemove = new ArrayList();
300
        for (int i = arrLocal.length-1; i>=0; i--){
301
        	obj = arrLocal[i];
302
        	observer = (Observer)((Reference)obj).get();
303
        	if (observer == null){
304
        		toRemove.add(obj);
305
        		continue;
306
        	}
307
            observer.update(observable, object);
308
        }
309

  
310
        Iterator iter = toRemove.iterator();
311
        while (iter.hasNext()){
312
        	obs.remove(iter.next());
313
        }
314

  
315
	}
316
}
0 317

  
trunk/libraries/libTools/src/org/gvsig/tools/observer/ComplexNotification.java
1
package org.gvsig.tools.observer;
2

  
3
import java.util.ArrayList;
4
import java.util.Iterator;
5
import java.util.List;
6

  
7
public class ComplexNotification {
8
	private List list=new ArrayList();
9
	public Iterator getIterator() {
10
		return list.iterator();
11
	}
12

  
13
	public void addNotification(Object arg) {
14
		list.add(arg);
15
	}
16

  
17
	protected void dispose(){
18

  
19
	}
20

  
21
	/**
22
	 *
23
	 */
24
	public void clear() {
25
		// TODO Auto-generated method stub
26

  
27
	}
28

  
29
}
0 30

  
trunk/libraries/libTools/src/org/gvsig/tools/visitor/Visitor.java
1
package org.gvsig.tools.visitor;
2

  
3
import org.gvsig.exceptions.BaseException;
4

  
5
public interface Visitor {
6
	public void visit(Object obj) throws BaseException;
7
}
0 8

  
trunk/libraries/libTools/src/org/gvsig/tools/visitor/Visitable.java
1
package org.gvsig.tools.visitor;
2

  
3
import org.gvsig.exceptions.BaseException;
4

  
5
public interface Visitable {
6
	
7
	public void accept(Visitor visitor) throws BaseException;
8

  
9
}
0 10

  
trunk/libraries/libTools/src/org/gvsig/tools/visitor/NotSupportedOperationException.java
1
package org.gvsig.tools.visitor;
2

  
3
import java.util.HashMap;
4
import java.util.Map;
5

  
6
import org.gvsig.exceptions.BaseException;
7

  
8
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
9
 *
10
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
11
 *
12
 * This program is free software; you can redistribute it and/or
13
 * modify it under the terms of the GNU General Public License
14
 * as published by the Free Software Foundation; either version 2
15
 * of the License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
25
 *
26
 * For more information, contact:
27
 *
28
 *  Generalitat Valenciana
29
 *   Conselleria d'Infraestructures i Transport
30
 *   Av. Blasco Ib??ez, 50
31
 *   46010 VALENCIA
32
 *   SPAIN
33
 *
34
 *      +34 963862235
35
 *   gvsig@gva.es
36
 *      www.gvsig.gva.es
37
 *
38
 *    or
39
 *
40
 *   IVER T.I. S.A
41
 *   Salamanca 50
42
 *   46005 Valencia
43
 *   Spain
44
 *
45
 *   +34 963163400
46
 *   dac@iver.es
47
 */
48
/* CVS MESSAGES:
49
 *
50
 * $Id$
51
 * $Log$
52
 *
53
 */
54
/**
55
 * @author Jorge Piera Llodr? (jorge.piera@iver.es)
56
 */
57
public class NotSupportedOperationException extends BaseException{
58
	private static final long serialVersionUID = 1L;
59
	private String visitorClassName = null;
60
	private String objectClassName = null;
61
		
62
	public NotSupportedOperationException(Visitor visitor, Object object){
63
		this.visitorClassName = visitor.getClass().getName();
64
		this.objectClassName = object.getClass().getName();
65
	}
66
	
67
	/**
68
	 * Initializes some values
69
	 */
70
	public void init() {
71
		messageKey="geometries_reader_not_exists";
72
		formatString="The visitor %(visitorClassName) doesn't implements any " +
73
			"operation for the object %(objectClassName)";
74
		code = serialVersionUID;
75
	}
76
	
77
	/*
78
	 * (non-Javadoc)
79
	 * @see org.gvsig.exceptions.BaseException#values()
80
	 */
81
	protected Map values() {
82
		HashMap map = new HashMap();
83
		map.put("visitorClassName", visitorClassName);
84
		map.put("objectClassName", objectClassName);
85
		return map;
86
	}
87

  
88
}
0 89

  

Also available in: Unified diff