/[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 544 by alfonx, Sat Nov 21 17:13:31 2009 UTC revision 560 by alfonx, Tue Nov 24 14:57:05 2009 UTC
# Line 26  import java.util.HashMap; Line 26  import java.util.HashMap;
26  import java.util.Map;  import java.util.Map;
27  import java.util.Vector;  import java.util.Vector;
28    
29    import javax.jws.soap.SOAPBinding.Style;
30  import javax.swing.Timer;  import javax.swing.Timer;
31    
32  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
33  import org.geotools.feature.FeatureCollection;  import org.geotools.feature.FeatureCollection;
 import org.geotools.geometry.Envelope2D;  
34  import org.geotools.geometry.jts.JTS;  import org.geotools.geometry.jts.JTS;
35  import org.geotools.geometry.jts.ReferencedEnvelope;  import org.geotools.geometry.jts.ReferencedEnvelope;
36  import org.geotools.map.DefaultMapContext;  import org.geotools.map.DefaultMapContext;
# Line 99  public class XMapPane extends JPanel { Line 99  public class XMapPane extends JPanel {
99          private static final int IMAGETYPE = BufferedImage.TYPE_INT_RGB;          private static final int IMAGETYPE = BufferedImage.TYPE_INT_RGB;
100          private static final int IMAGETYPE_withAlpha = BufferedImage.TYPE_INT_ARGB;          private static final int IMAGETYPE_withAlpha = BufferedImage.TYPE_INT_ARGB;
101    
102          private static Logger LOGGER = Logger.getLogger(XMapPane.class);          private final static Logger LOGGER = Logger.getLogger(XMapPane.class);
103    
104          private boolean acceptsRepaintCalls = true;          private boolean acceptsRepaintCalls = true;
105    
# Line 134  public class XMapPane extends JPanel { Line 134  public class XMapPane extends JPanel {
134           * The initial delay in milliseconds until the {@link #finalImage} is           * The initial delay in milliseconds until the {@link #finalImage} is
135           * updated the first time.           * updated the first time.
136           */           */
137          public static final int INITIAL_REPAINT_DELAY = 1000;          public static final int INITIAL_REPAINT_DELAY = 900;
138    
139          /**          /**
140           * While the {@link #bgExecuter} and {@link #localExecuter} are rendering,           * While the {@link #bgExecuter} and {@link #localExecuter} are rendering,
# Line 151  public class XMapPane extends JPanel { Line 151  public class XMapPane extends JPanel {
151           */           */
152          public static final int DEFAULT_RESIZING_PAINT_DELAY = 600;          public static final int DEFAULT_RESIZING_PAINT_DELAY = 600;
153    
154            /**
155             * This not-repeating {@link Timer} is re-started whenever the component is
156             * resized. That means => only if the component is not resizing for
157             * {@link #DEFAULT_RESIZING_PAINT_DELAY} milliseconds, does the
158             * {@link XMapPane} react.
159             */
160          private final Timer resizeTimer;          private final Timer resizeTimer;
161    
162            /**
163             * Flag for no-tool.
164             */
165          public static final int NONE = -123;          public static final int NONE = -123;
166    
167          /**          /**
# Line 164  public class XMapPane extends JPanel { Line 173  public class XMapPane extends JPanel {
173          public static final int PAN = 1;          public static final int PAN = 1;
174    
175          /**          /**
176             * Flag fuer Modus "Heran zoomen".
177             *
178             * @see #setState(int)
179             * @see #setState(int)
180             */
181            public static final int ZOOM_IN = 2;
182    
183            /**
184             * Flag fuer Modus "Heraus zoomen". Nicht fuer Window-Auswahl moeglich!
185             *
186             * @see #setState(int)
187             */
188            public static final int ZOOM_OUT = 3;
189    
190            /**
191           * Flag fuer Modus "SimpleFeature-Auswahl auf allen (sichtbaren) Layern".           * Flag fuer Modus "SimpleFeature-Auswahl auf allen (sichtbaren) Layern".
192           *           *
193           * @see #setState(int)           * @see #setState(int)
# Line 187  public class XMapPane extends JPanel { Line 211  public class XMapPane extends JPanel {
211           */           */
212          public static final int SELECT_TOP = 4;          public static final int SELECT_TOP = 4;
213    
         public static final Cursor WAIT_CURSOR = Cursor  
                         .getPredefinedCursor(Cursor.WAIT_CURSOR);  
   
214          /**          /**
215           * {@link Font} used to paint the wait messages into the map           * {@link Font} used to paint the wait messages into the map
216           *           *
# Line 213  public class XMapPane extends JPanel { Line 234  public class XMapPane extends JPanel {
234          final String waitMsg = SwingUtil.R("WaitMess");          final String waitMsg = SwingUtil.R("WaitMess");
235    
236          /**          /**
          * Flag fuer Modus "Heran zoomen".  
          *  
          * @see #setState(int)  
          * @see #setState(int)  
          */  
         public static final int ZOOM_IN = 2;  
   
         /**  
          * Flag fuer Modus "Heraus zoomen". Nicht fuer Window-Auswahl moeglich!  
          *  
          * @see #setState(int)  
          */  
         public static final int ZOOM_OUT = 3;  
   
         /**  
237           * Konvertiert die Maus-Koordinaten (relativ zum <code>JMapPane</code>) in           * Konvertiert die Maus-Koordinaten (relativ zum <code>JMapPane</code>) in
238           * Karten-Koordinaten.           * Karten-Koordinaten.
239           *           *
# Line 390  public class XMapPane extends JPanel { Line 396  public class XMapPane extends JPanel {
396                  public void layerAdded(final MapLayerListEvent event) {                  public void layerAdded(final MapLayerListEvent event) {
397                          event.getLayer().addMapLayerListener(localMapLayerListener);                          event.getLayer().addMapLayerListener(localMapLayerListener);
398    
399                          localRenderer.setContext(getContext());                          getLocalRenderer().setContext(getMapContext());
400                          requestStartRendering();                          requestStartRendering();
401    
402                  }                  }
403    
404                  @Override                  @Override
405                  public void layerChanged(final MapLayerListEvent event) {                  public void layerChanged(final MapLayerListEvent event) {
406                          localRenderer.setContext(getContext());  //                      localRenderer = GTUtil.createGTRenderer();
407                            getLocalRenderer().setContext(getMapContext());
408                          requestStartRendering();                          requestStartRendering();
409                  }                  }
410    
411                  @Override                  @Override
412                  public void layerMoved(final MapLayerListEvent event) {                  public void layerMoved(final MapLayerListEvent event) {
413                          localRenderer.setContext(getContext());                          getLocalRenderer().setContext(getMapContext());
414                          requestStartRendering();                          requestStartRendering();
415                  }                  }
416    
# Line 411  public class XMapPane extends JPanel { Line 418  public class XMapPane extends JPanel {
418                  public void layerRemoved(final MapLayerListEvent event) {                  public void layerRemoved(final MapLayerListEvent event) {
419                          if (event.getLayer() != null)                          if (event.getLayer() != null)
420                                  event.getLayer().removeMapLayerListener(localMapLayerListener);                                  event.getLayer().removeMapLayerListener(localMapLayerListener);
421                          localRenderer.setContext(getContext());                          getLocalRenderer().setContext(getMapContext());
422                          requestStartRendering();                          requestStartRendering();
423                  }                  }
424          };          };
# Line 424  public class XMapPane extends JPanel { Line 431  public class XMapPane extends JPanel {
431    
432                  @Override                  @Override
433                  public void layerChanged(final MapLayerEvent event) {                  public void layerChanged(final MapLayerEvent event) {
434                          localRenderer.setContext(getContext()); // betters for SLD changes?!                          getLocalRenderer().setContext(getMapContext()); // betters for SLD
435                            // changes?!
436                          requestStartRendering();                          requestStartRendering();
437                  }                  }
438    
# Line 439  public class XMapPane extends JPanel { Line 447  public class XMapPane extends JPanel {
447                  }                  }
448          };          };
449    
450          private final GTRenderer localRenderer = GTUtil.createGTRenderer();          final private GTRenderer localRenderer = GTUtil.createGTRenderer();
451    
452          private final GTRenderer bgRenderer = GTUtil.createGTRenderer();          private final GTRenderer bgRenderer = GTUtil.createGTRenderer();
453    
# Line 460  public class XMapPane extends JPanel { Line 468  public class XMapPane extends JPanel {
468          /**          /**
469           * This color is used as the default background color when painting a map.           * This color is used as the default background color when painting a map.
470           */           */
471          private Color mapBackgroundColor = Color.WHITE;          private Color mapBackgroundColor = null;
472    
473          /**          /**
474           * A flag indicating that the shown image is invalid and needs to be           * A flag indicating that the shown image is invalid and needs to be
# Line 504  public class XMapPane extends JPanel { Line 512  public class XMapPane extends JPanel {
512           * A flag indicating, that the image size has changed and the buffered           * A flag indicating, that the image size has changed and the buffered
513           * images are not big enough any more           * images are not big enough any more
514           **/           **/
515          protected boolean paneResized = false;          protected boolean paneResized = true;
516    
517          private BufferedImage preFinalImage;          private BufferedImage preFinalImage;
518    
519          // if null, no quick preview will be shown          // ** if 0, no quick preview will be shown **/
520          private int quickPreviewHint = 0;          private int quickPreviewHint = 0;
521    
522          private Map<Object, Object> rendererHints = GTUtil          private Map<Object, Object> rendererHints = GTUtil
523                          .getDefaultGTRendererHints(localRenderer);                          .getDefaultGTRendererHints(getLocalRenderer());
524    
525          /**          /**
526           * If set to <code>true</code>, the {@link #startRenderThreadsTimer} will           * If set to <code>true</code>, the {@link #startRenderThreadsTimer} will
# Line 556  public class XMapPane extends JPanel { Line 564  public class XMapPane extends JPanel {
564          /** Is set if a renderer has an error **/          /** Is set if a renderer has an error **/
565          protected ArrayList<Exception> renderingErrors = new ArrayList<Exception>();          protected ArrayList<Exception> renderingErrors = new ArrayList<Exception>();
566    
567          // /** If {@link #getVisibleRect()} of this {@link JPanel} is not yet          /**
568          // defined, but we are **/           * If <code>true</code>, then rendering exceptions are rendererd into the
569          // private Envelope mapAreaCondidate = null;           * map pane
570             **/
571            private boolean showExceptions = false;
572    
573          public XMapPane() {          public XMapPane() {
574                  this(null, null);                  this(null, null);
# Line 608  public class XMapPane extends JPanel { Line 618  public class XMapPane extends JPanel {
618                                                  if (!isWellDefined())                                                  if (!isWellDefined())
619                                                          return;                                                          return;
620    
621                                                    LOGGER.debug("resizeTimer performed");
622    
623                                                  final Rectangle bounds = getVisibleRect();                                                  final Rectangle bounds = getVisibleRect();
624                                                  //                                                  //
625                                                  // System.out.println("\n\ntimer performs with bounds = "                                                  // System.out.println("\n\ntimer performs with bounds = "
# Line 617  public class XMapPane extends JPanel { Line 629  public class XMapPane extends JPanel {
629                                                                  bounds.x, bounds.y, bounds.x + bounds.width,                                                                  bounds.x, bounds.y, bounds.x + bounds.width,
630                                                                  bounds.y + bounds.height);                                                                  bounds.y + bounds.height);
631    
632                                                  if (setMapArea(geoMapArea))                                                  if (setMapArea(geoMapArea)) {
633                                                            LOGGER.debug("  maparea changed");
634                                                          paneResized = true;                                                          paneResized = true;
635                                                  // else                                                  } else
636                                                  // syso                                                          LOGGER.debug("  maparea NOT changed");
637                                          }                                          }
638                                  });                                  });
639                  resizeTimer.setRepeats(false);                  resizeTimer.setRepeats(false);
640    
641                  this.addComponentListener(new ComponentAdapter() {                  this.addComponentListener(new ComponentAdapter() {
642    
643                            private Rectangle oldVisibleRect;
644    
645                          @Override                          @Override
646                          public void componentResized(final ComponentEvent e) {                          public void componentResized(final ComponentEvent e) {
647    
648                                  // Already cancel the tasks...                                  // Seems to be called twice with the same size..
649                                  if (bgExecuter != null)                                  if (oldVisibleRect != null
650                                          bgExecuter.cancelTask();                                                  && oldVisibleRect.equals(getVisibleRect())) {
651                                  if (localExecuter != null)                                          LOGGER.debug("skipping resize.");
652                                          localExecuter.cancelTask();                                          return;
653                                    }
654    
655                                    LOGGER.debug("resized: " + getVisibleRect());
656                                  resizeTimer.restart();                                  resizeTimer.restart();
657                                    oldVisibleRect = getVisibleRect();
658                          }                          }
659    
660                  });                  });
# Line 648  public class XMapPane extends JPanel { Line 666  public class XMapPane extends JPanel {
666    
667                          @Override                          @Override
668                          public void actionPerformed(final ActionEvent e) {                          public void actionPerformed(final ActionEvent e) {
669                                  if ((localExecuter != null && !localExecuter.isRunning())                                  if ((!localExecuter.isRunning())
670                                                  && (bgExecuter != null && !bgExecuter.isRunning())) {                                                  && (bgExecuter != null && !bgExecuter.isRunning())) {
671                                          repaintTimer.stop();                                          repaintTimer.stop();
672                                  } else {                                  } else {
673                                          updateFinalImage();                                          updateFinalImage();
674                                          XMapPane.this.repaint(100);                                          XMapPane.this.repaint(100);
675    
676                                  }                                  }
677                          }                          }
678                  });                  });
679    
680                  repaintTimer.setInitialDelay(INITIAL_REPAINT_DELAY);                  repaintTimer.setInitialDelay(INITIAL_REPAINT_DELAY);
681                  repaintTimer.setRepeats(true);                  repaintTimer.setRepeats(true);
682    
# Line 674  public class XMapPane extends JPanel { Line 694  public class XMapPane extends JPanel {
694                                                  if (localExecuter.isRunning()) {                                                  if (localExecuter.isRunning()) {
695                                                          localExecuter.cancelTask();                                                          localExecuter.cancelTask();
696                                                  } else {                                                  } else {
   
697                                                          // Stupidly, but we have to recheck the                                                          // Stupidly, but we have to recheck the
698                                                          setMapArea(getMapArea());                                                          setMapArea(getMapArea());
699                                                          requestStartRendering = false;                                                          requestStartRendering = false;
# Line 763  public class XMapPane extends JPanel { Line 782  public class XMapPane extends JPanel {
782                  }                  }
783    
784                  final Envelope maxAllowedExtend = getMaxExtend();                  final Envelope maxAllowedExtend = getMaxExtend();
785    
786                  while (maxAllowedExtend != null && !maxAllowedExtend.contains(newArea)                  while (maxAllowedExtend != null && !maxAllowedExtend.contains(newArea)
787                                  && newArea != null && !newArea.isNull()                                  && newArea != null && !newArea.isNull()
788                                  && !Double.isNaN(newArea.getMinX())                                  && !Double.isNaN(newArea.getMinX())
# Line 907  public class XMapPane extends JPanel { Line 927  public class XMapPane extends JPanel {
927                          bgExecuter.dispose();                          bgExecuter.dispose();
928                  }                  }
929    
930                  if (localExecuter != null) {                  if (localExecuter.isRunning()) {
931                          int i = 0;                          int i = 0;
932                          localExecuter.cancelTask();                          localExecuter.cancelTask();
933                          while (i++ < 10 && localExecuter.isRunning()) {                          while (i++ < 10 && localExecuter.isRunning()) {
# Line 936  public class XMapPane extends JPanel { Line 956  public class XMapPane extends JPanel {
956                  removeMouseListener(zoomMapPaneMouseListener);                  removeMouseListener(zoomMapPaneMouseListener);
957    
958                  if (localContext != null)                  if (localContext != null)
959                          getContext().clearLayerList();                          getMapContext().clearLayerList();
960                  if (bgContext != null)                  if (bgContext != null)
961                          getBgContext().clearLayerList();                          getBgContext().clearLayerList();
962    
# Line 1047  public class XMapPane extends JPanel { Line 1067  public class XMapPane extends JPanel {
1067                  return bgImage;                  return bgImage;
1068          }          }
1069    
1070          public MapContext getContext() {          public MapContext getMapContext() {
1071                  if (localContext == null) {                  if (localContext == null) {
1072                          setLocalContext(new DefaultMapContext());                          setLocalContext(new DefaultMapContext());
1073                  }                  }
# Line 1128  public class XMapPane extends JPanel { Line 1148  public class XMapPane extends JPanel {
1148          }          }
1149    
1150          /**          /**
1151           * Returns the background {@link Color} of the map pane. Default is white.           * Returns the background {@link Color} of the map pane. If not set, the
1152             * methods looks for a parent component and will use its background color.
1153             * If no parent component is available, WHITE is returned.
1154           **/           **/
1155          public Color getMapBackgroundColor() {          public Color getMapBackgroundColor() {
1156                    if (mapBackgroundColor == null) {
1157                            if (getParent() != null)
1158                                    return getParent().getBackground();
1159                            else
1160                                    return Color.WHITE;
1161                    }
1162                  return mapBackgroundColor;                  return mapBackgroundColor;
1163          }          }
1164    
# Line 1157  public class XMapPane extends JPanel { Line 1185  public class XMapPane extends JPanel {
1185    
1186          public Envelope getMaxExtend() {          public Envelope getMaxExtend() {
1187                  if (maxExtend == null) {                  if (maxExtend == null) {
1188                          final ReferencedEnvelope layerBounds = GTUtil                          // Commented-out because it takes soo much time!
1189                                          .getVisibleLayoutBounds(localContext);                          //
1190                          if (layerBounds == null) {                          // long start = System.currentTimeMillis();
1191                                  // TODO Last fallback could be the CRS valid area                          // final ReferencedEnvelope layerBounds = GTUtil
1192                                  return null;                          // .getVisibleLayoutBounds(localContext);
1193                          }                          //                      
1194                            // LOGGER.info(
1195                          // Kartenbereich um 10% vergroessern                          // (System.currentTimeMillis()-start)+"m to get maxExtend");
1196                          return JTSUtil.fixAspectRatio(getVisibleRect(), JTSUtil                          //                      
1197                                          .expandEnvelope(layerBounds, 0.1), true);                          // if (layerBounds == null) {
1198                            // // TODO Last fallback could be the CRS valid area
1199                            // return null;
1200                            // }
1201                            //
1202                            // // Kartenbereich um 10% vergroessern
1203                            // return JTSUtil.fixAspectRatio(getVisibleRect(), JTSUtil
1204                            // .expandEnvelope(layerBounds, 0.1), true);
1205                  }                  }
1206                  return maxExtend;                  return maxExtend;
1207          }          }
# Line 1281  public class XMapPane extends JPanel { Line 1316  public class XMapPane extends JPanel {
1316           */           */
1317          public boolean isWellDefined() {          public boolean isWellDefined() {
1318                  try {                  try {
1319                          if (getContext() == null)                          if (getMapContext() == null)
1320                                  return false;                                  return false;
1321                          if (getContext().getLayerCount() <= 0)                          if (getMapContext().getLayerCount() <= 0)
1322                                  return false;                                  return false;
1323                          if (getVisibleRect().getWidth() == 0)                          if (getVisibleRect().getWidth() == 0)
1324                                  return false;                                  return false;
# Line 1297  public class XMapPane extends JPanel { Line 1332  public class XMapPane extends JPanel {
1332                  return true;                  return true;
1333          }          }
1334    
1335            /**
1336             * Called from the listeners while the mouse is dragging, this method either
1337             * paints a translated (moved/panned) version of the image, or a rectangle.
1338             *
1339             * @param startPos
1340             *            in screen coordinates
1341             * @param lastPos
1342             *            in screen coordinates
1343             * @param event
1344             *            the {@link MouseEvent} to read the mouse buttons from
1345             */
1346          public void mouseDragged(final Point startPos, final Point lastPos,          public void mouseDragged(final Point startPos, final Point lastPos,
1347                          final MouseEvent event) {                          final MouseEvent event) {
1348    
1349                  if ((getState() == XMapPane.PAN)                  if ((getState() == XMapPane.PAN)
1350                                  || ((event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)) {                                  || ((event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)) {
1351    
1352                            // Panning needs a panning coursor
1353                          if (getCursor() != SwingUtil.PANNING_CURSOR) {                          if (getCursor() != SwingUtil.PANNING_CURSOR) {
1354                                  setCursor(SwingUtil.PANNING_CURSOR);                                  setCursor(SwingUtil.PANNING_CURSOR);
1355    
1356                                  // While panning, we deactivate the rendering. So the tasts are                                  // While panning, we deactivate the rendering. So the tasks are
1357                                  // ready to start when the panning os done.                                  // ready to start when the panning is finished.
1358                                  if (bgExecuter != null)                                  if (bgExecuter != null && bgExecuter.isRunning())
1359                                          bgExecuter.cancelTask();                                          bgExecuter.cancelTask();
1360                                  if (localExecuter != null)                                  if (localExecuter.isRunning())
1361                                          localExecuter.cancelTask();                                          localExecuter.cancelTask();
1362                          }                          }
1363    
# Line 1357  public class XMapPane extends JPanel { Line 1404  public class XMapPane extends JPanel {
1404                  } else if ((getState() == XMapPane.ZOOM_IN)                  } else if ((getState() == XMapPane.ZOOM_IN)
1405                                  || (getState() == XMapPane.ZOOM_OUT)                                  || (getState() == XMapPane.ZOOM_OUT)
1406                                  || (getState() == XMapPane.SELECT_ALL)                                  || (getState() == XMapPane.SELECT_ALL)
1407                                  || (getState() == XMapPane.SELECT_TOP)                                  || (getState() == XMapPane.SELECT_TOP)) {
                 // || (getState() == XMapPane.SELECT_ONE_FROM_TOP)  
                 ) {  
                         final Graphics graphics = getGraphics();  
1408    
1409                            // Draws a rectangle
1410                            final Graphics graphics = getGraphics();
1411                          drawRectangle(graphics, startPos, event.getPoint());                          drawRectangle(graphics, startPos, event.getPoint());
1412                            if ((lastPos.x > 0) && (lastPos.y > 0))
                         if ((lastPos.x > 0) && (lastPos.y > 0)) {  
1413                                  drawRectangle(graphics, startPos, lastPos);                                  drawRectangle(graphics, startPos, lastPos);
                         }  
   
1414                          graphics.dispose();                          graphics.dispose();
   
1415                  }                  }
   
1416          }          }
1417    
1418          /**          /**
# Line 1387  public class XMapPane extends JPanel { Line 1428  public class XMapPane extends JPanel {
1428           * completed.           * completed.
1429           */           */
1430          public void onRenderingCompleted() {          public void onRenderingCompleted() {
1431                    LOGGER.debug("complete");
1432    
1433                  repaintTimer.stop();                  repaintTimer.stop();
1434                  updateFinalImage();  
1435                  repaint();                  // We "forget" about an exception every time we complete a rendering
1436                    // thread successfully
1437                  if (renderingErrors.size() > 0)                  if (renderingErrors.size() > 0)
1438                          renderingErrors.remove(0);                          renderingErrors.remove(0);
1439    
1440                    updateFinalImage();
1441                    repaint();
1442          }          }
1443    
1444          /**          /**
# Line 1405  public class XMapPane extends JPanel { Line 1452  public class XMapPane extends JPanel {
1452           * @see MapPaneListener#onRenderingStopped(org.geotools.swing.event.MapPaneEvent)           * @see MapPaneListener#onRenderingStopped(org.geotools.swing.event.MapPaneEvent)
1453           */           */
1454          public void onRenderingFailed(final Exception renderingError) {          public void onRenderingFailed(final Exception renderingError) {
1455                  this.renderingErrors.add(renderingError);  
1456                    // Store the exceptions so we can show it to the user:
1457                    if (!(renderingError instanceof java.lang.IllegalArgumentException && renderingError
1458                                    .getMessage().equals(
1459                                                    "Argument \"sourceCRS\" should not be null.")))
1460                            this.renderingErrors.add(renderingError);
1461                  if (renderingErrors.size() > 3)                  if (renderingErrors.size() > 3)
1462                          renderingErrors.remove(0);                          renderingErrors.remove(0);
1463    
1464                  repaintTimer.stop();                  repaintTimer.stop();
1465    
1466                  LOGGER.warn("Rendering failed", renderingError);                  LOGGER.warn("Rendering failed", renderingError);
1467                  updateFinalImage();                  updateFinalImage();
1468                  repaint();                  repaint();
1469    
1470          }          }
1471    
         public void onRenderingPending() {  
                 // LOGGER.debug("Pending rendering updates the preview...");  
                 updateFinalImage();  
                 repaint();  
         }  
   
1472          @Override          @Override
1473          protected void paintComponent(final Graphics g) {          protected void paintComponent(final Graphics g) {
1474    
1475                    // Maybe update the cursor and maybe stop the repainting timer
1476                    updateCursor();
1477    
1478                  if (!acceptsRepaintCalls)                  if (!acceptsRepaintCalls)
1479                          return;                          return;
1480    
1481                  // Maybe update the cursor and maybe stop the repainting timer                  if (!isWellDefined())
1482                  updateCursor();                          return;
1483    
1484                  // super.paintComponent(g); // candidate for removal  //               super.paintComponent(g); // candidate for removal
1485    
1486                  boolean paintedSomething = false;                  boolean paintedSomething = false;
1487    
# Line 1448  public class XMapPane extends JPanel { Line 1499  public class XMapPane extends JPanel {
1499                                  mapAreaChanged = false;                                  mapAreaChanged = false;
1500    
1501                                  if (getMapArea().covers(oldMapArea)) {                                  if (getMapArea().covers(oldMapArea)) {
1502                                          setQuickPreviewHint(ZOOM_OUT);                                          quickPreviewHint = ZOOM_OUT;
1503                                          paintedSomething = drawScaledPreviewImage_Zoom((Graphics2D) g);                                          paintedSomething = drawScaledPreviewImage_Zoom((Graphics2D) g);
1504                                  } else if (oldMapArea.covers(getMapArea())) {                                  } else if (oldMapArea.covers(getMapArea())) {
1505                                          setQuickPreviewHint(ZOOM_IN);                                          quickPreviewHint = ZOOM_IN;
1506                                          paintedSomething = drawScaledPreviewImage_Zoom((Graphics2D) g);                                          paintedSomething = drawScaledPreviewImage_Zoom((Graphics2D) g);
1507                                  }                                  }
   
1508                          }                          }
   
                         // if (paneResized) {  
                         // paneResized = false;  
                         // disposeImages();  
                         // }  
                         //  
                         // // Start the Threads and Timers to render the image  
                         // requestStartRendering();  
   
1509                  }                  }
1510    
1511                  if (!paintedSomething) {                  if (!paintedSomething) {
# Line 1552  public class XMapPane extends JPanel { Line 1593  public class XMapPane extends JPanel {
1593                          setCursor(SwingUtil.PAN_CURSOR);                          setCursor(SwingUtil.PAN_CURSOR);
1594          }          }
1595    
         //  
         // /**  
         // * 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);  
         // }  
         // }  
   
1596          /**          /**
1597           * Entfernt einen Listener von der Map.           * Entfernt einen Listener von der Map.
1598           *           *
# Line 1592  public class XMapPane extends JPanel { Line 1611  public class XMapPane extends JPanel {
1611          private void requestStartRendering() {          private void requestStartRendering() {
1612                  if (bgExecuter != null)                  if (bgExecuter != null)
1613                          bgExecuter.cancelTask();                          bgExecuter.cancelTask();
1614                  if (localExecuter != null)  
1615                          localExecuter.cancelTask();                  localExecuter.cancelTask();
1616    
1617                  mapImageInvalid = true;                  mapImageInvalid = true;
1618                  if (paneResized) {                  if (paneResized) {
# Line 1619  public class XMapPane extends JPanel { Line 1638  public class XMapPane extends JPanel {
1638           */           */
1639          private void resetTransforms() {          private void resetTransforms() {
1640                  ReferencedEnvelope refMapEnv = new ReferencedEnvelope(mapArea,                  ReferencedEnvelope refMapEnv = new ReferencedEnvelope(mapArea,
1641                                  getContext().getCoordinateReferenceSystem());                                  getMapContext().getCoordinateReferenceSystem());
1642    
1643                  // System.out                  // System.out
1644                  // .println("paintArea in resetTeansofrms = " + getVisibleRect());                  // .println("paintArea in resetTeansofrms = " + getVisibleRect());
# Line 1688  public class XMapPane extends JPanel { Line 1707  public class XMapPane extends JPanel {
1707    
1708                          // setMapArea(localContext.getAreaOfInterest());                          // setMapArea(localContext.getAreaOfInterest());
1709    
1710                          localRenderer.setContext(localContext);                          getLocalRenderer().setContext(localContext);
1711    
1712                          this.localContext.addMapLayerListListener(localContextListener);                          this.localContext.addMapLayerListListener(localContextListener);
1713    
# Line 1702  public class XMapPane extends JPanel { Line 1721  public class XMapPane extends JPanel {
1721    
1722          }          }
1723    
1724            /**
1725             * Triggers to repaint (fast) and re-render (slow) the JMapPane.
1726             */
1727            public void refresh() {
1728                    mapImageInvalid = true;
1729                    repaint();
1730            }
1731    
1732            // /**
1733            // * Triggers to use new {@link GTRenderer} and refresh the map. Should be
1734            // * called after {@link Style}s have been changed because GTRenderer is
1735            // * otherwise not working well.
1736            // */
1737            // public void refreshRenderers() {
1738            // localRenderer = GTUtil.createGTRenderer();
1739            // setLocalContext(getMapContext());
1740            // mapImageInvalid = true;
1741            // repaint();
1742            // }
1743    
1744          public boolean setMapArea(final Envelope newMapArea) {          public boolean setMapArea(final Envelope newMapArea) {
1745                  return setMapArea(new ReferencedEnvelope(newMapArea, getContext()                  if (newMapArea == null)
1746                            return false;
1747                    if (getMapContext().getCoordinateReferenceSystem() == null)
1748                            return false;
1749                    return setMapArea(new ReferencedEnvelope(newMapArea, getMapContext()
1750                                  .getCoordinateReferenceSystem()));                                  .getCoordinateReferenceSystem()));
1751          }          }
1752    
# Line 1713  public class XMapPane extends JPanel { Line 1756  public class XMapPane extends JPanel {
1756           *         has been triggered.           *         has been triggered.
1757           */           */
1758          public boolean setMapArea(final ReferencedEnvelope newMapArea) {          public boolean setMapArea(final ReferencedEnvelope newMapArea) {
                 //                
                 // if (getVisibleRect().getWidth() == 0 || (!isVisible()) ||  
                 // getVisibleRect().getHeight() == 0) {  
                 // // The window is not yet defined.. so we can not really determine the  
                 // asepect ratio for the mapPane. We store this as a candidate mappane  
                 // and handle it when the component resizes or becomes visible)  
                 // mapAreaCondidate = newMapArea;  
                 // }  
1759    
1760                  if (newMapArea == null                  if (newMapArea == null
1761                                  || bestAllowedMapArea(newMapArea).equals(mapArea)) {                                  || bestAllowedMapArea(newMapArea).equals(mapArea)) {
# Line 1778  public class XMapPane extends JPanel { Line 1813  public class XMapPane extends JPanel {
1813    
1814                  mapAreaChanged = true;                  mapAreaChanged = true;
1815    
1816                  repaint(100);                  repaint(200); // Do not remove it!
1817    
1818                  requestStartRendering();                  requestStartRendering();
1819    
# Line 1791  public class XMapPane extends JPanel { Line 1826  public class XMapPane extends JPanel {
1826           * @param if <code>null</code>, white is used.           * @param if <code>null</code>, white is used.
1827           */           */
1828          public void setMapBackgroundColor(Color bgColor) {          public void setMapBackgroundColor(Color bgColor) {
                 if (bgColor == null)  
                         bgColor = Color.WHITE;  
1829                  this.mapBackgroundColor = bgColor;                  this.mapBackgroundColor = bgColor;
1830          }          }
1831    
# Line 1867  public class XMapPane extends JPanel { Line 1900  public class XMapPane extends JPanel {
1900          }          }
1901    
1902          /**          /**
1903           *           * If <code>true</code>, allow the {@link XMapPane} to process #repaint()
1904           * @param b           * requests. Otherwise the map will not paint anything and not start any
1905             * rendering {@link Thread}s.
1906           */           */
1907          public void setPainting(final boolean b) {          public void setPainting(final boolean b) {
1908                  acceptsRepaintCalls = b;                  acceptsRepaintCalls = b;
1909          }                  if (acceptsRepaintCalls == true)
1910                            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;  
   
1911          }          }
1912    
1913          private void setRendererHints(final Map<Object, Object> rendererHints) {          private void setRendererHints(final Map<Object, Object> rendererHints) {
# Line 1932  public class XMapPane extends JPanel { Line 1953  public class XMapPane extends JPanel {
1953          private void startRendering() {          private void startRendering() {
1954    
1955                  if (!isWellDefined() || !acceptsRepaintCalls) {                  if (!isWellDefined() || !acceptsRepaintCalls) {
1956                            // if we are not ready to start rendering, try it again the next
1957                            // time the timer is chacking the flag.
1958                          requestStartRendering = true;                          requestStartRendering = true;
1959                          return;                          return;
1960                  }                  }
# Line 1941  public class XMapPane extends JPanel { Line 1964  public class XMapPane extends JPanel {
1964                          bgExecuter.cancelTask();                          bgExecuter.cancelTask();
1965                  }                  }
1966    
1967                  if (localExecuter != null) {                  localExecuter.cancelTask();
                         localExecuter.cancelTask();  
                 }  
1968    
1969                  final Rectangle curPaintArea = getVisibleRect();                  final Rectangle curPaintArea = getVisibleRect();
1970    
# Line 1966  public class XMapPane extends JPanel { Line 1987  public class XMapPane extends JPanel {
1987                          // (Graphics2D) getBgImage().getGraphics(), createGTRenderer);                          // (Graphics2D) getBgImage().getGraphics(), createGTRenderer);
1988                  }                  }
1989    
1990                  if (getContext() != null) {                  if (getMapContext() != null) {
1991                          // localExecuter = new RenderingExecutor(this, 150l);                          // localExecuter = new RenderingExecutor(this, 150l);
1992                          // LOGGER.debug("starting local renderer:");                          // LOGGER.debug("starting local renderer:");
1993    
1994                          localRenderer.setJava2DHints(getJava2dHints());                          getLocalRenderer().setJava2DHints(getJava2dHints());
1995                          localRenderer.setRendererHints(getRendererHints());                          getLocalRenderer().setRendererHints(getRendererHints());
1996    
1997                          final boolean submitted = localExecuter.submit(getMapArea(),                          final boolean submitted = localExecuter.submit(getMapArea(),
1998                                          curPaintArea, (Graphics2D) getLocalImage().getGraphics(),                                          curPaintArea, (Graphics2D) getLocalImage().getGraphics(),
1999                                          localRenderer                                          getLocalRenderer());
                         // , getWorldToScreenTransform()  
                                         );  
2000                          if (submitted)                          if (submitted)
2001                                  repaintTimer.restart();                                  repaintTimer.restart();
2002                          else                          else
# Line 2052  public class XMapPane extends JPanel { Line 2071  public class XMapPane extends JPanel {
2071                  // if the renderers have stopped, also stop the timer that is updating                  // if the renderers have stopped, also stop the timer that is updating
2072                  // the final image                  // the final image
2073                  if (bgExecuter != null && bgExecuter.isRunning()                  if (bgExecuter != null && bgExecuter.isRunning()
2074                                  || localExecuter != null && localExecuter.isRunning()) {                                  || localExecuter.isRunning()) {
2075                          setCursor(WAIT_CURSOR);                          setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
2076                          return;                          return;
2077                  } else {                  } else {
2078                          // Allow one last rendering                          // Allow one last rendering
# Line 2166  public class XMapPane extends JPanel { Line 2185  public class XMapPane extends JPanel {
2185    
2186                  // If the rendering process is still running, indicate this is the image                  // If the rendering process is still running, indicate this is the image
2187                  if (forceWait || bgExecuter != null && bgExecuter.isRunning()                  if (forceWait || bgExecuter != null && bgExecuter.isRunning()
2188                                  || localExecuter != null && localExecuter.isRunning()) {                                  || localExecuter.isRunning()) {
2189    
2190                          y += 8;                          y += 8;
2191    
# Line 2185  public class XMapPane extends JPanel { Line 2204  public class XMapPane extends JPanel {
2204                          y += 24;                          y += 24;
2205                  }                  }
2206    
2207                  if (renderingErrors != null) {                  if (!renderingErrors.isEmpty() && isShowExceptions()) {
2208    
2209                          final Color c = graphics.getColor();                          final Color c = graphics.getColor();
2210                          graphics.setFont(errorFont);                          graphics.setFont(errorFont);
2211    
2212                          for (Exception ex : renderingErrors) {                          for (Exception ex : renderingErrors) {
2213    
                                 if (ex instanceof java.lang.IllegalArgumentException  
                                                 && ex.getMessage().equals(  
                                                                 "Argument \"sourceCRS\" should not be null."))  
                                         continue;  
   
2214                                  String errStr = ex.getLocalizedMessage();                                  String errStr = ex.getLocalizedMessage();
2215    
2216                                  if (errStr == null)                                  if (errStr == null)
# Line 2204  public class XMapPane extends JPanel { Line 2218  public class XMapPane extends JPanel {
2218                                  if (errStr == null)                                  if (errStr == null)
2219                                          errStr = "unknown error: " + ex.getClass().getSimpleName();                                          errStr = "unknown error: " + ex.getClass().getSimpleName();
2220    
2221                                  graphics.setColor(Color.WHITE);                                  graphics.setColor(getMapBackgroundColor());
2222                                  graphics.drawString(errStr, 5, y);                                  graphics.drawString(errStr, 5, y);
2223                                  graphics.setColor(Color.RED);                                  graphics.setColor(Color.RED);
2224                                  graphics.drawString(errStr, 6, y + 1);                                  graphics.drawString(errStr, 6, y + 1);
# Line 2227  public class XMapPane extends JPanel { Line 2241  public class XMapPane extends JPanel {
2241          public void zoomTo(          public void zoomTo(
2242                          final FeatureCollection<SimpleFeatureType, SimpleFeature> features) {                          final FeatureCollection<SimpleFeatureType, SimpleFeature> features) {
2243    
2244                  final CoordinateReferenceSystem mapCRS = getContext()                  // if (!isWellDefined()) return;
2245    
2246                    final CoordinateReferenceSystem mapCRS = getMapContext()
2247                                  .getCoordinateReferenceSystem();                                  .getCoordinateReferenceSystem();
2248                  final CoordinateReferenceSystem fCRS = features.getSchema()                  final CoordinateReferenceSystem fCRS = features.getSchema()
2249                                  .getGeometryDescriptor().getCoordinateReferenceSystem();                                  .getGeometryDescriptor().getCoordinateReferenceSystem();
2250    
2251                  double width = mapArea.getWidth();                  ReferencedEnvelope _mapArea;
2252                  double height = mapArea.getHeight();                  if (mapArea == null)
2253                            _mapArea = features.getBounds();
2254                    else
2255                            _mapArea = getMapArea();
2256                    double width = _mapArea.getWidth();
2257                    double height = _mapArea.getHeight();
2258                  final double ratio = height / width;                  final double ratio = height / width;
2259    
2260                  if (features == null || features.size() == 0) {                  if (features == null || features.size() == 0) {
# Line 2285  public class XMapPane extends JPanel { Line 2306  public class XMapPane extends JPanel {
2306                  } else {                  } else {
2307                          final ReferencedEnvelope fBounds = features.getBounds();                          final ReferencedEnvelope fBounds = features.getBounds();
2308    
2309                          Envelope bounds;                          ReferencedEnvelope bounds;
2310                          if (!mapCRS.equals(fCRS)) {                          if (!mapCRS.equals(fCRS)) {
2311                                  bounds = JTSUtil.transformEnvelope(fBounds, fCRS, mapCRS);                                  bounds = JTSUtil.transformEnvelope(fBounds, mapCRS);
2312                          } else {                          } else {
2313                                  bounds = fBounds;                                  bounds = fBounds;
2314                          }                          }
# Line 2352  public class XMapPane extends JPanel { Line 2373  public class XMapPane extends JPanel {
2373                  setMapArea(newMapArea);                  setMapArea(newMapArea);
2374          }          }
2375    
2376            /**
2377             * Shall non-fatal rendering exceptions be reported in the mappane or be
2378             * dropped quitely.
2379             */
2380            public void setShowExceptions(boolean showExceptions) {
2381                    this.showExceptions = showExceptions;
2382            }
2383    
2384            /**
2385             * Shall exceptions be reported in the mappane?
2386             */
2387            public boolean isShowExceptions() {
2388                    return showExceptions;
2389            }
2390    
2391            public GTRenderer getLocalRenderer() {
2392                    return localRenderer;
2393            }
2394    
2395  }  }

Legend:
Removed from v.544  
changed lines
  Added in v.560

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26