30 |
|
|
31 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
32 |
import org.geotools.feature.FeatureCollection; |
import org.geotools.feature.FeatureCollection; |
|
import org.geotools.geometry.Envelope2D; |
|
33 |
import org.geotools.geometry.jts.JTS; |
import org.geotools.geometry.jts.JTS; |
34 |
import org.geotools.geometry.jts.ReferencedEnvelope; |
import org.geotools.geometry.jts.ReferencedEnvelope; |
35 |
import org.geotools.map.DefaultMapContext; |
import org.geotools.map.DefaultMapContext; |
98 |
private static final int IMAGETYPE = BufferedImage.TYPE_INT_RGB; |
private static final int IMAGETYPE = BufferedImage.TYPE_INT_RGB; |
99 |
private static final int IMAGETYPE_withAlpha = BufferedImage.TYPE_INT_ARGB; |
private static final int IMAGETYPE_withAlpha = BufferedImage.TYPE_INT_ARGB; |
100 |
|
|
101 |
private static Logger LOGGER = Logger.getLogger(XMapPane.class); |
private final static Logger LOGGER = Logger.getLogger(XMapPane.class); |
102 |
|
|
103 |
private boolean acceptsRepaintCalls = true; |
private boolean acceptsRepaintCalls = true; |
104 |
|
|
124 |
* regularly and {@link #repaint()}. This {@link Timer} is stopped when all |
* regularly and {@link #repaint()}. This {@link Timer} is stopped when all |
125 |
* renderers have finished. |
* renderers have finished. |
126 |
* |
* |
127 |
* @see #INITIAL_REPAINT_DELAYAL |
* @see INITIAL_REPAINT_DELAYAL |
128 |
* @see #REPEATING_REPAINT_DELAY |
* @see #REPEATING_REPAINT_DELAY |
129 |
*/ |
*/ |
130 |
final private Timer repaintTimer; |
final private Timer repaintTimer; |
133 |
* The initial delay in milliseconds until the {@link #finalImage} is |
* The initial delay in milliseconds until the {@link #finalImage} is |
134 |
* updated the first time. |
* updated the first time. |
135 |
*/ |
*/ |
136 |
public static final int INITIAL_REPAINT_DELAY = 1000; |
public static final int INITIAL_REPAINT_DELAY = 900; |
137 |
|
|
138 |
/** |
/** |
139 |
* While the {@link #bgExecuter} and {@link #localExecuter} are rendering, |
* While the {@link #bgExecuter} and {@link #localExecuter} are rendering, |
140 |
* the {@link #repaintTimer} is regularly updating the {@link #finalImage} |
* the {@link #repaintTimer} is regularly updating the {@link #finalImage} |
141 |
* with previews. |
* with previews. |
142 |
*/ |
*/ |
143 |
public static final long REPEATING_REPAINT_DELAY = 500; |
public static final int REPEATING_REPAINT_DELAY = 600; |
144 |
|
|
145 |
/** |
/** |
146 |
* Default delay (milliseconds) before the map will be redrawn when resizing |
* Default delay (milliseconds) before the map will be redrawn when resizing |
147 |
* the pane. This is to avoid flickering while drag-resizing. |
* the pane. This is to avoid flickering while drag-resizing. |
148 |
|
* |
149 |
|
* @see #resizeTimer |
150 |
*/ |
*/ |
151 |
public static final int DEFAULT_RESIZING_PAINT_DELAY = 600; |
public static final int DEFAULT_RESIZING_PAINT_DELAY = 600; |
152 |
|
|
153 |
|
/** |
154 |
|
* This not-repeating {@link Timer} is re-started whenever the component is |
155 |
|
* resized. That means => only if the component is not resizing for |
156 |
|
* {@link #DEFAULT_RESIZING_PAINT_DELAY} milliseconds, does the |
157 |
|
* {@link XMapPane} react. |
158 |
|
*/ |
159 |
|
private final Timer resizeTimer; |
160 |
|
|
161 |
|
/** |
162 |
|
* Flag for no-tool. |
163 |
|
*/ |
164 |
public static final int NONE = -123; |
public static final int NONE = -123; |
165 |
|
|
166 |
/** |
/** |
172 |
public static final int PAN = 1; |
public static final int PAN = 1; |
173 |
|
|
174 |
/** |
/** |
175 |
|
* Flag fuer Modus "Heran zoomen". |
176 |
|
* |
177 |
|
* @see #setState(int) |
178 |
|
* @see #setState(int) |
179 |
|
*/ |
180 |
|
public static final int ZOOM_IN = 2; |
181 |
|
|
182 |
|
/** |
183 |
|
* Flag fuer Modus "Heraus zoomen". Nicht fuer Window-Auswahl moeglich! |
184 |
|
* |
185 |
|
* @see #setState(int) |
186 |
|
*/ |
187 |
|
public static final int ZOOM_OUT = 3; |
188 |
|
|
189 |
|
/** |
190 |
* Flag fuer Modus "SimpleFeature-Auswahl auf allen (sichtbaren) Layern". |
* Flag fuer Modus "SimpleFeature-Auswahl auf allen (sichtbaren) Layern". |
191 |
* |
* |
192 |
* @see #setState(int) |
* @see #setState(int) |
210 |
*/ |
*/ |
211 |
public static final int SELECT_TOP = 4; |
public static final int SELECT_TOP = 4; |
212 |
|
|
213 |
public static final Cursor WAIT_CURSOR = Cursor |
/** |
214 |
.getPredefinedCursor(Cursor.WAIT_CURSOR); |
* {@link Font} used to paint the wait messages into the map |
215 |
|
* |
216 |
|
* @see #addGadgets(Graphics2D, boolean) |
217 |
|
*/ |
218 |
final static Font waitFont = new Font("Arial", Font.BOLD, 30); |
final static Font waitFont = new Font("Arial", Font.BOLD, 30); |
|
final String waitMsg = SwingUtil.R("WaitMess"); |
|
|
final static Font errorFont = new Font("Arial", Font.BOLD, 13); |
|
219 |
|
|
220 |
/** |
/** |
221 |
* Flag fuer Modus "Heran zoomen". |
* {@link Font} used to paint error messages into the map |
222 |
* |
* |
223 |
* @see #setState(int) |
* @see #addGadgets(Graphics2D, boolean) |
|
* @see #setState(int) |
|
224 |
*/ |
*/ |
225 |
public static final int ZOOM_IN = 2; |
final static Font errorFont = new Font("Arial", Font.BOLD, 13); |
226 |
|
|
227 |
/** |
/** |
228 |
* Flag fuer Modus "Heraus zoomen". Nicht fuer Window-Auswahl moeglich! |
* The wait message painted into the map while rendering is going on on |
229 |
|
* another thread. |
230 |
* |
* |
231 |
* @see #setState(int) |
* @see #addGadgets(Graphics2D, boolean) |
232 |
*/ |
*/ |
233 |
public static final int ZOOM_OUT = 3; |
final String waitMsg = SwingUtil.R("WaitMess"); |
234 |
|
|
235 |
/** |
/** |
236 |
* Konvertiert die Maus-Koordinaten (relativ zum <code>JMapPane</code>) in |
* Konvertiert die Maus-Koordinaten (relativ zum <code>JMapPane</code>) in |
276 |
|
|
277 |
@Override |
@Override |
278 |
public void layerAdded(final MapLayerListEvent event) { |
public void layerAdded(final MapLayerListEvent event) { |
|
|
|
279 |
MapLayer layer = event.getLayer(); |
MapLayer layer = event.getLayer(); |
|
|
|
280 |
layer.addMapLayerListener(bgMapLayerListener); |
layer.addMapLayerListener(bgMapLayerListener); |
|
|
|
|
if (localContext.getLayers().length == 0 |
|
|
&& bgContext.getLayers().length == 1) { // the first one and |
|
|
// localContext is |
|
|
// empty |
|
|
if (!setMapArea(localContext.getAreaOfInterest())) |
|
|
requestStartRendering(); |
|
|
return; |
|
|
} |
|
|
|
|
|
// We need to redraw, even in case that the mapArea didn't change |
|
|
// mapImageInvalid = true; |
|
|
// repaint(); |
|
281 |
requestStartRendering(); |
requestStartRendering(); |
282 |
|
|
283 |
} |
} |
284 |
|
|
285 |
@Override |
@Override |
286 |
public void layerChanged(final MapLayerListEvent event) { |
public void layerChanged(final MapLayerListEvent event) { |
|
// mapImageInvalid = true; |
|
|
// repaint(); |
|
287 |
requestStartRendering(); |
requestStartRendering(); |
288 |
} |
} |
289 |
|
|
290 |
@Override |
@Override |
291 |
public void layerMoved(final MapLayerListEvent event) { |
public void layerMoved(final MapLayerListEvent event) { |
|
// mapImageInvalid = true; |
|
|
// repaint(); |
|
292 |
requestStartRendering(); |
requestStartRendering(); |
293 |
} |
} |
294 |
|
|
296 |
public void layerRemoved(final MapLayerListEvent event) { |
public void layerRemoved(final MapLayerListEvent event) { |
297 |
if (event.getLayer() != null) |
if (event.getLayer() != null) |
298 |
event.getLayer().removeMapLayerListener(bgMapLayerListener); |
event.getLayer().removeMapLayerListener(bgMapLayerListener); |
|
// mapImageInvalid = true; |
|
|
// repaint(); |
|
299 |
requestStartRendering(); |
requestStartRendering(); |
300 |
} |
} |
301 |
}; |
}; |
302 |
|
|
303 |
/** |
/** |
304 |
* compass and icon are rendered into this image |
* This {@link RenderingExecutor} manages the creation and cancellation of |
305 |
|
* up to one {@link Thread} for rendering the {@link #localContext}. |
306 |
*/ |
*/ |
307 |
// protected BufferedImage gadgetsImage; |
private final RenderingExecutor localExecuter = new RenderingExecutor(this); |
308 |
|
|
309 |
|
/** |
310 |
|
* This {@link RenderingExecutor} manages the creation and cancellation of |
311 |
|
* up to one {@link Thread} for rendering the {@link #bgContext}. |
312 |
|
*/ |
313 |
protected RenderingExecutor bgExecuter; |
protected RenderingExecutor bgExecuter; |
314 |
|
|
315 |
/** |
/** |
316 |
* The Renderer for the LocalLayers uses this Image. When set to null, |
* The {@link #localRenderer} for the {@link #localContext} uses this |
317 |
* please dispose this {@link Graphics2D} |
* {@link Image}. |
318 |
*/ |
*/ |
319 |
private BufferedImage localImage; |
private BufferedImage localImage; |
320 |
|
|
|
private BufferedImage finalImage; |
|
321 |
/** |
/** |
322 |
* If # {@link #bgExecuter} is using {@link #bgRenderer} for the Background |
* The {@link #bgRenderer} for the {@link #bgContext} uses this |
323 |
* uses this Image. When set to null, please dispose the {@link Graphics2D} |
* {@link Image}. |
324 |
*/ |
*/ |
325 |
private BufferedImage bgImage; |
private BufferedImage bgImage; |
326 |
|
|
327 |
/** |
/** |
328 |
|
* This {@link Image} is a merge of the {@link #bgImage}, |
329 |
|
* {@link #localImage} and {@link #addGadgets(Graphics2D, boolean)}. It is |
330 |
|
* updated with {@link #updateFinalImage()} and used for painting in |
331 |
|
* {@link #paintComponent(Graphics)} |
332 |
|
*/ |
333 |
|
private BufferedImage finalImage; |
334 |
|
|
335 |
|
/** |
336 |
* Optionally a transparent image to paint over the map in the lower right |
* Optionally a transparent image to paint over the map in the lower right |
337 |
* corner. |
* corner. |
338 |
* |
* |
349 |
|
|
350 |
@Override |
@Override |
351 |
public void layerChanged(final MapLayerEvent event) { |
public void layerChanged(final MapLayerEvent event) { |
|
// Change of SLD for example |
|
|
// mapImageInvalid = true; |
|
|
// repaint(); |
|
352 |
requestStartRendering(); |
requestStartRendering(); |
353 |
} |
} |
354 |
|
|
355 |
@Override |
@Override |
356 |
public void layerHidden(final MapLayerEvent event) { |
public void layerHidden(final MapLayerEvent event) { |
|
// mapImageInvalid = true; |
|
|
// repaint(); |
|
357 |
requestStartRendering(); |
requestStartRendering(); |
358 |
} |
} |
359 |
|
|
360 |
@Override |
@Override |
361 |
public void layerShown(final MapLayerEvent event) { |
public void layerShown(final MapLayerEvent event) { |
|
// mapImageInvalid = true; |
|
|
// repaint(); |
|
362 |
requestStartRendering(); |
requestStartRendering(); |
363 |
} |
} |
364 |
}; |
}; |
395 |
public void layerAdded(final MapLayerListEvent event) { |
public void layerAdded(final MapLayerListEvent event) { |
396 |
event.getLayer().addMapLayerListener(localMapLayerListener); |
event.getLayer().addMapLayerListener(localMapLayerListener); |
397 |
|
|
398 |
localRenderer.setContext(getContext()); |
localRenderer.setContext(getMapContext()); |
|
|
|
|
if (localContext.getLayers().length == 1) { // the first one |
|
|
|
|
|
// if the Area of Interest is unset, the LayerBounds are used |
|
|
if (!setMapArea(localContext.getAreaOfInterest())) |
|
|
repaint(); |
|
|
|
|
|
return; |
|
|
} |
|
|
|
|
|
// We need to redraw, even in case that the mapArea didn't change |
|
|
// mapImageInvalid = true; |
|
|
// repaint(); |
|
399 |
requestStartRendering(); |
requestStartRendering(); |
400 |
|
|
401 |
} |
} |
402 |
|
|
403 |
@Override |
@Override |
404 |
public void layerChanged(final MapLayerListEvent event) { |
public void layerChanged(final MapLayerListEvent event) { |
405 |
// mapImageInvalid = true; |
localRenderer.setContext(getMapContext()); |
|
// repaint(); |
|
|
localRenderer.setContext(getContext()); |
|
406 |
requestStartRendering(); |
requestStartRendering(); |
407 |
} |
} |
408 |
|
|
409 |
@Override |
@Override |
410 |
public void layerMoved(final MapLayerListEvent event) { |
public void layerMoved(final MapLayerListEvent event) { |
411 |
// mapImageInvalid = true; |
localRenderer.setContext(getMapContext()); |
|
// repaint(); |
|
|
localRenderer.setContext(getContext()); |
|
412 |
requestStartRendering(); |
requestStartRendering(); |
413 |
} |
} |
414 |
|
|
416 |
public void layerRemoved(final MapLayerListEvent event) { |
public void layerRemoved(final MapLayerListEvent event) { |
417 |
if (event.getLayer() != null) |
if (event.getLayer() != null) |
418 |
event.getLayer().removeMapLayerListener(localMapLayerListener); |
event.getLayer().removeMapLayerListener(localMapLayerListener); |
419 |
// mapImageInvalid = true; |
localRenderer.setContext(getMapContext()); |
|
// repaint(); |
|
|
localRenderer.setContext(getContext()); |
|
420 |
requestStartRendering(); |
requestStartRendering(); |
421 |
} |
} |
422 |
}; |
}; |
423 |
|
|
|
private final RenderingExecutor localExecuter = new RenderingExecutor(this); |
|
|
|
|
424 |
/** |
/** |
425 |
* Listens to each layer in the local {@link MapContext} for changes and |
* Listens to each layer in the local {@link MapContext} for changes and |
426 |
* triggers repaints. |
* triggers repaints. |
429 |
|
|
430 |
@Override |
@Override |
431 |
public void layerChanged(final MapLayerEvent event) { |
public void layerChanged(final MapLayerEvent event) { |
432 |
localRenderer.setContext(getContext()); // betters for SLD changes?! |
localRenderer.setContext(getMapContext()); // betters for SLD |
433 |
// Change of SLD for example |
// changes?! |
|
// mapImageInvalid = true; |
|
|
// repaint(); |
|
434 |
requestStartRendering(); |
requestStartRendering(); |
435 |
} |
} |
436 |
|
|
437 |
@Override |
@Override |
438 |
public void layerHidden(final MapLayerEvent event) { |
public void layerHidden(final MapLayerEvent event) { |
|
// mapImageInvalid = true; |
|
|
// repaint(); |
|
439 |
requestStartRendering(); |
requestStartRendering(); |
440 |
} |
} |
441 |
|
|
442 |
@Override |
@Override |
443 |
public void layerShown(final MapLayerEvent event) { |
public void layerShown(final MapLayerEvent event) { |
|
// mapImageInvalid = true; |
|
|
// repaint(); |
|
444 |
requestStartRendering(); |
requestStartRendering(); |
445 |
} |
} |
446 |
}; |
}; |
510 |
* A flag indicating, that the image size has changed and the buffered |
* A flag indicating, that the image size has changed and the buffered |
511 |
* images are not big enough any more |
* images are not big enough any more |
512 |
**/ |
**/ |
513 |
protected boolean paneResized = false; |
protected boolean paneResized = true; |
514 |
|
|
515 |
private BufferedImage preFinalImage; |
private BufferedImage preFinalImage; |
516 |
|
|
517 |
// if null, no quick preview will be shown |
// ** if 0, no quick preview will be shown **/ |
518 |
private int quickPreviewHint = 0; |
private int quickPreviewHint = 0; |
519 |
|
|
520 |
private Map<Object, Object> rendererHints = GTUtil |
private Map<Object, Object> rendererHints = GTUtil |
521 |
.getDefaultGTRendererHints(localRenderer); |
.getDefaultGTRendererHints(localRenderer); |
522 |
|
|
523 |
|
/** |
524 |
|
* If set to <code>true</code>, the {@link #startRenderThreadsTimer} will |
525 |
|
* start rendering {@link Thread}s |
526 |
|
**/ |
527 |
private volatile Boolean requestStartRendering = false; |
private volatile Boolean requestStartRendering = false; |
528 |
|
|
|
private final Timer resizeTimer; |
|
|
|
|
529 |
/** |
/** |
530 |
* Transformation zwischen Fenster-Koordinaten und Karten-Koordinaten |
* Transformation zwischen Fenster-Koordinaten und Karten-Koordinaten |
531 |
* (lat/lon) |
* (lat/lon) |
562 |
/** Is set if a renderer has an error **/ |
/** Is set if a renderer has an error **/ |
563 |
protected ArrayList<Exception> renderingErrors = new ArrayList<Exception>(); |
protected ArrayList<Exception> renderingErrors = new ArrayList<Exception>(); |
564 |
|
|
565 |
// TODO doku |
/** |
566 |
|
* If <code>true</code>, then rendering exceptions are rendererd into the |
567 |
|
* map pane |
568 |
|
**/ |
569 |
|
private boolean showExceptions = false; |
570 |
|
|
571 |
public XMapPane() { |
public XMapPane() { |
572 |
this(null, null); |
this(null, null); |
573 |
} |
} |
576 |
* full constructor extending JPanel |
* full constructor extending JPanel |
577 |
* |
* |
578 |
* @param rendererHints |
* @param rendererHints |
579 |
|
* may be <code>null</code>. Otherwise a {@link Map<Object, |
580 |
|
* Object>} of {@link RenderingHints} to override the default |
581 |
|
* from {@link GTUtil#getDefaultGTRendererHints(GTRenderer)} |
582 |
* |
* |
|
* @param layout |
|
|
* - layout (probably shouldn't be set) |
|
|
* @param isDoubleBuffered |
|
|
* - a Swing thing I don't really understand |
|
|
* @param render |
|
|
* - what to draw the map with |
|
583 |
* @param localContext |
* @param localContext |
584 |
* - {@link MapContext} of layer to render. |
* The main {@link MapContext} to use. If <code>null</code>, an |
585 |
|
* empty {@link DefaultMapContext} will be created. |
586 |
*/ |
*/ |
587 |
public XMapPane(final MapContext localContext_, |
public XMapPane(final MapContext localContext_, |
588 |
final Map<Object, Object> rendererHints) { |
final Map<Object, Object> rendererHints) { |
609 |
* Using a ComponentListener doesn't work because, unlike a JFrame, the |
* Using a ComponentListener doesn't work because, unlike a JFrame, the |
610 |
* pane receives a stream of events during drag-resizing. |
* pane receives a stream of events during drag-resizing. |
611 |
*/ |
*/ |
612 |
resizeTimer = new Timer(DEFAULT_RESIZING_PAINT_DELAY, new ActionListener() { |
resizeTimer = new Timer(DEFAULT_RESIZING_PAINT_DELAY, |
613 |
|
new ActionListener() { |
|
public void actionPerformed(final ActionEvent e) { |
|
|
paneResized = true; |
|
|
|
|
|
if (!isWellDefined()) |
|
|
return; |
|
614 |
|
|
615 |
final Rectangle bounds = getVisibleRect(); |
public void actionPerformed(final ActionEvent e) { |
616 |
|
if (!isWellDefined()) |
617 |
|
return; |
618 |
|
|
619 |
final Envelope geoMapArea = tranformWindowToGeo(bounds.x, |
LOGGER.debug("resizeTimer performed"); |
|
bounds.y, bounds.x + bounds.width, bounds.y |
|
|
+ bounds.height); |
|
620 |
|
|
621 |
setMapArea(bestAllowedMapArea(geoMapArea)); |
final Rectangle bounds = getVisibleRect(); |
622 |
} |
// |
623 |
}); |
// System.out.println("\n\ntimer performs with bounds = " |
624 |
|
// + bounds); |
625 |
|
|
626 |
|
final Envelope geoMapArea = tranformWindowToGeo( |
627 |
|
bounds.x, bounds.y, bounds.x + bounds.width, |
628 |
|
bounds.y + bounds.height); |
629 |
|
|
630 |
|
if (setMapArea(geoMapArea)) { |
631 |
|
LOGGER.debug(" maparea changed"); |
632 |
|
paneResized = true; |
633 |
|
} else |
634 |
|
LOGGER.debug(" maparea NOT changed"); |
635 |
|
} |
636 |
|
}); |
637 |
resizeTimer.setRepeats(false); |
resizeTimer.setRepeats(false); |
638 |
|
|
639 |
this.addComponentListener(new ComponentAdapter() { |
this.addComponentListener(new ComponentAdapter() { |
640 |
|
|
641 |
|
private Rectangle oldVisibleRect; |
642 |
|
|
643 |
@Override |
@Override |
644 |
public void componentResized(final ComponentEvent e) { |
public void componentResized(final ComponentEvent e) { |
645 |
if (bgExecuter != null) |
|
646 |
bgExecuter.cancelTask(); |
// Seems to be called twice with the same size.. |
647 |
if (localExecuter != null) |
if (oldVisibleRect != null |
648 |
localExecuter.cancelTask(); |
&& oldVisibleRect.equals(getVisibleRect())) { |
649 |
|
LOGGER.debug("skipping resize."); |
650 |
|
return; |
651 |
|
} |
652 |
|
|
653 |
|
LOGGER.debug("resized: " + getVisibleRect()); |
654 |
resizeTimer.restart(); |
resizeTimer.restart(); |
655 |
|
oldVisibleRect = getVisibleRect(); |
656 |
} |
} |
657 |
|
|
658 |
}); |
}); |
660 |
/* |
/* |
661 |
* Setting up the repaintTimer. Not started automatically. |
* Setting up the repaintTimer. Not started automatically. |
662 |
*/ |
*/ |
663 |
repaintTimer = new Timer((int) REPEATING_REPAINT_DELAY, |
repaintTimer = new Timer(REPEATING_REPAINT_DELAY, new ActionListener() { |
664 |
new ActionListener() { |
|
665 |
|
@Override |
666 |
|
public void actionPerformed(final ActionEvent e) { |
667 |
|
if ((!localExecuter.isRunning()) |
668 |
|
&& (bgExecuter != null && !bgExecuter.isRunning())) { |
669 |
|
repaintTimer.stop(); |
670 |
|
} else { |
671 |
|
updateFinalImage(); |
672 |
|
XMapPane.this.repaint(100); |
673 |
|
|
674 |
|
} |
675 |
|
} |
676 |
|
}); |
677 |
|
|
|
@Override |
|
|
public void actionPerformed(final ActionEvent e) { |
|
|
updateFinalImage(); |
|
|
XMapPane.this.repaint(); |
|
|
} |
|
|
}); |
|
678 |
repaintTimer.setInitialDelay(INITIAL_REPAINT_DELAY); |
repaintTimer.setInitialDelay(INITIAL_REPAINT_DELAY); |
679 |
repaintTimer.setRepeats(true); |
repaintTimer.setRepeats(true); |
680 |
|
|
692 |
if (localExecuter.isRunning()) { |
if (localExecuter.isRunning()) { |
693 |
localExecuter.cancelTask(); |
localExecuter.cancelTask(); |
694 |
} else { |
} else { |
695 |
|
// Stupidly, but we have to recheck the |
696 |
|
setMapArea(getMapArea()); |
697 |
requestStartRendering = false; |
requestStartRendering = false; |
698 |
startRendering(); |
startRendering(); |
699 |
} |
} |
726 |
* @author <a href="mailto:[email protected]">Stefan Alfons |
* @author <a href="mailto:[email protected]">Stefan Alfons |
727 |
* Krüger</a> |
* Krüger</a> |
728 |
*/ |
*/ |
729 |
public Envelope bestAllowedMapArea(Envelope env) { |
public ReferencedEnvelope bestAllowedMapArea(ReferencedEnvelope env) { |
730 |
// return env; |
|
731 |
if (getWidth() == 0) |
if (getWidth() == 0) |
732 |
return env; |
return env; |
733 |
|
|
734 |
if (env == null) |
if (env == null) |
735 |
return null; |
return null; |
736 |
|
|
780 |
} |
} |
781 |
|
|
782 |
final Envelope maxAllowedExtend = getMaxExtend(); |
final Envelope maxAllowedExtend = getMaxExtend(); |
783 |
|
|
784 |
while (maxAllowedExtend != null && !maxAllowedExtend.contains(newArea) |
while (maxAllowedExtend != null && !maxAllowedExtend.contains(newArea) |
785 |
&& newArea != null && !newArea.isNull() |
&& newArea != null && !newArea.isNull() |
786 |
&& !Double.isNaN(newArea.getMinX()) |
&& !Double.isNaN(newArea.getMinX()) |
810 |
|
|
811 |
// LOGGER.debug("and fix aspect ratio"); |
// LOGGER.debug("and fix aspect ratio"); |
812 |
|
|
813 |
newArea = JTSUtil.fixAspectRatio(getVisibleRect(), newArea, |
newArea = JTSUtil.fixAspectRatio(getVisibleRect(), |
814 |
false); |
new ReferencedEnvelope(newArea, env |
815 |
|
.getCoordinateReferenceSystem()), false); |
816 |
} |
} |
817 |
} |
} |
818 |
|
|
835 |
|
|
836 |
// LOGGER.debug("and fix aspect ratio"); |
// LOGGER.debug("and fix aspect ratio"); |
837 |
|
|
838 |
newArea = JTSUtil.fixAspectRatio(getVisibleRect(), newArea, |
newArea = JTSUtil.fixAspectRatio(getVisibleRect(), |
839 |
false); |
new ReferencedEnvelope(newArea, env |
840 |
|
.getCoordinateReferenceSystem()), false); |
841 |
} |
} |
842 |
} |
} |
843 |
|
|
862 |
|
|
863 |
// LOGGER.debug("and fix aspect ratio"); |
// LOGGER.debug("and fix aspect ratio"); |
864 |
|
|
865 |
newArea = JTSUtil.fixAspectRatio(getVisibleRect(), newArea, |
newArea = JTSUtil.fixAspectRatio(getVisibleRect(), |
866 |
false); |
new ReferencedEnvelope(newArea, env |
867 |
|
.getCoordinateReferenceSystem()), false); |
868 |
} |
} |
869 |
} |
} |
870 |
|
|
889 |
|
|
890 |
// LOGGER.debug("and fix aspect ratio"); |
// LOGGER.debug("and fix aspect ratio"); |
891 |
|
|
892 |
newArea = JTSUtil.fixAspectRatio(getVisibleRect(), newArea, |
newArea = JTSUtil.fixAspectRatio(getVisibleRect(), |
893 |
false); |
new ReferencedEnvelope(newArea, env |
894 |
|
.getCoordinateReferenceSystem()), false); |
895 |
} |
} |
896 |
} |
} |
897 |
} |
} |
898 |
|
|
899 |
return newArea; |
return new ReferencedEnvelope(newArea, env |
900 |
|
.getCoordinateReferenceSystem()); |
901 |
} |
} |
902 |
|
|
903 |
/** |
/** |
925 |
bgExecuter.dispose(); |
bgExecuter.dispose(); |
926 |
} |
} |
927 |
|
|
928 |
if (localExecuter != null) { |
if (localExecuter.isRunning()) { |
929 |
int i = 0; |
int i = 0; |
930 |
localExecuter.cancelTask(); |
localExecuter.cancelTask(); |
931 |
while (i++ < 10 && localExecuter.isRunning()) { |
while (i++ < 10 && localExecuter.isRunning()) { |
932 |
try { |
try { |
933 |
Thread.sleep(100); |
Thread.sleep(200); |
934 |
} catch (final InterruptedException e) { |
} catch (final InterruptedException e) { |
935 |
// TODO Auto-generated catch block |
LOGGER |
936 |
e.printStackTrace(); |
.warn( |
937 |
|
"while XMapPane we are waiting for the localExcutor to stop", |
938 |
|
e); |
939 |
} |
} |
940 |
} |
} |
941 |
if (localExecuter.isRunning()) { |
if (localExecuter.isRunning()) { |
942 |
System.out |
LOGGER |
943 |
.println("BAD BAD BAD... still running the thread...."); |
.warn("localExecutor Thread still running after 2s! Continuing anyways..."); |
944 |
} |
} |
945 |
localExecuter.dispose(); |
localExecuter.dispose(); |
946 |
} |
} |
947 |
|
|
948 |
disposeImages(); |
disposeImages(); |
949 |
|
|
950 |
// LangUtil.gcTotal(); |
// Remove all mapPaneListeners that have registered with us |
|
|
|
|
// Alle mapPaneListener entfernen |
|
951 |
mapPaneListeners.clear(); |
mapPaneListeners.clear(); |
952 |
|
|
953 |
removeMouseMotionListener(zoomMapPaneMouseListener); |
removeMouseMotionListener(zoomMapPaneMouseListener); |
954 |
removeMouseListener(zoomMapPaneMouseListener); |
removeMouseListener(zoomMapPaneMouseListener); |
955 |
|
|
956 |
if (localContext != null) |
if (localContext != null) |
957 |
getContext().clearLayerList(); |
getMapContext().clearLayerList(); |
958 |
if (bgContext != null) |
if (bgContext != null) |
959 |
getBgContext().clearLayerList(); |
getBgContext().clearLayerList(); |
960 |
|
|
1000 |
*/ |
*/ |
1001 |
protected boolean drawScaledPreviewImage_Zoom(final Graphics2D graphics) { |
protected boolean drawScaledPreviewImage_Zoom(final Graphics2D graphics) { |
1002 |
|
|
1003 |
// if (1 == 1)return false; |
// if (1 == 1)return false; |
1004 |
|
|
1005 |
if (quickPreviewHint == 0) |
if (quickPreviewHint == 0) |
1006 |
return false; |
return false; |
1007 |
|
|
1065 |
return bgImage; |
return bgImage; |
1066 |
} |
} |
1067 |
|
|
1068 |
public MapContext getContext() { |
public MapContext getMapContext() { |
1069 |
if (localContext == null) { |
if (localContext == null) { |
1070 |
setLocalContext(new DefaultMapContext()); |
setLocalContext(new DefaultMapContext()); |
1071 |
} |
} |
1112 |
try { |
try { |
1113 |
mapArea_ = localContext.getLayerBounds(); |
mapArea_ = localContext.getLayerBounds(); |
1114 |
} catch (final IOException e) { |
} catch (final IOException e) { |
1115 |
LOGGER.warn("context.getLayerBounds()", e); |
LOGGER.warn("localContext.getLayerBounds()", e); |
1116 |
|
} |
1117 |
|
|
1118 |
|
if (mapArea_ == null && bgContext != null) { |
1119 |
|
try { |
1120 |
|
mapArea_ = bgContext.getLayerBounds(); |
1121 |
|
} catch (final IOException e) { |
1122 |
|
LOGGER.warn("bgContext.getLayerBounds()", e); |
1123 |
|
} |
1124 |
} |
} |
1125 |
|
|
1126 |
if (mapArea_ != null) { |
if (mapArea_ != null) { |
|
mapImageInvalid = true; /* note we need to redraw */ |
|
|
// setMapArea(mapArea_); // results in a loop |
|
1127 |
mapArea = bestAllowedMapArea(mapArea_); |
mapArea = bestAllowedMapArea(mapArea_); |
1128 |
|
requestStartRendering(); |
1129 |
} |
} |
1130 |
} |
} |
1131 |
|
|
1146 |
} |
} |
1147 |
|
|
1148 |
/** |
/** |
1149 |
* Returns the background {@link Color} of the map pane. Default is white. |
* Returns the background {@link Color} of the map pane. If not set, the |
1150 |
|
* methods looks for a parent component and will use its background color. |
1151 |
|
* If no parent component is available, WHITE is returned. |
1152 |
**/ |
**/ |
1153 |
public Color getMapBackgroundColor() { |
public Color getMapBackgroundColor() { |
1154 |
|
if (mapBackgroundColor == null){ |
1155 |
|
if (getParent() != null) return getParent().getBackground(); |
1156 |
|
else return Color.WHITE; |
1157 |
|
} |
1158 |
return mapBackgroundColor; |
return mapBackgroundColor; |
1159 |
} |
} |
1160 |
|
|
1181 |
|
|
1182 |
public Envelope getMaxExtend() { |
public Envelope getMaxExtend() { |
1183 |
if (maxExtend == null) { |
if (maxExtend == null) { |
1184 |
final ReferencedEnvelope layerBounds = GTUtil |
// Commented-out because it takes soo much time! |
1185 |
.getVisibleLayoutBounds(localContext); |
// |
1186 |
if (layerBounds == null) { |
// long start = System.currentTimeMillis(); |
1187 |
// TODO Last fallback could be the CRS valid area |
// final ReferencedEnvelope layerBounds = GTUtil |
1188 |
return null; |
// .getVisibleLayoutBounds(localContext); |
1189 |
} |
// |
1190 |
|
// LOGGER.info( |
1191 |
// Kartenbereich um 10% vergroessern |
// (System.currentTimeMillis()-start)+"m to get maxExtend"); |
1192 |
return JTSUtil.fixAspectRatio(getVisibleRect(), JTSUtil |
// |
1193 |
.expandEnvelope(layerBounds, 0.1), true); |
// if (layerBounds == null) { |
1194 |
|
// // TODO Last fallback could be the CRS valid area |
1195 |
|
// return null; |
1196 |
|
// } |
1197 |
|
// |
1198 |
|
// // Kartenbereich um 10% vergroessern |
1199 |
|
// return JTSUtil.fixAspectRatio(getVisibleRect(), JTSUtil |
1200 |
|
// .expandEnvelope(layerBounds, 0.1), true); |
1201 |
} |
} |
1202 |
return maxExtend; |
return maxExtend; |
1203 |
} |
} |
1311 |
* {@link XMapPane} is visible and has bounds set. |
* {@link XMapPane} is visible and has bounds set. |
1312 |
*/ |
*/ |
1313 |
public boolean isWellDefined() { |
public boolean isWellDefined() { |
|
|
|
1314 |
try { |
try { |
1315 |
|
if (getMapContext() == null) |
|
if (getContext() == null) |
|
|
return false; |
|
|
if (getContext().getLayerCount() <= 0) |
|
1316 |
return false; |
return false; |
1317 |
if (getMapArea() == null) |
if (getMapContext().getLayerCount() <= 0) |
1318 |
return false; |
return false; |
1319 |
if (getBounds().getWidth() == 0) |
if (getVisibleRect().getWidth() == 0) |
1320 |
return false; |
return false; |
1321 |
if (getBounds().getHeight() == 0) |
if (getVisibleRect().getHeight() == 0) |
1322 |
return false; |
return false; |
1323 |
|
// if (getMapArea() == null) |
1324 |
|
// return false; |
1325 |
} catch (final Exception e) { |
} catch (final Exception e) { |
1326 |
return false; |
return false; |
1327 |
} |
} |
1328 |
return true; |
return true; |
1329 |
} |
} |
1330 |
|
|
1331 |
|
/** |
1332 |
|
* Called from the listeners while the mouse is dragging, this method either |
1333 |
|
* paints a translated (moved/panned) version of the image, or a rectangle. |
1334 |
|
* |
1335 |
|
* @param startPos |
1336 |
|
* in screen coordinates |
1337 |
|
* @param lastPos |
1338 |
|
* in screen coordinates |
1339 |
|
* @param event |
1340 |
|
* the {@link MouseEvent} to read the mouse buttons from |
1341 |
|
*/ |
1342 |
public void mouseDragged(final Point startPos, final Point lastPos, |
public void mouseDragged(final Point startPos, final Point lastPos, |
1343 |
final MouseEvent event) { |
final MouseEvent event) { |
1344 |
|
|
1345 |
if ((getState() == XMapPane.PAN) |
if ((getState() == XMapPane.PAN) |
1346 |
|| ((event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)) { |
|| ((event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)) { |
1347 |
|
|
1348 |
|
// Panning needs a panning coursor |
1349 |
if (getCursor() != SwingUtil.PANNING_CURSOR) { |
if (getCursor() != SwingUtil.PANNING_CURSOR) { |
1350 |
setCursor(SwingUtil.PANNING_CURSOR); |
setCursor(SwingUtil.PANNING_CURSOR); |
1351 |
|
|
1352 |
// While panning, we deactivate the rendering. So the tasts are |
// While panning, we deactivate the rendering. So the tasks are |
1353 |
// ready to start when the panning os done. |
// ready to start when the panning is finished. |
1354 |
if (bgExecuter != null) |
if (bgExecuter != null && bgExecuter.isRunning()) |
1355 |
bgExecuter.cancelTask(); |
bgExecuter.cancelTask(); |
1356 |
if (localExecuter != null) |
if (localExecuter.isRunning()) |
1357 |
localExecuter.cancelTask(); |
localExecuter.cancelTask(); |
1358 |
} |
} |
1359 |
|
|
1400 |
} else if ((getState() == XMapPane.ZOOM_IN) |
} else if ((getState() == XMapPane.ZOOM_IN) |
1401 |
|| (getState() == XMapPane.ZOOM_OUT) |
|| (getState() == XMapPane.ZOOM_OUT) |
1402 |
|| (getState() == XMapPane.SELECT_ALL) |
|| (getState() == XMapPane.SELECT_ALL) |
1403 |
|| (getState() == XMapPane.SELECT_TOP) |
|| (getState() == XMapPane.SELECT_TOP)) { |
|
// || (getState() == XMapPane.SELECT_ONE_FROM_TOP) |
|
|
) { |
|
|
final Graphics graphics = getGraphics(); |
|
1404 |
|
|
1405 |
|
// Draws a rectangle |
1406 |
|
final Graphics graphics = getGraphics(); |
1407 |
drawRectangle(graphics, startPos, event.getPoint()); |
drawRectangle(graphics, startPos, event.getPoint()); |
1408 |
|
if ((lastPos.x > 0) && (lastPos.y > 0)) |
|
if ((lastPos.x > 0) && (lastPos.y > 0)) { |
|
1409 |
drawRectangle(graphics, startPos, lastPos); |
drawRectangle(graphics, startPos, lastPos); |
|
} |
|
|
|
|
1410 |
graphics.dispose(); |
graphics.dispose(); |
|
|
|
1411 |
} |
} |
|
|
|
1412 |
} |
} |
1413 |
|
|
1414 |
/** |
/** |
1415 |
* Called by the {@link RenderingExecutor} when rendering was cancelled. |
* Called by the {@link RenderingExecutor} when rendering was cancelled. |
1416 |
*/ |
*/ |
1417 |
public void onRenderingCancelled() { |
public void onRenderingCancelled() { |
|
repaintTimer.stop(); |
|
1418 |
LOGGER.debug("Rendering cancelled"); |
LOGGER.debug("Rendering cancelled"); |
1419 |
|
repaintTimer.stop(); |
1420 |
} |
} |
1421 |
|
|
1422 |
/** |
/** |
1424 |
* completed. |
* completed. |
1425 |
*/ |
*/ |
1426 |
public void onRenderingCompleted() { |
public void onRenderingCompleted() { |
1427 |
|
LOGGER.debug("complete"); |
1428 |
|
|
1429 |
repaintTimer.stop(); |
repaintTimer.stop(); |
1430 |
updateFinalImage(); |
|
1431 |
repaint(); |
// We "forget" about an exception every time we complete a rendering |
1432 |
|
// thread successfully |
1433 |
if (renderingErrors.size() > 0) |
if (renderingErrors.size() > 0) |
1434 |
renderingErrors.remove(0); |
renderingErrors.remove(0); |
1435 |
|
|
1436 |
|
updateFinalImage(); |
1437 |
|
repaint(); |
1438 |
} |
} |
1439 |
|
|
1440 |
/** |
/** |
1448 |
* @see MapPaneListener#onRenderingStopped(org.geotools.swing.event.MapPaneEvent) |
* @see MapPaneListener#onRenderingStopped(org.geotools.swing.event.MapPaneEvent) |
1449 |
*/ |
*/ |
1450 |
public void onRenderingFailed(final Exception renderingError) { |
public void onRenderingFailed(final Exception renderingError) { |
1451 |
this.renderingErrors.add(renderingError); |
|
1452 |
|
// Store the exceptions so we can show it to the user: |
1453 |
|
if (!(renderingError instanceof java.lang.IllegalArgumentException && renderingError |
1454 |
|
.getMessage().equals( |
1455 |
|
"Argument \"sourceCRS\" should not be null."))) |
1456 |
|
this.renderingErrors.add(renderingError); |
1457 |
if (renderingErrors.size() > 3) |
if (renderingErrors.size() > 3) |
1458 |
renderingErrors.remove(0); |
renderingErrors.remove(0); |
1459 |
|
|
1460 |
repaintTimer.stop(); |
repaintTimer.stop(); |
1461 |
|
|
1462 |
LOGGER.warn("Rendering failed", renderingError); |
LOGGER.warn("Rendering failed", renderingError); |
1463 |
updateFinalImage(); |
updateFinalImage(); |
1464 |
repaint(); |
repaint(); |
1465 |
|
|
1466 |
} |
} |
1467 |
|
|
|
public void onRenderingPending() { |
|
|
// LOGGER.debug("Pending rendering updates the preview..."); |
|
|
updateFinalImage(); |
|
|
repaint(); |
|
|
} |
|
|
|
|
1468 |
@Override |
@Override |
1469 |
protected void paintComponent(final Graphics g) { |
protected void paintComponent(final Graphics g) { |
|
if (!acceptsRepaintCalls) |
|
|
return; |
|
1470 |
|
|
1471 |
// Maybe update the cursor and maybe stop the repainting timer |
// Maybe update the cursor and maybe stop the repainting timer |
1472 |
updateCursor(); |
updateCursor(); |
1473 |
|
|
1474 |
|
if (!acceptsRepaintCalls) |
1475 |
|
return; |
1476 |
|
|
1477 |
|
if (!isWellDefined()) |
1478 |
|
return; |
1479 |
|
|
1480 |
// super.paintComponent(g); // candidate for removal |
// super.paintComponent(g); // candidate for removal |
1481 |
|
|
1482 |
boolean paintedSomething = false; |
boolean paintedSomething = false; |
1490 |
// happening. |
// happening. |
1491 |
if (mapAreaChanged && oldMapArea != null |
if (mapAreaChanged && oldMapArea != null |
1492 |
&& getMapArea().intersects(oldMapArea) |
&& getMapArea().intersects(oldMapArea) |
1493 |
& !getMapArea().equals(oldMapArea)) { |
& !getMapArea().equals(oldMapArea) && !paneResized) { |
1494 |
|
|
1495 |
mapAreaChanged = false; |
mapAreaChanged = false; |
1496 |
|
|
1497 |
if (getMapArea().covers(oldMapArea)) { |
if (getMapArea().covers(oldMapArea)) { |
1498 |
setQuickPreviewHint(ZOOM_OUT); |
quickPreviewHint = ZOOM_OUT; |
1499 |
paintedSomething = drawScaledPreviewImage_Zoom((Graphics2D) g); |
paintedSomething = drawScaledPreviewImage_Zoom((Graphics2D) g); |
1500 |
} else if (oldMapArea.covers(getMapArea())) { |
} else if (oldMapArea.covers(getMapArea())) { |
1501 |
setQuickPreviewHint(ZOOM_IN); |
quickPreviewHint = ZOOM_IN; |
1502 |
paintedSomething = drawScaledPreviewImage_Zoom((Graphics2D) g); |
paintedSomething = drawScaledPreviewImage_Zoom((Graphics2D) g); |
1503 |
} |
} |
|
|
|
1504 |
} |
} |
|
|
|
|
if (paneResized) { |
|
|
paneResized = false; |
|
|
disposeImages(); |
|
|
} |
|
|
|
|
|
// Start the Threads and Timers to render the image |
|
|
requestStartRendering(); |
|
|
|
|
1505 |
} |
} |
1506 |
|
|
1507 |
if (!paintedSomething) { |
if (!paintedSomething) { |
1566 |
public void performPan() { |
public void performPan() { |
1567 |
|
|
1568 |
Rectangle winBounds = getVisibleRect(); |
Rectangle winBounds = getVisibleRect(); |
1569 |
|
|
1570 |
winBounds.translate(-imageOrigin.x, -imageOrigin.y); |
winBounds.translate(-imageOrigin.x, -imageOrigin.y); |
1571 |
final Envelope newMapArea = tranformWindowToGeo(winBounds.x, |
final Envelope newMapArea = tranformWindowToGeo(winBounds.x, |
1572 |
winBounds.y, winBounds.x + winBounds.width, winBounds.y |
winBounds.y, winBounds.x + winBounds.width, winBounds.y |
1589 |
setCursor(SwingUtil.PAN_CURSOR); |
setCursor(SwingUtil.PAN_CURSOR); |
1590 |
} |
} |
1591 |
|
|
|
// |
|
|
// /** |
|
|
// * Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot |
|
|
// gemacht |
|
|
// * wird) wird. Dann werden wird der Hintergrund auf WEISS gesetzt. |
|
|
// * |
|
|
// * @author <a href="mailto:[email protected]">Stefan Alfons |
|
|
// * Krüger</a> |
|
|
// */ |
|
|
// @Override |
|
|
// public void print(final Graphics g) { |
|
|
// final Color orig = getBackground(); |
|
|
// setBackground(Color.WHITE); |
|
|
// |
|
|
// // wrap in try/finally so that we always restore the state |
|
|
// try { |
|
|
// super.print(g); |
|
|
// } finally { |
|
|
// setBackground(orig); |
|
|
// } |
|
|
// } |
|
|
|
|
1592 |
/** |
/** |
1593 |
* Entfernt einen Listener von der Map. |
* Entfernt einen Listener von der Map. |
1594 |
* |
* |
1607 |
private void requestStartRendering() { |
private void requestStartRendering() { |
1608 |
if (bgExecuter != null) |
if (bgExecuter != null) |
1609 |
bgExecuter.cancelTask(); |
bgExecuter.cancelTask(); |
1610 |
if (localExecuter != null) |
|
1611 |
localExecuter.cancelTask(); |
localExecuter.cancelTask(); |
1612 |
|
|
1613 |
|
mapImageInvalid = true; |
1614 |
|
if (paneResized) { |
1615 |
|
paneResized = false; |
1616 |
|
disposeImages(); |
1617 |
|
} |
1618 |
requestStartRendering = true; |
requestStartRendering = true; |
1619 |
|
|
1620 |
} |
} |
1621 |
|
|
|
// |
|
|
// /** |
|
|
// * Berechnet die Transformation zwischen Fenster- und Karten-Koordinaten |
|
|
// * neu. |
|
|
// */ |
|
|
// protected void resetTransforms() { |
|
|
// if (getMapArea() == null || getWidth() == 0 || getHeight() == 0) |
|
|
// return; |
|
|
// |
|
|
// // We store the last Transform |
|
|
// oldScreenToWorld = screenToWorld; |
|
|
// |
|
|
// this.screenToWorld = new AffineTransform( |
|
|
// // Genauso wie die Fenster-Koordinaten, werden die Longitude-Koordinaten |
|
|
// // nach rechts (Osten) hin groesser |
|
|
// // --> positive Verschiebung |
|
|
// getMapArea().getWidth() / getWidth(), |
|
|
// // keine Verzerrung |
|
|
// 0.0, 0.0, |
|
|
// // Waehrend die Fenster-Koordinaten nach unten hin groesser |
|
|
// // werden, |
|
|
// // werden Latitude-Koordinaten nach Sueden hin keiner |
|
|
// // --> negative Verschiebung |
|
|
// -getMapArea().getHeight() / getHeight(), |
|
|
// // Die Longitude-Koordinaten werden nach Osten hin groesser |
|
|
// // --> obere linke Ecke des Fensters hat also den Minimalwert |
|
|
// getMapArea().getMinX(), |
|
|
// // Die Latitude-Koordinaten werden nach Norden hin groesser |
|
|
// // --> obere linke Ecke des Fensters hat also den Maximalwert |
|
|
// getMapArea().getMaxY()); |
|
|
// |
|
|
// try { |
|
|
// this.worldToScreen = screenToWorld.createInverse(); |
|
|
// } catch (final NoninvertibleTransformException e) { |
|
|
// LOGGER.error(e); |
|
|
// } |
|
|
// } |
|
|
|
|
1622 |
/** |
/** |
1623 |
* Calculate the affine transforms used to convert between world and pixel |
* Calculate the affine transforms used to convert between world and pixel |
1624 |
* coordinates. The calculations here are very basic and assume a cartesian |
* coordinates. The calculations here are very basic and assume a cartesian |
1634 |
*/ |
*/ |
1635 |
private void resetTransforms() { |
private void resetTransforms() { |
1636 |
ReferencedEnvelope refMapEnv = new ReferencedEnvelope(mapArea, |
ReferencedEnvelope refMapEnv = new ReferencedEnvelope(mapArea, |
1637 |
getContext().getCoordinateReferenceSystem()); |
getMapContext().getCoordinateReferenceSystem()); |
1638 |
|
|
1639 |
Rectangle paintArea = getVisibleRect(); // NOT USE GET BOUNDS! |
// System.out |
1640 |
|
// .println("paintArea in resetTeansofrms = " + getVisibleRect()); |
1641 |
// |
if (!isWellDefined()) |
1642 |
// double xscale = paintArea.getWidth() / refEnv.getWidth(); |
return; |
|
// double yscale = paintArea.getHeight() / refEnv.getHeight(); |
|
|
// |
|
|
// double scale = Math.min(xscale, yscale); |
|
|
// |
|
|
// double xoff = refEnv.getMedian(0) * scale - paintArea.getCenterX(); |
|
|
// double yoff = refEnv.getMedian(1) * scale + paintArea.getCenterY(); |
|
1643 |
|
|
1644 |
worldToScreen = RendererUtilities.worldToScreenTransform(refMapEnv, |
worldToScreen = RendererUtilities.worldToScreenTransform(refMapEnv, |
1645 |
paintArea); |
getVisibleRect()); |
1646 |
|
|
|
// worldToScreen = new AffineTransform(scale, 0, 0, -scale, -xoff, |
|
|
// yoff); |
|
1647 |
try { |
try { |
1648 |
screenToWorld = worldToScreen.createInverse(); |
screenToWorld = worldToScreen.createInverse(); |
1649 |
|
|
1650 |
} catch (NoninvertibleTransformException ex) { |
} catch (NoninvertibleTransformException ex) { |
1651 |
ex.printStackTrace(); |
LOGGER |
1652 |
|
.error("can't invert worldToScreen to get screenToWorld!", |
1653 |
|
ex); |
1654 |
} |
} |
1655 |
} |
} |
1656 |
|
|
1669 |
this.bgContext = context; |
this.bgContext = context; |
1670 |
|
|
1671 |
if (context != null) { |
if (context != null) { |
1672 |
setMapArea(bgContext.getAreaOfInterest()); |
// setMapArea(bgContext.getAreaOfInterest()); |
1673 |
|
|
1674 |
this.bgContext.addMapLayerListListener(bgContextListener); |
this.bgContext.addMapLayerListListener(bgContextListener); |
1675 |
|
|
1678 |
mapLayer.addMapLayerListener(bgMapLayerListener); |
mapLayer.addMapLayerListener(bgMapLayerListener); |
1679 |
} |
} |
1680 |
} |
} |
1681 |
mapImageInvalid = true; |
|
1682 |
repaint(); |
requestStartRendering(); |
1683 |
} |
} |
1684 |
|
|
1685 |
public void setJava2dHints(final RenderingHints java2dHints) { |
public void setJava2dHints(final RenderingHints java2dHints) { |
1686 |
this.java2dHints = java2dHints; |
this.java2dHints = java2dHints; |
1687 |
} |
} |
1688 |
|
|
|
/** |
|
|
* |
|
|
* @param context |
|
|
*/ |
|
1689 |
public void setLocalContext(final MapContext context) { |
public void setLocalContext(final MapContext context) { |
1690 |
// Remove the default listener from the old context |
// Remove the default listener from the old context |
1691 |
if (this.localContext != null) { |
if (this.localContext != null) { |
1701 |
|
|
1702 |
if (context != null) { |
if (context != null) { |
1703 |
|
|
1704 |
setMapArea(localContext.getAreaOfInterest()); |
// setMapArea(localContext.getAreaOfInterest()); |
1705 |
|
|
1706 |
localRenderer.setContext(localContext); |
localRenderer.setContext(localContext); |
1707 |
|
|
1713 |
} |
} |
1714 |
} |
} |
1715 |
|
|
1716 |
mapImageInvalid = true; |
requestStartRendering(); |
1717 |
repaint(); |
|
1718 |
|
} |
1719 |
|
|
1720 |
|
public boolean setMapArea(final Envelope newMapArea) { |
1721 |
|
if (newMapArea == null) |
1722 |
|
return false; |
1723 |
|
if (getMapContext() |
1724 |
|
.getCoordinateReferenceSystem() == null) return false; |
1725 |
|
return setMapArea(new ReferencedEnvelope(newMapArea, getMapContext() |
1726 |
|
.getCoordinateReferenceSystem())); |
1727 |
} |
} |
1728 |
|
|
1729 |
/** |
/** |
1731 |
* @return <code>true</code> if the mapArea has been changed and a repaint |
* @return <code>true</code> if the mapArea has been changed and a repaint |
1732 |
* has been triggered. |
* has been triggered. |
1733 |
*/ |
*/ |
1734 |
public boolean setMapArea(final Envelope newMapArea) { |
public boolean setMapArea(final ReferencedEnvelope newMapArea) { |
1735 |
|
|
1736 |
if (newMapArea == null |
if (newMapArea == null |
1737 |
|| bestAllowedMapArea(newMapArea).equals(mapArea)) { |
|| bestAllowedMapArea(newMapArea).equals(mapArea)) { |
1786 |
bgContext.setAreaOfInterest(mapArea, localContext |
bgContext.setAreaOfInterest(mapArea, localContext |
1787 |
.getCoordinateReferenceSystem()); |
.getCoordinateReferenceSystem()); |
1788 |
} |
} |
1789 |
mapImageInvalid = true; |
|
1790 |
mapAreaChanged = true; |
mapAreaChanged = true; |
|
repaint(); |
|
1791 |
|
|
1792 |
LOGGER.debug("New maparea = " + mapArea); |
repaint(200); // Do not remove it! |
1793 |
|
|
1794 |
|
requestStartRendering(); |
1795 |
|
|
1796 |
return true; |
return true; |
1797 |
} |
} |
1798 |
|
|
1802 |
* @param if <code>null</code>, white is used. |
* @param if <code>null</code>, white is used. |
1803 |
*/ |
*/ |
1804 |
public void setMapBackgroundColor(Color bgColor) { |
public void setMapBackgroundColor(Color bgColor) { |
|
if (bgColor == null) |
|
|
bgColor = Color.WHITE; |
|
1805 |
this.mapBackgroundColor = bgColor; |
this.mapBackgroundColor = bgColor; |
1806 |
} |
} |
1807 |
|
|
1876 |
} |
} |
1877 |
|
|
1878 |
/** |
/** |
1879 |
* |
* If <code>true</code>, allow the {@link XMapPane} to process #repaint() |
1880 |
* @param b |
* requests. Otherwise the map will not paint anything and not start any |
1881 |
|
* rendering {@link Thread}s. |
1882 |
*/ |
*/ |
1883 |
public void setPainting(final boolean b) { |
public void setPainting(final boolean b) { |
1884 |
acceptsRepaintCalls = b; |
acceptsRepaintCalls = b; |
1885 |
} |
if (acceptsRepaintCalls == true) |
1886 |
|
repaint(); |
|
// /** |
|
|
// * Returns in milli seconds the time the last rending of the |
|
|
// * {@link SelectableXMapPane} took. #Long.MAX_VALUE if the JMapPane has |
|
|
// not |
|
|
// * been rendered yet. |
|
|
// */ |
|
|
// public long getLastRenderingDuration() { |
|
|
// return lastRenderingDuration; |
|
|
// } |
|
|
|
|
|
public void setQuickPreviewHint(final int quickPreviewHint) { |
|
|
this.quickPreviewHint = quickPreviewHint; |
|
|
|
|
1887 |
} |
} |
1888 |
|
|
1889 |
private void setRendererHints(final Map<Object, Object> rendererHints) { |
private void setRendererHints(final Map<Object, Object> rendererHints) { |
1928 |
*/ |
*/ |
1929 |
private void startRendering() { |
private void startRendering() { |
1930 |
|
|
1931 |
if (!isWellDefined()) |
if (!isWellDefined() || !acceptsRepaintCalls) { |
1932 |
|
// if we are not ready to start rendering, try it again the next |
1933 |
|
// time the timer is chacking the flag. |
1934 |
|
requestStartRendering = true; |
1935 |
return; |
return; |
1936 |
|
} |
1937 |
|
|
1938 |
if (bgExecuter != null) { |
if (bgExecuter != null) { |
1939 |
// Stop all renderers |
// Stop all renderers |
1940 |
bgExecuter.cancelTask(); |
bgExecuter.cancelTask(); |
1941 |
} |
} |
1942 |
|
|
1943 |
if (localExecuter != null) { |
localExecuter.cancelTask(); |
|
localExecuter.cancelTask(); |
|
|
} |
|
1944 |
|
|
1945 |
final Rectangle curPaintArea = getVisibleRect(); |
final Rectangle curPaintArea = getVisibleRect(); |
1946 |
|
|
1963 |
// (Graphics2D) getBgImage().getGraphics(), createGTRenderer); |
// (Graphics2D) getBgImage().getGraphics(), createGTRenderer); |
1964 |
} |
} |
1965 |
|
|
1966 |
if (getContext() != null) { |
if (getMapContext() != null) { |
1967 |
// localExecuter = new RenderingExecutor(this, 150l); |
// localExecuter = new RenderingExecutor(this, 150l); |
1968 |
// LOGGER.debug("starting local renderer:"); |
// LOGGER.debug("starting local renderer:"); |
1969 |
|
|
1970 |
localRenderer.setJava2DHints(getJava2dHints()); |
localRenderer.setJava2DHints(getJava2dHints()); |
1971 |
localRenderer.setRendererHints(getRendererHints()); |
localRenderer.setRendererHints(getRendererHints()); |
1972 |
|
|
1973 |
ReferencedEnvelope areaOfInterest = getMapArea(); |
final boolean submitted = localExecuter.submit(getMapArea(), |
|
final boolean submitted = localExecuter.submit(areaOfInterest, |
|
1974 |
curPaintArea, (Graphics2D) getLocalImage().getGraphics(), |
curPaintArea, (Graphics2D) getLocalImage().getGraphics(), |
1975 |
localRenderer, getWorldToScreenTransform()); |
localRenderer); |
1976 |
if (submitted) |
if (submitted) |
1977 |
repaintTimer.restart(); |
repaintTimer.restart(); |
1978 |
else |
else |
1979 |
requestStartRendering = true; // Try to start rendering again in |
requestStartRendering = true; // Try to start rendering |
1980 |
|
// again in |
1981 |
// a moment |
// a moment |
1982 |
} |
} |
1983 |
|
|
2030 |
final AffineTransform at = getScreenToWorld(); |
final AffineTransform at = getScreenToWorld(); |
2031 |
final Point2D geoO = at.transform(new Point2D.Double(ox, oy), null); |
final Point2D geoO = at.transform(new Point2D.Double(ox, oy), null); |
2032 |
final Point2D geoP = at.transform(new Point2D.Double(px, py), null); |
final Point2D geoP = at.transform(new Point2D.Double(px, py), null); |
2033 |
|
|
2034 |
// Mmmmm... don't really understand why its x,x,y,y |
// Mmmmm... don't really understand why its x,x,y,y |
2035 |
// return new Envelope(geoO.getX(), geoP.getX(), geoO.getY(), geoP.getY()); |
// return new Envelope(geoO.getX(), geoP.getX(), geoO.getY(), |
2036 |
return new Envelope( new Coordinate(geoO.getX(), geoO.getY()), new Coordinate(geoP.getX(), geoP.getY())); |
// geoP.getY()); |
2037 |
|
return new Envelope(new Coordinate(geoO.getX(), geoO.getY()), |
2038 |
|
new Coordinate(geoP.getX(), geoP.getY())); |
2039 |
} |
} |
2040 |
|
|
2041 |
/** |
/** |
2047 |
// if the renderers have stopped, also stop the timer that is updating |
// if the renderers have stopped, also stop the timer that is updating |
2048 |
// the final image |
// the final image |
2049 |
if (bgExecuter != null && bgExecuter.isRunning() |
if (bgExecuter != null && bgExecuter.isRunning() |
2050 |
|| localExecuter != null && localExecuter.isRunning()) { |
|| localExecuter.isRunning()) { |
2051 |
setCursor(WAIT_CURSOR); |
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
2052 |
return; |
return; |
2053 |
} else { |
} else { |
2054 |
// Allow one last rendering |
// Allow one last rendering |
2055 |
if (repaintTimer.isRunning()) { |
if (repaintTimer.isRunning()) { |
2056 |
System.out.println("one last rendering...."); |
// System.out.println("one last rendering...."); |
2057 |
repaintTimer.stop(); |
repaintTimer.stop(); |
2058 |
updateFinalImage(); |
updateFinalImage(); |
2059 |
repaint(); |
repaint(); |
2161 |
|
|
2162 |
// If the rendering process is still running, indicate this is the image |
// If the rendering process is still running, indicate this is the image |
2163 |
if (forceWait || bgExecuter != null && bgExecuter.isRunning() |
if (forceWait || bgExecuter != null && bgExecuter.isRunning() |
2164 |
|| localExecuter != null && localExecuter.isRunning()) { |
|| localExecuter.isRunning()) { |
2165 |
|
|
2166 |
y += 8; |
y += 8; |
2167 |
|
|
2180 |
y += 24; |
y += 24; |
2181 |
} |
} |
2182 |
|
|
2183 |
if (renderingErrors != null) { |
if (!renderingErrors.isEmpty() && isShowExceptions()) { |
2184 |
|
|
2185 |
final Color c = graphics.getColor(); |
final Color c = graphics.getColor(); |
2186 |
graphics.setFont(errorFont); |
graphics.setFont(errorFont); |
2187 |
|
|
2188 |
for (Exception ex : renderingErrors) { |
for (Exception ex : renderingErrors) { |
2189 |
|
|
|
if (ex instanceof java.lang.IllegalArgumentException |
|
|
&& ex.getMessage().equals( |
|
|
"Argument \"sourceCRS\" should not be null.")) |
|
|
continue; |
|
|
|
|
2190 |
String errStr = ex.getLocalizedMessage(); |
String errStr = ex.getLocalizedMessage(); |
2191 |
|
|
2192 |
|
if (errStr == null) |
2193 |
|
errStr = ex.getMessage(); |
2194 |
|
if (errStr == null) |
2195 |
|
errStr = "unknown error: " + ex.getClass().getSimpleName(); |
2196 |
|
|
2197 |
graphics.setColor(Color.WHITE); |
graphics.setColor(Color.WHITE); |
2198 |
graphics.drawString(errStr, 5, y); |
graphics.drawString(errStr, 5, y); |
2199 |
graphics.setColor(Color.RED); |
graphics.setColor(Color.RED); |
2217 |
public void zoomTo( |
public void zoomTo( |
2218 |
final FeatureCollection<SimpleFeatureType, SimpleFeature> features) { |
final FeatureCollection<SimpleFeatureType, SimpleFeature> features) { |
2219 |
|
|
2220 |
final CoordinateReferenceSystem mapCRS = getContext() |
// if (!isWellDefined()) return; |
2221 |
|
|
2222 |
|
final CoordinateReferenceSystem mapCRS = getMapContext() |
2223 |
.getCoordinateReferenceSystem(); |
.getCoordinateReferenceSystem(); |
2224 |
final CoordinateReferenceSystem fCRS = features.getSchema() |
final CoordinateReferenceSystem fCRS = features.getSchema() |
2225 |
.getGeometryDescriptor().getCoordinateReferenceSystem(); |
.getGeometryDescriptor().getCoordinateReferenceSystem(); |
2226 |
|
|
2227 |
double width = mapArea.getWidth(); |
ReferencedEnvelope _mapArea; |
2228 |
double height = mapArea.getHeight(); |
if (mapArea == null) |
2229 |
|
_mapArea = features.getBounds(); |
2230 |
|
else |
2231 |
|
_mapArea = getMapArea(); |
2232 |
|
double width = _mapArea.getWidth(); |
2233 |
|
double height = _mapArea.getHeight(); |
2234 |
final double ratio = height / width; |
final double ratio = height / width; |
2235 |
|
|
2236 |
if (features == null || features.size() == 0) { |
if (features == null || features.size() == 0) { |
2282 |
} else { |
} else { |
2283 |
final ReferencedEnvelope fBounds = features.getBounds(); |
final ReferencedEnvelope fBounds = features.getBounds(); |
2284 |
|
|
2285 |
Envelope bounds; |
ReferencedEnvelope bounds; |
2286 |
if (!mapCRS.equals(fCRS)) { |
if (!mapCRS.equals(fCRS)) { |
2287 |
bounds = JTSUtil.transformEnvelope(fBounds, fCRS, mapCRS); |
bounds = JTSUtil.transformEnvelope(fBounds, mapCRS); |
2288 |
} else { |
} else { |
2289 |
bounds = fBounds; |
bounds = fBounds; |
2290 |
} |
} |
2339 |
newMapArea.expandBy((mapArea.getWidth() * zoomFaktor - mapArea |
newMapArea.expandBy((mapArea.getWidth() * zoomFaktor - mapArea |
2340 |
.getWidth()) / 2., (mapArea.getHeight() * zoomFaktor - mapArea |
.getWidth()) / 2., (mapArea.getHeight() * zoomFaktor - mapArea |
2341 |
.getHeight()) / 2.); |
.getHeight()) / 2.); |
2342 |
|
|
2343 |
// TODO we actually want that |
// TODO we actually want that |
2344 |
// // Move the newMapArea above the new center |
// // Move the newMapArea above the new center |
2345 |
// newMapArea.translate(gcenter.getX() - mapArea.centre().x, gcenter |
// newMapArea.translate(gcenter.getX() - mapArea.centre().x, gcenter |
2346 |
// .getY() |
// .getY() |
2347 |
// - mapArea.centre().y); |
// - mapArea.centre().y); |
2348 |
|
|
2349 |
setMapArea(newMapArea); |
setMapArea(newMapArea); |
2350 |
} |
} |
2351 |
|
|
2352 |
|
/** |
2353 |
|
* Shall non-fatal rendering exceptions be reported in the mappane or be |
2354 |
|
* dropped quitely. |
2355 |
|
*/ |
2356 |
|
public void setShowExceptions(boolean showExceptions) { |
2357 |
|
this.showExceptions = showExceptions; |
2358 |
|
} |
2359 |
|
|
2360 |
|
/** |
2361 |
|
* Shall exceptions be reported in the mappane? |
2362 |
|
*/ |
2363 |
|
public boolean isShowExceptions() { |
2364 |
|
return showExceptions; |
2365 |
|
} |
2366 |
|
|
2367 |
} |
} |