gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster.tools / org.gvsig.raster.tools.swing / org.gvsig.raster.tools.swing.impl / src / main / java / org / gvsig / raster / tools / swing / impl / clip / ClipPanelController.java @ 6700
History | View | Annotate | Download (44.2 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2017 gvSIG Association
|
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., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.raster.tools.swing.impl.clip; |
24 |
|
25 |
import java.awt.Image; |
26 |
import java.awt.event.ActionEvent; |
27 |
import java.awt.event.ActionListener; |
28 |
import java.io.File; |
29 |
import java.text.NumberFormat; |
30 |
import java.util.List; |
31 |
import java.util.Locale; |
32 |
|
33 |
import javax.swing.JComponent; |
34 |
import javax.swing.JFileChooser; |
35 |
import javax.swing.JOptionPane; |
36 |
import javax.swing.ListSelectionModel; |
37 |
import javax.swing.event.AncestorEvent; |
38 |
import javax.swing.event.AncestorListener; |
39 |
import javax.swing.event.DocumentEvent; |
40 |
import javax.swing.event.DocumentListener; |
41 |
import javax.swing.event.ListSelectionEvent; |
42 |
import javax.swing.event.ListSelectionListener; |
43 |
import javax.swing.table.DefaultTableColumnModel; |
44 |
import javax.swing.table.TableColumn; |
45 |
import javax.swing.table.TableColumnModel; |
46 |
import javax.swing.text.DefaultFormatterFactory; |
47 |
import javax.swing.text.NumberFormatter; |
48 |
|
49 |
import org.cresques.cts.ICoordTrans; |
50 |
import org.cresques.cts.IProjection; |
51 |
import org.slf4j.Logger; |
52 |
import org.slf4j.LoggerFactory; |
53 |
|
54 |
import org.gvsig.fmap.IconThemeHelper; |
55 |
import org.gvsig.fmap.dal.exception.DataException; |
56 |
import org.gvsig.fmap.dal.exception.InitializeException; |
57 |
import org.gvsig.fmap.dal.raster.api.RasterQuery; |
58 |
import org.gvsig.fmap.dal.raster.api.RasterSet; |
59 |
import org.gvsig.fmap.dal.raster.api.RasterStore; |
60 |
import org.gvsig.fmap.geom.Geometry.DIMENSIONS; |
61 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
62 |
import org.gvsig.fmap.geom.GeometryLocator; |
63 |
import org.gvsig.fmap.geom.GeometryManager; |
64 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
65 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
66 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
67 |
import org.gvsig.fmap.geom.primitive.Envelope; |
68 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
69 |
import org.gvsig.fmap.mapcontext.raster.api.RasterLayer; |
70 |
import org.gvsig.fmap.mapcontrol.MapControl; |
71 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
72 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.RectangleBehavior; |
73 |
import org.gvsig.fmap.mapcontrol.tools.Events.EnvelopeEvent; |
74 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.RectangleListener; |
75 |
import org.gvsig.raster.lib.buffer.api.BufferDimensions; |
76 |
import org.gvsig.raster.tools.lib.api.RasterClip; |
77 |
import org.gvsig.raster.tools.lib.api.exceptions.RasterToolCreatingPanelException; |
78 |
import org.gvsig.raster.tools.swing.api.ClipPanel; |
79 |
import org.gvsig.raster.tools.swing.api.RasterToolsSwingLocator; |
80 |
import org.gvsig.raster.tools.swing.impl.SelectableBandsTableModel; |
81 |
import org.gvsig.tools.ToolsLocator; |
82 |
import org.gvsig.tools.i18n.I18nManager; |
83 |
import org.gvsig.tools.locator.LocatorException; |
84 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
85 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
86 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
87 |
|
88 |
/**
|
89 |
* @author fdiaz
|
90 |
*
|
91 |
*/
|
92 |
public class ClipPanelController extends ClipPanelView implements ClipPanel { |
93 |
|
94 |
/**
|
95 |
*
|
96 |
*/
|
97 |
private static final long serialVersionUID = 6979744092502525949L; |
98 |
private static final Logger LOG = LoggerFactory.getLogger(ClipPanelController.class); |
99 |
private static final String DEFAULT_LAYER_NAME= "Clip"; |
100 |
private static final String CLIP_TOOL_NAME = "raster-clip-tool"; |
101 |
|
102 |
// The elements are defined in the Buffer interface, the array should not be
|
103 |
// changed.
|
104 |
private static String[] interpolationMethods = { "_nearest_neighbour", "_bilinear", "_inverse_distance", |
105 |
"_bicubic_spline", "_bspline" }; |
106 |
|
107 |
private RasterLayer layer;
|
108 |
private Envelope layerEnvelope;
|
109 |
private double layerPixelSizeX; |
110 |
private double layerPixelSizeY; |
111 |
private int layerColumns; |
112 |
private int layerRows; |
113 |
|
114 |
private Envelope envelope;
|
115 |
private boolean updatingResolution; |
116 |
private boolean updatingEnvelope; |
117 |
private boolean updatingPixels; |
118 |
private MapControl mapControl;
|
119 |
private String previousTool; |
120 |
ICoordTrans ct; |
121 |
|
122 |
/**
|
123 |
* @throws RasterToolCreatingPanelException
|
124 |
*/
|
125 |
public ClipPanelController() throws RasterToolCreatingPanelException { |
126 |
translate(); |
127 |
initializeComponents(); |
128 |
} |
129 |
|
130 |
public void set(FLayer layer) throws RasterToolCreatingPanelException{ |
131 |
this.layer = (RasterLayer) layer;
|
132 |
if(mapControl!=null){ |
133 |
init(); |
134 |
} |
135 |
} |
136 |
|
137 |
public void set(MapControl mapControl) throws RasterToolCreatingPanelException{ |
138 |
this.mapControl = mapControl;
|
139 |
this.previousTool = mapControl.getCurrentTool();
|
140 |
if(this.layer != null){ |
141 |
init(); |
142 |
} |
143 |
} |
144 |
|
145 |
private void init() throws RasterToolCreatingPanelException{ |
146 |
this.addAncestorListener(new AncestorListener() { |
147 |
@Override
|
148 |
public void ancestorRemoved(AncestorEvent event) { |
149 |
returnMapControlToPreviousTool(); |
150 |
} |
151 |
@Override
|
152 |
public void ancestorMoved(AncestorEvent event) { |
153 |
returnMapControlToPreviousTool(); |
154 |
} |
155 |
@Override
|
156 |
public void ancestorAdded(AncestorEvent event) { |
157 |
// Do nothing
|
158 |
} |
159 |
}); |
160 |
IProjection layerProj = this.layer.getProjection();
|
161 |
IProjection viewProj = mapControl.getProjection(); |
162 |
ct = null;
|
163 |
if(!layerProj.equals(viewProj)){
|
164 |
ct = layerProj.getCT(viewProj); |
165 |
} |
166 |
// Solo cogemos de las dimensiones del store las filas y columnas porque si la capa est? reproyectada
|
167 |
// su envelope y sus pixels size no se corresponden con los del raster store.
|
168 |
try {
|
169 |
layerEnvelope = this.layer.getFullEnvelope();
|
170 |
} catch (LocatorException | DataException e1) {
|
171 |
LOG.warn("Can't get the envelope of the layer", e1);
|
172 |
throw new RasterToolCreatingPanelException("Can't get the envelope of the layer "+layer.getName(),e1); |
173 |
} |
174 |
|
175 |
RasterStore rasterStore = this.layer.getRasterStore();
|
176 |
try {
|
177 |
BufferDimensions rasterStoreDimensions = rasterStore.getDimensions(); |
178 |
layerColumns = rasterStoreDimensions.getColumns(); |
179 |
layerRows = rasterStoreDimensions.getRows(); |
180 |
layerPixelSizeX = layerEnvelope.getLength(DIMENSIONS.X)/layerColumns; |
181 |
layerPixelSizeY = layerEnvelope.getLength(DIMENSIONS.Y)/layerRows; |
182 |
} catch (InitializeException e) {
|
183 |
LOG.warn("Can't get dimensions of the layer's raster store.", e);
|
184 |
throw new RasterToolCreatingPanelException("Can't get the raster set of the layer " + layer.getName(), e); |
185 |
} |
186 |
|
187 |
SelectableBandsTableModel model = new SelectableBandsTableModel(this.layer.getRasterStore()); |
188 |
tblBands.setModel(model); |
189 |
|
190 |
tblBands.setColumnModel(new DefaultTableColumnModel()); |
191 |
TableColumnModel columnModel = tblBands.getColumnModel();
|
192 |
columnModel.setColumnSelectionAllowed(false);
|
193 |
for (int i = 0; i < model.getColumnCount(); i++) { |
194 |
TableColumn tableColumn = new TableColumn(i); |
195 |
tableColumn.setIdentifier(model.getColumnName(i)); |
196 |
tableColumn.setHeaderValue(model.getColumnName(i)); |
197 |
columnModel.addColumn(tableColumn); |
198 |
} |
199 |
|
200 |
setFullEnvelope(); |
201 |
txtCellWidth.setValue(layerPixelSizeX); |
202 |
txtCellHeight.setValue(layerPixelSizeY); |
203 |
|
204 |
updateResolution(true);
|
205 |
updatingResolution=false;
|
206 |
|
207 |
File folder = RasterToolsSwingLocator.getSwingManager().getLastFolderUsedToSaveRaster();
|
208 |
if(folder!=null){ |
209 |
txtFolder.setText(folder.getAbsolutePath()); |
210 |
} |
211 |
} |
212 |
|
213 |
@Override
|
214 |
public JComponent asJComponent() { |
215 |
return this; |
216 |
} |
217 |
|
218 |
private void initializeComponents() { |
219 |
// Tab coordinates
|
220 |
IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
221 |
lblIconPix1.setIcon(iconTheme.get("upleft"));
|
222 |
lblIconPix2.setIcon(iconTheme.get("downright"));
|
223 |
lblIconReal1.setIcon(iconTheme.get("upleft"));
|
224 |
lblIconReal2.setIcon(iconTheme.get("downright"));
|
225 |
|
226 |
PixelCoordinatesDocumentListener pixelCoordinatesDocumentListener = new PixelCoordinatesDocumentListener();
|
227 |
txtPix1X.getDocument().addDocumentListener(pixelCoordinatesDocumentListener); |
228 |
txtPix1Y.getDocument().addDocumentListener(pixelCoordinatesDocumentListener); |
229 |
txtPix2X.getDocument().addDocumentListener(pixelCoordinatesDocumentListener); |
230 |
txtPix2Y.getDocument().addDocumentListener(pixelCoordinatesDocumentListener); |
231 |
|
232 |
RealCoordinatesDocumentListener realCoordinatesDocumentListener = new RealCoordinatesDocumentListener();
|
233 |
txtReal1X.getDocument().addDocumentListener(realCoordinatesDocumentListener); |
234 |
txtReal1Y.getDocument().addDocumentListener(realCoordinatesDocumentListener); |
235 |
txtReal2X.getDocument().addDocumentListener(realCoordinatesDocumentListener); |
236 |
txtReal2Y.getDocument().addDocumentListener(realCoordinatesDocumentListener); |
237 |
|
238 |
btnLoad.setIcon(iconTheme.get("loadparameters-icon"));
|
239 |
btnSave.setIcon(iconTheme.get("saveparameters-icon"));
|
240 |
btnPixelsRound.setIcon(iconTheme.get("round-icon"));
|
241 |
btnRois.setIcon(iconTheme.get("roi"));
|
242 |
btnFullLayer.setIcon(iconTheme.get("fullextent-icon"));
|
243 |
btnView.setIcon(iconTheme.get("viewextent-icon"));
|
244 |
btnViewSelection.setIcon(iconTheme.get("selecttool-icon"));
|
245 |
|
246 |
btnLoad.addActionListener(new ActionListener() { |
247 |
@Override
|
248 |
public void actionPerformed(ActionEvent e) { |
249 |
doLoadEnvelope(); |
250 |
} |
251 |
}); |
252 |
|
253 |
btnSave.addActionListener(new ActionListener() { |
254 |
@Override
|
255 |
public void actionPerformed(ActionEvent e) { |
256 |
doSaveEnvelope(); |
257 |
} |
258 |
}); |
259 |
|
260 |
btnPixelsRound.addActionListener(new ActionListener() { |
261 |
@Override
|
262 |
public void actionPerformed(ActionEvent e) { |
263 |
doPixelsRound(); |
264 |
} |
265 |
}); |
266 |
|
267 |
//FIXME: habilitarlo cuando est? implementado
|
268 |
btnRois.setEnabled(false);
|
269 |
btnRois.addActionListener(new ActionListener() { |
270 |
@Override
|
271 |
public void actionPerformed(ActionEvent e) { |
272 |
doImportROIsEnvelope(); |
273 |
} |
274 |
}); |
275 |
|
276 |
btnFullLayer.addActionListener(new ActionListener() { |
277 |
@Override
|
278 |
public void actionPerformed(ActionEvent e) { |
279 |
doImportFullLayerEnvelope(); |
280 |
} |
281 |
}); |
282 |
|
283 |
btnView.addActionListener(new ActionListener() { |
284 |
@Override
|
285 |
public void actionPerformed(ActionEvent e) { |
286 |
doImportViewExtentEnvelope(); |
287 |
} |
288 |
}); |
289 |
|
290 |
btnViewSelection.addActionListener(new ActionListener() { |
291 |
@Override
|
292 |
public void actionPerformed(ActionEvent e) { |
293 |
doImportViewSelection(); |
294 |
} |
295 |
}); |
296 |
|
297 |
// Tab resolution
|
298 |
|
299 |
rbtnCellSize.addActionListener(new ActionListener() { |
300 |
@Override
|
301 |
public void actionPerformed(ActionEvent e) { |
302 |
doSelectModeSize(); |
303 |
} |
304 |
}); |
305 |
rbtnRasterSize.addActionListener(new ActionListener() { |
306 |
@Override
|
307 |
public void actionPerformed(ActionEvent e) { |
308 |
doSelectModeSize(); |
309 |
} |
310 |
}); |
311 |
|
312 |
cmbInterpolationMethod.setSelectedIndex(0);
|
313 |
cmbInterpolationMethod.setEditable(false);
|
314 |
|
315 |
txtCellWidth.getDocument().addDocumentListener(new DocumentListener() { |
316 |
@Override
|
317 |
public void removeUpdate(DocumentEvent e) { |
318 |
changedUpdate(e); |
319 |
} |
320 |
@Override
|
321 |
public void insertUpdate(DocumentEvent e) { |
322 |
changedUpdate(e); |
323 |
} |
324 |
@Override
|
325 |
public void changedUpdate(DocumentEvent e) { |
326 |
doChangeUpdateCellWidth(); |
327 |
} |
328 |
}); |
329 |
|
330 |
txtCellHeight.getDocument().addDocumentListener(new DocumentListener() { |
331 |
@Override
|
332 |
public void removeUpdate(DocumentEvent e) { |
333 |
changedUpdate(e); |
334 |
} |
335 |
@Override
|
336 |
public void insertUpdate(DocumentEvent e) { |
337 |
changedUpdate(e); |
338 |
} |
339 |
@Override
|
340 |
public void changedUpdate(DocumentEvent e) { |
341 |
doChangeUpdateCellHeight(); |
342 |
} |
343 |
}); |
344 |
|
345 |
txtWidth.getDocument().addDocumentListener(new DocumentListener() { |
346 |
@Override
|
347 |
public void removeUpdate(DocumentEvent e) { |
348 |
changedUpdate(e); |
349 |
} |
350 |
@Override
|
351 |
public void insertUpdate(DocumentEvent e) { |
352 |
changedUpdate(e); |
353 |
} |
354 |
@Override
|
355 |
public void changedUpdate(DocumentEvent e) { |
356 |
doChangeUpdateWidth(); |
357 |
|
358 |
} |
359 |
}); |
360 |
|
361 |
txtHeight.getDocument().addDocumentListener(new DocumentListener() { |
362 |
@Override
|
363 |
public void removeUpdate(DocumentEvent e) { |
364 |
changedUpdate(e); |
365 |
} |
366 |
@Override
|
367 |
public void insertUpdate(DocumentEvent e) { |
368 |
changedUpdate(e); |
369 |
} |
370 |
@Override
|
371 |
public void changedUpdate(DocumentEvent e) { |
372 |
doChangeUpdateHeight(); |
373 |
} |
374 |
}); |
375 |
|
376 |
btnRestore.addActionListener(new ActionListener() { |
377 |
@Override
|
378 |
public void actionPerformed(ActionEvent e) { |
379 |
doRestore(); |
380 |
} |
381 |
}); |
382 |
|
383 |
btnImportCellWidthFromView.addActionListener(new ActionListener() { |
384 |
@Override
|
385 |
public void actionPerformed(ActionEvent e) { |
386 |
doImportCellWidthFromView(); |
387 |
} |
388 |
}); |
389 |
|
390 |
// Tab Bands
|
391 |
|
392 |
tblBands.setRowSelectionAllowed(true);
|
393 |
tblBands.setColumnSelectionAllowed(false);
|
394 |
tblBands.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
395 |
tblBands.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
396 |
|
397 |
@Override
|
398 |
public void valueChanged(ListSelectionEvent e) { |
399 |
doSelectionTblBandsChanged(); |
400 |
} |
401 |
}); |
402 |
|
403 |
|
404 |
this.btnUpBand.addActionListener(new ActionListener() { |
405 |
@Override
|
406 |
public void actionPerformed(ActionEvent e) { |
407 |
doUpBand(); |
408 |
} |
409 |
}); |
410 |
|
411 |
this.btnDownBand.addActionListener(new ActionListener() { |
412 |
@Override
|
413 |
public void actionPerformed(ActionEvent e) { |
414 |
doDownBand(); |
415 |
} |
416 |
}); |
417 |
|
418 |
// Tab other options
|
419 |
|
420 |
btnUpBand.setIcon(iconTheme.get("up"));
|
421 |
btnDownBand.setIcon(iconTheme.get("down"));
|
422 |
|
423 |
|
424 |
chkSaveTo.addActionListener(new ActionListener() { |
425 |
@Override
|
426 |
public void actionPerformed(ActionEvent e) { |
427 |
doCheckSaveToFolder(); |
428 |
} |
429 |
}); |
430 |
|
431 |
btnFolderChooser.addActionListener(new ActionListener() { |
432 |
@Override
|
433 |
public void actionPerformed(ActionEvent e) { |
434 |
doSelectFolder(); |
435 |
} |
436 |
}); |
437 |
|
438 |
} |
439 |
|
440 |
protected void doImportCellWidthFromView() { |
441 |
double ratio = layerPixelSizeX/layerPixelSizeY;
|
442 |
|
443 |
double pixelSizeX = mapControl.getViewPort().getDist1pixel();
|
444 |
txtCellWidth.setValue(pixelSizeX); |
445 |
// double pixelSizeY = pixelSizeX/ratio;
|
446 |
// txtCellHeight.setValue(pixelSizeY);
|
447 |
// txtWidth.setValue((int) Math.round(envelope.getLength(DIMENSIONS.X) / pixelSizeX));
|
448 |
// txtHeight.setValue((int) Math.round(envelope.getLength(DIMENSIONS.Y) / pixelSizeY));
|
449 |
updateSize(); |
450 |
} |
451 |
|
452 |
protected void doChangeUpdateCellHeight() { |
453 |
if(updatingResolution){
|
454 |
return;
|
455 |
} |
456 |
updatingResolution = true;
|
457 |
Double pixelSizeY = ((Number) txtCellHeight.getValue()).doubleValue(); |
458 |
if (pixelSizeY != null) { |
459 |
Envelope envelope = this.envelope; //calculateEnvelope(); |
460 |
double ratio = layerPixelSizeX/layerPixelSizeY;
|
461 |
double pixelSizeX = pixelSizeY*ratio;
|
462 |
txtCellWidth.setValue(pixelSizeX); |
463 |
txtWidth.setValue((int) Math.round(envelope.getLength(DIMENSIONS.X) / pixelSizeX)); |
464 |
txtHeight.setValue((int) Math.round(envelope.getLength(DIMENSIONS.Y) / pixelSizeY)); |
465 |
} |
466 |
updatingResolution = false;
|
467 |
} |
468 |
|
469 |
protected void doChangeUpdateCellWidth() { |
470 |
|
471 |
if (updatingResolution) {
|
472 |
return;
|
473 |
} |
474 |
updatingResolution = true;
|
475 |
Double pixelSizeX = ((Number) txtCellWidth.getValue()).doubleValue(); |
476 |
if (pixelSizeX != null) { |
477 |
Envelope envelope = this.envelope; // calculateEnvelope(); |
478 |
double ratio = layerPixelSizeX / layerPixelSizeY;
|
479 |
double pixelSizeY = pixelSizeX / ratio;
|
480 |
txtCellHeight.setValue(pixelSizeY); |
481 |
txtWidth.setValue((int) Math.round(envelope.getLength(DIMENSIONS.X) / pixelSizeX)); |
482 |
txtHeight.setValue((int) Math.round(envelope.getLength(DIMENSIONS.Y) / pixelSizeY)); |
483 |
} |
484 |
updatingResolution = false;
|
485 |
} |
486 |
|
487 |
protected void doChangeUpdateHeight() { |
488 |
if(updatingResolution){
|
489 |
return;
|
490 |
} |
491 |
updatingResolution = true;
|
492 |
Integer value = (Integer) txtHeight.getValue(); |
493 |
Envelope envelope = this.envelope; //calculateEnvelope(); |
494 |
double pixelSizeY = envelope.getLength(DIMENSIONS.Y) / value;
|
495 |
double ratio = layerPixelSizeX/layerPixelSizeY;
|
496 |
double pixelSizeX = pixelSizeY*ratio;
|
497 |
txtCellWidth.setValue(pixelSizeX); |
498 |
txtCellHeight.setValue(pixelSizeY); |
499 |
txtWidth.setValue((int)Math.round(envelope.getLength(DIMENSIONS.X) / pixelSizeX)); |
500 |
updatingResolution = false;
|
501 |
} |
502 |
|
503 |
protected void doChangeUpdateWidth() { |
504 |
if(updatingResolution){
|
505 |
return;
|
506 |
} |
507 |
updatingResolution = true;
|
508 |
Integer value = (Integer) txtWidth.getValue(); |
509 |
Envelope envelope = this.envelope; //calculateEnvelope(); |
510 |
double pixelSizeX = envelope.getLength(DIMENSIONS.X) / value;
|
511 |
double ratio = layerPixelSizeX/layerPixelSizeY;
|
512 |
double pixelSizeY = pixelSizeX/ratio;
|
513 |
txtCellWidth.setValue(pixelSizeX); |
514 |
txtCellHeight.setValue(pixelSizeY); |
515 |
txtHeight.setValue((int)Math.round(envelope.getLength(DIMENSIONS.Y) / pixelSizeY)); |
516 |
updatingResolution = false;
|
517 |
} |
518 |
|
519 |
protected void doImportROIsEnvelope() { |
520 |
try {
|
521 |
RasterStore rasterStore = this.layer.getRasterStore();
|
522 |
// FIXME: Ver qu? hacemos con las ROIs y coger el envelope
|
523 |
Envelope envelope = GeometryLocator.getGeometryManager().createEnvelope(SUBTYPES.GEOM2D); |
524 |
setEnvelope(envelope); |
525 |
RasterQuery query = rasterStore.createRasterQuery().setClip(envelope); |
526 |
RasterSet rasterSet = rasterStore.getRasterSet(query); |
527 |
setPixels(0, 0, rasterSet.getColumns() - 1, rasterSet.getRows() - 1); |
528 |
} catch (LocatorException | CreateEnvelopeException e1) {
|
529 |
LOG.warn("Can't get the envelope of the rois of the layer", e1);
|
530 |
// TODO Gestionar la excepci?n ?mostrar di?logo de advertencia?
|
531 |
} catch (DataException e1) {
|
532 |
LOG.warn("Can't get the raster set of the envelope of the ROIs of the layer", e1);
|
533 |
// TODO Gestionar la excepci?n ?mostrar di?logo de advertencia?
|
534 |
} |
535 |
updateResolution(false);
|
536 |
} |
537 |
|
538 |
protected void doLoadEnvelope() { |
539 |
Envelope lastEnvelopeSaved = RasterToolsSwingLocator.getSwingManager().getLastClipEnvelopeSaved(); |
540 |
if (lastEnvelopeSaved == null) { |
541 |
ThreadSafeDialogsManager dlgManager = ToolsSwingLocator.getThreadSafeDialogsManager(); |
542 |
String message = "_there_is_no_previously_saved_envelope"; |
543 |
String title = "_error_creating_clip"; |
544 |
dlgManager.messageDialog(message, title, JOptionPane.WARNING_MESSAGE);
|
545 |
} else {
|
546 |
if (lastEnvelopeSaved.intersects(ClipPanelController.this.layerEnvelope)) {
|
547 |
Envelope intersection = null;
|
548 |
try {
|
549 |
intersection = |
550 |
(lastEnvelopeSaved.getGeometry().intersection(ClipPanelController.this.layerEnvelope |
551 |
.getGeometry())).getEnvelope(); |
552 |
} catch (GeometryOperationNotSupportedException | GeometryOperationException e) {
|
553 |
LOG.warn("Can't gets intersection between last envelope saved and the layer's envelope", e);
|
554 |
} |
555 |
|
556 |
if (intersection != null) { |
557 |
this.setEnvelope(intersection);
|
558 |
this.updatePixelsFromEnvelope();
|
559 |
} |
560 |
} |
561 |
} |
562 |
} |
563 |
|
564 |
protected void doSaveEnvelope() { |
565 |
try {
|
566 |
RasterToolsSwingLocator.getSwingManager().setLastClipEnvelopeSaved((Envelope) this.envelope.clone());
|
567 |
// this.lastEnvelopeSaved = (Envelope) this.envelope.clone();
|
568 |
} catch (CloneNotSupportedException e1) { |
569 |
LOG.warn("Can't clone the envelope", e1);
|
570 |
} |
571 |
} |
572 |
|
573 |
protected void doImportViewSelection() { |
574 |
|
575 |
if (!CLIP_TOOL_NAME.equalsIgnoreCase(this.mapControl.getCurrentTool())) { |
576 |
this.previousTool = this.mapControl.getCurrentTool(); |
577 |
} |
578 |
|
579 |
RectangleBehavior behavior = new RectangleBehavior(new RectangleListener() { |
580 |
|
581 |
@Override
|
582 |
public Image getImageCursor() { |
583 |
return IconThemeHelper.getImage("cursor-select-by-rectangle"); |
584 |
} |
585 |
|
586 |
@Override
|
587 |
public boolean cancelDrawing() { |
588 |
return false; |
589 |
} |
590 |
|
591 |
@Override
|
592 |
public void rectangle(EnvelopeEvent event) throws BehaviorException { |
593 |
Envelope selectedEnvelope = event.getWorldCoordRect(); |
594 |
if (selectedEnvelope.intersects(ClipPanelController.this.layerEnvelope)) {
|
595 |
Envelope intersection = null;
|
596 |
try {
|
597 |
intersection = |
598 |
(selectedEnvelope.getGeometry().intersection(ClipPanelController.this.layerEnvelope |
599 |
.getGeometry())).getEnvelope(); |
600 |
} catch (GeometryOperationNotSupportedException | GeometryOperationException e) {
|
601 |
LOG.warn("Can't gets intersection between selected envelope and the layer's envelope", e);
|
602 |
} |
603 |
|
604 |
if (intersection != null) { |
605 |
ClipPanelController.this.setEnvelope(intersection); |
606 |
updatingPixels = true;
|
607 |
if (ClipPanelController.this.updatePixelsFromEnvelope()) {
|
608 |
updateSize(); |
609 |
} |
610 |
; |
611 |
updatingPixels = false;
|
612 |
|
613 |
} |
614 |
} |
615 |
ClipPanelController.this.returnMapControlToPreviousTool(); |
616 |
} |
617 |
}); |
618 |
|
619 |
// if(!this.mapControl.hasTool(CLIP_TOOL_NAME)){
|
620 |
this.mapControl.addBehavior(CLIP_TOOL_NAME, behavior);
|
621 |
// }
|
622 |
this.mapControl.setTool(CLIP_TOOL_NAME);
|
623 |
} |
624 |
|
625 |
protected void doPixelsRound() { |
626 |
if (updateEnvelopeFromPixels()) {
|
627 |
updateSize(); |
628 |
} |
629 |
} |
630 |
|
631 |
protected void doRestore() { |
632 |
updateResolution(true);
|
633 |
} |
634 |
|
635 |
protected void doSelectFolder() { |
636 |
// FIXME:
|
637 |
JFileChooser fc = null; |
638 |
if (!txtFolder.getText().isEmpty()) {
|
639 |
fc = new JFileChooser(txtFolder.getText()); |
640 |
} else {
|
641 |
fc = new JFileChooser(); |
642 |
} |
643 |
|
644 |
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
|
645 |
fc.setMultiSelectionEnabled(false);
|
646 |
int result = fc.showOpenDialog(this); |
647 |
if (result == JFileChooser.APPROVE_OPTION) { |
648 |
File folder = fc.getSelectedFile();
|
649 |
txtFolder.setText(folder.getAbsolutePath()); |
650 |
RasterToolsSwingLocator.getSwingManager().setLastFolderUsedToSaveRaster(folder); |
651 |
} |
652 |
} |
653 |
|
654 |
protected void doCheckSaveToFolder() { |
655 |
boolean selected = chkSaveTo.isSelected();
|
656 |
txtFolder.setEnabled(selected); |
657 |
btnFolderChooser.setEnabled(selected); |
658 |
lblFolder.setEnabled(selected); |
659 |
} |
660 |
|
661 |
protected void doImportFullLayerEnvelope() { |
662 |
setFullEnvelope(); |
663 |
updateResolution(true);
|
664 |
} |
665 |
|
666 |
protected void doImportViewExtentEnvelope() { |
667 |
setEnvelope(mapControl.getViewPort().getAdjustedEnvelope()); |
668 |
updatePixelsFromEnvelope(); |
669 |
doImportCellWidthFromView(); |
670 |
} |
671 |
|
672 |
protected void doSelectModeSize() { |
673 |
lblCellHeight.setEnabled(rbtnCellSize.isSelected()); |
674 |
txtCellWidth.setEnabled(rbtnCellSize.isSelected()); |
675 |
lblCellWidth.setEnabled(rbtnCellSize.isSelected()); |
676 |
txtCellHeight.setEnabled(rbtnCellSize.isSelected()); |
677 |
|
678 |
lblRasterWidth.setEnabled(rbtnRasterSize.isSelected()); |
679 |
txtWidth.setEnabled(rbtnRasterSize.isSelected()); |
680 |
lblRasterHeight.setEnabled(rbtnRasterSize.isSelected()); |
681 |
txtHeight.setEnabled(rbtnRasterSize.isSelected()); |
682 |
} |
683 |
|
684 |
protected void doDownBand() { |
685 |
SelectableBandsTableModel bandsTableModel = (SelectableBandsTableModel) tblBands.getModel(); |
686 |
int selectedRow = tblBands.getSelectedRow();
|
687 |
bandsTableModel.down(selectedRow); |
688 |
tblBands.getSelectionModel().setSelectionInterval(selectedRow + 1, selectedRow + 1); |
689 |
} |
690 |
|
691 |
protected void doUpBand() { |
692 |
SelectableBandsTableModel bandsTableModel = (SelectableBandsTableModel) tblBands.getModel(); |
693 |
int selectedRow = tblBands.getSelectedRow();
|
694 |
bandsTableModel.up(selectedRow); |
695 |
tblBands.getSelectionModel().setSelectionInterval(selectedRow - 1, selectedRow - 1); |
696 |
} |
697 |
|
698 |
|
699 |
private void translate() { |
700 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
701 |
|
702 |
lblPixelCoord.setText(i18nManager.getTranslation(lblPixelCoord.getText())); |
703 |
lblRealCoord.setText(i18nManager.getTranslation(lblRealCoord.getText())); |
704 |
for (int i = 0; i < tabClip.getTabCount(); i++) { |
705 |
tabClip.setTitleAt(i, i18nManager.getTranslation(tabClip.getTitleAt(i))); |
706 |
} |
707 |
|
708 |
lblPix1X.setText(i18nManager.getTranslation(lblPix1X.getText())); |
709 |
lblPix1Y.setText(i18nManager.getTranslation(lblPix1Y.getText())); |
710 |
lblPix2X.setText(i18nManager.getTranslation(lblPix2X.getText())); |
711 |
lblPix2Y.setText(i18nManager.getTranslation(lblPix2Y.getText())); |
712 |
|
713 |
btnViewSelection.setText(""); //i18nManager.getTranslation(btnViewSelection.getText())); |
714 |
btnViewSelection.setToolTipText(i18nManager.getTranslation(btnViewSelection.getToolTipText())); |
715 |
|
716 |
btnFullLayer.setText(""); //i18nManager.getTranslation(btnFullLayer.getText())); |
717 |
btnFullLayer.setToolTipText(i18nManager.getTranslation(btnFullLayer.getToolTipText())); |
718 |
|
719 |
btnView.setText(""); //i18nManager.getTranslation(btnView.getText())); |
720 |
btnView.setToolTipText(i18nManager.getTranslation(btnView.getToolTipText())); |
721 |
|
722 |
btnPixelsRound.setText(""); //i18nManager.getTranslation(btnPixelsRound.getText())); |
723 |
btnPixelsRound.setToolTipText(i18nManager.getTranslation(btnPixelsRound.getToolTipText())); |
724 |
|
725 |
btnLoad.setText(""); //i18nManager.getTranslation(btnLoad.getText())); |
726 |
btnLoad.setToolTipText(i18nManager.getTranslation(btnLoad.getToolTipText())); |
727 |
|
728 |
btnSave.setText(""); //i18nManager.getTranslation(btnSave.getText())); |
729 |
btnSave.setToolTipText(i18nManager.getTranslation(btnSave.getToolTipText())); |
730 |
|
731 |
btnRois.setText(""); //i18nManager.getTranslation(btnRois.getText())); |
732 |
btnRois.setToolTipText(i18nManager.getTranslation(btnRois.getToolTipText())); |
733 |
|
734 |
lblRasterWidth.setText(i18nManager.getTranslation(lblRasterWidth.getText())); |
735 |
lblCellWidth.setText(i18nManager.getTranslation(lblCellWidth.getText())); |
736 |
lblCellHeight.setText(i18nManager.getTranslation(lblCellHeight.getText())); |
737 |
lblRasterHeight.setText(i18nManager.getTranslation(lblRasterHeight.getText())); |
738 |
|
739 |
lblResolutionMode.setText(i18nManager.getTranslation(lblResolutionMode.getText())); |
740 |
lblInterpolation.setText(i18nManager.getTranslation(lblInterpolation.getText())); |
741 |
|
742 |
btnRestore.setText(i18nManager.getTranslation(btnRestore.getText())); |
743 |
btnRestore.setToolTipText(i18nManager.getTranslation(btnRestore.getToolTipText())); |
744 |
|
745 |
btnImportCellWidthFromView.setText(i18nManager.getTranslation(btnImportCellWidthFromView.getText())); |
746 |
btnImportCellWidthFromView.setToolTipText(i18nManager.getTranslation(btnImportCellWidthFromView.getToolTipText())); |
747 |
|
748 |
rbtnCellSize.setText(i18nManager.getTranslation(rbtnCellSize.getText())); |
749 |
rbtnCellSize.setToolTipText(i18nManager.getTranslation(rbtnCellSize.getToolTipText())); |
750 |
|
751 |
rbtnRasterSize.setText(i18nManager.getTranslation(rbtnRasterSize.getText())); |
752 |
rbtnRasterSize.setToolTipText(i18nManager.getTranslation(rbtnRasterSize.getToolTipText())); |
753 |
|
754 |
lblLayerNames.setText(i18nManager.getTranslation(lblLayerNames.getText())); |
755 |
txtLayerNames.setText(i18nManager.getTranslation("_newlayer"));
|
756 |
|
757 |
chkOnePerBand.setText(i18nManager.getTranslation(chkOnePerBand.getText())); |
758 |
chkOnePerBand.setToolTipText(i18nManager.getTranslation(chkOnePerBand.getToolTipText())); |
759 |
|
760 |
chkSaveTo.setText(i18nManager.getTranslation(chkSaveTo.getText())); |
761 |
chkSaveTo.setToolTipText(i18nManager.getTranslation(chkSaveTo.getToolTipText())); |
762 |
|
763 |
lblFolder.setText(i18nManager.getTranslation(lblFolder.getText())); |
764 |
|
765 |
btnFolderChooser.setText(i18nManager.getTranslation(btnFolderChooser.getText())); |
766 |
btnFolderChooser.setToolTipText(i18nManager.getTranslation(btnFolderChooser.getToolTipText())); |
767 |
|
768 |
btnUpBand.setText(""); //i18nManager.getTranslation(btnUpBand.getText())); |
769 |
btnUpBand.setToolTipText(i18nManager.getTranslation(btnUpBand.getToolTipText())); |
770 |
|
771 |
btnDownBand.setText(""); //i18nManager.getTranslation(btnDownBand.getText())); |
772 |
btnDownBand.setToolTipText(i18nManager.getTranslation(btnDownBand.getToolTipText())); |
773 |
|
774 |
cmbInterpolationMethod.removeAllItems(); |
775 |
|
776 |
for (int i = 0; i < interpolationMethods.length; i++) { |
777 |
cmbInterpolationMethod.addItem(i18nManager.getTranslation(interpolationMethods[i])); |
778 |
} |
779 |
|
780 |
|
781 |
NumberFormat numberInstance = NumberFormat.getNumberInstance(); |
782 |
numberInstance.setGroupingUsed(false);
|
783 |
numberInstance.setMaximumFractionDigits(Integer.MAX_VALUE);
|
784 |
NumberFormatter numberFormatter = new NumberFormatter(numberInstance); |
785 |
DefaultFormatterFactory tf = new DefaultFormatterFactory( |
786 |
numberFormatter, |
787 |
numberFormatter, |
788 |
numberFormatter, |
789 |
numberFormatter); |
790 |
txtReal1X.setFormatterFactory(tf); |
791 |
|
792 |
txtReal1Y.setFormatterFactory(tf); |
793 |
txtReal2X.setFormatterFactory(tf); |
794 |
txtReal2Y.setFormatterFactory(tf); |
795 |
txtCellWidth.setFormatterFactory(tf); |
796 |
txtCellHeight.setFormatterFactory(tf); |
797 |
|
798 |
NumberFormat integerInstance = NumberFormat.getIntegerInstance(); |
799 |
integerInstance.setGroupingUsed(false);
|
800 |
numberFormatter = new NumberFormatter(integerInstance); |
801 |
numberFormatter.setMinimum(0);
|
802 |
numberFormatter.setMaximum(layerColumns-1);
|
803 |
tf = new DefaultFormatterFactory( |
804 |
numberFormatter, |
805 |
numberFormatter, |
806 |
numberFormatter, |
807 |
numberFormatter); |
808 |
txtPix1X.setFormatterFactory(tf); |
809 |
|
810 |
numberFormatter = new NumberFormatter(integerInstance); |
811 |
numberFormatter.setMinimum(0);
|
812 |
numberFormatter.setMaximum(layerRows-1);
|
813 |
tf = new DefaultFormatterFactory( |
814 |
numberFormatter, |
815 |
numberFormatter, |
816 |
numberFormatter, |
817 |
numberFormatter); |
818 |
txtPix1Y.setFormatterFactory(tf); |
819 |
numberFormatter = new NumberFormatter(integerInstance); |
820 |
numberFormatter.setMinimum(0);
|
821 |
numberFormatter.setMaximum(layerColumns-1);
|
822 |
tf = new DefaultFormatterFactory( |
823 |
numberFormatter, |
824 |
numberFormatter, |
825 |
numberFormatter, |
826 |
numberFormatter); |
827 |
txtPix2X.setFormatterFactory(tf); |
828 |
numberFormatter = new NumberFormatter(integerInstance); |
829 |
numberFormatter.setMinimum(0);
|
830 |
numberFormatter.setMaximum(layerRows-1);
|
831 |
tf = new DefaultFormatterFactory( |
832 |
numberFormatter, |
833 |
numberFormatter, |
834 |
numberFormatter, |
835 |
numberFormatter); |
836 |
txtPix2Y.setFormatterFactory(tf); |
837 |
|
838 |
numberFormatter = new NumberFormatter(integerInstance); |
839 |
numberFormatter.setMinimum(0);
|
840 |
tf = new DefaultFormatterFactory( |
841 |
numberFormatter, |
842 |
numberFormatter, |
843 |
numberFormatter, |
844 |
numberFormatter); |
845 |
txtWidth.setFormatterFactory(tf); |
846 |
txtHeight.setFormatterFactory(tf); |
847 |
|
848 |
} |
849 |
|
850 |
/**
|
851 |
* @param locale
|
852 |
*
|
853 |
*/
|
854 |
public void setLocate(Locale locale) { |
855 |
Locale l = super.getLocale(); |
856 |
if (!l.equals(locale)) {
|
857 |
translate(); |
858 |
} |
859 |
super.setLocale(locale);
|
860 |
} |
861 |
|
862 |
private void doSelectionTblBandsChanged() { |
863 |
if (tblBands.getSelectedRowCount() == 1) { |
864 |
if (tblBands.getSelectedRow() > 0) { |
865 |
btnUpBand.setEnabled(true);
|
866 |
} else {
|
867 |
btnUpBand.setEnabled(false);
|
868 |
} |
869 |
if (tblBands.getSelectedRow() < tblBands.getRowCount() - 1) { |
870 |
btnDownBand.setEnabled(true);
|
871 |
} else {
|
872 |
btnDownBand.setEnabled(false);
|
873 |
} |
874 |
} else {
|
875 |
btnUpBand.setEnabled(false);
|
876 |
btnDownBand.setEnabled(false);
|
877 |
} |
878 |
} |
879 |
|
880 |
private void returnMapControlToPreviousTool() { |
881 |
mapControl.setTool(this.previousTool);
|
882 |
} |
883 |
|
884 |
/**
|
885 |
*
|
886 |
*/
|
887 |
private void setFullEnvelope() { |
888 |
this.envelope = this.layerEnvelope; |
889 |
setPixels(0, 0, layerColumns - 1, layerRows - 1); |
890 |
setEnvelope(envelope); |
891 |
} |
892 |
|
893 |
|
894 |
public void fetch(RasterClip rasterClip) { |
895 |
rasterClip.setLayer(this.layer);
|
896 |
|
897 |
rasterClip.setEnvelope(calculateEnvelope()); |
898 |
rasterClip.setPixelSizeX(getCellWidth()); |
899 |
rasterClip.setPixelSizeY(getCellHeight()); |
900 |
rasterClip.setInterpolationMethod(getInterpolationMethod()); |
901 |
rasterClip.setRows(getResolutionHeight()); |
902 |
rasterClip.setColumns(getResolutionWidth()); |
903 |
|
904 |
String layerNamePrefix = txtLayerNames.getText();
|
905 |
if(layerNamePrefix.isEmpty()){
|
906 |
layerNamePrefix = DEFAULT_LAYER_NAME; |
907 |
} |
908 |
rasterClip.setLayerNamePrefix(layerNamePrefix); |
909 |
rasterClip.setCreateOneLayerPerBand(chkOnePerBand.isSelected()); |
910 |
rasterClip.setSaveToNewRasterFile(chkSaveTo.isSelected()); |
911 |
|
912 |
List<Integer> bands = ((SelectableBandsTableModel)tblBands.getModel()).getSelectedBands(); |
913 |
|
914 |
rasterClip.setBands(bands); |
915 |
|
916 |
boolean saveToNewRasterFile = chkSaveTo.isSelected();
|
917 |
rasterClip.setSaveToNewRasterFile(saveToNewRasterFile); |
918 |
if(saveToNewRasterFile){
|
919 |
File folder = new File(txtFolder.getText()); |
920 |
rasterClip.setFolder(folder); |
921 |
} |
922 |
} |
923 |
|
924 |
private void updateResolution(boolean updatePixelSize) { |
925 |
updatingResolution=true;
|
926 |
txtWidth.setValue(calculateColumns()); |
927 |
txtHeight.setValue(calculateRows()); |
928 |
if(updatePixelSize){
|
929 |
txtCellWidth.setValue(calculatePixelSizeX()); |
930 |
txtCellHeight.setValue(calculatePixelSizeY()); |
931 |
} |
932 |
updatingResolution=false;
|
933 |
} |
934 |
|
935 |
private Integer calculateColumns() { |
936 |
Integer x1 = (Integer) txtPix1X.getValue(); |
937 |
Integer x2 = (Integer) txtPix2X.getValue(); |
938 |
return x2 - x1 + 1; |
939 |
} |
940 |
|
941 |
private Integer calculateRows() { |
942 |
Integer y1 = (Integer) txtPix1Y.getValue(); |
943 |
Integer y2 = (Integer) txtPix2Y.getValue(); |
944 |
return y2 - y1 + 1; |
945 |
} |
946 |
|
947 |
private Double calculatePixelSizeX() { |
948 |
return this.envelope.getLength(DIMENSIONS.X) / calculateColumns(); |
949 |
} |
950 |
|
951 |
private Double calculatePixelSizeY() { |
952 |
return this.envelope.getLength(DIMENSIONS.Y) / calculateRows(); |
953 |
} |
954 |
|
955 |
private double getCellWidth() { |
956 |
return ((Number) txtCellWidth.getValue()).doubleValue(); |
957 |
} |
958 |
|
959 |
private double getCellHeight() { |
960 |
return ((Number) txtCellHeight.getValue()).doubleValue(); |
961 |
} |
962 |
|
963 |
private int getResolutionWidth() { |
964 |
return ((Integer) txtWidth.getValue()).intValue(); |
965 |
} |
966 |
|
967 |
private int getResolutionHeight() { |
968 |
return ((Integer) txtHeight.getValue()).intValue(); |
969 |
} |
970 |
|
971 |
private void setEnvelope(Envelope envelope) { |
972 |
updatingEnvelope = true;
|
973 |
this.envelope = envelope;
|
974 |
txtReal1X.setValue(envelope.getMinimum(DIMENSIONS.X)); |
975 |
txtReal1Y.setValue(envelope.getMaximum(DIMENSIONS.Y)); |
976 |
txtReal2X.setValue(envelope.getMaximum(DIMENSIONS.X)); |
977 |
txtReal2Y.setValue(envelope.getMinimum(DIMENSIONS.Y)); |
978 |
updatingEnvelope = false;
|
979 |
} |
980 |
|
981 |
private void setPixels(int x1, int y1, int x2, int y2) { |
982 |
updatingPixels = true;
|
983 |
txtPix1X.setValue(new Integer(x1)); |
984 |
txtPix1Y.setValue(new Integer(y1)); |
985 |
txtPix2X.setValue(new Integer(x2)); |
986 |
txtPix2Y.setValue(new Integer(y2)); |
987 |
updatingPixels = false;
|
988 |
} |
989 |
|
990 |
private int getInterpolationMethod() { |
991 |
return cmbInterpolationMethod.getSelectedIndex() + 1; |
992 |
} |
993 |
|
994 |
/*
|
995 |
* Creates an envelope from the data of the corresponding text fields.
|
996 |
*/
|
997 |
private Envelope calculateEnvelope() {
|
998 |
// Coercion coercion = new CoerceToDouble();
|
999 |
boolean fail = false; |
1000 |
Double x1 = ((Number) txtReal1X.getValue()).doubleValue(); |
1001 |
Double y1 = ((Number) txtReal1Y.getValue()).doubleValue(); |
1002 |
Double x2 = ((Number) txtReal2X.getValue()).doubleValue(); |
1003 |
Double y2 = ((Number) txtReal2Y.getValue()).doubleValue(); |
1004 |
try {
|
1005 |
if (x1 != null && y1 != null && x2 != null && y2 != null && x1 != null && x2 >= x1 && y2 <= y1) { |
1006 |
this.envelope = GeometryLocator.getGeometryManager().createEnvelope(x1, y2, x2, y1, SUBTYPES.GEOM2D);
|
1007 |
} else {
|
1008 |
fail = true;
|
1009 |
} |
1010 |
} catch (Exception e2) { |
1011 |
fail = true;
|
1012 |
} |
1013 |
|
1014 |
if (fail) {
|
1015 |
ThreadSafeDialogsManager dlgManager = ToolsSwingLocator.getThreadSafeDialogsManager(); |
1016 |
String message =
|
1017 |
"_cant_create_the_envelope_with_this_coordinates_XcolonX_XparenthesisX_XUL1X_XUL2X_XdashX_XDR1X_XDR2X_XparenthesisX";
|
1018 |
String title = "_error_updating_envelope"; |
1019 |
dlgManager.messageDialog(message, |
1020 |
new String[] { txtReal1X.getText(), txtReal2Y.getText(), txtReal2X.getText(), txtReal1Y.getText() }, |
1021 |
title, JOptionPane.WARNING_MESSAGE);
|
1022 |
LOG.warn(message); |
1023 |
} |
1024 |
return this.envelope; |
1025 |
} |
1026 |
|
1027 |
private void updateSize() { |
1028 |
double pixelSizeX = ((Number)txtCellWidth.getValue()).doubleValue(); |
1029 |
double ratio = layerPixelSizeX/layerPixelSizeY;
|
1030 |
|
1031 |
txtWidth.setValue((int)Math.round(this.envelope.getLength(DIMENSIONS.X) / pixelSizeX)); |
1032 |
txtHeight.setValue((int)Math.round(this.envelope.getLength(DIMENSIONS.Y) / (pixelSizeX/ratio))); |
1033 |
} |
1034 |
|
1035 |
private boolean updatePixelsFromEnvelope() { |
1036 |
updatingPixels = true;
|
1037 |
Double real1X = ((Number) txtReal1X.getValue()).doubleValue(); |
1038 |
Double real2X = ((Number) txtReal2X.getValue()).doubleValue(); |
1039 |
Double real1Y = ((Number) txtReal1Y.getValue()).doubleValue(); |
1040 |
Double real2Y = ((Number) txtReal2Y.getValue()).doubleValue(); |
1041 |
if (real1X == null || real2X < real1X || real1X < layerEnvelope.getMinimum(DIMENSIONS.X)) { |
1042 |
real1X = layerEnvelope.getMinimum(DIMENSIONS.X); |
1043 |
} |
1044 |
if (real1Y == null || real2Y > real1Y || real1Y > layerEnvelope.getMaximum(DIMENSIONS.Y)) { |
1045 |
real1Y = layerEnvelope.getMaximum(DIMENSIONS.Y); |
1046 |
} |
1047 |
if (real2X == null || real2X < real1X || real2X > layerEnvelope.getMaximum(DIMENSIONS.X)) { |
1048 |
real2X = layerEnvelope.getMaximum(DIMENSIONS.X); |
1049 |
} |
1050 |
if (real2Y == null || real2Y > real1Y || real2Y < layerEnvelope.getMinimum(DIMENSIONS.Y)) { |
1051 |
real2Y = layerEnvelope.getMinimum(DIMENSIONS.Y); |
1052 |
} |
1053 |
|
1054 |
setPixels((int) Math.round((real1X - layerEnvelope.getMinimum(DIMENSIONS.X)) / layerPixelSizeX), |
1055 |
(int) Math.round((layerEnvelope.getMaximum(DIMENSIONS.Y) - real1Y) / layerPixelSizeY), |
1056 |
(int) Math.round((real2X - layerEnvelope.getMinimum(DIMENSIONS.X)) / layerPixelSizeX) - 1, |
1057 |
(int) Math.round((layerEnvelope.getMaximum(DIMENSIONS.Y) - real2Y) / layerPixelSizeY) - 1); |
1058 |
updatingPixels = false;
|
1059 |
return true; |
1060 |
} |
1061 |
|
1062 |
|
1063 |
private boolean updateEnvelopeFromPixels() { |
1064 |
updatingEnvelope = true;
|
1065 |
|
1066 |
Integer pix1X = null; |
1067 |
pix1X = (Integer) txtPix1X.getValue();
|
1068 |
Integer pix2X = null; |
1069 |
pix2X = (Integer) txtPix2X.getValue();
|
1070 |
Integer pix1Y = null; |
1071 |
pix1Y = (Integer) txtPix1Y.getValue();
|
1072 |
Integer pix2Y = null; |
1073 |
pix2Y = (Integer) txtPix2Y.getValue();
|
1074 |
if (pix1X == null || pix1X < 0 || pix2X < pix1X) { |
1075 |
pix1X = 0;
|
1076 |
} |
1077 |
if (pix1Y == null || pix1Y < 0 || pix2Y < pix1Y) { |
1078 |
pix1Y = 0;
|
1079 |
} |
1080 |
if (pix2X == null || pix2X < pix1X || pix2X >= layerColumns) { |
1081 |
pix2X = layerColumns - 1;
|
1082 |
} |
1083 |
if (pix2Y == null || pix2Y < pix1Y || pix2Y >= layerRows) { |
1084 |
pix2Y = layerRows - 1;
|
1085 |
} |
1086 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
1087 |
try {
|
1088 |
setEnvelope(geomManager.createEnvelope( |
1089 |
pix1X * layerPixelSizeX + layerEnvelope.getMinimum(DIMENSIONS.X), |
1090 |
layerEnvelope.getMaximum(DIMENSIONS.Y) - ((pix2Y + 1) * layerPixelSizeY) ,
|
1091 |
(pix2X + 1) * layerPixelSizeX + layerEnvelope.getMinimum(DIMENSIONS.X),
|
1092 |
layerEnvelope.getMaximum(DIMENSIONS.Y) - (pix1Y * layerPixelSizeY) , |
1093 |
SUBTYPES.GEOM2D)); |
1094 |
updatingEnvelope = false;
|
1095 |
return true; |
1096 |
} catch (CreateEnvelopeException e) {
|
1097 |
LOG.debug("Can't create the envelope", e);
|
1098 |
updatingEnvelope = false;
|
1099 |
return false; |
1100 |
} |
1101 |
} |
1102 |
|
1103 |
private class RealCoordinatesDocumentListener implements DocumentListener { |
1104 |
|
1105 |
@Override
|
1106 |
public void insertUpdate(DocumentEvent e) { |
1107 |
changedUpdate(e); |
1108 |
} |
1109 |
|
1110 |
@Override
|
1111 |
public void removeUpdate(DocumentEvent e) { |
1112 |
changedUpdate(e); |
1113 |
} |
1114 |
|
1115 |
@Override
|
1116 |
public void changedUpdate(DocumentEvent e) { |
1117 |
doChangedUpdateRealCoordinates(e); |
1118 |
} |
1119 |
|
1120 |
} |
1121 |
|
1122 |
private void doChangedUpdateRealCoordinates(DocumentEvent e) { |
1123 |
if(updatingEnvelope){
|
1124 |
return;
|
1125 |
} |
1126 |
if(txtReal1X.getValue()==null || txtReal2X.getValue()==null || txtReal1Y.getValue()==null || txtReal2Y.getValue()==null){ |
1127 |
return;
|
1128 |
} |
1129 |
updatingPixels = true;
|
1130 |
if(updatePixelsFromEnvelope()){
|
1131 |
updateSize(); |
1132 |
}; |
1133 |
updatingPixels = false;
|
1134 |
} |
1135 |
|
1136 |
private class PixelCoordinatesDocumentListener implements DocumentListener { |
1137 |
|
1138 |
@Override
|
1139 |
public void insertUpdate(DocumentEvent e) { |
1140 |
changedUpdate(e); |
1141 |
} |
1142 |
|
1143 |
@Override
|
1144 |
public void removeUpdate(DocumentEvent e) { |
1145 |
changedUpdate(e); |
1146 |
} |
1147 |
|
1148 |
@Override
|
1149 |
public void changedUpdate(DocumentEvent e) { |
1150 |
doChangedUpdatePixelCoordinates(e); |
1151 |
} |
1152 |
|
1153 |
} |
1154 |
|
1155 |
private void doChangedUpdatePixelCoordinates(DocumentEvent e) { |
1156 |
if(updatingPixels){
|
1157 |
return;
|
1158 |
} |
1159 |
if(txtPix1X.getValue()==null || txtPix2X.getValue()==null || txtPix1Y.getValue()==null || txtPix2Y.getValue()==null){ |
1160 |
return;
|
1161 |
} |
1162 |
updatingEnvelope = true;
|
1163 |
if(updateEnvelopeFromPixels()){
|
1164 |
updateSize(); |
1165 |
}; |
1166 |
updatingEnvelope = false;
|
1167 |
} |
1168 |
} |