gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster_dataaccess_refactoring / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / store / ParamImpl.java @ 2341
History | View | Annotate | Download (5.67 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.lang.reflect.Constructor; |
25 |
import java.lang.reflect.InvocationTargetException; |
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.fmap.dal.coverage.grid.RegistrableFilterListener; |
31 |
import org.gvsig.raster.util.persistence.GenericPersistentObject; |
32 |
import org.gvsig.tools.ToolsLocator; |
33 |
import org.gvsig.tools.dynobject.DynStruct; |
34 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
35 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
36 |
import org.gvsig.tools.persistence.PersistenceManager; |
37 |
import org.gvsig.tools.persistence.PersistentState; |
38 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
39 |
import org.slf4j.Logger; |
40 |
import org.slf4j.LoggerFactory; |
41 |
|
42 |
|
43 |
/**
|
44 |
* Generic parameter to store data for filters
|
45 |
*
|
46 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
47 |
*/
|
48 |
public class ParamImpl implements Param { |
49 |
private Logger log = LoggerFactory.getLogger(ParamImpl.class); |
50 |
private int type = -1; |
51 |
private String id = null; |
52 |
private Object defaultValue = null; |
53 |
private String[] list = null; |
54 |
|
55 |
private String managerID = null; |
56 |
|
57 |
public void setManagerID(String id) { |
58 |
managerID = id; |
59 |
} |
60 |
|
61 |
public int getType() { |
62 |
return type;
|
63 |
} |
64 |
|
65 |
public void setType(int type) { |
66 |
this.type = type;
|
67 |
} |
68 |
|
69 |
public String getId() { |
70 |
return id;
|
71 |
} |
72 |
|
73 |
public void setId(String id) { |
74 |
this.id = id;
|
75 |
} |
76 |
|
77 |
public Object getDefaultValue() { |
78 |
return defaultValue;
|
79 |
} |
80 |
|
81 |
public void setDefaultValue(Object defaultValue) { |
82 |
this.defaultValue = defaultValue;
|
83 |
} |
84 |
|
85 |
public String[] getList() { |
86 |
return list;
|
87 |
} |
88 |
|
89 |
public void setList(String[] list) { |
90 |
this.list = list;
|
91 |
} |
92 |
|
93 |
@SuppressWarnings("unchecked") |
94 |
public void loadFromState(PersistentState state) throws PersistenceException { |
95 |
this.type = state.getInt("type"); |
96 |
this.id = state.getString("id"); |
97 |
if(state.get("defaultValue") == null) { |
98 |
loadPanel(state); |
99 |
} else
|
100 |
this.defaultValue = ((GenericPersistentObject)state.get("defaultValue")).getObject(); |
101 |
List<String> list = state.getList("list"); |
102 |
if(list != null) { |
103 |
this.list = new String[list.size()]; |
104 |
for (int i = 0; i < list.size(); i++) { |
105 |
this.list[i] = list.get(i);
|
106 |
} |
107 |
} |
108 |
} |
109 |
|
110 |
private void loadPanel(PersistentState state) throws PersistenceException { |
111 |
this.managerID = state.getString("managerID"); |
112 |
Params paramsForUI = (Params)state.get("paramsForUI");
|
113 |
RegistrableFilterListener panel = (RegistrableFilterListener)getPanelObject(managerID); |
114 |
panel.setParams(paramsForUI); |
115 |
this.defaultValue = panel;
|
116 |
} |
117 |
|
118 |
private Object getPanelObject(String filterID) { |
119 |
try {
|
120 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
121 |
ExtensionPoint point = extensionPoints.get("PanelRasterFilter");
|
122 |
Class<?> c = point.get(filterID).getExtension();
|
123 |
Constructor<?> hazNuevo = c.getConstructor();
|
124 |
return hazNuevo.newInstance();
|
125 |
} catch (IllegalArgumentException e) { |
126 |
log.debug("Error loading filter class", e);
|
127 |
} catch (InstantiationException e) { |
128 |
log.debug("Error loading filter class", e);
|
129 |
} catch (IllegalAccessException e) { |
130 |
log.debug("Error loading filter class", e);
|
131 |
} catch (InvocationTargetException e) { |
132 |
log.debug("Error loading filter class", e);
|
133 |
} catch (SecurityException e) { |
134 |
log.debug("Error loading filter class", e);
|
135 |
} catch (NoSuchMethodException e) { |
136 |
log.debug("Error loading filter class", e);
|
137 |
} |
138 |
return null; |
139 |
} |
140 |
|
141 |
public void saveToState(PersistentState state) throws PersistenceException { |
142 |
state.set("type", type);
|
143 |
state.set("id", id);
|
144 |
|
145 |
if(defaultValue instanceof RegistrableFilterListener) { |
146 |
savePanel(state); |
147 |
state.setNull("defaultValue");
|
148 |
} else {
|
149 |
state.set("defaultValue", GenericPersistentObject.create(defaultValue));
|
150 |
} |
151 |
|
152 |
state.set("list", list);
|
153 |
} |
154 |
|
155 |
private void savePanel(PersistentState state) throws PersistenceException { |
156 |
Params p = ((RegistrableFilterListener)defaultValue).getParams(); |
157 |
state.set("paramsForUI", p);
|
158 |
state.set("managerID", managerID);
|
159 |
} |
160 |
|
161 |
public static void registerPersistence() { |
162 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
163 |
|
164 |
DynStruct definition = manager.addDefinition( |
165 |
ParamImpl.class, |
166 |
"ParamImpl_Persistence",
|
167 |
"ParamImpl Persistence",
|
168 |
null,
|
169 |
null
|
170 |
); |
171 |
definition.addDynFieldInt("type")
|
172 |
.setMandatory(false);
|
173 |
|
174 |
definition.addDynFieldString("id")
|
175 |
.setMandatory(false);
|
176 |
|
177 |
definition.addDynFieldObject("defaultValue")
|
178 |
.setClassOfValue(GenericPersistentObject.class) |
179 |
.setMandatory(false);
|
180 |
|
181 |
definition.addDynFieldList("list")
|
182 |
.setClassOfItems(String.class)
|
183 |
.setMandatory(false);
|
184 |
|
185 |
definition.addDynFieldObject("paramsForUI")
|
186 |
.setClassOfValue(Params.class) |
187 |
.setMandatory(false);
|
188 |
|
189 |
definition.addDynFieldString("managerID")
|
190 |
.setMandatory(false);
|
191 |
} |
192 |
} |