Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / store / ParamsImpl.java @ 2192

History | View | Annotate | Download (5.02 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.impl.store;
23

    
24
import java.util.ArrayList;
25
import java.util.Iterator;
26
import java.util.List;
27

    
28
import org.gvsig.fmap.dal.coverage.datastruct.Param;
29
import org.gvsig.fmap.dal.coverage.datastruct.Params;
30
import org.gvsig.tools.ToolsLocator;
31
import org.gvsig.tools.dynobject.DynStruct;
32
import org.gvsig.tools.persistence.PersistenceManager;
33
import org.gvsig.tools.persistence.PersistentState;
34
import org.gvsig.tools.persistence.exception.PersistenceException;
35

    
36

    
37
/**
38
* Par?metros para los drivers de escritura. Las variables estaticas contenidas representan
39
* los tipos de par?metro posibles.
40
*
41
* @version 17/04/2007
42
* @author Nacho Brodin (nachobrodin@gmail.com)
43
*/
44
public class ParamsImpl implements Params {
45
        public static final int NONE        = -1;
46
        public static final int CHECK       = 1;
47
        public static final int CHOICE      = 2;
48
        public static final int SLIDER      = 3;
49
        public static final int MULTI_CHECK = 4;
50
        private List<Param> params = new ArrayList<Param>();
51

    
52
        /**
53
         * Obtiene el par?metro de la posici?n definida por param
54
         * @param param Posici?n del par?metro
55
         * @return Objeto Param
56
         */
57
        public Param getParam(int param) {
58
                return (Param)params.get(param);
59
        }
60

    
61
        /**
62
         * Asigna el par?metro pasado a la lista de par?metros necesitados por el driver
63
         * @param param
64
         */
65
        public void setParam(Param param) {
66
                params.add(param);
67
        }
68

    
69
        /**
70
         * Inicializa la lista de par?metros
71
         */
72
        public void clear() {
73
                params.clear();
74
        }
75

    
76
        /**
77
         * Obtiene un par?metro de la lista a partir de su identificador
78
         * @param id Identificador del par?metro
79
         * @return Par?metro o null si no existe
80
         */
81
        public Param getParamById(String id) {
82
                for (Iterator<Param> iter = params.iterator(); iter.hasNext();) {
83
                        ParamImpl p = (ParamImpl) iter.next();
84
                        if (p.getId().equals(id))
85
                                return p;
86
                }
87
                return null;
88
        }
89

    
90
        public void setParam(String id, Object value, int type, String[] list) {
91
                ParamImpl p = (ParamImpl)getParamById(id);
92
                if (p == null)
93
                        p = new ParamImpl();
94

    
95
                p.setId(id);
96
                p.setDefaultValue(value);
97
                p.setType(type);
98
                p.setList(list);
99
                params.add(p);
100
        }
101
        
102
        public void setParam(String id, Object value) {
103
                ParamImpl p = (ParamImpl)getParamById(id);
104
                if (p == null)
105
                        p = new ParamImpl();
106

    
107
                p.setId(id);
108
                p.setDefaultValue(value);
109
                p.setType(-1);
110
                p.setList(null);
111
                params.add(p);
112
        }
113

    
114
        /**
115
         * Borra los parametros asociados a ese id
116
         * @param id
117
         */
118
        public void removeParam(String id) {
119
                for (int i = 0; i < params.size(); i++) {
120
                        ParamImpl p = (ParamImpl) params.get(i);
121
                        if (p.getId().equals(id)) {
122
                                params.remove(i);
123
                                i--;
124
                        }
125
                }
126
        }
127

    
128
        /**
129
         * Asigna un valor para un par?metro existens. Si no existe no hace nada.
130
         *
131
         * @param id Identificador del par?metro
132
         * @param value Valor a asignar
133
         */
134
        public void changeParamValue(String id, Object value) {
135
                for (Iterator<Param> iter = params.iterator(); iter.hasNext();) {
136
                        ParamImpl p = (ParamImpl) iter.next();
137
                        if(p.getId().equals(id))
138
                                p.setDefaultValue(value);
139
                }
140
        }
141

    
142
        /**
143
         * Obtiene el n?mero de par?metros.
144
         * @return N?mero de par?metros.
145
         */
146
        public int getNumParams() {
147
                return params.size();
148
        }
149

    
150
        /**
151
         * Devuelve el array de los Params
152
         * @return
153
         */
154
        public List<Param> getParams() {
155
                return params;
156
        }
157

    
158

    
159
        @SuppressWarnings("unchecked")
160
        public Object clone() throws CloneNotSupportedException {
161
                ParamsImpl aux = new ParamsImpl();
162
                ((ParamsImpl) aux).params = (List<Param>)((ArrayList<Param>) this.params).clone();
163
                return aux;
164
        }
165
        
166
        public void loadFromState(PersistentState state)
167
                        throws PersistenceException {
168
                @SuppressWarnings("unchecked")
169
                List<Param> list = state.getList("list");
170
                if(list != null) {
171
                        this.params = new ArrayList<Param>();
172
                        this.params.addAll(list);
173
                }
174
        }
175

    
176
        public void saveToState(PersistentState state) throws PersistenceException {
177
                state.set("list", params);
178
        }        
179
        
180
        public static void registerPersistence() {
181
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
182
                
183
                DynStruct definition = manager.addDefinition(
184
                                ParamsImpl.class,
185
                                "ParamsImpl_Persistence",
186
                                "ParamsImpl Persistence",
187
                                null, 
188
                                null
189
                );
190
                definition.addDynFieldList("list").setClassOfItems(Param.class).setMandatory(false);
191
        }
192
}