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 / viewclip / ViewClipPanelController.java @ 8843
History | View | Annotate | Download (28.8 KB)
1 |
package org.gvsig.raster.tools.swing.impl.viewclip; |
---|---|
2 |
|
3 |
import java.awt.Dimension; |
4 |
import java.awt.Graphics2D; |
5 |
import java.awt.Image; |
6 |
import java.awt.event.ActionEvent; |
7 |
import java.awt.event.ActionListener; |
8 |
import java.awt.event.ItemEvent; |
9 |
import java.awt.event.ItemListener; |
10 |
import java.awt.image.BufferedImage; |
11 |
import java.io.IOException; |
12 |
import java.text.NumberFormat; |
13 |
|
14 |
import javax.swing.JComponent; |
15 |
import javax.swing.JOptionPane; |
16 |
import javax.swing.event.AncestorEvent; |
17 |
import javax.swing.event.AncestorListener; |
18 |
import javax.swing.event.DocumentEvent; |
19 |
import javax.swing.event.DocumentListener; |
20 |
import javax.swing.text.DefaultFormatterFactory; |
21 |
import javax.swing.text.NumberFormatter; |
22 |
|
23 |
import org.slf4j.Logger; |
24 |
import org.slf4j.LoggerFactory; |
25 |
|
26 |
import org.gvsig.fmap.IconThemeHelper; |
27 |
import org.gvsig.fmap.dal.exception.ReadException; |
28 |
import org.gvsig.fmap.geom.Geometry.DIMENSIONS; |
29 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
30 |
import org.gvsig.fmap.geom.GeometryLocator; |
31 |
import org.gvsig.fmap.geom.primitive.Envelope; |
32 |
import org.gvsig.fmap.mapcontext.MapContext; |
33 |
import org.gvsig.fmap.mapcontext.MapContextDrawer; |
34 |
import org.gvsig.fmap.mapcontext.MapContextException; |
35 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
36 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
37 |
import org.gvsig.fmap.mapcontext.ViewPort; |
38 |
import org.gvsig.fmap.mapcontext.events.ColorEvent; |
39 |
import org.gvsig.fmap.mapcontext.events.ExtentEvent; |
40 |
import org.gvsig.fmap.mapcontext.events.ProjectionEvent; |
41 |
import org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener; |
42 |
import org.gvsig.fmap.mapcontrol.MapControl; |
43 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
44 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.RectangleBehavior; |
45 |
import org.gvsig.fmap.mapcontrol.tools.Events.EnvelopeEvent; |
46 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.RectangleListener; |
47 |
import org.gvsig.raster.lib.buffer.api.Buffer; |
48 |
import org.gvsig.raster.lib.buffer.api.BufferLocator; |
49 |
import org.gvsig.raster.lib.buffer.api.BufferManager; |
50 |
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException; |
51 |
import org.gvsig.raster.swing.buffer.save.SaveBufferDialog; |
52 |
import org.gvsig.raster.tools.lib.api.exceptions.RasterToolCreatingPanelException; |
53 |
import org.gvsig.raster.tools.swing.api.viewclip.ViewClipPanel; |
54 |
import org.gvsig.tools.ToolsLocator; |
55 |
import org.gvsig.tools.i18n.I18nManager; |
56 |
import org.gvsig.tools.locator.LocatorException; |
57 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
58 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
59 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
60 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
61 |
import org.gvsig.tools.task.Cancellable; |
62 |
|
63 |
/**
|
64 |
* @author fdiaz
|
65 |
*
|
66 |
*/
|
67 |
public class ViewClipPanelController extends ViewClipPanelView implements ViewClipPanel { |
68 |
|
69 |
/**
|
70 |
*
|
71 |
*/
|
72 |
private static final long serialVersionUID = 3796048566550997832L; |
73 |
private static final Logger LOG = LoggerFactory.getLogger(ViewClipPanelController.class); |
74 |
private static final int BANDS = 4; |
75 |
private static int METERS_TO_CENTIMETERS = 100; |
76 |
private static double INCHES_TO_CENTIMETERS = 2.54; |
77 |
private static int[] RESOLUTIONS = new int[] { 72, 96, 160, 300, 600, 2540, 4000 };; |
78 |
|
79 |
private MapControl mapControl;
|
80 |
private String previousTool; |
81 |
private ViewPortListener viewPortListener;
|
82 |
|
83 |
private Envelope envelope;
|
84 |
private boolean updatingEnvelope; |
85 |
private boolean updatingPixelSection; |
86 |
private boolean updatingPrintSection; |
87 |
private RectangleBehavior behavior;
|
88 |
|
89 |
/**
|
90 |
*
|
91 |
*/
|
92 |
public ViewClipPanelController() {
|
93 |
translate(); |
94 |
initializeComponents(); |
95 |
} |
96 |
|
97 |
@Override
|
98 |
public JComponent asJComponent() { |
99 |
return this; |
100 |
} |
101 |
|
102 |
@Override
|
103 |
public void set(MapControl mapControl) throws RasterToolCreatingPanelException { |
104 |
if (this.mapControl != null) { |
105 |
if (mapControl != this.mapControl) { |
106 |
returnMapControlToPreviousTool(); |
107 |
this.mapControl = mapControl;
|
108 |
this.previousTool = mapControl.getCurrentTool();
|
109 |
} |
110 |
} else {
|
111 |
this.mapControl = mapControl;
|
112 |
this.previousTool = mapControl.getCurrentTool();
|
113 |
} |
114 |
init(); |
115 |
} |
116 |
|
117 |
private void init() { |
118 |
this.addAncestorListener(new AncestorListener() { |
119 |
|
120 |
@Override
|
121 |
public void ancestorRemoved(AncestorEvent event) { |
122 |
returnMapControlToPreviousTool(); |
123 |
} |
124 |
|
125 |
@Override
|
126 |
public void ancestorMoved(AncestorEvent event) { |
127 |
} |
128 |
|
129 |
@Override
|
130 |
public void ancestorAdded(AncestorEvent event) { |
131 |
doAddRectangularSelectionTool(); |
132 |
} |
133 |
}); |
134 |
|
135 |
doUpdatePixelSize(); |
136 |
doUpdateScaleDenominator(); |
137 |
doAddRectangularSelectionTool(); |
138 |
this.mapControl.getViewPort().addViewPortListener(this.getViewPortListener()); |
139 |
} |
140 |
|
141 |
private ViewPortListener getViewPortListener() {
|
142 |
if (this.viewPortListener == null) { |
143 |
viewPortListener = new ViewPortListener() {
|
144 |
|
145 |
@Override
|
146 |
public void projectionChanged(ProjectionEvent e) { |
147 |
// FIXME: ??
|
148 |
doUpdatePixelSize(); |
149 |
doUpdateScaleDenominator(); |
150 |
} |
151 |
|
152 |
@Override
|
153 |
public void extentChanged(ExtentEvent e) { |
154 |
doUpdatePixelSize(); |
155 |
doUpdateScaleDenominator(); |
156 |
} |
157 |
|
158 |
@Override
|
159 |
public void backColorChanged(ColorEvent e) { |
160 |
// Do nothing
|
161 |
} |
162 |
}; |
163 |
} |
164 |
return this.viewPortListener; |
165 |
} |
166 |
|
167 |
protected void doUpdatePixelSize() { |
168 |
if (updatingPixelSection) {
|
169 |
return;
|
170 |
} |
171 |
updatingPixelSection = true;
|
172 |
double pixelSize = this.mapControl.getViewPort().getDist1pixel(); |
173 |
this.txtPixelSize.setValue(pixelSize);
|
174 |
if (this.envelope != null) { |
175 |
txtWidth.setValue(Math.round(this.envelope.getLength(DIMENSIONS.X) / pixelSize)); |
176 |
txtHeight.setValue(Math.round(this.envelope.getLength(DIMENSIONS.Y) / pixelSize)); |
177 |
updatePrintSectionFromPixelSection(); |
178 |
updateResultSize(); |
179 |
} |
180 |
|
181 |
updatingPixelSection = false;
|
182 |
|
183 |
} |
184 |
|
185 |
protected void doUpdateScaleDenominator() { |
186 |
if (updatingPrintSection) {
|
187 |
return;
|
188 |
} |
189 |
updatingPrintSection = true;
|
190 |
|
191 |
MapContext mapContext = this.mapControl.getMapContext();
|
192 |
long scaleView = mapContext.getScaleView();
|
193 |
txtScaleDenominator.setValue(scaleView); |
194 |
ViewPort viewPort = this.mapControl.getViewPort();
|
195 |
double distanceTransToMeter = mapContext.getDistanceTrans2Meter()[viewPort.getMapUnits()];
|
196 |
|
197 |
if (this.envelope != null && rdbPrintSelection.isSelected()) { |
198 |
double width = (distanceTransToMeter * METERS_TO_CENTIMETERS * this.envelope.getLength(DIMENSIONS.X) / scaleView); |
199 |
double height = (distanceTransToMeter * METERS_TO_CENTIMETERS * this.envelope.getLength(DIMENSIONS.Y) / scaleView); |
200 |
txtPrintWidth.setValue(width); |
201 |
txtPrintHeight.setValue(height); |
202 |
|
203 |
updatePixelSectionFromPrintSection(); |
204 |
updateResultSize(); |
205 |
} |
206 |
|
207 |
updatingPrintSection = false;
|
208 |
|
209 |
} |
210 |
|
211 |
private void returnMapControlToPreviousTool() { |
212 |
mapControl.setTool(this.previousTool);
|
213 |
mapControl.removeCombinedTool(this.getBehavior());
|
214 |
} |
215 |
|
216 |
private void translate() { |
217 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
218 |
|
219 |
lblRealCoord.setText(i18nManager.getTranslation(lblRealCoord.getText())); |
220 |
lblRealCoord.setText(i18nManager.getTranslation(lblRealCoord.getText())); |
221 |
|
222 |
lblWidth.setText(i18nManager.getTranslation(lblWidth.getText())); |
223 |
lblHeight.setText(i18nManager.getTranslation(lblHeight.getText())); |
224 |
lblPixelSize.setText(i18nManager.getTranslation(lblPixelSize.getText())); |
225 |
|
226 |
lblPrintWidth.setText(i18nManager.getTranslation(lblPrintWidth.getText())); |
227 |
lblPrintHeight.setText(i18nManager.getTranslation(lblPrintHeight.getText())); |
228 |
lblScaleDenominator.setText(i18nManager.getTranslation(lblScaleDenominator.getText())); |
229 |
|
230 |
rdbPixelSelection.setText(i18nManager.getTranslation(rdbPixelSelection.getText())); |
231 |
rdbPrintSelection.setText(i18nManager.getTranslation(rdbPrintSelection.getText())); |
232 |
|
233 |
lblResolution.setText(i18nManager.getTranslation(lblResolution.getText())); |
234 |
lblResultSize.setText(i18nManager.getTranslation(lblResultSize.getText())); |
235 |
|
236 |
// btnTool.setText(i18nManager.getTranslation(btnTool.getText()));
|
237 |
btnTool.setToolTipText(i18nManager.getTranslation(btnTool.getToolTipText())); |
238 |
|
239 |
NumberFormat numberInstance = NumberFormat.getNumberInstance(); |
240 |
numberInstance.setGroupingUsed(false);
|
241 |
numberInstance.setMaximumFractionDigits(Integer.MAX_VALUE);
|
242 |
NumberFormatter numberFormatter = new NumberFormatter(numberInstance); |
243 |
DefaultFormatterFactory tf =
|
244 |
new DefaultFormatterFactory(numberFormatter, numberFormatter, numberFormatter, numberFormatter); |
245 |
txtReal1X.setFormatterFactory(tf); |
246 |
txtReal1Y.setFormatterFactory(tf); |
247 |
txtReal2X.setFormatterFactory(tf); |
248 |
txtReal2Y.setFormatterFactory(tf); |
249 |
txtPixelSize.setFormatterFactory(tf); |
250 |
txtPrintWidth.setFormatterFactory(tf); |
251 |
txtPrintHeight.setFormatterFactory(tf); |
252 |
txtScaleDenominator.setFormatterFactory(tf); |
253 |
|
254 |
NumberFormat integerInstance = NumberFormat.getIntegerInstance(); |
255 |
integerInstance.setGroupingUsed(false);
|
256 |
numberFormatter = new NumberFormatter(integerInstance); |
257 |
numberFormatter.setMinimum(0);
|
258 |
numberFormatter.setMaximum(Integer.MAX_VALUE);
|
259 |
tf = new DefaultFormatterFactory(numberFormatter, numberFormatter, numberFormatter, numberFormatter); |
260 |
txtWidth.setFormatterFactory(tf); |
261 |
txtHeight.setFormatterFactory(tf); |
262 |
txtResultSize.setFormatterFactory(tf); |
263 |
} |
264 |
|
265 |
private void initializeComponents() { |
266 |
// Tab coordinates
|
267 |
IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
268 |
lblIconReal1.setIcon(iconTheme.get("upleft"));
|
269 |
lblIconReal2.setIcon(iconTheme.get("downright"));
|
270 |
|
271 |
RealCoordinatesDocumentListener realCoordinatesDocumentListener = new RealCoordinatesDocumentListener();
|
272 |
txtReal1X.getDocument().addDocumentListener(realCoordinatesDocumentListener); |
273 |
txtReal1Y.getDocument().addDocumentListener(realCoordinatesDocumentListener); |
274 |
txtReal2X.getDocument().addDocumentListener(realCoordinatesDocumentListener); |
275 |
txtReal2Y.getDocument().addDocumentListener(realCoordinatesDocumentListener); |
276 |
|
277 |
rdbPixelSelection.addActionListener(new ActionListener() { |
278 |
|
279 |
@Override
|
280 |
public void actionPerformed(ActionEvent e) { |
281 |
doSelectByPixel(); |
282 |
} |
283 |
}); |
284 |
rdbPrintSelection.addActionListener(new ActionListener() { |
285 |
|
286 |
@Override
|
287 |
public void actionPerformed(ActionEvent e) { |
288 |
doSelectForPrint(); |
289 |
} |
290 |
}); |
291 |
|
292 |
txtWidth.getDocument().addDocumentListener(new DocumentListener() { |
293 |
|
294 |
@Override
|
295 |
public void removeUpdate(DocumentEvent e) { |
296 |
// Do nothing
|
297 |
} |
298 |
|
299 |
@Override
|
300 |
public void insertUpdate(DocumentEvent e) { |
301 |
changedUpdate(e); |
302 |
} |
303 |
|
304 |
@Override
|
305 |
public void changedUpdate(DocumentEvent e) { |
306 |
doChangeUpdateWidth(); |
307 |
} |
308 |
}); |
309 |
|
310 |
txtHeight.getDocument().addDocumentListener(new DocumentListener() { |
311 |
|
312 |
@Override
|
313 |
public void removeUpdate(DocumentEvent e) { |
314 |
// Do nothing
|
315 |
} |
316 |
|
317 |
@Override
|
318 |
public void insertUpdate(DocumentEvent e) { |
319 |
changedUpdate(e); |
320 |
} |
321 |
|
322 |
@Override
|
323 |
public void changedUpdate(DocumentEvent e) { |
324 |
doChangeUpdateHeight(); |
325 |
} |
326 |
}); |
327 |
|
328 |
txtPixelSize.getDocument().addDocumentListener(new DocumentListener() { |
329 |
|
330 |
@Override
|
331 |
public void removeUpdate(DocumentEvent e) { |
332 |
// Do nothing
|
333 |
} |
334 |
|
335 |
@Override
|
336 |
public void insertUpdate(DocumentEvent e) { |
337 |
changedUpdate(e); |
338 |
} |
339 |
|
340 |
@Override
|
341 |
public void changedUpdate(DocumentEvent e) { |
342 |
doChangedUpdatePixelSize(); |
343 |
} |
344 |
}); |
345 |
|
346 |
txtPrintWidth.getDocument().addDocumentListener(new DocumentListener() { |
347 |
|
348 |
@Override
|
349 |
public void removeUpdate(DocumentEvent e) { |
350 |
// Do nothing
|
351 |
} |
352 |
|
353 |
@Override
|
354 |
public void insertUpdate(DocumentEvent e) { |
355 |
changedUpdate(e); |
356 |
} |
357 |
|
358 |
@Override
|
359 |
public void changedUpdate(DocumentEvent e) { |
360 |
doChangeUpdatePrintWidth(); |
361 |
} |
362 |
}); |
363 |
|
364 |
txtPrintHeight.getDocument().addDocumentListener(new DocumentListener() { |
365 |
|
366 |
@Override
|
367 |
public void removeUpdate(DocumentEvent e) { |
368 |
// Do nothing
|
369 |
} |
370 |
|
371 |
@Override
|
372 |
public void insertUpdate(DocumentEvent e) { |
373 |
changedUpdate(e); |
374 |
} |
375 |
|
376 |
@Override
|
377 |
public void changedUpdate(DocumentEvent e) { |
378 |
doChangeUpdatePrintHeight(); |
379 |
} |
380 |
}); |
381 |
|
382 |
txtScaleDenominator.getDocument().addDocumentListener(new DocumentListener() { |
383 |
|
384 |
@Override
|
385 |
public void removeUpdate(DocumentEvent e) { |
386 |
// Do nothing
|
387 |
} |
388 |
|
389 |
@Override
|
390 |
public void insertUpdate(DocumentEvent e) { |
391 |
changedUpdate(e); |
392 |
} |
393 |
|
394 |
@Override
|
395 |
public void changedUpdate(DocumentEvent e) { |
396 |
doChangeUpdateScaleDenominator(); |
397 |
} |
398 |
}); |
399 |
|
400 |
for (int i = 0; i < RESOLUTIONS.length; i++) { |
401 |
cmbResolution.addItem(RESOLUTIONS[i]); |
402 |
} |
403 |
cmbResolution.addItemListener(new ItemListener() { |
404 |
|
405 |
@Override
|
406 |
public void itemStateChanged(ItemEvent e) { |
407 |
doChangeStateCmbResolution(); |
408 |
} |
409 |
}); |
410 |
cmbResolution.setSelectedIndex(0);
|
411 |
cmbResolution.setEditable(false);
|
412 |
btnTool.setIcon(iconTheme.get("view-clip-icon"));
|
413 |
|
414 |
btnTool.addActionListener(new ActionListener() { |
415 |
|
416 |
@Override
|
417 |
public void actionPerformed(ActionEvent e) { |
418 |
doAddRectangularSelectionTool(); |
419 |
} |
420 |
}); |
421 |
|
422 |
} |
423 |
|
424 |
protected void doChangeUpdateWidth() { |
425 |
if (updatingPixelSection) {
|
426 |
return;
|
427 |
} |
428 |
updatingPixelSection = true;
|
429 |
double pixelSize = this.envelope.getLength(DIMENSIONS.X) / ((Number) txtWidth.getValue()).intValue(); |
430 |
txtPixelSize.setValue(pixelSize); |
431 |
txtHeight.setValue(Math.round(this.envelope.getLength(DIMENSIONS.Y) / pixelSize)); |
432 |
updatePrintSectionFromPixelSection(); |
433 |
updateResultSize(); |
434 |
updatingPixelSection = false;
|
435 |
} |
436 |
|
437 |
protected void doChangeUpdateHeight() { |
438 |
if (updatingPixelSection) {
|
439 |
return;
|
440 |
} |
441 |
updatingPixelSection = true;
|
442 |
double pixelSize = this.envelope.getLength(DIMENSIONS.Y) / ((Number) txtHeight.getValue()).intValue(); |
443 |
txtPixelSize.setValue(pixelSize); |
444 |
txtWidth.setValue(Math.round(this.envelope.getLength(DIMENSIONS.X) / pixelSize)); |
445 |
updatePrintSectionFromPixelSection(); |
446 |
updateResultSize(); |
447 |
updatingPixelSection = false;
|
448 |
} |
449 |
|
450 |
protected void doChangeUpdatePrintWidth() { |
451 |
if (updatingPrintSection) {
|
452 |
return;
|
453 |
} |
454 |
updatingPrintSection = true;
|
455 |
double scaleDenominator =
|
456 |
this.envelope.getLength(DIMENSIONS.X) / ((Number) txtPrintWidth.getValue()).doubleValue(); |
457 |
txtScaleDenominator.setValue(scaleDenominator); |
458 |
txtPrintHeight.setValue(this.envelope.getLength(DIMENSIONS.Y) / scaleDenominator);
|
459 |
updatePixelSectionFromPrintSection(); |
460 |
updateResultSize(); |
461 |
updatingPrintSection = false;
|
462 |
} |
463 |
|
464 |
protected void doChangeUpdatePrintHeight() { |
465 |
if (updatingPrintSection) {
|
466 |
return;
|
467 |
} |
468 |
updatingPrintSection = true;
|
469 |
double scaleDenominator =
|
470 |
this.envelope.getLength(DIMENSIONS.Y) / ((Number) txtPrintHeight.getValue()).doubleValue(); |
471 |
txtScaleDenominator.setValue(scaleDenominator); |
472 |
txtPrintWidth.setValue(this.envelope.getLength(DIMENSIONS.X) / scaleDenominator);
|
473 |
updatePixelSectionFromPrintSection(); |
474 |
updateResultSize(); |
475 |
updatingPrintSection = false;
|
476 |
} |
477 |
|
478 |
protected void doChangeUpdateScaleDenominator() { |
479 |
if (updatingPrintSection) {
|
480 |
return;
|
481 |
} |
482 |
updatingPrintSection = true;
|
483 |
double scaleDenominator = ((Number) txtScaleDenominator.getValue()).doubleValue(); |
484 |
txtPrintWidth.setValue(this.envelope.getLength(DIMENSIONS.X) / scaleDenominator);
|
485 |
txtPrintHeight.setValue(this.envelope.getLength(DIMENSIONS.Y) / scaleDenominator);
|
486 |
updatePixelSectionFromPrintSection(); |
487 |
updateResultSize(); |
488 |
updatingPrintSection = false;
|
489 |
} |
490 |
|
491 |
protected void doChangeStateCmbResolution() { |
492 |
updatePixelSectionFromPrintSection(); |
493 |
updateResultSize(); |
494 |
} |
495 |
|
496 |
protected void doSelectForPrint() { |
497 |
enableComponents(); |
498 |
} |
499 |
|
500 |
protected void doSelectByPixel() { |
501 |
enableComponents(); |
502 |
} |
503 |
|
504 |
private void enableComponents() { |
505 |
|
506 |
// PixelSelection
|
507 |
lblWidth.setEnabled(rdbPixelSelection.isSelected()); |
508 |
txtWidth.setEnabled(rdbPixelSelection.isSelected()); |
509 |
lblHeight.setEnabled(rdbPixelSelection.isSelected()); |
510 |
txtHeight.setEnabled(rdbPixelSelection.isSelected()); |
511 |
lblPixelSize.setEnabled(rdbPixelSelection.isSelected()); |
512 |
txtPixelSize.setEnabled(rdbPixelSelection.isSelected()); |
513 |
|
514 |
// PrintSelection
|
515 |
lblPrintWidth.setEnabled(rdbPrintSelection.isSelected()); |
516 |
txtPrintWidth.setEnabled(rdbPrintSelection.isSelected()); |
517 |
lblPrintHeight.setEnabled(rdbPrintSelection.isSelected()); |
518 |
txtPrintHeight.setEnabled(rdbPrintSelection.isSelected()); |
519 |
lblScaleDenominator.setEnabled(rdbPrintSelection.isSelected()); |
520 |
txtScaleDenominator.setEnabled(rdbPrintSelection.isSelected()); |
521 |
lblResolution.setEnabled(rdbPrintSelection.isSelected()); |
522 |
cmbResolution.setEnabled(rdbPrintSelection.isSelected()); |
523 |
|
524 |
} |
525 |
|
526 |
protected void doAddRectangularSelectionTool() { |
527 |
|
528 |
if (!VIEW_CLIP_TOOL_NAME.equalsIgnoreCase(this.mapControl.getCurrentTool())) { |
529 |
this.previousTool = this.mapControl.getCurrentTool(); |
530 |
} |
531 |
|
532 |
this.mapControl.addBehavior(VIEW_CLIP_TOOL_NAME, this.getBehavior()); |
533 |
this.mapControl.setTool(VIEW_CLIP_TOOL_NAME);
|
534 |
} |
535 |
|
536 |
private RectangleBehavior getBehavior() {
|
537 |
if (this.behavior == null) { |
538 |
this.behavior = new RectangleBehavior(new RectangleListener() { |
539 |
|
540 |
@Override
|
541 |
public Image getImageCursor() { |
542 |
return IconThemeHelper.getImage("cursor-select-by-rectangle"); |
543 |
} |
544 |
|
545 |
@Override
|
546 |
public boolean cancelDrawing() { |
547 |
return false; |
548 |
} |
549 |
|
550 |
@Override
|
551 |
public void rectangle(EnvelopeEvent event) throws BehaviorException { |
552 |
setEnvelope(event.getWorldCoordRect()); |
553 |
} |
554 |
}); |
555 |
} |
556 |
return this.behavior; |
557 |
} |
558 |
|
559 |
protected void setEnvelope(Envelope envelope) { |
560 |
updatingEnvelope = true;
|
561 |
this.envelope = envelope;
|
562 |
txtReal1X.setValue(envelope.getMinimum(DIMENSIONS.X)); |
563 |
txtReal1Y.setValue(envelope.getMaximum(DIMENSIONS.Y)); |
564 |
txtReal2X.setValue(envelope.getMaximum(DIMENSIONS.X)); |
565 |
txtReal2Y.setValue(envelope.getMinimum(DIMENSIONS.Y)); |
566 |
updatingEnvelope = false;
|
567 |
updatePixelsSectionFromEnvelope(); |
568 |
} |
569 |
|
570 |
private class RealCoordinatesDocumentListener implements DocumentListener { |
571 |
|
572 |
@Override
|
573 |
public void insertUpdate(DocumentEvent e) { |
574 |
// changedUpdate(e);
|
575 |
} |
576 |
|
577 |
@Override
|
578 |
public void removeUpdate(DocumentEvent e) { |
579 |
// changedUpdate(e);
|
580 |
} |
581 |
|
582 |
@Override
|
583 |
public void changedUpdate(DocumentEvent e) { |
584 |
doChangedUpdateRealCoordinates(e); |
585 |
} |
586 |
|
587 |
} |
588 |
|
589 |
private void doChangedUpdateRealCoordinates(DocumentEvent e) { |
590 |
if (updatingEnvelope) {
|
591 |
return;
|
592 |
} |
593 |
if (txtReal1X.getValue() == null || txtReal2X.getValue() == null || txtReal1Y.getValue() == null |
594 |
|| txtReal2Y.getValue() == null) {
|
595 |
return;
|
596 |
} |
597 |
|
598 |
if (rdbPixelSelection.isSelected()) {
|
599 |
updatePixelsSectionFromEnvelope(); |
600 |
} else if (rdbPrintSelection.isSelected()) { |
601 |
updatePrintSectionFromEnvelope(); |
602 |
|
603 |
} |
604 |
} |
605 |
|
606 |
protected void doChangedUpdatePixelSize() { |
607 |
if (updatingPixelSection) {
|
608 |
return;
|
609 |
} |
610 |
updatingPixelSection = true;
|
611 |
double pixelSize = ((Number) this.txtPixelSize.getValue()).doubleValue(); |
612 |
if (this.envelope != null) { |
613 |
txtWidth.setValue(Math.round(this.envelope.getLength(DIMENSIONS.X) / pixelSize)); |
614 |
txtHeight.setValue(Math.round(this.envelope.getLength(DIMENSIONS.Y) / pixelSize)); |
615 |
updatePrintSectionFromPixelSection(); |
616 |
updateResultSize(); |
617 |
} |
618 |
|
619 |
updatingPixelSection = false;
|
620 |
|
621 |
} |
622 |
|
623 |
private boolean updatePixelsSectionFromEnvelope() { |
624 |
this.envelope = calculateEnvelope();
|
625 |
if (updatingPixelSection) {
|
626 |
return false; |
627 |
} |
628 |
updatingPixelSection = true;
|
629 |
|
630 |
double pixelSize = ((Number) txtPixelSize.getValue()).doubleValue(); |
631 |
txtWidth.setValue(Math.round(this.envelope.getLength(DIMENSIONS.X) / pixelSize)); |
632 |
txtHeight.setValue(Math.round(this.envelope.getLength(DIMENSIONS.Y) / pixelSize)); |
633 |
updatePrintSectionFromPixelSection(); |
634 |
updateResultSize(); |
635 |
|
636 |
updatingPixelSection = false;
|
637 |
return true; |
638 |
} |
639 |
|
640 |
private boolean updatePrintSectionFromEnvelope() { |
641 |
this.envelope = calculateEnvelope();
|
642 |
if (updatingPrintSection) {
|
643 |
return false; |
644 |
} |
645 |
updatingPrintSection = true;
|
646 |
|
647 |
double scaleDenominator = ((Number) txtScaleDenominator.getValue()).doubleValue(); |
648 |
double printWidth = this.envelope.getLength(DIMENSIONS.X) / scaleDenominator; |
649 |
double printHeight = this.envelope.getLength(DIMENSIONS.Y) / scaleDenominator; |
650 |
|
651 |
txtPrintWidth.setValue(printWidth); |
652 |
txtPrintHeight.setValue(printHeight); |
653 |
updatePixelSectionFromPrintSection(); |
654 |
updateResultSize(); |
655 |
|
656 |
updatingPrintSection = false;
|
657 |
return true; |
658 |
} |
659 |
|
660 |
/*
|
661 |
* Creates an envelope from the data of the corresponding text fields.
|
662 |
*/
|
663 |
private Envelope calculateEnvelope() {
|
664 |
boolean fail = false; |
665 |
Double x1 = ((Number) txtReal1X.getValue()).doubleValue(); |
666 |
Double y1 = ((Number) txtReal1Y.getValue()).doubleValue(); |
667 |
Double x2 = ((Number) txtReal2X.getValue()).doubleValue(); |
668 |
Double y2 = ((Number) txtReal2Y.getValue()).doubleValue(); |
669 |
try {
|
670 |
if (x1 != null && y1 != null && x2 != null && y2 != null && x1 != null && x2 >= x1 && y2 <= y1) { |
671 |
this.envelope = GeometryLocator.getGeometryManager().createEnvelope(x1, y2, x2, y1, SUBTYPES.GEOM2D);
|
672 |
} else {
|
673 |
fail = true;
|
674 |
} |
675 |
} catch (Exception e2) { |
676 |
fail = true;
|
677 |
} |
678 |
|
679 |
if (fail) {
|
680 |
ThreadSafeDialogsManager dlgManager = ToolsSwingLocator.getThreadSafeDialogsManager(); |
681 |
String message =
|
682 |
"_cant_create_the_envelope_with_this_coordinates_XcolonX_XparenthesisX_XUL1X_XUL2X_XdashX_XDR1X_XDR2X_XparenthesisX";
|
683 |
String title = "_error_updating_envelope"; |
684 |
dlgManager.messageDialog(message, |
685 |
new String[] { txtReal1X.getText(), txtReal2Y.getText(), txtReal2X.getText(), txtReal1Y.getText() }, |
686 |
title, JOptionPane.WARNING_MESSAGE);
|
687 |
LOG.warn(message); |
688 |
} |
689 |
return this.envelope; |
690 |
} |
691 |
|
692 |
private void updateResultSize() { |
693 |
if (rdbPixelSelection.isSelected()) {
|
694 |
int width = ((Number) txtWidth.getValue()).intValue(); |
695 |
int height = ((Number) txtHeight.getValue()).intValue(); |
696 |
this.txtResultSize.setValue(width * height * BANDS);
|
697 |
} else if (rdbPrintSelection.isSelected()) { |
698 |
int dpi = ((Integer) cmbResolution.getSelectedItem()).intValue(); |
699 |
double dpcms = dpi / INCHES_TO_CENTIMETERS;
|
700 |
double printWidth = ((Number) txtPrintWidth.getValue()).doubleValue(); |
701 |
double printHeight = ((Number) txtPrintHeight.getValue()).doubleValue(); |
702 |
this.txtResultSize.setValue(printWidth * dpcms * printHeight * dpcms * BANDS);
|
703 |
} |
704 |
} |
705 |
|
706 |
private void updatePrintSectionFromPixelSection() { |
707 |
if (updatingPrintSection) {
|
708 |
return;
|
709 |
} |
710 |
updatingPrintSection = true;
|
711 |
|
712 |
int width = ((Number) txtWidth.getValue()).intValue(); |
713 |
int height = ((Number) txtHeight.getValue()).intValue(); |
714 |
|
715 |
int dpi = ((Number) cmbResolution.getSelectedItem()).intValue(); |
716 |
double dpcms = dpi / INCHES_TO_CENTIMETERS;
|
717 |
|
718 |
double printWidth = width / dpcms;
|
719 |
double printHeight = height / dpcms;
|
720 |
double scaleDenominator = this.envelope.getLength(DIMENSIONS.X) / printWidth; |
721 |
|
722 |
txtPrintWidth.setValue(printWidth); |
723 |
txtPrintHeight.setValue(printHeight); |
724 |
txtScaleDenominator.setValue(scaleDenominator); |
725 |
|
726 |
updatingPrintSection = false;
|
727 |
} |
728 |
|
729 |
private void updatePixelSectionFromPrintSection() { |
730 |
if (updatingPixelSection) {
|
731 |
return;
|
732 |
} |
733 |
updatingPixelSection = true;
|
734 |
int dpi = ((Integer) cmbResolution.getSelectedItem()).intValue(); |
735 |
double dpcms = dpi / INCHES_TO_CENTIMETERS;
|
736 |
double printWidth = ((Number) txtPrintWidth.getValue()).doubleValue(); |
737 |
double printHeight = ((Number) txtPrintHeight.getValue()).doubleValue(); |
738 |
|
739 |
double width = printWidth * dpcms;
|
740 |
double height = printHeight * dpcms;
|
741 |
double pixelSize = this.envelope.getLength(DIMENSIONS.X) / width; |
742 |
|
743 |
txtPixelSize.setValue(pixelSize); |
744 |
txtWidth.setValue(width); |
745 |
txtHeight.setValue(height); |
746 |
updatingPixelSection = false;
|
747 |
} |
748 |
|
749 |
@Override
|
750 |
public void fetch(SaveBufferDialog saveBufferDialog) throws BufferException { |
751 |
envelope = calculateEnvelope(); |
752 |
int width = ((Number) txtWidth.getValue()).intValue(); |
753 |
int height = ((Number) txtHeight.getValue()).intValue(); |
754 |
double pixelSize = ((Number) txtPixelSize.getValue()).intValue(); |
755 |
double scaleDenominator = ((Number) txtScaleDenominator.getValue()).doubleValue(); |
756 |
double scale = 1 / scaleDenominator; |
757 |
|
758 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
759 |
BufferedImage image;
|
760 |
image = toolsSwingManager.createBufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
|
761 |
|
762 |
Graphics2D g = image.createGraphics();
|
763 |
|
764 |
MapContext mapContext = this.mapControl.getMapContext();
|
765 |
|
766 |
MapContextManager mapContextManager = MapContextLocator.getMapContextManager(); |
767 |
MapContextDrawer drawer; |
768 |
try {
|
769 |
drawer = mapContextManager.createMapContextDrawerInstance(mapContext.getMapContextDrawer().getClass()); |
770 |
} catch (ReadException | MapContextException e) {
|
771 |
throw new BufferException(e); |
772 |
} |
773 |
|
774 |
drawer.setMapContext(mapContext); |
775 |
ViewPort viewPort; |
776 |
try {
|
777 |
viewPort = (ViewPort) mapContext.getViewPort().clone(); |
778 |
} catch (CloneNotSupportedException e) { |
779 |
throw new BufferException(e); |
780 |
} |
781 |
|
782 |
viewPort.setDist1pixel(pixelSize); |
783 |
viewPort.setImageSize(new Dimension(width, height)); |
784 |
viewPort.setEnvelope(envelope); |
785 |
|
786 |
drawer.setViewPort(viewPort); |
787 |
|
788 |
try {
|
789 |
drawer.draw(mapContext.getLayers(), image, g, new Cancellable() {
|
790 |
|
791 |
/**
|
792 |
* @see org.gvsig.utils.swing.threads.Cancellable#isCanceled()
|
793 |
*/
|
794 |
public boolean isCanceled() { |
795 |
return false; |
796 |
} |
797 |
|
798 |
public void setCanceled(boolean canceled) { |
799 |
// Do nothing
|
800 |
} |
801 |
}, scale); |
802 |
} catch (ReadException e) {
|
803 |
throw new BufferException(e); |
804 |
} |
805 |
|
806 |
BufferManager bufferManager = BufferLocator.getBufferManager(); |
807 |
Buffer buffer = null; |
808 |
try {
|
809 |
buffer = bufferManager.createBufferFromAwtRaster(image.getRaster(), viewPort.getProjection(), envelope); |
810 |
} catch (IOException e1) { |
811 |
LOG.warn("Can't create buffer from view's image.", e1);
|
812 |
return;
|
813 |
} |
814 |
saveBufferDialog.setBuffer(buffer); |
815 |
} |
816 |
|
817 |
} |