gvsig-3d / 2.1 / trunk / org.gvsig.view3d / org.gvsig.view3d.swing / org.gvsig.view3d.swing.impl / src / main / java / org / gvsig / view3d / swing / impl / loader / rasterelevation / JRasterElevationLoaderParametersController.java @ 773
History | View | Annotate | Download (9.49 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright ? 2007-2017 gvSIG Association
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.view3d.swing.impl.loader.rasterelevation; |
25 |
|
26 |
import gov.nasa.worldwind.util.UnitsFormat; |
27 |
|
28 |
import java.util.Hashtable; |
29 |
import java.util.Locale; |
30 |
|
31 |
import javax.swing.JComponent; |
32 |
import javax.swing.JLabel; |
33 |
|
34 |
import org.gvsig.tools.ToolsLocator; |
35 |
import org.gvsig.tools.dataTypes.CoercionException; |
36 |
import org.gvsig.tools.dataTypes.DataTypes; |
37 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
38 |
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion; |
39 |
import org.gvsig.tools.i18n.I18nManager; |
40 |
import org.gvsig.view3d.lib.api.View3DLocator; |
41 |
import org.gvsig.view3d.lib.api.View3DManager; |
42 |
import org.gvsig.view3d.lib.api.exception.FactoryNotRegisteredException; |
43 |
import org.gvsig.view3d.lib.api.loader.LoaderFactory; |
44 |
import org.gvsig.view3d.lib.api.loader.LoaderParameters; |
45 |
import org.gvsig.view3d.lib.api.loader.RasterElevationLoaderParameters; |
46 |
import org.gvsig.view3d.swing.api.exception.InvalidLoaderParametersException; |
47 |
import org.gvsig.view3d.swing.api.loader.JLoaderParameters; |
48 |
import org.slf4j.Logger; |
49 |
import org.slf4j.LoggerFactory; |
50 |
|
51 |
/**
|
52 |
*
|
53 |
* @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
|
54 |
*
|
55 |
*/
|
56 |
public class JRasterElevationLoaderParametersController extends |
57 |
JRasterElevationLoaderParametersView implements JLoaderParameters {
|
58 |
|
59 |
private static final Logger LOG = |
60 |
LoggerFactory.getLogger(JRasterElevationLoaderParametersController.class); |
61 |
|
62 |
private static final long serialVersionUID = -4758336789000643038L; |
63 |
|
64 |
private RasterElevationLoaderParameters parameters;
|
65 |
|
66 |
/**
|
67 |
*
|
68 |
* @param layer
|
69 |
* @param parameters
|
70 |
*/
|
71 |
public JRasterElevationLoaderParametersController(LoaderParameters parameters) {
|
72 |
super();
|
73 |
|
74 |
if(!(parameters instanceof RasterElevationLoaderParameters)){ |
75 |
throw new IllegalArgumentException( |
76 |
"Illegal parameters. Parameters must be an instance of RasterElevationLoaderParameters");
|
77 |
} |
78 |
|
79 |
this.parameters = (RasterElevationLoaderParameters) parameters;
|
80 |
|
81 |
translate(); |
82 |
initComponents(); |
83 |
} |
84 |
|
85 |
private void translate() { |
86 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
87 |
minDetailLevelText.setText(i18nManager.getTranslation(minDetailLevelText.getText())); |
88 |
maxDetailLevelText.setText(i18nManager.getTranslation(maxDetailLevelText.getText())); |
89 |
zeroLevelResolutionText.setText(i18nManager.getTranslation(zeroLevelResolutionText.getText())); |
90 |
tileSizeText.setText(i18nManager.getTranslation(tileSizeText.getText())); |
91 |
elevationUnitsText.setText(i18nManager.getTranslation(elevationUnitsText.getText())); |
92 |
noDataText.setText(i18nManager.getTranslation(noDataText.getText())); |
93 |
} |
94 |
|
95 |
@Override
|
96 |
public void setLocale(Locale l) { |
97 |
if(!l.equals(this.getLocale())){ |
98 |
translate(); |
99 |
} |
100 |
super.setLocale(l);
|
101 |
} |
102 |
|
103 |
private void initComponents() { |
104 |
|
105 |
//Slider
|
106 |
Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer,JLabel>(); |
107 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
108 |
labelTable.put(new Integer(0), new JLabel(i18nManager.getTranslation("low"))); |
109 |
labelTable.put(new Integer(10), new JLabel(i18nManager.getTranslation("high"))); |
110 |
zeroLevelResolution.setLabelTable(labelTable); |
111 |
zeroLevelResolution.setPaintLabels(true);
|
112 |
zeroLevelResolution.setMajorTickSpacing(1);
|
113 |
zeroLevelResolution.setPaintTicks(true);
|
114 |
|
115 |
try {
|
116 |
fill(parameters); |
117 |
} catch (CoercionException e) {
|
118 |
LOG.warn("Can not parse loader parameters to fill panel. Setting default values", e);
|
119 |
View3DManager manager = View3DLocator.getManager(); |
120 |
LoaderFactory loaderFactory = null;
|
121 |
try {
|
122 |
loaderFactory = manager.getLoaderFactory(RasterElevationLoaderParameters.NAME); |
123 |
this.parameters =
|
124 |
(RasterElevationLoaderParameters) loaderFactory.createParameters(parameters |
125 |
.getLayer()); |
126 |
fill(parameters); |
127 |
} catch (FactoryNotRegisteredException e1) {
|
128 |
LOG.error("There are not registered loader factory with {} name",
|
129 |
new String[] { RasterElevationLoaderParameters.NAME }, e1); |
130 |
throw new IllegalArgumentException("Raster loader is not registered."); |
131 |
} catch (CoercionException e2) {
|
132 |
LOG.error( |
133 |
"Default values of RasterLoader can not be parsed to fill JRasterLoader panel",
|
134 |
new String[] { RasterElevationLoaderParameters.NAME }, e2); |
135 |
throw new IllegalArgumentException( |
136 |
"Default Raster loader parameter values are not valid");
|
137 |
} |
138 |
} |
139 |
} |
140 |
|
141 |
@SuppressWarnings("unchecked") |
142 |
private void fill(RasterElevationLoaderParameters parameters) throws CoercionException { |
143 |
DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
144 |
Coercion stringCoercion = dataTypesManager.getCoercion(DataTypes.STRING); |
145 |
this.minDetailLevel.setText((String) stringCoercion.coerce(parameters.getMinLevel())); |
146 |
this.maxDetailLevel.setText((String) stringCoercion.coerce(parameters.getMaxLevel())); |
147 |
this.zeroLevelResolution
|
148 |
.setValue((int) ((1 / parameters.getLevelZeroResolution()) * (this.zeroLevelResolution |
149 |
.getMaximum() / 2)));
|
150 |
this.tileSize.setText((String) stringCoercion.coerce(parameters.getTileSize())); |
151 |
this.noData.setText((String) stringCoercion.coerce(parameters.getNoData())); |
152 |
|
153 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
154 |
String meters = i18nManager.getTranslation("meters"); |
155 |
String foots = i18nManager.getTranslation("Pies"); |
156 |
elevationUnits.addItem(meters); |
157 |
elevationUnits.addItem(foots); |
158 |
} |
159 |
|
160 |
@Override
|
161 |
public JComponent asJComponent() { |
162 |
return this; |
163 |
} |
164 |
|
165 |
@Override
|
166 |
public LoaderParameters getLoaderParameters() throws InvalidLoaderParametersException { |
167 |
|
168 |
DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
169 |
Coercion doubleCoercion = dataTypesManager.getCoercion(DataTypes.DOUBLE); |
170 |
Coercion intCoercion = dataTypesManager.getCoercion(DataTypes.INT); |
171 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
172 |
|
173 |
try {
|
174 |
this.parameters.setMinLevel((int) intCoercion.coerce(minDetailLevel.getText())); |
175 |
} catch (CoercionException e) {
|
176 |
LOG.warn("Can not cource minimum detail level", e);
|
177 |
throw new InvalidLoaderParametersException("Minimum detail level has to be a number", |
178 |
"minimum_detail_level_has_to_be_a_number", e);
|
179 |
} |
180 |
|
181 |
try {
|
182 |
this.parameters.setMaxLevel((int) intCoercion.coerce(maxDetailLevel.getText())); |
183 |
} catch (CoercionException e) {
|
184 |
LOG.warn("Can not cource maximum detail level", e);
|
185 |
throw new InvalidLoaderParametersException("Maximum detail level has to be a number", |
186 |
"maximum_detail_level_has_to_be_a_number", e);
|
187 |
} |
188 |
|
189 |
try {
|
190 |
this.parameters.setTileSize((int) intCoercion.coerce(tileSize.getText())); |
191 |
} catch (CoercionException e) {
|
192 |
LOG.warn("Can not cource tile size", e);
|
193 |
throw new InvalidLoaderParametersException("Tile size has to be a number", |
194 |
"tile_size_has_to_be_a_number", e);
|
195 |
} |
196 |
|
197 |
try {
|
198 |
this.parameters.setNoData((double) doubleCoercion.coerce(this.noData.getText())); |
199 |
} catch (CoercionException e) {
|
200 |
LOG.warn("Can not cource no data value", e);
|
201 |
throw new InvalidLoaderParametersException("NoData value has to be a number", |
202 |
"nodata_value_has_to_be_a_number", e);
|
203 |
} |
204 |
|
205 |
this.parameters.setLevelZeroResolution(getResolutionMultiplier());
|
206 |
|
207 |
String units = (String) elevationUnits.getSelectedItem(); |
208 |
if (units.equalsIgnoreCase(i18nManager.getTranslation("meters"))) { |
209 |
this.parameters.setElevationUnits(UnitsFormat.METERS);
|
210 |
} else if (units.equalsIgnoreCase(i18nManager.getTranslation("Pies"))) { |
211 |
this.parameters.setElevationUnits(UnitsFormat.FEET);
|
212 |
} |
213 |
return this.parameters; |
214 |
} |
215 |
|
216 |
private double getResolutionMultiplier() { |
217 |
// Operation to convert slider value to valid multiplier.
|
218 |
if (zeroLevelResolution.getValue() == 0) { |
219 |
return 1 / (0.5 / (zeroLevelResolution.getMaximum() / 2.0)); |
220 |
} |
221 |
return 1 / (zeroLevelResolution.getValue() / (zeroLevelResolution.getMaximum() / 2.0)); |
222 |
} |
223 |
|
224 |
} |