Statistics
| Revision:

root / trunk / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / GeometryManager.java @ 20861

History | View | Annotate | Download (6.3 KB)

1
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib��ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.fmap.geom;
42

    
43
import java.util.ArrayList;
44
import java.util.HashMap;
45
import java.util.List;
46
import java.util.Map;
47

    
48
import org.gvsig.fmap.geom.operation.GeometryOperation;
49
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
50
import org.gvsig.fmap.geom.type.GeometryType;
51

    
52
public class GeometryManager {
53

    
54
        private static GeometryManager instance;
55

    
56
        /** Índice con el nombre único de cada operación */
57
        private List geometryOperations = new ArrayList();
58

    
59
        /** Mapa con las instancias de cada tipo de geometría, la clave es el nombre de la clase de geometría. 
60
         * Es decir para "org.gvsig.fmap.geom.Point2D" se obtiene una instancia de GeometryType que contiene las 
61
         * operaciones asociadas a Point2D.
62
         */
63
        private Map geometryTypes = new HashMap();
64

    
65
        /**
66
         * Constructor privado (singleton)
67
         */
68
        private GeometryManager() {
69
        }
70

    
71
        /**
72
         * Método para obtener la instancia única (singleton)
73
         * @return
74
         */
75
        public static GeometryManager getInstance() {
76
                if (instance == null) {
77
                        instance = new GeometryManager();
78
                }
79
                return instance;
80
        }
81

    
82
        /**
83
         * Método para registrar una operación asociada a un tipo de geometría.
84
         * 
85
         * @param geomOpName Nombre de la operación, este nombre es único
86
         * @param geomOp Instancia de GeometryOperation que implementa la operación
87
         * @param geomType Instancia de GeometryType a la que se asocia la operación
88
         * @return índice asignado a la operación. Este índice se debe usar despues para acceder a la operación
89
         * 
90
         */
91
        public int registerGeometryOperation(String geomOpName,
92
                        GeometryOperation geomOp, GeometryType geomType) {
93
                if (geomOpName == null)
94
                        throw new IllegalArgumentException("geomOpName cannot be null.");
95
                if (geomOp == null)
96
                        throw new IllegalArgumentException("geomOp cannot be null.");
97
                if (geomType == null)
98
                        throw new IllegalArgumentException("geomType cannot be null.");
99

    
100
                int index = geometryOperations.indexOf(geomOpName);
101
                if (index == -1) {
102
                        geometryOperations.add(geomOpName);
103
                        index = geometryOperations.indexOf(geomOpName);
104
                }
105

    
106
                geomType.setGeometryOperation(index, geomOp);
107

    
108
                return index;
109
        }
110

    
111
        /**
112
         * 
113
         * @param geomOpName
114
         * @param geomOpClass
115
         * @param geomType
116
         * @return
117
         * @throws GeometryOperationNotSupportedException
118
         */
119
        public int registerGeometryOperation(String geomOpName, Class geomOpClass,
120
                        GeometryType geomType)
121
                        throws IllegalAccessException, InstantiationException {
122

    
123
                if (geomOpName == null)
124
                        throw new IllegalArgumentException("geomOpName cannot be null.");
125
                if (geomOpClass == null)
126
                        throw new IllegalArgumentException("geomOpClass cannot be null.");
127
                if (geomType == null)
128
                        throw new IllegalArgumentException("geomType cannot be null.");
129

    
130
                int index = geometryOperations.indexOf(geomOpName);
131
                if (index == -1) {
132
                        geometryOperations.add(geomOpName);
133
                        index = geometryOperations.indexOf(geomOpName);
134
                }
135
                geomType.setGeometryOperation(index, (GeometryOperation) geomOpClass.newInstance());
136
                
137
                return index;
138
        }
139

    
140
        /**
141
         * Registra una clase de geometría y devuelve la instancia GeometryType
142
         * asociada. Si la clase ya está registrada sólo se devuelve la instancia
143
         * GeometryType asociada.
144
         * 
145
         * geomClass no debe ser nulo y debe implementar la interfaz Geometry, en
146
         * caso contrario se lanza una excepción.
147
         * 
148
         * @param geomClass
149
         *            Clase de Geometry
150
         * @return Instancia de GeometryType asociada a la clase de geometría
151
         *         geomClass
152
         * @throws IllegalArgumentException
153
         *             si geomClass es nulo o no implementa Geometry
154
         */
155
        public GeometryType registerGeometryType(Class geomClass) {
156

    
157
                if (geomClass == null) {
158
                        throw new IllegalArgumentException("geomClass cannot be null.");
159
                }
160

    
161
                if (!Geometry.class.isAssignableFrom(geomClass)) {
162
                        throw new IllegalArgumentException(geomClass.getName()
163
                                        + " must implement the Geometry interface");
164
                }
165

    
166
                // Comprobar si ya está registrada
167
                GeometryType geomType = (GeometryType) geometryTypes.get(geomClass
168
                                .getName());
169

    
170
                // Si no está registrada, registrarla
171
                if (geomType == null) {
172
                        geomType = new GeometryType(geomClass);
173
                        geometryTypes.put(geomClass.getName(), geomType);
174
                }
175

    
176
                return geomType;
177
        }
178

    
179
        /**
180
         * Devuelve un GeometryType dada la clase que lo implementa
181
         * 
182
         * @param geomClass
183
         * @return
184
         */
185
        public GeometryType getGeometryType(Class geomClass) {
186
                return (GeometryType) geometryTypes.get(geomClass.getName());
187
        }
188

    
189
        /**
190
         * Devuelve un GeometryType dado el nombre cualificado de la clase de
191
         * geometría
192
         * 
193
         * @param className
194
         *            Nombre cualificado de la clase de geometría
195
         * @return GeometryType asociado
196
         */
197
        public GeometryType getGeometryType(String className) {
198
                return (GeometryType) geometryTypes.get(className);
199
        }
200

    
201
        /**
202
         * Devuelve una operación dada la clase de geometría y el índice de la
203
         * operación. Este índice se obtiene al registrar la operación.
204
         * 
205
         * @param geomClass
206
         * @param index
207
         * @return
208
         */
209
        public GeometryOperation getGeometryOperation(Class geomClass, int index) {
210
                GeometryType geomType = getGeometryType(geomClass);
211
                if (geomType != null) {
212
                        return geomType.getGeometryOperation(index);
213
                }
214
                return null;
215
        }
216
}