/[schmitzm]/trunk/src/skrueger/geotools/XMapPane.java
ViewVC logotype

Diff of /trunk/src/skrueger/geotools/XMapPane.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 531 by alfonx, Thu Nov 19 09:42:59 2009 UTC revision 553 by alfonx, Mon Nov 23 21:39:17 2009 UTC
# Line 21  import java.awt.geom.NoninvertibleTransf Line 21  import java.awt.geom.NoninvertibleTransf
21  import java.awt.geom.Point2D;  import java.awt.geom.Point2D;
22  import java.awt.image.BufferedImage;  import java.awt.image.BufferedImage;
23  import java.io.IOException;  import java.io.IOException;
24    import java.util.ArrayList;
25  import java.util.HashMap;  import java.util.HashMap;
26  import java.util.Map;  import java.util.Map;
27  import java.util.Vector;  import java.util.Vector;
# Line 42  import org.geotools.referencing.CRS; Line 43  import org.geotools.referencing.CRS;
43  import org.geotools.renderer.GTRenderer;  import org.geotools.renderer.GTRenderer;
44  import org.geotools.renderer.label.LabelCacheImpl;  import org.geotools.renderer.label.LabelCacheImpl;
45  import org.geotools.renderer.lite.LabelCache;  import org.geotools.renderer.lite.LabelCache;
46    import org.geotools.renderer.lite.RendererUtilities;
47  import org.geotools.renderer.lite.StreamingRenderer;  import org.geotools.renderer.lite.StreamingRenderer;
48  import org.geotools.swing.JMapPane;  import org.geotools.swing.JMapPane;
49  import org.geotools.swing.event.MapMouseEvent;  import org.geotools.swing.event.MapMouseEvent;
# Line 57  import org.opengis.referencing.operation Line 59  import org.opengis.referencing.operation
59  import schmitzm.geotools.GTUtil;  import schmitzm.geotools.GTUtil;
60  import schmitzm.geotools.JTSUtil;  import schmitzm.geotools.JTSUtil;
61  import schmitzm.geotools.gui.SelectableXMapPane;  import schmitzm.geotools.gui.SelectableXMapPane;
62    import schmitzm.geotools.io.GeoImportUtil;
63  import schmitzm.geotools.map.event.JMapPaneListener;  import schmitzm.geotools.map.event.JMapPaneListener;
64  import schmitzm.geotools.map.event.MapLayerAdapter;  import schmitzm.geotools.map.event.MapLayerAdapter;
65  import schmitzm.lang.LangUtil;  import schmitzm.lang.LangUtil;
# Line 121  public class XMapPane extends JPanel { Line 124  public class XMapPane extends JPanel {
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;
# Line 137  public class XMapPane extends JPanel { Line 140  public class XMapPane extends JPanel {
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 = 500;
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 = 500;          public static final int DEFAULT_RESIZING_PAINT_DELAY = 600;
152    
153            private final Timer resizeTimer;
154    
155          public static final int NONE = -123;          public static final int NONE = -123;
156    
# Line 182  public class XMapPane extends JPanel { Line 189  public class XMapPane extends JPanel {
189          public static final Cursor WAIT_CURSOR = Cursor          public static final Cursor WAIT_CURSOR = Cursor
190                          .getPredefinedCursor(Cursor.WAIT_CURSOR);                          .getPredefinedCursor(Cursor.WAIT_CURSOR);
191    
192            /**
193             * {@link Font} used to paint the wait messages into the map
194             *
195             * @see #addGadgets(Graphics2D, boolean)
196             */
197          final static Font waitFont = new Font("Arial", Font.BOLD, 30);          final static Font waitFont = new Font("Arial", Font.BOLD, 30);
198    
199          /**          /**
200             * {@link Font} used to paint error messages into the map
201             *
202             * @see #addGadgets(Graphics2D, boolean)
203             */
204            final static Font errorFont = new Font("Arial", Font.BOLD, 13);
205    
206            /**
207             * The wait message painted into the map while rendering is going on on
208             * another thread.
209             *
210             * @see #addGadgets(Graphics2D, boolean)
211             */
212            final String waitMsg = SwingUtil.R("WaitMess");
213    
214            /**
215           * Flag fuer Modus "Heran zoomen".           * Flag fuer Modus "Heran zoomen".
216           *           *
217           * @see #setState(int)           * @see #setState(int)
# Line 243  public class XMapPane extends JPanel { Line 270  public class XMapPane extends JPanel {
270    
271                  @Override                  @Override
272                  public void layerAdded(final MapLayerListEvent event) {                  public void layerAdded(final MapLayerListEvent event) {
273                          event.getLayer().addMapLayerListener(bgMapLayerListener);                          MapLayer layer = event.getLayer();
274                            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();  
275                          requestStartRendering();                          requestStartRendering();
276    
277                  }                  }
278    
279                  @Override                  @Override
280                  public void layerChanged(final MapLayerListEvent event) {                  public void layerChanged(final MapLayerListEvent event) {
                         // mapImageInvalid = true;  
                         // repaint();  
281                          requestStartRendering();                          requestStartRendering();
282                  }                  }
283    
284                  @Override                  @Override
285                  public void layerMoved(final MapLayerListEvent event) {                  public void layerMoved(final MapLayerListEvent event) {
                         // mapImageInvalid = true;  
                         // repaint();  
286                          requestStartRendering();                          requestStartRendering();
287                  }                  }
288    
# Line 279  public class XMapPane extends JPanel { Line 290  public class XMapPane extends JPanel {
290                  public void layerRemoved(final MapLayerListEvent event) {                  public void layerRemoved(final MapLayerListEvent event) {
291                          if (event.getLayer() != null)                          if (event.getLayer() != null)
292                                  event.getLayer().removeMapLayerListener(bgMapLayerListener);                                  event.getLayer().removeMapLayerListener(bgMapLayerListener);
                         // mapImageInvalid = true;  
                         // repaint();  
293                          requestStartRendering();                          requestStartRendering();
294                  }                  }
295          };          };
296    
297          /**          /**
298           * compass and icon are rendered into this image           * This {@link RenderingExecutor} manages the creation and cancellation of
299             * up to one {@link Thread} for rendering the {@link #localContext}.
300           */           */
301          // protected BufferedImage gadgetsImage;          private final RenderingExecutor localExecuter = new RenderingExecutor(this);
302    
303            /**
304             * This {@link RenderingExecutor} manages the creation and cancellation of
305             * up to one {@link Thread} for rendering the {@link #bgContext}.
306             */
307          protected RenderingExecutor bgExecuter;          protected RenderingExecutor bgExecuter;
308    
309          /**          /**
310           * The Renderer for the LocalLayers uses this Image. When set to null,           * The {@link #localRenderer} for the {@link #localContext} uses this
311           * please dispose this {@link Graphics2D}           * {@link Image}.
312           */           */
313          private BufferedImage localImage;          private BufferedImage localImage;
314    
         private BufferedImage finalImage;  
315          /**          /**
316           * If # {@link #bgExecuter} is using {@link #bgRenderer} for the Background           * The {@link #bgRenderer} for the {@link #bgContext} uses this
317           * uses this Image. When set to null, please dispose the {@link Graphics2D}           * {@link Image}.
318           */           */
319          private BufferedImage bgImage;          private BufferedImage bgImage;
320    
321          /**          /**
322             * This {@link Image} is a merge of the {@link #bgImage},
323             * {@link #localImage} and {@link #addGadgets(Graphics2D, boolean)}. It is
324             * updated with {@link #updateFinalImage()} and used for painting in
325             * {@link #paintComponent(Graphics)}
326             */
327            private BufferedImage finalImage;
328    
329            /**
330           * 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
331           * corner.           * corner.
332           *           *
# Line 322  public class XMapPane extends JPanel { Line 343  public class XMapPane extends JPanel {
343    
344                  @Override                  @Override
345                  public void layerChanged(final MapLayerEvent event) {                  public void layerChanged(final MapLayerEvent event) {
                         // Change of SLD for example  
                         // mapImageInvalid = true;  
                         // repaint();  
346                          requestStartRendering();                          requestStartRendering();
347                  }                  }
348    
349                  @Override                  @Override
350                  public void layerHidden(final MapLayerEvent event) {                  public void layerHidden(final MapLayerEvent event) {
                         // mapImageInvalid = true;  
                         // repaint();  
351                          requestStartRendering();                          requestStartRendering();
352                  }                  }
353    
354                  @Override                  @Override
355                  public void layerShown(final MapLayerEvent event) {                  public void layerShown(final MapLayerEvent event) {
                         // mapImageInvalid = true;  
                         // repaint();  
356                          requestStartRendering();                          requestStartRendering();
357                  }                  }
358          };          };
# Line 375  public class XMapPane extends JPanel { Line 389  public class XMapPane extends JPanel {
389                  public void layerAdded(final MapLayerListEvent event) {                  public void layerAdded(final MapLayerListEvent event) {
390                          event.getLayer().addMapLayerListener(localMapLayerListener);                          event.getLayer().addMapLayerListener(localMapLayerListener);
391    
392                          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();  
393                          requestStartRendering();                          requestStartRendering();
394    
395                  }                  }
396    
397                  @Override                  @Override
398                  public void layerChanged(final MapLayerListEvent event) {                  public void layerChanged(final MapLayerListEvent event) {
399                          // mapImageInvalid = true;                          localRenderer.setContext(getMapContext());
                         // repaint();  
                         localRenderer.setContext(getContext());  
400                          requestStartRendering();                          requestStartRendering();
401                  }                  }
402    
403                  @Override                  @Override
404                  public void layerMoved(final MapLayerListEvent event) {                  public void layerMoved(final MapLayerListEvent event) {
405                          // mapImageInvalid = true;                          localRenderer.setContext(getMapContext());
                         // repaint();  
                         localRenderer.setContext(getContext());  
406                          requestStartRendering();                          requestStartRendering();
407                  }                  }
408    
# Line 412  public class XMapPane extends JPanel { Line 410  public class XMapPane extends JPanel {
410                  public void layerRemoved(final MapLayerListEvent event) {                  public void layerRemoved(final MapLayerListEvent event) {
411                          if (event.getLayer() != null)                          if (event.getLayer() != null)
412                                  event.getLayer().removeMapLayerListener(localMapLayerListener);                                  event.getLayer().removeMapLayerListener(localMapLayerListener);
413                          // mapImageInvalid = true;                          localRenderer.setContext(getMapContext());
                         // repaint();  
                         localRenderer.setContext(getContext());  
414                          requestStartRendering();                          requestStartRendering();
415                  }                  }
416          };          };
417    
         private final RenderingExecutor localExecuter = new RenderingExecutor(this);  
   
418          /**          /**
419           * Listens to each layer in the local {@link MapContext} for changes and           * Listens to each layer in the local {@link MapContext} for changes and
420           * triggers repaints.           * triggers repaints.
# Line 429  public class XMapPane extends JPanel { Line 423  public class XMapPane extends JPanel {
423    
424                  @Override                  @Override
425                  public void layerChanged(final MapLayerEvent event) {                  public void layerChanged(final MapLayerEvent event) {
426                          localRenderer.setContext(getContext()); // betters for SLD changes?!                          localRenderer.setContext(getMapContext()); // betters for SLD changes?!
                         // Change of SLD for example  
                         // mapImageInvalid = true;  
                         // repaint();  
427                          requestStartRendering();                          requestStartRendering();
428                  }                  }
429    
430                  @Override                  @Override
431                  public void layerHidden(final MapLayerEvent event) {                  public void layerHidden(final MapLayerEvent event) {
                         // mapImageInvalid = true;  
                         // repaint();  
432                          requestStartRendering();                          requestStartRendering();
433                  }                  }
434    
435                  @Override                  @Override
436                  public void layerShown(final MapLayerEvent event) {                  public void layerShown(final MapLayerEvent event) {
                         // mapImageInvalid = true;  
                         // repaint();  
437                          requestStartRendering();                          requestStartRendering();
438                  }                  }
439          };          };
440    
441          private final GTRenderer localRenderer = GTUtil.createGTRenderer();          private final GTRenderer localRenderer = GTUtil.createGTRenderer();
442            
443          private final GTRenderer bgRenderer = GTUtil.createGTRenderer();          private final GTRenderer bgRenderer = GTUtil.createGTRenderer();
444    
445          /**          /**
# Line 523  public class XMapPane extends JPanel { Line 510  public class XMapPane extends JPanel {
510          // if null, no quick preview will be shown          // if null, no quick preview will be shown
511          private int quickPreviewHint = 0;          private int quickPreviewHint = 0;
512    
513          private Map<Object, Object> rendererHints;          private Map<Object, Object> rendererHints = GTUtil
514                            .getDefaultGTRendererHints(localRenderer);
515    
516            /**
517             * If set to <code>true</code>, the {@link #startRenderThreadsTimer} will
518             * start rendering {@link Thread}s
519             **/
520          private volatile Boolean requestStartRendering = false;          private volatile Boolean requestStartRendering = false;
521    
         private final Timer resizeTimer;  
   
         private final int resizingPaintDelay;  
   
522          /**          /**
523           * Transformation zwischen Fenster-Koordinaten und Karten-Koordinaten           * Transformation zwischen Fenster-Koordinaten und Karten-Koordinaten
524           * (lat/lon)           * (lat/lon)
# Line 564  public class XMapPane extends JPanel { Line 552  public class XMapPane extends JPanel {
552          public final ZoomXMapPaneMouseListener zoomMapPaneMouseListener = new ZoomXMapPaneMouseListener(          public final ZoomXMapPaneMouseListener zoomMapPaneMouseListener = new ZoomXMapPaneMouseListener(
553                          this);                          this);
554    
555          // TODO doku          /** Is set if a renderer has an error **/
556            protected ArrayList<Exception> renderingErrors = new ArrayList<Exception>();
557            
558            /** If <code>true</code>, then rendering exceptions are rendererd into the map pane **/
559            private boolean showExceptions= false;
560    
561            // /** If {@link #getVisibleRect()} of this {@link JPanel} is not yet
562            // defined, but we are **/
563            // private Envelope mapAreaCondidate = null;
564    
565          public XMapPane() {          public XMapPane() {
566                  this(null, null);                  this(null, null);
567          }          }
# Line 573  public class XMapPane extends JPanel { Line 570  public class XMapPane extends JPanel {
570           * full constructor extending JPanel           * full constructor extending JPanel
571           *           *
572           * @param rendererHints           * @param rendererHints
573             *            may be <code>null</code>. Otherwise a {@link Map<Object,
574             *            Object>} of {@link RenderingHints} to override the default
575             *            from {@link GTUtil#getDefaultGTRendererHints(GTRenderer)}
576           *           *
          * @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  
577           * @param localContext           * @param localContext
578           *            - {@link MapContext} of layer to render.           *            The main {@link MapContext} to use. If <code>null</code>, an
579             *            empty {@link DefaultMapContext} will be created.
580           */           */
581          public XMapPane(final MapContext localContext_,          public XMapPane(final MapContext localContext_,
582                          final Map<Object, Object> rendererHints) {                          final Map<Object, Object> rendererHints) {
# Line 591  public class XMapPane extends JPanel { Line 586  public class XMapPane extends JPanel {
586    
587                  setOpaque(true);                  setOpaque(true);
588    
                 localRenderer.setJava2DHints(getJava2dHints());  
   
589                  if (localContext_ != null)                  if (localContext_ != null)
590                          setLocalContext(localContext_);                          setLocalContext(localContext_);
591    
# Line 610  public class XMapPane extends JPanel { Line 603  public class XMapPane extends JPanel {
603                   * Using a ComponentListener doesn't work because, unlike a JFrame, the                   * Using a ComponentListener doesn't work because, unlike a JFrame, the
604                   * pane receives a stream of events during drag-resizing.                   * pane receives a stream of events during drag-resizing.
605                   */                   */
606                  resizingPaintDelay = DEFAULT_RESIZING_PAINT_DELAY;                  resizeTimer = new Timer(DEFAULT_RESIZING_PAINT_DELAY,
607                  resizeTimer = new Timer(resizingPaintDelay, new ActionListener() {                                  new ActionListener() {
   
                         public void actionPerformed(final ActionEvent e) {  
                                 paneResized = true;  
   
                                 if (!isWellDefined())  
                                         return;  
   
                                 final Rectangle bounds = getVisibleRect();  
608    
609                                  final Envelope geoMapArea = tranformWindowToGeo(bounds.x,                                          public void actionPerformed(final ActionEvent e) {
610                                                  bounds.y, bounds.x + bounds.width, bounds.y                                                  if (!isWellDefined())
611                                                                  + bounds.height);                                                          return;
612    
613                                  setMapArea(bestAllowedMapArea(geoMapArea));                                                  final Rectangle bounds = getVisibleRect();
614                          }                                                  //
615                  });                                                  // System.out.println("\n\ntimer performs with bounds = "
616                                                    // + bounds);
617    
618                                                    final Envelope geoMapArea = tranformWindowToGeo(
619                                                                    bounds.x, bounds.y, bounds.x + bounds.width,
620                                                                    bounds.y + bounds.height);
621    
622                                                    if (setMapArea(geoMapArea))
623                                                            paneResized = true;
624                                                    // else
625                                                    // syso
626                                            }
627                                    });
628                  resizeTimer.setRepeats(false);                  resizeTimer.setRepeats(false);
629    
630                  this.addComponentListener(new ComponentAdapter() {                  this.addComponentListener(new ComponentAdapter() {
631    
632                          @Override                          @Override
633                          public void componentResized(final ComponentEvent e) {                          public void componentResized(final ComponentEvent e) {
634    
635                                    // Already cancel the tasks...
636                                  if (bgExecuter != null)                                  if (bgExecuter != null)
637                                          bgExecuter.cancelTask();                                          bgExecuter.cancelTask();
638                                  if (localExecuter != null)                                  if (localExecuter != null)
639                                          localExecuter.cancelTask();                                          localExecuter.cancelTask();
640    
641                                  resizeTimer.restart();                                  resizeTimer.restart();
642                          }                          }
643    
# Line 645  public class XMapPane extends JPanel { Line 646  public class XMapPane extends JPanel {
646                  /*                  /*
647                   * Setting up the repaintTimer. Not started automatically.                   * Setting up the repaintTimer. Not started automatically.
648                   */                   */
649                  repaintTimer = new Timer((int) REPEATING_REPAINT_DELAY,                  repaintTimer = new Timer(REPEATING_REPAINT_DELAY, new ActionListener() {
                                 new ActionListener() {  
650    
651                                          @Override                          @Override
652                                          public void actionPerformed(final ActionEvent e) {                          public void actionPerformed(final ActionEvent e) {
653                                                  updateFinalImage();                                  if ((localExecuter != null && !localExecuter.isRunning())
654                                                  XMapPane.this.repaint();                                                  && (bgExecuter != null && !bgExecuter.isRunning())) {
655                                          }                                          repaintTimer.stop();
656                                  });                                  } else {
657                                            updateFinalImage();
658                                            XMapPane.this.repaint(100);
659                                    }
660                            }
661                    });
662                  repaintTimer.setInitialDelay(INITIAL_REPAINT_DELAY);                  repaintTimer.setInitialDelay(INITIAL_REPAINT_DELAY);
663                  repaintTimer.setRepeats(true);                  repaintTimer.setRepeats(true);
664    
# Line 671  public class XMapPane extends JPanel { Line 676  public class XMapPane extends JPanel {
676                                                  if (localExecuter.isRunning()) {                                                  if (localExecuter.isRunning()) {
677                                                          localExecuter.cancelTask();                                                          localExecuter.cancelTask();
678                                                  } else {                                                  } else {
679    
680                                                            // Stupidly, but we have to recheck the
681                                                            setMapArea(getMapArea());
682                                                          requestStartRendering = false;                                                          requestStartRendering = false;
683                                                          startRendering();                                                          startRendering();
684                                                  }                                                  }
# Line 703  public class XMapPane extends JPanel { Line 711  public class XMapPane extends JPanel {
711           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons
712           *         Kr&uuml;ger</a>           *         Kr&uuml;ger</a>
713           */           */
714          public Envelope bestAllowedMapArea(Envelope env) {          public ReferencedEnvelope bestAllowedMapArea(ReferencedEnvelope env) {
715    
716                  if (getWidth() == 0)                  if (getWidth() == 0)
717                          return env;                          return env;
718    
719                  if (env == null)                  if (env == null)
720                          return null;                          return null;
721    
# Line 755  public class XMapPane extends JPanel { Line 765  public class XMapPane extends JPanel {
765                  }                  }
766    
767                  final Envelope maxAllowedExtend = getMaxExtend();                  final Envelope maxAllowedExtend = getMaxExtend();
768                    
769                  while (maxAllowedExtend != null && !maxAllowedExtend.contains(newArea)                  while (maxAllowedExtend != null && !maxAllowedExtend.contains(newArea)
770                                  && newArea != null && !newArea.isNull()                                  && newArea != null && !newArea.isNull()
771                                  && !Double.isNaN(newArea.getMinX())                                  && !Double.isNaN(newArea.getMinX())
# Line 784  public class XMapPane extends JPanel { Line 795  public class XMapPane extends JPanel {
795    
796                                          // LOGGER.debug("and fix aspect ratio");                                          // LOGGER.debug("and fix aspect ratio");
797    
798                                          newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,                                          newArea = JTSUtil.fixAspectRatio(getVisibleRect(),
799                                                          false);                                                          new ReferencedEnvelope(newArea, env
800                                                                            .getCoordinateReferenceSystem()), false);
801                                  }                                  }
802                          }                          }
803    
# Line 808  public class XMapPane extends JPanel { Line 820  public class XMapPane extends JPanel {
820    
821                                          // LOGGER.debug("and fix aspect ratio");                                          // LOGGER.debug("and fix aspect ratio");
822    
823                                          newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,                                          newArea = JTSUtil.fixAspectRatio(getVisibleRect(),
824                                                          false);                                                          new ReferencedEnvelope(newArea, env
825                                                                            .getCoordinateReferenceSystem()), false);
826                                  }                                  }
827                          }                          }
828    
# Line 834  public class XMapPane extends JPanel { Line 847  public class XMapPane extends JPanel {
847    
848                                          // LOGGER.debug("and fix aspect ratio");                                          // LOGGER.debug("and fix aspect ratio");
849    
850                                          newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,                                          newArea = JTSUtil.fixAspectRatio(getVisibleRect(),
851                                                          false);                                                          new ReferencedEnvelope(newArea, env
852                                                                            .getCoordinateReferenceSystem()), false);
853                                  }                                  }
854                          }                          }
855    
# Line 860  public class XMapPane extends JPanel { Line 874  public class XMapPane extends JPanel {
874    
875                                          // LOGGER.debug("and fix aspect ratio");                                          // LOGGER.debug("and fix aspect ratio");
876    
877                                          newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,                                          newArea = JTSUtil.fixAspectRatio(getVisibleRect(),
878                                                          false);                                                          new ReferencedEnvelope(newArea, env
879                                                                            .getCoordinateReferenceSystem()), false);
880                                  }                                  }
881                          }                          }
882                  }                  }
883    
884                  return newArea;                  return new ReferencedEnvelope(newArea, env
885                                    .getCoordinateReferenceSystem());
886          }          }
887    
888          /**          /**
# Line 901  public class XMapPane extends JPanel { Line 917  public class XMapPane extends JPanel {
917                                  try {                                  try {
918                                          Thread.sleep(200);                                          Thread.sleep(200);
919                                  } catch (final InterruptedException e) {                                  } catch (final InterruptedException e) {
920                                          // TODO Auto-generated catch block                                          LOGGER
921                                          e.printStackTrace();                                                          .warn(
922                                                                            "while XMapPane we are waiting for the localExcutor to stop",
923                                                                            e);
924                                  }                                  }
925                          }                          }
926                          if (localExecuter.isRunning()) {                          if (localExecuter.isRunning()) {
927                                  System.out                                  LOGGER
928                                                  .println("BAD BAD BAD... still running the thread....");                                                  .warn("localExecutor Thread still running after 2s! Continuing anyways...");
929                          }                          }
930                          localExecuter.dispose();                          localExecuter.dispose();
931                  }                  }
                 //  
                 // if (bgImage != null) {  
                 // bgImage = dispose(bgImage);  
                 // bgImage = null;  
                 // // LangUtil.gcTotal();  
                 // }  
                 //  
                 // if (localImage != null) {  
                 // localImage = dispose(localImage);  
                 // localImage = null;  
                 // // LangUtil.gcTotal();  
                 // }  
                 //  
                 // if (finalImage != null) {  
                 // finalImage = dispose(finalImage);  
                 // finalImage = null;  
                 // // LangUtil.gcTotal();  
                 // }  
                 //  
                 // if (preFinalImage != null) {  
                 // preFinalImage = dispose(preFinalImage);  
                 // preFinalImage = null;  
                 // }  
                 disposeImages();  
932    
933                  // LangUtil.gcTotal();                  disposeImages();
934    
935                  // Alle mapPaneListener entfernen                  // Remove all mapPaneListeners that have registered with us
936                  mapPaneListeners.clear();                  mapPaneListeners.clear();
937    
938                  removeMouseMotionListener(zoomMapPaneMouseListener);                  removeMouseMotionListener(zoomMapPaneMouseListener);
939                  removeMouseListener(zoomMapPaneMouseListener);                  removeMouseListener(zoomMapPaneMouseListener);
940    
941                  if (localContext != null)                  if (localContext != null)
942                          getContext().clearLayerList();                          getMapContext().clearLayerList();
943                  if (bgContext != null)                  if (bgContext != null)
944                          getBgContext().clearLayerList();                          getBgContext().clearLayerList();
945    
# Line 991  public class XMapPane extends JPanel { Line 985  public class XMapPane extends JPanel {
985           */           */
986          protected boolean drawScaledPreviewImage_Zoom(final Graphics2D graphics) {          protected boolean drawScaledPreviewImage_Zoom(final Graphics2D graphics) {
987    
988                    // if (1 == 1)return false;
989    
990                  if (quickPreviewHint == 0)                  if (quickPreviewHint == 0)
991                          return false;                          return false;
992    
# Line 1009  public class XMapPane extends JPanel { Line 1005  public class XMapPane extends JPanel {
1005                  // Calculate the oldMapArea in the current WindowCoordinates:                  // Calculate the oldMapArea in the current WindowCoordinates:
1006                  final Envelope oldMapWindow = tranformGeoToWindow(oldMapArea.getMinX(),                  final Envelope oldMapWindow = tranformGeoToWindow(oldMapArea.getMinX(),
1007                                  oldMapArea.getMinY(), oldMapArea.getMaxX(), oldMapArea                                  oldMapArea.getMinY(), oldMapArea.getMaxX(), oldMapArea
1008                                                  .getMaxY(), null);                                                  .getMaxY());
1009    
1010                  final int xx1 = (int) Math.round(oldMapWindow.getMinX());                  final int xx1 = (int) Math.round(oldMapWindow.getMinX());
1011                  final int yy1 = (int) Math.round(oldMapWindow.getMinY());                  final int yy1 = (int) Math.round(oldMapWindow.getMinY());
# Line 1026  public class XMapPane extends JPanel { Line 1022  public class XMapPane extends JPanel {
1022    
1023                  SwingUtil.clearAround(graphics, painedArea, visibleArea);                  SwingUtil.clearAround(graphics, painedArea, visibleArea);
1024    
1025                  addGadgets(graphics);                  addGadgets(graphics, true);
1026    
1027                  quickPreviewHint = 0;                  quickPreviewHint = 0;
1028    
1029                    repaintTimer.restart();
1030    
1031                  graphics.dispose();                  graphics.dispose();
1032    
1033                  // Something has been drawn                  // Something has been drawn
# Line 1052  public class XMapPane extends JPanel { Line 1050  public class XMapPane extends JPanel {
1050                  return bgImage;                  return bgImage;
1051          }          }
1052    
1053          public MapContext getContext() {          public MapContext getMapContext() {
1054                  if (localContext == null) {                  if (localContext == null) {
1055                          setLocalContext(new DefaultMapContext());                          setLocalContext(new DefaultMapContext());
1056                  }                  }
# Line 1063  public class XMapPane extends JPanel { Line 1061  public class XMapPane extends JPanel {
1061                  //                  //
1062                  if (finalImage == null) {                  if (finalImage == null) {
1063                          // Rectangle curPaintArea = getVisibleRect();                          // Rectangle curPaintArea = getVisibleRect();
1064                          finalImage = new BufferedImage(getBounds().width,                          finalImage = new BufferedImage(getVisibleRect().width,
1065                                          getBounds().height, IMAGETYPE);                                          getVisibleRect().height, IMAGETYPE);
1066    
1067                          requestStartRendering();                          requestStartRendering();
1068                  }                  }
# Line 1081  public class XMapPane extends JPanel { Line 1079  public class XMapPane extends JPanel {
1079          private BufferedImage getLocalImage() {          private BufferedImage getLocalImage() {
1080    
1081                  if (localImage == null) {                  if (localImage == null) {
1082                          localImage = new BufferedImage(getBounds().width,                          localImage = new BufferedImage(getVisibleRect().width,
1083                                          getBounds().height, IMAGETYPE_withAlpha);                                          getVisibleRect().height, IMAGETYPE_withAlpha);
1084                  }                  }
1085    
1086                  return localImage;                  return localImage;
# Line 1093  public class XMapPane extends JPanel { Line 1091  public class XMapPane extends JPanel {
1091           *           *
1092           * @return           * @return
1093           */           */
1094          public Envelope getMapArea() {          public ReferencedEnvelope getMapArea() {
1095                  if (mapArea == null) {                  if (mapArea == null) {
1096                          ReferencedEnvelope mapArea_ = null;                          ReferencedEnvelope mapArea_ = null;
1097                          try {                          try {
1098                                  mapArea_ = localContext.getLayerBounds();                                  mapArea_ = localContext.getLayerBounds();
1099                          } catch (final IOException e) {                          } catch (final IOException e) {
1100                                  LOGGER.warn("context.getLayerBounds()", e);                                  LOGGER.warn("localContext.getLayerBounds()", e);
1101                            }
1102    
1103                            if (mapArea_ == null && bgContext != null) {
1104                                    try {
1105                                            mapArea_ = bgContext.getLayerBounds();
1106                                    } catch (final IOException e) {
1107                                            LOGGER.warn("bgContext.getLayerBounds()", e);
1108                                    }
1109                          }                          }
1110    
1111                          if (mapArea_ != null) {                          if (mapArea_ != null) {
                                 mapImageInvalid = true; /* note we need to redraw */  
 //                              setMapArea(mapArea_); // results in a loop  
1112                                  mapArea = bestAllowedMapArea(mapArea_);                                  mapArea = bestAllowedMapArea(mapArea_);
1113                                    requestStartRendering();
1114                          }                          }
1115                  }                  }
1116    
1117                  if (mapArea == null)                  if (mapArea == null)
1118                          return null;                          return null;
1119    
1120                  return new Envelope(mapArea);                  // TODO is needed at all, this should go to setMapArea maybe
1121                    if (localContext.getCoordinateReferenceSystem() == null)
1122                            try {
1123                                    localContext.setCoordinateReferenceSystem(GeoImportUtil
1124                                                    .getDefaultCRS());
1125                            } catch (Exception e) {
1126                                    throw new RuntimeException("setting context CRS:", e);
1127                            }
1128    
1129                    return new ReferencedEnvelope(mapArea, localContext
1130                                    .getCoordinateReferenceSystem());
1131          }          }
1132    
1133          /**          /**
# Line 1145  public class XMapPane extends JPanel { Line 1160  public class XMapPane extends JPanel {
1160    
1161          public Envelope getMaxExtend() {          public Envelope getMaxExtend() {
1162                  if (maxExtend == null) {                  if (maxExtend == null) {
1163                          final ReferencedEnvelope layerBounds = GTUtil                          // Commented-out because it takes soo much time!
1164                                          .getVisibleLayoutBounds(localContext);  //
1165                          if (layerBounds == null) {  //                      long start = System.currentTimeMillis();
1166                                  // TODO Last fallback could be the CRS valid area  //                      final ReferencedEnvelope layerBounds = GTUtil
1167                                  return null;  //                                      .getVisibleLayoutBounds(localContext);
1168                          }  //                      
1169    //                      LOGGER.info( (System.currentTimeMillis()-start)+"m to get maxExtend");
1170                          // Kartenbereich um 10% vergroessern  //                      
1171                          return JTSUtil.fixAspectRatio(this.getBounds(), JTSUtil  //                      if (layerBounds == null) {
1172                                          .expandEnvelope(layerBounds, 0.1), true);  //                              // TODO Last fallback could be the CRS valid area
1173    //                              return null;
1174    //                      }
1175    //
1176    //                      // Kartenbereich um 10% vergroessern
1177    //                      return JTSUtil.fixAspectRatio(getVisibleRect(), JTSUtil
1178    //                                      .expandEnvelope(layerBounds, 0.1), true);
1179                  }                  }
1180                  return maxExtend;                  return maxExtend;
1181          }          }
# Line 1196  public class XMapPane extends JPanel { Line 1217  public class XMapPane extends JPanel {
1217          }          }
1218    
1219          public Map<Object, Object> getRendererHints() {          public Map<Object, Object> getRendererHints() {
1220                    // Clear label cache
1221                    labelCache.clear();
1222                    rendererHints.put(StreamingRenderer.LABEL_CACHE_KEY, labelCache);
1223    
1224                  return rendererHints;                  return rendererHints;
1225          }          }
1226    
# Line 1264  public class XMapPane extends JPanel { Line 1289  public class XMapPane extends JPanel {
1289           * {@link XMapPane} is visible and has bounds set.           * {@link XMapPane} is visible and has bounds set.
1290           */           */
1291          public boolean isWellDefined() {          public boolean isWellDefined() {
   
1292                  try {                  try {
1293                            if (getMapContext() == null)
                         if (getContext() == null)  
                                 return false;  
                         if (getContext().getLayerCount() <= 0)  
1294                                  return false;                                  return false;
1295                          if (getMapArea() == null)                          if (getMapContext().getLayerCount() <= 0)
1296                                  return false;                                  return false;
1297                          if (getBounds().getWidth() == 0)                          if (getVisibleRect().getWidth() == 0)
1298                                  return false;                                  return false;
1299                          if (getBounds().getHeight() == 0)                          if (getVisibleRect().getHeight() == 0)
1300                                  return false;                                  return false;
1301                            // if (getMapArea() == null)
1302                            // return false;
1303                  } catch (final Exception e) {                  } catch (final Exception e) {
1304                          return false;                          return false;
1305                  }                  }
# Line 1364  public class XMapPane extends JPanel { Line 1387  public class XMapPane extends JPanel {
1387           * Called by the {@link RenderingExecutor} when rendering was cancelled.           * Called by the {@link RenderingExecutor} when rendering was cancelled.
1388           */           */
1389          public void onRenderingCancelled() {          public void onRenderingCancelled() {
                 repaintTimer.stop();  
1390                  LOGGER.debug("Rendering cancelled");                  LOGGER.debug("Rendering cancelled");
1391                    repaintTimer.stop();
1392          }          }
1393    
1394          /**          /**
# Line 1376  public class XMapPane extends JPanel { Line 1399  public class XMapPane extends JPanel {
1399                  repaintTimer.stop();                  repaintTimer.stop();
1400                  updateFinalImage();                  updateFinalImage();
1401                  repaint();                  repaint();
1402                    if (renderingErrors.size() > 0)
1403                            renderingErrors.remove(0);
1404          }          }
1405    
1406          /**          /**
# Line 1389  public class XMapPane extends JPanel { Line 1414  public class XMapPane extends JPanel {
1414           * @see MapPaneListener#onRenderingStopped(org.geotools.swing.event.MapPaneEvent)           * @see MapPaneListener#onRenderingStopped(org.geotools.swing.event.MapPaneEvent)
1415           */           */
1416          public void onRenderingFailed(final Exception renderingError) {          public void onRenderingFailed(final Exception renderingError) {
1417    
1418                    // Store the exceptions so we can show it to the user:
1419                    if (!(renderingError instanceof java.lang.IllegalArgumentException
1420                                    && renderingError.getMessage().equals(
1421                                                    "Argument \"sourceCRS\" should not be null.")))
1422                            this.renderingErrors.add(renderingError);
1423                    if (renderingErrors.size() > 3)
1424                            renderingErrors.remove(0);
1425                    
1426                  repaintTimer.stop();                  repaintTimer.stop();
1427                  LOGGER.warn("Rendering failed", renderingError);                  LOGGER.warn("Rendering failed", renderingError);
1428                  updateFinalImage();                  updateFinalImage();
# Line 1404  public class XMapPane extends JPanel { Line 1438  public class XMapPane extends JPanel {
1438    
1439          @Override          @Override
1440          protected void paintComponent(final Graphics g) {          protected void paintComponent(final Graphics g) {
1441    
1442                  if (!acceptsRepaintCalls)                  if (!acceptsRepaintCalls)
1443                          return;                          return;
1444    
1445                  // Maybe update the cursor and maybe stop the repainting timer                  // Maybe update the cursor and maybe stop the repainting timer
1446                  updateCursor();                  updateCursor();
1447    
1448                  super.paintComponent(g); // candidate for removal                  // super.paintComponent(g); // candidate for removal
1449    
1450                  boolean paintedSomething = false;                  boolean paintedSomething = false;
1451    
# Line 1423  public class XMapPane extends JPanel { Line 1458  public class XMapPane extends JPanel {
1458                          // happening.                          // happening.
1459                          if (mapAreaChanged && oldMapArea != null                          if (mapAreaChanged && oldMapArea != null
1460                                          && getMapArea().intersects(oldMapArea)                                          && getMapArea().intersects(oldMapArea)
1461                                          & !getMapArea().equals(oldMapArea)) {                                          & !getMapArea().equals(oldMapArea) && !paneResized) {
1462    
1463                                  mapAreaChanged = false;                                  mapAreaChanged = false;
1464    
# Line 1437  public class XMapPane extends JPanel { Line 1472  public class XMapPane extends JPanel {
1472    
1473                          }                          }
1474    
1475                          if (paneResized) {                          // if (paneResized) {
1476                                  paneResized = false;                          // paneResized = false;
1477                                  disposeImages();                          // disposeImages();
1478                          }                          // }
1479                            //
1480                          // Start the Threads and Timers to render the image                          // // Start the Threads and Timers to render the image
1481                          requestStartRendering();                          // requestStartRendering();
1482    
1483                  }                  }
1484    
# Line 1508  public class XMapPane extends JPanel { Line 1543  public class XMapPane extends JPanel {
1543           */           */
1544          public void performPan() {          public void performPan() {
1545    
1546                  final Rectangle winBounds = getBounds();                  Rectangle winBounds = getVisibleRect();
1547    
1548                  winBounds.translate(-imageOrigin.x, -imageOrigin.y);                  winBounds.translate(-imageOrigin.x, -imageOrigin.y);
1549                  final Envelope newMapArea = tranformWindowToGeo(winBounds.x,                  final Envelope newMapArea = tranformWindowToGeo(winBounds.x,
1550                                  winBounds.y, winBounds.x + winBounds.width, winBounds.y                                  winBounds.y, winBounds.x + winBounds.width, winBounds.y
# Line 1573  public class XMapPane extends JPanel { Line 1609  public class XMapPane extends JPanel {
1609                          bgExecuter.cancelTask();                          bgExecuter.cancelTask();
1610                  if (localExecuter != null)                  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    
1622          /**          /**
1623           * Berechnet die Transformation zwischen Fenster- und Karten-Koordinaten           * Calculate the affine transforms used to convert between world and pixel
1624           * neu.           * coordinates. The calculations here are very basic and assume a cartesian
1625           */           * reference system.
1626          protected void resetTransforms() {           * <p>
1627                  if (getMapArea() == null || getWidth() == 0 || getHeight() == 0)           * Tne transform is calculated such that {@code envelope} will be centred in
1628                          return;           * the display
1629             *
1630             * @param envelope
1631             *            the current map extent (world coordinates)
1632             * @param paintArea
1633             *            the current map pane extent (screen units)
1634             */
1635            private void resetTransforms() {
1636                    ReferencedEnvelope refMapEnv = new ReferencedEnvelope(mapArea,
1637                                    getMapContext().getCoordinateReferenceSystem());
1638    
1639                  // We store the last Transform                  // System.out
1640                  oldScreenToWorld = screenToWorld;                  // .println("paintArea in resetTeansofrms = " + getVisibleRect());
1641                    if (!isWellDefined())
1642                            return;
1643    
1644                  this.screenToWorld = new AffineTransform(                  worldToScreen = RendererUtilities.worldToScreenTransform(refMapEnv,
1645                  // Genauso wie die Fenster-Koordinaten, werden die Longitude-Koordinaten                                  getVisibleRect());
                                 // 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());  
1646    
1647                  try {                  try {
1648                          this.worldToScreen = screenToWorld.createInverse();                          screenToWorld = worldToScreen.createInverse();
1649                  } catch (final NoninvertibleTransformException e) {  
1650                          LOGGER.error(e);                  } catch (NoninvertibleTransformException ex) {
1651                            LOGGER
1652                                            .error("can't invert worldToScreen to get screenToWorld!",
1653                                                            ex);
1654                  }                  }
1655          }          }
1656    
# Line 1629  public class XMapPane extends JPanel { Line 1669  public class XMapPane extends JPanel {
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    
# Line 1638  public class XMapPane extends JPanel { Line 1678  public class XMapPane extends JPanel {
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) {
# Line 1665  public class XMapPane extends JPanel { Line 1701  public class XMapPane extends JPanel {
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    
# Line 1677  public class XMapPane extends JPanel { Line 1713  public class XMapPane extends JPanel {
1713                          }                          }
1714                  }                  }
1715    
1716                  mapImageInvalid = true;                  requestStartRendering();
1717                  repaint();  
1718            }
1719    
1720            public boolean setMapArea(final Envelope newMapArea) {
1721                    return setMapArea(new ReferencedEnvelope(newMapArea, getMapContext()
1722                                    .getCoordinateReferenceSystem()));
1723          }          }
1724    
1725          /**          /**
# Line 1686  public class XMapPane extends JPanel { Line 1727  public class XMapPane extends JPanel {
1727           * @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
1728           *         has been triggered.           *         has been triggered.
1729           */           */
1730          public boolean setMapArea(final Envelope newMapArea) {          public boolean setMapArea(final ReferencedEnvelope newMapArea) {
1731                    //              
1732                    // if (getVisibleRect().getWidth() == 0 || (!isVisible()) ||
1733                    // getVisibleRect().getHeight() == 0) {
1734                    // // The window is not yet defined.. so we can not really determine the
1735                    // asepect ratio for the mapPane. We store this as a candidate mappane
1736                    // and handle it when the component resizes or becomes visible)
1737                    // mapAreaCondidate = newMapArea;
1738                    // }
1739    
1740                  if (newMapArea == null                  if (newMapArea == null
1741                                  || bestAllowedMapArea(newMapArea).equals(mapArea)) {                                  || bestAllowedMapArea(newMapArea).equals(mapArea)) {
# Line 1705  public class XMapPane extends JPanel { Line 1754  public class XMapPane extends JPanel {
1754                          return false;                          return false;
1755                  }                  }
1756    
1757                    final Envelope candNew = bestAllowedMapArea(newMapArea);
1758    
1759                  // Testing, whether the difference if just minimal                  // Testing, whether the difference if just minimal
1760                  if (mapArea != null) {                  if (mapArea != null) {
                         final Envelope candNew = bestAllowedMapArea(newMapArea);  
1761                          final double tolX = mapArea.getWidth() / 1000.;                          final double tolX = mapArea.getWidth() / 1000.;
1762                          final double tolY = mapArea.getHeight() / 1000.;                          final double tolY = mapArea.getHeight() / 1000.;
1763                          if ((candNew.getMinX() - tolX < mapArea.getMinX())                          if ((candNew.getMinX() - tolX < mapArea.getMinX())
# Line 1727  public class XMapPane extends JPanel { Line 1777  public class XMapPane extends JPanel {
1777                          }                          }
1778                  }                  }
1779    
1780                    // New map are is accepted:
1781                  oldMapArea = mapArea;                  oldMapArea = mapArea;
1782                    mapArea = candNew;
1783                  this.mapArea = bestAllowedMapArea(newMapArea);                  resetTransforms();
1784    
1785                  if (localContext != null) {                  if (localContext != null) {
1786                          localContext.setAreaOfInterest(mapArea, localContext                          localContext.setAreaOfInterest(mapArea, localContext
# Line 1739  public class XMapPane extends JPanel { Line 1790  public class XMapPane extends JPanel {
1790                          bgContext.setAreaOfInterest(mapArea, localContext                          bgContext.setAreaOfInterest(mapArea, localContext
1791                                          .getCoordinateReferenceSystem());                                          .getCoordinateReferenceSystem());
1792                  }                  }
1793                  resetTransforms();  
                 mapImageInvalid = true;  
1794                  mapAreaChanged = true;                  mapAreaChanged = true;
                 repaint();  
1795    
1796                  // LOGGER.debug("New maparea = " + mapArea);                  repaint(100);
1797    
1798                    requestStartRendering();
1799    
1800                  return true;                  return true;
1801          }          }
1802    
# Line 1853  public class XMapPane extends JPanel { Line 1905  public class XMapPane extends JPanel {
1905          }          }
1906    
1907          private void setRendererHints(final Map<Object, Object> rendererHints) {          private void setRendererHints(final Map<Object, Object> rendererHints) {
1908                  this.rendererHints = rendererHints;                  if (rendererHints != null)
1909                            this.rendererHints = rendererHints;
1910          }          }
1911    
1912          /**          /**
# Line 1893  public class XMapPane extends JPanel { Line 1946  public class XMapPane extends JPanel {
1946           */           */
1947          private void startRendering() {          private void startRendering() {
1948    
1949                  if (!isWellDefined())                  if (!isWellDefined() || !acceptsRepaintCalls) {
1950                            requestStartRendering = true;
1951                          return;                          return;
1952                    }
1953    
1954                  if (bgExecuter != null) {                  if (bgExecuter != null) {
1955                          // Stop all renderers                          // Stop all renderers
# Line 1912  public class XMapPane extends JPanel { Line 1967  public class XMapPane extends JPanel {
1967                   */                   */
1968    
1969                  if (getBgContext() != null) {                  if (getBgContext() != null) {
1970                            bgRenderer.setJava2DHints(getJava2dHints());
1971                            bgRenderer.setRendererHints(getRendererHints());
1972    
1973                          // bgExecuter = new RenderingExecutor();                          // bgExecuter = new RenderingExecutor();
1974                          // LOGGER.debug("starting bg renderer:");                          // LOGGER.debug("starting bg renderer:");
1975                          // // /* System.out.println("rendering"); */                          // // /* System.out.println("rendering"); */
# Line 1923  public class XMapPane extends JPanel { Line 1981  public class XMapPane extends JPanel {
1981                          // (Graphics2D) getBgImage().getGraphics(), createGTRenderer);                          // (Graphics2D) getBgImage().getGraphics(), createGTRenderer);
1982                  }                  }
1983    
1984                  if (getContext() != null) {                  if (getMapContext() != null) {
1985                          // localExecuter = new RenderingExecutor(this, 150l);                          // localExecuter = new RenderingExecutor(this, 150l);
1986                          // LOGGER.debug("starting local renderer:");                          // LOGGER.debug("starting local renderer:");
1987    
1988                          // Clear label cache                          localRenderer.setJava2DHints(getJava2dHints());
1989                          labelCache.clear();                          localRenderer.setRendererHints(getRendererHints());
1990                          final Map<Object, Object> rh = localRenderer.getRendererHints();  
1991                          rh.put(StreamingRenderer.LABEL_CACHE_KEY, labelCache);                          final boolean submitted = localExecuter.submit(getMapArea(),
1992                          localRenderer.setRendererHints(rh);                                          curPaintArea, (Graphics2D) getLocalImage().getGraphics(),
1993                                            localRenderer
1994                          final boolean submitted = localExecuter.submit(getContext()                          // , getWorldToScreenTransform()
1995                                          .getAreaOfInterest(), curPaintArea,                                          );
                                         (Graphics2D) getLocalImage().getGraphics(), localRenderer,  
                                         getWorldToScreenTransform());  
1996                          if (submitted)                          if (submitted)
1997                                  repaintTimer.restart();                                  repaintTimer.restart();
1998                          else                          else
1999                                  requestStartRendering = true; // Try to start rendering again in                                  requestStartRendering = true; // Try to start rendering
2000                            // again in
2001                          // a moment                          // a moment
2002                  }                  }
2003    
# Line 1963  public class XMapPane extends JPanel { Line 2020  public class XMapPane extends JPanel {
2020           *            {@link #getScreenToWorld()} is used.           *            {@link #getScreenToWorld()} is used.
2021           */           */
2022          public Envelope tranformGeoToWindow(final double ox, final double oy,          public Envelope tranformGeoToWindow(final double ox, final double oy,
2023                          final double px, final double py,                          final double px, final double py) {
2024                          final AffineTransform winToGeotransform) {                  final AffineTransform at = getWorldToScreenTransform();
                 final AffineTransform at = winToGeotransform == null ? getScreenToWorld()  
                                 : winToGeotransform;  
2025                  Point2D geoO;                  Point2D geoO;
2026                  try {                  // try {
2027                          geoO = at.inverseTransform(new Point2D.Double(ox, oy), null);                  geoO = at.transform(new Point2D.Double(ox, oy), null);
2028                          final Point2D geoP = at.inverseTransform(                  final Point2D geoP = at.transform(new Point2D.Double(px, py), null);
2029                                          new Point2D.Double(px, py), null);                  return new Envelope(geoO.getX(), geoP.getX(), geoO.getY(), geoP.getY());
2030                          return new Envelope(geoO.getX(), geoP.getX(), geoO.getY(), geoP                  // } catch (final NoninvertibleTransformException e) {
2031                                          .getY());                  // LOGGER.error(e);
2032                  } catch (final NoninvertibleTransformException e) {                  // return new Envelope(ox, oy, px, py);
2033                          LOGGER.error(e);                  // }
                         return new Envelope(ox, oy, px, py);  
                 }  
2034          }          }
2035    
2036          /**          /**
# Line 1997  public class XMapPane extends JPanel { Line 2050  public class XMapPane extends JPanel {
2050                  final AffineTransform at = getScreenToWorld();                  final AffineTransform at = getScreenToWorld();
2051                  final Point2D geoO = at.transform(new Point2D.Double(ox, oy), null);                  final Point2D geoO = at.transform(new Point2D.Double(ox, oy), null);
2052                  final Point2D geoP = at.transform(new Point2D.Double(px, py), null);                  final Point2D geoP = at.transform(new Point2D.Double(px, py), null);
2053                  return new Envelope(geoO.getX(), geoP.getX(), geoO.getY(), geoP.getY());  
2054                    // Mmmmm... don't really understand why its x,x,y,y
2055                    // return new Envelope(geoO.getX(), geoP.getX(), geoO.getY(),
2056                    // geoP.getY());
2057                    return new Envelope(new Coordinate(geoO.getX(), geoO.getY()),
2058                                    new Coordinate(geoP.getX(), geoP.getY()));
2059          }          }
2060    
2061          /**          /**
# Line 2015  public class XMapPane extends JPanel { Line 2073  public class XMapPane extends JPanel {
2073                  } else {                  } else {
2074                          // Allow one last rendering                          // Allow one last rendering
2075                          if (repaintTimer.isRunning()) {                          if (repaintTimer.isRunning()) {
2076                                    // System.out.println("one last rendering....");
2077                                  repaintTimer.stop();                                  repaintTimer.stop();
2078                                  updateFinalImage();                                  updateFinalImage();
2079                                  repaint();                                  repaint();
# Line 2091  public class XMapPane extends JPanel { Line 2150  public class XMapPane extends JPanel {
2150                                  imageOrigin.y, finalImageWidth, finalImageHeight);                                  imageOrigin.y, finalImageWidth, finalImageHeight);
2151                  SwingUtil.clearAround(finalG, painedArea, getVisibleRect());                  SwingUtil.clearAround(finalG, painedArea, getVisibleRect());
2152    
2153                  addGadgets(finalG);                  addGadgets(finalG, false);
2154    
2155                  finalG.dispose();                  finalG.dispose();
2156    
# Line 2101  public class XMapPane extends JPanel { Line 2160  public class XMapPane extends JPanel {
2160          /**          /**
2161           * Paints some optional stuff into the given {@link Graphics2D}. Usually           * Paints some optional stuff into the given {@link Graphics2D}. Usually
2162           * called as the last layer when {@link #updateFinalImage()}           * called as the last layer when {@link #updateFinalImage()}
2163             *
2164             * @param forceWait
2165             *            if <code>true</code>, a Wait-message will be painted even
2166             *            though the rendering threads may not yet have started. If
2167             *            <code>false</code>, it will only depend on
2168             *            {@link #localExecuter.isRunning} and #bgExecuter.isRunning
2169           */           */
2170          private void addGadgets(final Graphics2D graphics) {          private void addGadgets(final Graphics2D graphics, boolean forceWait) {
2171    
2172                  // Paint a logo to the bottom right if available                  // Paint a logo to the bottom right if available
2173                  if (mapImage != null) {                  if (mapImage != null) {
2174                          graphics.drawImage(mapImage, getBounds().width                          Rectangle visibleRect = getVisibleRect();
2175                                          - mapImage.getWidth() - 10, getBounds().height                          graphics.drawImage(mapImage, visibleRect.width
2176                                            - mapImage.getWidth() - 10, getVisibleRect().height
2177                                          - mapImage.getHeight() - 10, null);                                          - mapImage.getHeight() - 10, null);
2178                  }                  }
2179    
2180                    int y = 17;
2181    
2182                  // If the rendering process is still running, indicate this is the image                  // If the rendering process is still running, indicate this is the image
2183                  if (bgExecuter != null && bgExecuter.isRunning()                  if (forceWait || bgExecuter != null && bgExecuter.isRunning()
2184                                  || localExecuter != null && localExecuter.isRunning()) {                                  || localExecuter != null && localExecuter.isRunning()) {
2185    
2186                            y += 8;
2187    
2188                          final Color c = graphics.getColor();                          final Color c = graphics.getColor();
2189                          graphics.setFont(waitFont);                          graphics.setFont(waitFont);
2190                          String waitStr = "Wait..."; //i8n  
2191                                                    graphics.setColor(getMapBackgroundColor());
2192                          graphics.setColor(Color.WHITE);                          graphics.drawString(waitMsg, 5, y);
2193                          graphics.drawString(waitStr, 39, 69);                          graphics.setColor(getMapBackgroundColor());
2194                            graphics.drawString(waitMsg, 7, y + 2);
2195                          graphics.setColor(Color.BLACK);                          graphics.setColor(Color.BLACK);
2196                          graphics.drawString(waitStr, 40, 70);                          graphics.drawString(waitMsg, 6, y + 1);
2197    
2198                            graphics.setColor(c);
2199    
2200                            y += 24;
2201                    }
2202    
2203                    if (renderingErrors != null && isShowExceptions()) {
2204    
2205                            final Color c = graphics.getColor();
2206                            graphics.setFont(errorFont);
2207    
2208                            for (Exception ex : renderingErrors) {
2209    
2210                                    String errStr = ex.getLocalizedMessage();
2211    
2212                                    if (errStr == null)
2213                                            errStr = ex.getMessage();
2214                                    if (errStr == null)
2215                                            errStr = "unknown error: " + ex.getClass().getSimpleName();
2216    
2217                                    graphics.setColor(Color.WHITE);
2218                                    graphics.drawString(errStr, 5, y);
2219                                    graphics.setColor(Color.RED);
2220                                    graphics.drawString(errStr, 6, y + 1);
2221    
2222                                    y += 19;
2223                            }
2224    
2225                          graphics.setColor(c);                          graphics.setColor(c);
2226                  }                  }
# Line 2130  public class XMapPane extends JPanel { Line 2228  public class XMapPane extends JPanel {
2228          }          }
2229    
2230          /**          /**
2231           * Sets the {@link #mapArea} to best possibly present the given features. If only one           * Sets the {@link #mapArea} to best possibly present the given features. If
2232           * single point is given, the window is moved over the point.           * only one single point is given, the window is moved over the point.
2233           *           *
2234           * @param features           * @param features
2235           *            if <code>null</code> or size==0, the function doesn nothing.           *            if <code>null</code> or size==0, the function doesn nothing.
# Line 2139  public class XMapPane extends JPanel { Line 2237  public class XMapPane extends JPanel {
2237          public void zoomTo(          public void zoomTo(
2238                          final FeatureCollection<SimpleFeatureType, SimpleFeature> features) {                          final FeatureCollection<SimpleFeatureType, SimpleFeature> features) {
2239    
2240                  final CoordinateReferenceSystem mapCRS = getContext()                  // if (!isWellDefined()) return;
2241    
2242                    final CoordinateReferenceSystem mapCRS = getMapContext()
2243                                  .getCoordinateReferenceSystem();                                  .getCoordinateReferenceSystem();
2244                  final CoordinateReferenceSystem fCRS = features.getSchema()                  final CoordinateReferenceSystem fCRS = features.getSchema()
2245                                  .getGeometryDescriptor().getCoordinateReferenceSystem();                                  .getGeometryDescriptor().getCoordinateReferenceSystem();
2246    
2247                  double width = mapArea.getWidth();                  ReferencedEnvelope _mapArea;
2248                  double height = mapArea.getHeight();                  if (mapArea == null)
2249                            _mapArea = features.getBounds();
2250                    else _mapArea = getMapArea();
2251                    double width = _mapArea.getWidth();
2252                    double height = _mapArea.getHeight();
2253                  final double ratio = height / width;                  final double ratio = height / width;
2254    
2255                  if (features == null || features.size() == 0) {                  if (features == null || features.size() == 0) {
# Line 2197  public class XMapPane extends JPanel { Line 2301  public class XMapPane extends JPanel {
2301                  } else {                  } else {
2302                          final ReferencedEnvelope fBounds = features.getBounds();                          final ReferencedEnvelope fBounds = features.getBounds();
2303    
2304                          Envelope bounds;                          ReferencedEnvelope bounds;
2305                          if (!mapCRS.equals(fCRS)) {                          if (!mapCRS.equals(fCRS)) {
2306                                  bounds = JTSUtil.transformEnvelope(fBounds, fCRS, mapCRS);                                  bounds = JTSUtil.transformEnvelope(fBounds, mapCRS);
2307                          } else {                          } else {
2308                                  bounds = fBounds;                                  bounds = fBounds;
2309                          }                          }
# Line 2255  public class XMapPane extends JPanel { Line 2359  public class XMapPane extends JPanel {
2359                                  .getWidth()) / 2., (mapArea.getHeight() * zoomFaktor - mapArea                                  .getWidth()) / 2., (mapArea.getHeight() * zoomFaktor - mapArea
2360                                  .getHeight()) / 2.);                                  .getHeight()) / 2.);
2361    
2362                  // Move the newMapArea above the new center                  // TODO we actually want that
2363                  newMapArea.translate(gcenter.getX() - mapArea.centre().x, gcenter                  // // Move the newMapArea above the new center
2364                                  .getY()                  // newMapArea.translate(gcenter.getX() - mapArea.centre().x, gcenter
2365                                  - mapArea.centre().y);                  // .getY()
2366                    // - mapArea.centre().y);
2367    
2368                  setMapArea(newMapArea);                  setMapArea(newMapArea);
2369          }          }
2370    
2371            /**
2372             * Shall non-fatal rendering exceptions be reported in the mappane or be
2373             * dropped quitely.
2374             */
2375            public void setShowExceptions(boolean showExceptions) {
2376                    this.showExceptions = showExceptions;
2377            }
2378    
2379            /**
2380             * Shall exceptions be reported in the mappane?
2381             */
2382            public boolean isShowExceptions() {
2383                    return showExceptions;
2384            }
2385    
2386  }  }

Legend:
Removed from v.531  
changed lines
  Added in v.553

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26