/[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 540 by alfonx, Fri Nov 20 19:34:50 2009 UTC revision 555 by alfonx, Tue Nov 24 09:17:22 2009 UTC
# Line 30  import javax.swing.Timer; Line 30  import javax.swing.Timer;
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;
# Line 99  public class XMapPane extends JPanel { Line 98  public class XMapPane extends JPanel {
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    
# Line 125  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 134  public class XMapPane extends JPanel { Line 133  public class XMapPane extends JPanel {
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          /**          /**
# Line 160  public class XMapPane extends JPanel { Line 172  public class XMapPane extends JPanel {
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)
# Line 183  public class XMapPane extends JPanel { Line 210  public class XMapPane extends JPanel {
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
# Line 249  public class XMapPane extends JPanel { Line 276  public class XMapPane extends JPanel {
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    
# Line 288  public class XMapPane extends JPanel { Line 296  public class XMapPane extends JPanel {
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           *           *
# Line 331  public class XMapPane extends JPanel { Line 349  public class XMapPane extends JPanel {
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          };          };
# Line 384  public class XMapPane extends JPanel { Line 395  public class XMapPane extends JPanel {
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    
# Line 422  public class XMapPane extends JPanel { Line 416  public class XMapPane extends JPanel {
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.
# Line 439  public class XMapPane extends JPanel { Line 429  public class XMapPane extends JPanel {
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          };          };
# Line 526  public class XMapPane extends JPanel { Line 510  public class XMapPane extends JPanel {
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)
# Line 576  public class XMapPane extends JPanel { Line 562  public class XMapPane extends JPanel {
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          }          }
# Line 585  public class XMapPane extends JPanel { Line 576  public class XMapPane extends JPanel {
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) {
# Line 620  public class XMapPane extends JPanel { Line 609  public class XMapPane extends JPanel {
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.info("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.info("  maparea changed");
632                                                            paneResized = true;
633                                                    } else
634                                                            LOGGER.info("  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.info("skipping resize.");
650                                            return;
651                                    }
652    
653                                    LOGGER.info("resized: " + getVisibleRect());
654                                  resizeTimer.restart();                                  resizeTimer.restart();
655                                    oldVisibleRect = getVisibleRect();
656                          }                          }
657    
658                  });                  });
# Line 654  public class XMapPane extends JPanel { Line 660  public class XMapPane extends JPanel {
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    
# Line 680  public class XMapPane extends JPanel { Line 692  public class XMapPane extends JPanel {
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                                                  }                                                  }
# Line 712  public class XMapPane extends JPanel { Line 726  public class XMapPane extends JPanel {
726           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons
727           *         Kr&uuml;ger</a>           *         Kr&uuml;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    
# Line 765  public class XMapPane extends JPanel { Line 780  public class XMapPane extends JPanel {
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())
# Line 794  public class XMapPane extends JPanel { Line 810  public class XMapPane extends JPanel {
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    
# Line 818  public class XMapPane extends JPanel { Line 835  public class XMapPane extends JPanel {
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    
# Line 844  public class XMapPane extends JPanel { Line 862  public class XMapPane extends JPanel {
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    
# Line 870  public class XMapPane extends JPanel { Line 889  public class XMapPane extends JPanel {
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          /**          /**
# Line 904  public class XMapPane extends JPanel { Line 925  public class XMapPane extends JPanel {
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    
# Line 979  public class XMapPane extends JPanel { Line 1000  public class XMapPane extends JPanel {
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    
# Line 1044  public class XMapPane extends JPanel { Line 1065  public class XMapPane extends JPanel {
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                  }                  }
# Line 1091  public class XMapPane extends JPanel { Line 1112  public class XMapPane extends JPanel {
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    
# Line 1147  public class XMapPane extends JPanel { Line 1175  public class XMapPane extends JPanel {
1175    
1176          public Envelope getMaxExtend() {          public Envelope getMaxExtend() {
1177                  if (maxExtend == null) {                  if (maxExtend == null) {
1178                          final ReferencedEnvelope layerBounds = GTUtil                          // Commented-out because it takes soo much time!
1179                                          .getVisibleLayoutBounds(localContext);                          //
1180                          if (layerBounds == null) {                          // long start = System.currentTimeMillis();
1181                                  // TODO Last fallback could be the CRS valid area                          // final ReferencedEnvelope layerBounds = GTUtil
1182                                  return null;                          // .getVisibleLayoutBounds(localContext);
1183                          }                          //                      
1184                            // LOGGER.info(
1185                          // Kartenbereich um 10% vergroessern                          // (System.currentTimeMillis()-start)+"m to get maxExtend");
1186                          return JTSUtil.fixAspectRatio(getVisibleRect(), JTSUtil                          //                      
1187                                          .expandEnvelope(layerBounds, 0.1), true);                          // if (layerBounds == null) {
1188                            // // TODO Last fallback could be the CRS valid area
1189                            // return null;
1190                            // }
1191                            //
1192                            // // Kartenbereich um 10% vergroessern
1193                            // return JTSUtil.fixAspectRatio(getVisibleRect(), JTSUtil
1194                            // .expandEnvelope(layerBounds, 0.1), true);
1195                  }                  }
1196                  return maxExtend;                  return maxExtend;
1197          }          }
# Line 1270  public class XMapPane extends JPanel { Line 1305  public class XMapPane extends JPanel {
1305           * {@link XMapPane} is visible and has bounds set.           * {@link XMapPane} is visible and has bounds set.
1306           */           */
1307          public boolean isWellDefined() {          public boolean isWellDefined() {
   
1308                  try {                  try {
1309                            if (getMapContext() == null)
                         if (getContext() == null)  
                                 return false;  
                         if (getContext().getLayerCount() <= 0)  
1310                                  return false;                                  return false;
1311                          if (getMapArea() == null)                          if (getMapContext().getLayerCount() <= 0)
1312                                  return false;                                  return false;
1313                          if (getBounds().getWidth() == 0)                          if (getVisibleRect().getWidth() == 0)
1314                                  return false;                                  return false;
1315                          if (getBounds().getHeight() == 0)                          if (getVisibleRect().getHeight() == 0)
1316                                  return false;                                  return false;
1317                            // if (getMapArea() == null)
1318                            // return false;
1319                  } catch (final Exception e) {                  } catch (final Exception e) {
1320                          return false;                          return false;
1321                  }                  }
1322                  return true;                  return true;
1323          }          }
1324    
1325            /**
1326             * Called from the listeners while the mouse is dragging, this method either
1327             * paints a translated (moved/panned) version of the image, or a rectangle.
1328             *
1329             * @param startPos
1330             *            in screen coordinates
1331             * @param lastPos
1332             *            in screen coordinates
1333             * @param event
1334             *            the {@link MouseEvent} to read the mouse buttons from
1335             */
1336          public void mouseDragged(final Point startPos, final Point lastPos,          public void mouseDragged(final Point startPos, final Point lastPos,
1337                          final MouseEvent event) {                          final MouseEvent event) {
1338    
1339                  if ((getState() == XMapPane.PAN)                  if ((getState() == XMapPane.PAN)
1340                                  || ((event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)) {                                  || ((event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)) {
1341    
1342                            // Panning needs a panning coursor
1343                          if (getCursor() != SwingUtil.PANNING_CURSOR) {                          if (getCursor() != SwingUtil.PANNING_CURSOR) {
1344                                  setCursor(SwingUtil.PANNING_CURSOR);                                  setCursor(SwingUtil.PANNING_CURSOR);
1345    
1346                                  // While panning, we deactivate the rendering. So the tasts are                                  // While panning, we deactivate the rendering. So the tasks are
1347                                  // ready to start when the panning os done.                                  // ready to start when the panning is finished.
1348                                  if (bgExecuter != null)                                  if (bgExecuter != null && bgExecuter.isRunning())
1349                                          bgExecuter.cancelTask();                                          bgExecuter.cancelTask();
1350                                  if (localExecuter != null)                                  if (localExecuter.isRunning())
1351                                          localExecuter.cancelTask();                                          localExecuter.cancelTask();
1352                          }                          }
1353    
# Line 1349  public class XMapPane extends JPanel { Line 1394  public class XMapPane extends JPanel {
1394                  } else if ((getState() == XMapPane.ZOOM_IN)                  } else if ((getState() == XMapPane.ZOOM_IN)
1395                                  || (getState() == XMapPane.ZOOM_OUT)                                  || (getState() == XMapPane.ZOOM_OUT)
1396                                  || (getState() == XMapPane.SELECT_ALL)                                  || (getState() == XMapPane.SELECT_ALL)
1397                                  || (getState() == XMapPane.SELECT_TOP)                                  || (getState() == XMapPane.SELECT_TOP)) {
                 // || (getState() == XMapPane.SELECT_ONE_FROM_TOP)  
                 ) {  
                         final Graphics graphics = getGraphics();  
1398    
1399                            // Draws a rectangle
1400                            final Graphics graphics = getGraphics();
1401                          drawRectangle(graphics, startPos, event.getPoint());                          drawRectangle(graphics, startPos, event.getPoint());
1402                            if ((lastPos.x > 0) && (lastPos.y > 0))
                         if ((lastPos.x > 0) && (lastPos.y > 0)) {  
1403                                  drawRectangle(graphics, startPos, lastPos);                                  drawRectangle(graphics, startPos, lastPos);
                         }  
   
1404                          graphics.dispose();                          graphics.dispose();
   
1405                  }                  }
   
1406          }          }
1407    
1408          /**          /**
1409           * Called by the {@link RenderingExecutor} when rendering was cancelled.           * Called by the {@link RenderingExecutor} when rendering was cancelled.
1410           */           */
1411          public void onRenderingCancelled() {          public void onRenderingCancelled() {
                 repaintTimer.stop();  
1412                  LOGGER.debug("Rendering cancelled");                  LOGGER.debug("Rendering cancelled");
1413                    repaintTimer.stop();
1414          }          }
1415    
1416          /**          /**
# Line 1379  public class XMapPane extends JPanel { Line 1418  public class XMapPane extends JPanel {
1418           * completed.           * completed.
1419           */           */
1420          public void onRenderingCompleted() {          public void onRenderingCompleted() {
1421                    LOGGER.info("complete");
1422    
1423                  repaintTimer.stop();                  repaintTimer.stop();
1424                  updateFinalImage();  
1425                  repaint();                  // We "forget" about an exception every time we complete a rendering
1426                    // thread successfully
1427                  if (renderingErrors.size() > 0)                  if (renderingErrors.size() > 0)
1428                          renderingErrors.remove(0);                          renderingErrors.remove(0);
1429    
1430                    updateFinalImage();
1431                    repaint();
1432          }          }
1433    
1434          /**          /**
# Line 1397  public class XMapPane extends JPanel { Line 1442  public class XMapPane extends JPanel {
1442           * @see MapPaneListener#onRenderingStopped(org.geotools.swing.event.MapPaneEvent)           * @see MapPaneListener#onRenderingStopped(org.geotools.swing.event.MapPaneEvent)
1443           */           */
1444          public void onRenderingFailed(final Exception renderingError) {          public void onRenderingFailed(final Exception renderingError) {
1445                  this.renderingErrors.add(renderingError);  
1446                    // Store the exceptions so we can show it to the user:
1447                    if (!(renderingError instanceof java.lang.IllegalArgumentException && renderingError
1448                                    .getMessage().equals(
1449                                                    "Argument \"sourceCRS\" should not be null.")))
1450                            this.renderingErrors.add(renderingError);
1451                  if (renderingErrors.size() > 3)                  if (renderingErrors.size() > 3)
1452                          renderingErrors.remove(0);                          renderingErrors.remove(0);
1453    
1454                  repaintTimer.stop();                  repaintTimer.stop();
1455    
1456                  LOGGER.warn("Rendering failed", renderingError);                  LOGGER.warn("Rendering failed", renderingError);
1457                  updateFinalImage();                  updateFinalImage();
1458                  repaint();                  repaint();
1459    
1460          }          }
1461    
         public void onRenderingPending() {  
                 // LOGGER.debug("Pending rendering updates the preview...");  
                 updateFinalImage();  
                 repaint();  
         }  
   
1462          @Override          @Override
1463          protected void paintComponent(final Graphics g) {          protected void paintComponent(final Graphics g) {
                 if (!acceptsRepaintCalls)  
                         return;  
1464    
1465                  // Maybe update the cursor and maybe stop the repainting timer                  // Maybe update the cursor and maybe stop the repainting timer
1466                  updateCursor();                  updateCursor();
1467    
1468                    if (!acceptsRepaintCalls)
1469                            return;
1470    
1471                    if (!isWellDefined())
1472                            return;
1473    
1474                  // super.paintComponent(g); // candidate for removal                  // super.paintComponent(g); // candidate for removal
1475    
1476                  boolean paintedSomething = false;                  boolean paintedSomething = false;
# Line 1434  public class XMapPane extends JPanel { Line 1484  public class XMapPane extends JPanel {
1484                          // happening.                          // happening.
1485                          if (mapAreaChanged && oldMapArea != null                          if (mapAreaChanged && oldMapArea != null
1486                                          && getMapArea().intersects(oldMapArea)                                          && getMapArea().intersects(oldMapArea)
1487                                          & !getMapArea().equals(oldMapArea)) {                                          & !getMapArea().equals(oldMapArea) && !paneResized) {
1488    
1489                                  mapAreaChanged = false;                                  mapAreaChanged = false;
1490    
1491                                  if (getMapArea().covers(oldMapArea)) {                                  if (getMapArea().covers(oldMapArea)) {
1492                                          setQuickPreviewHint(ZOOM_OUT);                                          quickPreviewHint = ZOOM_OUT;
1493                                          paintedSomething = drawScaledPreviewImage_Zoom((Graphics2D) g);                                          paintedSomething = drawScaledPreviewImage_Zoom((Graphics2D) g);
1494                                  } else if (oldMapArea.covers(getMapArea())) {                                  } else if (oldMapArea.covers(getMapArea())) {
1495                                          setQuickPreviewHint(ZOOM_IN);                                          quickPreviewHint = ZOOM_IN;
1496                                          paintedSomething = drawScaledPreviewImage_Zoom((Graphics2D) g);                                          paintedSomething = drawScaledPreviewImage_Zoom((Graphics2D) g);
1497                                  }                                  }
   
1498                          }                          }
   
                         if (paneResized) {  
                                 paneResized = false;  
                                 disposeImages();  
                         }  
   
                         // Start the Threads and Timers to render the image  
                         requestStartRendering();  
   
1499                  }                  }
1500    
1501                  if (!paintedSomething) {                  if (!paintedSomething) {
# Line 1520  public class XMapPane extends JPanel { Line 1560  public class XMapPane extends JPanel {
1560          public void performPan() {          public void performPan() {
1561    
1562                  Rectangle winBounds = getVisibleRect();                  Rectangle winBounds = getVisibleRect();
1563                    
1564                  winBounds.translate(-imageOrigin.x, -imageOrigin.y);                  winBounds.translate(-imageOrigin.x, -imageOrigin.y);
1565                  final Envelope newMapArea = tranformWindowToGeo(winBounds.x,                  final Envelope newMapArea = tranformWindowToGeo(winBounds.x,
1566                                  winBounds.y, winBounds.x + winBounds.width, winBounds.y                                  winBounds.y, winBounds.x + winBounds.width, winBounds.y
# Line 1543  public class XMapPane extends JPanel { Line 1583  public class XMapPane extends JPanel {
1583                          setCursor(SwingUtil.PAN_CURSOR);                          setCursor(SwingUtil.PAN_CURSOR);
1584          }          }
1585    
         //  
         // /**  
         // * 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&uuml;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);  
         // }  
         // }  
   
1586          /**          /**
1587           * Entfernt einen Listener von der Map.           * Entfernt einen Listener von der Map.
1588           *           *
# Line 1583  public class XMapPane extends JPanel { Line 1601  public class XMapPane extends JPanel {
1601          private void requestStartRendering() {          private void requestStartRendering() {
1602                  if (bgExecuter != null)                  if (bgExecuter != null)
1603                          bgExecuter.cancelTask();                          bgExecuter.cancelTask();
1604                  if (localExecuter != null)  
1605                          localExecuter.cancelTask();                  localExecuter.cancelTask();
1606    
1607                    mapImageInvalid = true;
1608                    if (paneResized) {
1609                            paneResized = false;
1610                            disposeImages();
1611                    }
1612                  requestStartRendering = true;                  requestStartRendering = true;
1613    
1614          }          }
1615    
         //  
         // /**  
         // * 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);  
         // }  
         // }  
   
1616          /**          /**
1617           * Calculate the affine transforms used to convert between world and pixel           * Calculate the affine transforms used to convert between world and pixel
1618           * coordinates. The calculations here are very basic and assume a cartesian           * coordinates. The calculations here are very basic and assume a cartesian
# Line 1642  public class XMapPane extends JPanel { Line 1628  public class XMapPane extends JPanel {
1628           */           */
1629          private void resetTransforms() {          private void resetTransforms() {
1630                  ReferencedEnvelope refMapEnv = new ReferencedEnvelope(mapArea,                  ReferencedEnvelope refMapEnv = new ReferencedEnvelope(mapArea,
1631                                  getContext().getCoordinateReferenceSystem());                                  getMapContext().getCoordinateReferenceSystem());
1632    
1633                  Rectangle paintArea = getVisibleRect(); // NOT USE GET BOUNDS!                  // System.out
1634                                    // .println("paintArea in resetTeansofrms = " + getVisibleRect());
1635                  //                          if (!isWellDefined())
1636                  // 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();  
1637    
1638                  worldToScreen = RendererUtilities.worldToScreenTransform(refMapEnv,                  worldToScreen = RendererUtilities.worldToScreenTransform(refMapEnv,
1639                                  paintArea);                                  getVisibleRect());
1640    
                 // worldToScreen = new AffineTransform(scale, 0, 0, -scale, -xoff,  
                 // yoff);  
1641                  try {                  try {
1642                          screenToWorld = worldToScreen.createInverse();                          screenToWorld = worldToScreen.createInverse();
1643    
1644                  } catch (NoninvertibleTransformException ex) {                  } catch (NoninvertibleTransformException ex) {
1645                          ex.printStackTrace();                          LOGGER
1646                                            .error("can't invert worldToScreen to get screenToWorld!",
1647                                                            ex);
1648                  }                  }
1649          }          }
1650    
# Line 1683  public class XMapPane extends JPanel { Line 1663  public class XMapPane extends JPanel {
1663                  this.bgContext = context;                  this.bgContext = context;
1664    
1665                  if (context != null) {                  if (context != null) {
1666                          setMapArea(bgContext.getAreaOfInterest());                          // setMapArea(bgContext.getAreaOfInterest());
1667    
1668                          this.bgContext.addMapLayerListListener(bgContextListener);                          this.bgContext.addMapLayerListListener(bgContextListener);
1669    
# Line 1692  public class XMapPane extends JPanel { Line 1672  public class XMapPane extends JPanel {
1672                                  mapLayer.addMapLayerListener(bgMapLayerListener);                                  mapLayer.addMapLayerListener(bgMapLayerListener);
1673                          }                          }
1674                  }                  }
1675                  mapImageInvalid = true;  
1676                  repaint();                  requestStartRendering();
1677          }          }
1678    
1679          public void setJava2dHints(final RenderingHints java2dHints) {          public void setJava2dHints(final RenderingHints java2dHints) {
1680                  this.java2dHints = java2dHints;                  this.java2dHints = java2dHints;
1681          }          }
1682    
         /**  
          *  
          * @param context  
          */  
1683          public void setLocalContext(final MapContext context) {          public void setLocalContext(final MapContext context) {
1684                  // Remove the default listener from the old context                  // Remove the default listener from the old context
1685                  if (this.localContext != null) {                  if (this.localContext != null) {
# Line 1719  public class XMapPane extends JPanel { Line 1695  public class XMapPane extends JPanel {
1695    
1696                  if (context != null) {                  if (context != null) {
1697    
1698                          setMapArea(localContext.getAreaOfInterest());                          // setMapArea(localContext.getAreaOfInterest());
1699    
1700                          localRenderer.setContext(localContext);                          localRenderer.setContext(localContext);
1701    
# Line 1731  public class XMapPane extends JPanel { Line 1707  public class XMapPane extends JPanel {
1707                          }                          }
1708                  }                  }
1709    
1710                  mapImageInvalid = true;                  requestStartRendering();
1711                  repaint();  
1712            }
1713    
1714            public boolean setMapArea(final Envelope newMapArea) {
1715                    return setMapArea(new ReferencedEnvelope(newMapArea, getMapContext()
1716                                    .getCoordinateReferenceSystem()));
1717          }          }
1718    
1719          /**          /**
# Line 1740  public class XMapPane extends JPanel { Line 1721  public class XMapPane extends JPanel {
1721           * @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
1722           *         has been triggered.           *         has been triggered.
1723           */           */
1724          public boolean setMapArea(final Envelope newMapArea) {          public boolean setMapArea(final ReferencedEnvelope newMapArea) {
1725    
1726                  if (newMapArea == null                  if (newMapArea == null
1727                                  || bestAllowedMapArea(newMapArea).equals(mapArea)) {                                  || bestAllowedMapArea(newMapArea).equals(mapArea)) {
# Line 1795  public class XMapPane extends JPanel { Line 1776  public class XMapPane extends JPanel {
1776                          bgContext.setAreaOfInterest(mapArea, localContext                          bgContext.setAreaOfInterest(mapArea, localContext
1777                                          .getCoordinateReferenceSystem());                                          .getCoordinateReferenceSystem());
1778                  }                  }
1779                  mapImageInvalid = true;  
1780                  mapAreaChanged = true;                  mapAreaChanged = true;
                 repaint();  
1781    
1782                  LOGGER.debug("New maparea = " + mapArea);                  repaint(200); // Do not remove it!
1783    
1784                    requestStartRendering();
1785    
1786                  return true;                  return true;
1787          }          }
1788    
# Line 1885  public class XMapPane extends JPanel { Line 1868  public class XMapPane extends JPanel {
1868          }          }
1869    
1870          /**          /**
1871           *           * If <code>true</code>, allow the {@link XMapPane} to process #repaint()
1872           * @param b           * requests. Otherwise the map will not paint anything and not start any
1873             * rendering {@link Thread}s.
1874           */           */
1875          public void setPainting(final boolean b) {          public void setPainting(final boolean b) {
1876                  acceptsRepaintCalls = b;                  acceptsRepaintCalls = b;
1877          }                  if (acceptsRepaintCalls == true)
1878                            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;  
   
1879          }          }
1880    
1881          private void setRendererHints(final Map<Object, Object> rendererHints) {          private void setRendererHints(final Map<Object, Object> rendererHints) {
# Line 1949  public class XMapPane extends JPanel { Line 1920  public class XMapPane extends JPanel {
1920           */           */
1921          private void startRendering() {          private void startRendering() {
1922    
1923                  if (!isWellDefined())                  if (!isWellDefined() || !acceptsRepaintCalls) {
1924                            // if we are not ready to start rendering, try it again the next
1925                            // time the timer is chacking the flag.
1926                            requestStartRendering = true;
1927                          return;                          return;
1928                    }
1929    
1930                  if (bgExecuter != null) {                  if (bgExecuter != null) {
1931                          // Stop all renderers                          // Stop all renderers
1932                          bgExecuter.cancelTask();                          bgExecuter.cancelTask();
1933                  }                  }
1934    
1935                  if (localExecuter != null) {                  localExecuter.cancelTask();
                         localExecuter.cancelTask();  
                 }  
1936    
1937                  final Rectangle curPaintArea = getVisibleRect();                  final Rectangle curPaintArea = getVisibleRect();
1938    
# Line 1982  public class XMapPane extends JPanel { Line 1955  public class XMapPane extends JPanel {
1955                          // (Graphics2D) getBgImage().getGraphics(), createGTRenderer);                          // (Graphics2D) getBgImage().getGraphics(), createGTRenderer);
1956                  }                  }
1957    
1958                  if (getContext() != null) {                  if (getMapContext() != null) {
1959                          // localExecuter = new RenderingExecutor(this, 150l);                          // localExecuter = new RenderingExecutor(this, 150l);
1960                          // LOGGER.debug("starting local renderer:");                          // LOGGER.debug("starting local renderer:");
1961    
1962                          localRenderer.setJava2DHints(getJava2dHints());                          localRenderer.setJava2DHints(getJava2dHints());
1963                          localRenderer.setRendererHints(getRendererHints());                          localRenderer.setRendererHints(getRendererHints());
1964    
1965                          ReferencedEnvelope areaOfInterest = getMapArea();                          final boolean submitted = localExecuter.submit(getMapArea(),
                         final boolean submitted = localExecuter.submit(areaOfInterest,  
1966                                          curPaintArea, (Graphics2D) getLocalImage().getGraphics(),                                          curPaintArea, (Graphics2D) getLocalImage().getGraphics(),
1967                                          localRenderer, getWorldToScreenTransform());                                          localRenderer);
1968                          if (submitted)                          if (submitted)
1969                                  repaintTimer.restart();                                  repaintTimer.restart();
1970                          else                          else
1971                                  requestStartRendering = true; // Try to start rendering again in                                  requestStartRendering = true; // Try to start rendering
1972                            // again in
1973                          // a moment                          // a moment
1974                  }                  }
1975    
# Line 2049  public class XMapPane extends JPanel { Line 2022  public class XMapPane extends JPanel {
2022                  final AffineTransform at = getScreenToWorld();                  final AffineTransform at = getScreenToWorld();
2023                  final Point2D geoO = at.transform(new Point2D.Double(ox, oy), null);                  final Point2D geoO = at.transform(new Point2D.Double(ox, oy), null);
2024                  final Point2D geoP = at.transform(new Point2D.Double(px, py), null);                  final Point2D geoP = at.transform(new Point2D.Double(px, py), null);
2025                    
2026                  // Mmmmm... don't really understand why its x,x,y,y                  // Mmmmm... don't really understand why its x,x,y,y
2027  //              return new Envelope(geoO.getX(), geoP.getX(), geoO.getY(), geoP.getY());                  // return new Envelope(geoO.getX(), geoP.getX(), geoO.getY(),
2028                  return new Envelope( new Coordinate(geoO.getX(), geoO.getY()), new Coordinate(geoP.getX(), geoP.getY()));                  // geoP.getY());
2029                    return new Envelope(new Coordinate(geoO.getX(), geoO.getY()),
2030                                    new Coordinate(geoP.getX(), geoP.getY()));
2031          }          }
2032    
2033          /**          /**
# Line 2064  public class XMapPane extends JPanel { Line 2039  public class XMapPane extends JPanel {
2039                  // if the renderers have stopped, also stop the timer that is updating                  // if the renderers have stopped, also stop the timer that is updating
2040                  // the final image                  // the final image
2041                  if (bgExecuter != null && bgExecuter.isRunning()                  if (bgExecuter != null && bgExecuter.isRunning()
2042                                  || localExecuter != null && localExecuter.isRunning()) {                                  || localExecuter.isRunning()) {
2043                          setCursor(WAIT_CURSOR);                          setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
2044                          return;                          return;
2045                  } else {                  } else {
2046                          // Allow one last rendering                          // Allow one last rendering
2047                          if (repaintTimer.isRunning()) {                          if (repaintTimer.isRunning()) {
2048                                  System.out.println("one last rendering....");                                  // System.out.println("one last rendering....");
2049                                  repaintTimer.stop();                                  repaintTimer.stop();
2050                                  updateFinalImage();                                  updateFinalImage();
2051                                  repaint();                                  repaint();
# Line 2178  public class XMapPane extends JPanel { Line 2153  public class XMapPane extends JPanel {
2153    
2154                  // If the rendering process is still running, indicate this is the image                  // If the rendering process is still running, indicate this is the image
2155                  if (forceWait || bgExecuter != null && bgExecuter.isRunning()                  if (forceWait || bgExecuter != null && bgExecuter.isRunning()
2156                                  || localExecuter != null && localExecuter.isRunning()) {                                  || localExecuter.isRunning()) {
2157    
2158                          y += 8;                          y += 8;
2159    
# Line 2197  public class XMapPane extends JPanel { Line 2172  public class XMapPane extends JPanel {
2172                          y += 24;                          y += 24;
2173                  }                  }
2174    
2175                  if (renderingErrors != null) {                  if (!renderingErrors.isEmpty() && isShowExceptions()) {
2176    
2177                          final Color c = graphics.getColor();                          final Color c = graphics.getColor();
2178                          graphics.setFont(errorFont);                          graphics.setFont(errorFont);
2179    
2180                          for (Exception ex : renderingErrors) {                          for (Exception ex : renderingErrors) {
2181    
                                 if (ex instanceof java.lang.IllegalArgumentException  
                                                 && ex.getMessage().equals(  
                                                                 "Argument \"sourceCRS\" should not be null."))  
                                         continue;  
   
2182                                  String errStr = ex.getLocalizedMessage();                                  String errStr = ex.getLocalizedMessage();
2183    
2184                                    if (errStr == null)
2185                                            errStr = ex.getMessage();
2186                                    if (errStr == null)
2187                                            errStr = "unknown error: " + ex.getClass().getSimpleName();
2188    
2189                                  graphics.setColor(Color.WHITE);                                  graphics.setColor(Color.WHITE);
2190                                  graphics.drawString(errStr, 5, y);                                  graphics.drawString(errStr, 5, y);
2191                                  graphics.setColor(Color.RED);                                  graphics.setColor(Color.RED);
# Line 2234  public class XMapPane extends JPanel { Line 2209  public class XMapPane extends JPanel {
2209          public void zoomTo(          public void zoomTo(
2210                          final FeatureCollection<SimpleFeatureType, SimpleFeature> features) {                          final FeatureCollection<SimpleFeatureType, SimpleFeature> features) {
2211    
2212                  final CoordinateReferenceSystem mapCRS = getContext()                  // if (!isWellDefined()) return;
2213    
2214                    final CoordinateReferenceSystem mapCRS = getMapContext()
2215                                  .getCoordinateReferenceSystem();                                  .getCoordinateReferenceSystem();
2216                  final CoordinateReferenceSystem fCRS = features.getSchema()                  final CoordinateReferenceSystem fCRS = features.getSchema()
2217                                  .getGeometryDescriptor().getCoordinateReferenceSystem();                                  .getGeometryDescriptor().getCoordinateReferenceSystem();
2218    
2219                  double width = mapArea.getWidth();                  ReferencedEnvelope _mapArea;
2220                  double height = mapArea.getHeight();                  if (mapArea == null)
2221                            _mapArea = features.getBounds();
2222                    else
2223                            _mapArea = getMapArea();
2224                    double width = _mapArea.getWidth();
2225                    double height = _mapArea.getHeight();
2226                  final double ratio = height / width;                  final double ratio = height / width;
2227    
2228                  if (features == null || features.size() == 0) {                  if (features == null || features.size() == 0) {
# Line 2292  public class XMapPane extends JPanel { Line 2274  public class XMapPane extends JPanel {
2274                  } else {                  } else {
2275                          final ReferencedEnvelope fBounds = features.getBounds();                          final ReferencedEnvelope fBounds = features.getBounds();
2276    
2277                          Envelope bounds;                          ReferencedEnvelope bounds;
2278                          if (!mapCRS.equals(fCRS)) {                          if (!mapCRS.equals(fCRS)) {
2279                                  bounds = JTSUtil.transformEnvelope(fBounds, fCRS, mapCRS);                                  bounds = JTSUtil.transformEnvelope(fBounds, mapCRS);
2280                          } else {                          } else {
2281                                  bounds = fBounds;                                  bounds = fBounds;
2282                          }                          }
# Line 2349  public class XMapPane extends JPanel { Line 2331  public class XMapPane extends JPanel {
2331                  newMapArea.expandBy((mapArea.getWidth() * zoomFaktor - mapArea                  newMapArea.expandBy((mapArea.getWidth() * zoomFaktor - mapArea
2332                                  .getWidth()) / 2., (mapArea.getHeight() * zoomFaktor - mapArea                                  .getWidth()) / 2., (mapArea.getHeight() * zoomFaktor - mapArea
2333                                  .getHeight()) / 2.);                                  .getHeight()) / 2.);
2334                    
2335  // TODO we actually want that                  // TODO we actually want that
2336  //              // Move the newMapArea above the new center                  // // Move the newMapArea above the new center
2337  //              newMapArea.translate(gcenter.getX() - mapArea.centre().x, gcenter                  // newMapArea.translate(gcenter.getX() - mapArea.centre().x, gcenter
2338  //                              .getY()                  // .getY()
2339  //                              - mapArea.centre().y);                  // - mapArea.centre().y);
2340    
2341                  setMapArea(newMapArea);                  setMapArea(newMapArea);
2342          }          }
2343    
2344            /**
2345             * Shall non-fatal rendering exceptions be reported in the mappane or be
2346             * dropped quitely.
2347             */
2348            public void setShowExceptions(boolean showExceptions) {
2349                    this.showExceptions = showExceptions;
2350            }
2351    
2352            /**
2353             * Shall exceptions be reported in the mappane?
2354             */
2355            public boolean isShowExceptions() {
2356                    return showExceptions;
2357            }
2358    
2359  }  }

Legend:
Removed from v.540  
changed lines
  Added in v.555

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26