/[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 516 by alfonx, Wed Nov 11 21:17:33 2009 UTC revision 529 by alfonx, Wed Nov 18 20:47:00 2009 UTC
# Line 20  package gtmig.org.geotools.swing; Line 20  package gtmig.org.geotools.swing;
20   * In addition to   * In addition to
21   */   */
22    
 import java.awt.AlphaComposite;  
23  import java.awt.Color;  import java.awt.Color;
24  import java.awt.Cursor;  import java.awt.Cursor;
25  import java.awt.Font;  import java.awt.Font;
# Line 41  import java.awt.geom.AffineTransform; Line 40  import java.awt.geom.AffineTransform;
40  import java.awt.geom.NoninvertibleTransformException;  import java.awt.geom.NoninvertibleTransformException;
41  import java.awt.geom.Point2D;  import java.awt.geom.Point2D;
42  import java.awt.image.BufferedImage;  import java.awt.image.BufferedImage;
 import java.beans.PropertyChangeEvent;  
 import java.beans.PropertyChangeListener;  
43  import java.io.IOException;  import java.io.IOException;
44  import java.util.HashMap;  import java.util.HashMap;
45  import java.util.Map;  import java.util.Map;
46  import java.util.Vector;  import java.util.Vector;
47    
 import javax.swing.JFrame;  
48  import javax.swing.JPanel;  import javax.swing.JPanel;
49  import javax.swing.Timer;  import javax.swing.Timer;
50    
# Line 67  import org.geotools.referencing.CRS; Line 63  import org.geotools.referencing.CRS;
63  import org.geotools.renderer.GTRenderer;  import org.geotools.renderer.GTRenderer;
64  import org.geotools.renderer.label.LabelCacheImpl;  import org.geotools.renderer.label.LabelCacheImpl;
65  import org.geotools.renderer.lite.LabelCache;  import org.geotools.renderer.lite.LabelCache;
66    import org.geotools.renderer.lite.StreamingRenderer;
67  import org.geotools.swing.JMapPane;  import org.geotools.swing.JMapPane;
68  import org.geotools.swing.event.MapMouseEvent;  import org.geotools.swing.event.MapMouseEvent;
69  import org.geotools.swing.event.MapPaneEvent;  import org.geotools.swing.event.MapPaneEvent;
# Line 83  import schmitzm.geotools.JTSUtil; Line 80  import schmitzm.geotools.JTSUtil;
80  import schmitzm.geotools.gui.SelectableXMapPane;  import schmitzm.geotools.gui.SelectableXMapPane;
81  import schmitzm.geotools.map.event.JMapPaneListener;  import schmitzm.geotools.map.event.JMapPaneListener;
82  import schmitzm.geotools.map.event.MapLayerAdapter;  import schmitzm.geotools.map.event.MapLayerAdapter;
83    import schmitzm.lang.LangUtil;
84  import schmitzm.swing.SwingUtil;  import schmitzm.swing.SwingUtil;
85  import skrueger.geotools.RenderingExecutor;  import skrueger.geotools.RenderingExecutor;
86    import skrueger.swing.formatter.MbDecimalFormatter;
87    
88  import com.vividsolutions.jts.geom.Coordinate;  import com.vividsolutions.jts.geom.Coordinate;
89  import com.vividsolutions.jts.geom.Envelope;  import com.vividsolutions.jts.geom.Envelope;
90  import com.vividsolutions.jts.geom.Geometry;  import com.vividsolutions.jts.geom.Geometry;
91    
92  public class XMapPane extends JPanel implements PropertyChangeListener {  public class XMapPane extends JPanel {
93          private static Logger LOGGER = Logger.getLogger(XMapPane.class);          private static Logger LOGGER = Logger.getLogger(XMapPane.class);
94    
95          /**          /**
# Line 174  public class XMapPane extends JPanel imp Line 173  public class XMapPane extends JPanel imp
173           * @see #setState(int)           * @see #setState(int)
174           */           */
175          public static final int SELECT_ONE_FROM_TOP = 104;          public static final int SELECT_ONE_FROM_TOP = 104;
176  //  
177  //      /**          /**
178  //       * While threads are working, we call {@link XMapPane#updateFinalImage()}           * While threads are working, we call {@link XMapPane#updateFinalImage()}
179  //       * regularly and repaint(). This {@link Timer} is stopped when all renderers           * regularly and repaint(). This {@link Timer} is stopped when all renderers
180  //       * have finished.           * have finished.
181  //       */           */
182  //      final private Timer repainterTimer;          final private Timer repainterTimer;
183  //  
184  //      /**          /**
185  //       * While the {@link #bgExecuter} and {@link #localExecuter} are rendering,           * While the {@link #bgExecuter} and {@link #localExecuter} are rendering,
186  //       * the {@link #repainterTimer} is regularly updating the {@link #finalImage}           * the {@link #repainterTimer} is regularly updating the {@link #finalImage}
187  //       * with previews.           * with previews.
188  //       */           */
189  //      public static final long REPEATING_REPAINT_DELAY = 500;          public static final long REPEATING_REPAINT_DELAY = 500;
190  //        
191  //      /**          /**
192  //       * The initial delay in milliseconds until the {@link #finalImage} is updated the first time.             * The initial delay in milliseconds until the {@link #finalImage} is
193  //       */           * updated the first time.
194  //      public static final long INITIAL_REPAINT_DELAY = 333;           */
195            public static final long INITIAL_REPAINT_DELAY = 1000;
196    
197          protected RenderingExecutor bgExecuter;          protected RenderingExecutor bgExecuter;
198    
# Line 216  public class XMapPane extends JPanel imp Line 216  public class XMapPane extends JPanel imp
216          protected Envelope oldMapArea = null;          protected Envelope oldMapArea = null;
217    
218          /**          /**
          * the size of the pane last time we drew  
          */  
         protected Rectangle oldRect = null;  
   
         /**  
          * The Renderer for the Background uses this Image. When set to null, please  
          * dispose the {@link Graphics2D}  
          */  
         protected BufferedImage bgImage;  
   
         /**  
          * The Renderer for the LocalLayers uses this Image. When set to null,  
          * please dispose this {@link Graphics2D}  
          */  
         volatile protected BufferedImage localImage;  
   
         /**  
219           * compass and icon are rendered into this image           * compass and icon are rendered into this image
220           */           */
221          // protected BufferedImage gadgetsImage;          // protected BufferedImage gadgetsImage;
# Line 360  public class XMapPane extends JPanel imp Line 343  public class XMapPane extends JPanel imp
343           * @param localContext           * @param localContext
344           *            - {@link MapContext} of layer to render.           *            - {@link MapContext} of layer to render.
345           */           */
346          public XMapPane(final MapContext localContext,          public XMapPane(final MapContext localContext_,
347                          Map<Object, Object> rendererHints) {                          Map<Object, Object> rendererHints) {
348                  super(true);                  super(true);
349    
# Line 368  public class XMapPane extends JPanel imp Line 351  public class XMapPane extends JPanel imp
351    
352                  setOpaque(true);                  setOpaque(true);
353    
354                  setLocalContext(localContext);                  localRenderer = GTUtil.createGTRenderer();
355                    localRenderer.setJava2DHints(getJava2dHints());
356    
357                    if (localContext_ != null)
358                            setLocalContext(localContext_);
359    
360                  /**                  /**
361                   * Adding the #zoomMapPaneMouseListener                   * Adding the #zoomMapPaneMouseListener
# Line 415  public class XMapPane extends JPanel imp Line 402  public class XMapPane extends JPanel imp
402    
403                  });                  });
404    
405  //              repainterTimer = new Timer((int) REPEATING_REPAINT_DELAY,                  repainterTimer = new Timer((int) REPEATING_REPAINT_DELAY,
406  //                              new ActionListener() {                                  new ActionListener() {
 //  
 //                                      @Override  
 //                                      public void actionPerformed(ActionEvent e) {  
 //                                              updateFinalImage();  
 //                                              XMapPane.this.repaint();  
 //                                      }  
 //                              });  
 //              repainterTimer.setInitialDelay(300);  
 //              repainterTimer.setRepeats(true);  
407    
408                  startRenderThreadsTimer = new Timer(200, new ActionListener() {                                          @Override
409                                            public void actionPerformed(ActionEvent e) {
410                                                    updateFinalImage();
411                                                    XMapPane.this.repaint();
412                                            }
413                                    });
414                    repainterTimer.setInitialDelay(300);
415                    repainterTimer.setRepeats(true);
416    
417                    startRenderThreadsTimer = new Timer(100, new ActionListener() {
418    
419                          @Override                          @Override
420                          public void actionPerformed(ActionEvent e) {                          public void actionPerformed(ActionEvent e) {
421                                  synchronized (requestStartRendering) {                                  synchronized (requestStartRendering) {
422                                          if (requestStartRendering && isValid())                                          if (requestStartRendering && isWellDefined()) {
423                                                  startRendering();  
424                                          requestStartRendering = false;                                                  if (localExecuter.isRunning()) {
425                                                            localExecuter.cancelTask();
426                                                    } else {
427                                                            requestStartRendering = false;
428                                                            startRendering();
429                                                    }
430                                            }
431                                  }                                  }
432                          }                          }
433                  });                  });
# Line 620  public class XMapPane extends JPanel imp Line 613  public class XMapPane extends JPanel imp
613                          setCursor(WAIT_CURSOR);                          setCursor(WAIT_CURSOR);
614                          return;                          return;
615                  } else {                  } else {
616  //                      // Allow one last rendering                          // Allow one last rendering
617  //                      if (repainterTimer.isRunning()) {                          if (repainterTimer.isRunning()) {
618  //                              repainterTimer.stop();                                  repainterTimer.stop();
619  //                              updateFinalImage();                                  updateFinalImage();
620  //                              repaint();                                  repaint();
621  //                      }                          }
622                  }                  }
623    
624                  // wenn manueller Cursor gesetzt ist, dann diesen verwenden (unabhaengig                  // wenn manueller Cursor gesetzt ist, dann diesen verwenden (unabhaengig
# Line 741  public class XMapPane extends JPanel imp Line 734  public class XMapPane extends JPanel imp
734                  public void layerAdded(final MapLayerListEvent event) {                  public void layerAdded(final MapLayerListEvent event) {
735                          event.getLayer().addMapLayerListener(localMapLayerListener);                          event.getLayer().addMapLayerListener(localMapLayerListener);
736    
737    //                      System.out.println("added a layer context, now it'S "
738    //                                      + getContext().getLayerCount() + " layers");
739    //                      ;
740    
741                            localRenderer.setContext(getContext());
742    
743                          if (localContext.getLayers().length == 1) { // the first one                          if (localContext.getLayers().length == 1) { // the first one
744                                  // if the Area of Interest is unset, the LayerBounds are used                                  // if the Area of Interest is unset, the LayerBounds are used
745                                  if (!setMapArea(localContext.getAreaOfInterest()))                                  if (!setMapArea(localContext.getAreaOfInterest()))
# Line 762  public class XMapPane extends JPanel imp Line 761  public class XMapPane extends JPanel imp
761                                  event.getLayer().removeMapLayerListener(localMapLayerListener);                                  event.getLayer().removeMapLayerListener(localMapLayerListener);
762                          // mapImageInvalid = true;                          // mapImageInvalid = true;
763                          // repaint();                          // repaint();
764                            localRenderer.setContext(getContext());
765                          requestStartRendering();                          requestStartRendering();
766                  }                  }
767    
# Line 769  public class XMapPane extends JPanel imp Line 769  public class XMapPane extends JPanel imp
769                  public void layerChanged(final MapLayerListEvent event) {                  public void layerChanged(final MapLayerListEvent event) {
770                          // mapImageInvalid = true;                          // mapImageInvalid = true;
771                          // repaint();                          // repaint();
772                            localRenderer.setContext(getContext());
773                          requestStartRendering();                          requestStartRendering();
774                  }                  }
775    
# Line 776  public class XMapPane extends JPanel imp Line 777  public class XMapPane extends JPanel imp
777                  public void layerMoved(final MapLayerListEvent event) {                  public void layerMoved(final MapLayerListEvent event) {
778                          // mapImageInvalid = true;                          // mapImageInvalid = true;
779                          // repaint();                          // repaint();
780                            localRenderer.setContext(getContext());
781                          requestStartRendering();                          requestStartRendering();
782                  }                  }
783          };          };
# Line 852  public class XMapPane extends JPanel imp Line 854  public class XMapPane extends JPanel imp
854    
855                  @Override                  @Override
856                  public void layerChanged(MapLayerEvent event) {                  public void layerChanged(MapLayerEvent event) {
857                            localRenderer.setContext(getContext()); // betters for SLD changes?!
858                          // Change of SLD for example                          // Change of SLD for example
859                          // mapImageInvalid = true;                          // mapImageInvalid = true;
860                          // repaint();                          // repaint();
# Line 914  public class XMapPane extends JPanel imp Line 917  public class XMapPane extends JPanel imp
917    
918          public MapContext getContext() {          public MapContext getContext() {
919                  if (localContext == null) {                  if (localContext == null) {
920                          this.localContext = new DefaultMapContext();                          setLocalContext(new DefaultMapContext());
                         this.localContext.addMapLayerListListener(localContextListener);  
921                  }                  }
922                  return localContext;                  return localContext;
923          }          }
# Line 942  public class XMapPane extends JPanel imp Line 944  public class XMapPane extends JPanel imp
944                  this.localContext = context;                  this.localContext = context;
945    
946                  if (context != null) {                  if (context != null) {
947    
948                          setMapArea(localContext.getAreaOfInterest());                          setMapArea(localContext.getAreaOfInterest());
949    
950                            localRenderer.setContext(localContext);
951    
952                          this.localContext.addMapLayerListListener(localContextListener);                          this.localContext.addMapLayerListListener(localContextListener);
953    
954                          // adding listener to all layers                          // adding listener to all layers
# Line 991  public class XMapPane extends JPanel imp Line 996  public class XMapPane extends JPanel imp
996           */           */
997          public Envelope getMapArea() {          public Envelope getMapArea() {
998                  if (mapArea == null) {                  if (mapArea == null) {
999                          final Rectangle paneBounds = getBounds();                          ReferencedEnvelope mapArea_ = null;
   
1000                          try {                          try {
1001                                  mapArea = localContext.getLayerBounds();                                  mapArea_ = localContext.getLayerBounds();
1002                          } catch (final IOException e) {                          } catch (final IOException e) {
1003                                  LOGGER.warn("context.getLayerBounds()", e);                                  LOGGER.warn("context.getLayerBounds()", e);
1004                          }                          }
1005    
1006                          if (mapArea != null) {                          if (mapArea_ != null) {
                                 /* either the viewer size has changed or we've done a reset */  
1007                                  mapImageInvalid = true; /* note we need to redraw */                                  mapImageInvalid = true; /* note we need to redraw */
1008                                  oldRect = paneBounds; /* store what the current size is */                                  setMapArea(mapArea_);
                                 mapArea = bestAllowedMapArea(mapArea);  
1009                          }                          }
1010                  }                  }
1011    
# Line 1076  public class XMapPane extends JPanel imp Line 1078  public class XMapPane extends JPanel imp
1078                  mapAreaChanged = true;                  mapAreaChanged = true;
1079                  repaint();                  repaint();
1080    
1081  //              LOGGER.debug("New maparea = " + mapArea);                  // LOGGER.debug("New maparea = " + mapArea);
1082                  return true;                  return true;
1083          }          }
1084    
# Line 1109  public class XMapPane extends JPanel imp Line 1111  public class XMapPane extends JPanel imp
1111    
1112          public static final int NONE = -123;          public static final int NONE = -123;
1113    
1114          private RenderingExecutor localExecuter;          private RenderingExecutor localExecuter = new RenderingExecutor(this);
   
         private BufferedImage finalImage;  
1115    
1116          /**          /**
1117           * A flag set it {@link #setMapArea(Envelope)} to indicated the           * A flag set it {@link #setMapArea(Envelope)} to indicated the
# Line 1122  public class XMapPane extends JPanel imp Line 1122  public class XMapPane extends JPanel imp
1122           **/           **/
1123          private boolean mapAreaChanged = false;          private boolean mapAreaChanged = false;
1124    
         private JFrame finalImageFrame;  
   
1125          private volatile Boolean requestStartRendering = false;          private volatile Boolean requestStartRendering = false;
         private BufferedImage preFinalImage;  
1126    
1127          protected void paintComponent(final Graphics g) {          /**
1128                  // Maybe update the cursor           * The Renderer for the Background uses this Image. When set to null, please
1129                  updateCursor();           * dispose the {@link Graphics2D}
1130             */
1131            private BufferedImage bgImage;// = new
1132            // BufferedImage(600,600,BufferedImage.TYPE_INT_ARGB);
1133    
1134            private BufferedImage finalImage;
1135    
1136            /**
1137             * The Renderer for the LocalLayers uses this Image. When set to null,
1138             * please dispose this {@link Graphics2D}
1139             */
1140            private BufferedImage localImage;// = new
1141            // BufferedImage(600,600,BufferedImage.TYPE_INT_ARGB);
1142            private BufferedImage preFinalImage;// = new
1143    
1144            // BufferedImage(600,600,BufferedImage.TYPE_INT_ARGB);
1145    
1146            protected void paintComponent(final Graphics g) {
1147                  if (!acceptsRepaintCalls)                  if (!acceptsRepaintCalls)
1148                          return;                          return;
1149                    
1150                    // Maybe update the cursor
1151                    updateCursor();
1152                    
1153                    super.paintComponent(g);
1154    
1155                  boolean paintedSomething = false;                  boolean paintedSomething = false;
1156    
# Line 1161  public class XMapPane extends JPanel imp Line 1179  public class XMapPane extends JPanel imp
1179    
1180                          if (paneResized) {                          if (paneResized) {
1181                                  paneResized = false;                                  paneResized = false;
1182                                  preFinalImage = null;  
1183                                  finalImage = null;                                  // if (preFinalImage != null)
1184                                  localImage = null;                                  // preFinalImage.flush();
1185                                  bgImage = null;                                  // preFinalImage = null;
1186                                    // if (finalImage != null) {
1187                                    // finalImage.flush();
1188                                    // }
1189                                    // finalImage = null;
1190                                    // if (localImage != null)
1191                                    // localImage.flush();
1192                                    // localImage = null;
1193                                    // if (bgImage != null)
1194                                    // bgImage.flush();
1195                                    // bgImage = null;
1196    
1197                                  // gadgetsImage = null;                                  // gadgetsImage = null;
1198                          }                          }
1199    
# Line 1178  public class XMapPane extends JPanel imp Line 1207  public class XMapPane extends JPanel imp
1207                          // TODO Should just paint the getFinalImage(). Update should be                          // TODO Should just paint the getFinalImage(). Update should be
1208                          // called by timer every 300ms, and the repaint() until all threads                          // called by timer every 300ms, and the repaint() until all threads
1209                          // are done.                          // are done.
1210                          g.drawImage(getFinalImage(), 0, 0, this);                          g.drawImage(getFinalImage(), 0, 0, null);
1211    
1212                            g.dispose();
1213    
1214                          paintedSomething = true;                          paintedSomething = true;
1215                  }                  }
# Line 1196  public class XMapPane extends JPanel imp Line 1227  public class XMapPane extends JPanel imp
1227                  if (localExecuter != null)                  if (localExecuter != null)
1228                          localExecuter.cancelTask();                          localExecuter.cancelTask();
1229                  requestStartRendering = true;                  requestStartRendering = true;
1230    
1231          }          }
1232    
1233          /**          /**
# Line 1254  public class XMapPane extends JPanel imp Line 1286  public class XMapPane extends JPanel imp
1286           * to give the user something to look at while we are rendering. Method           * to give the user something to look at while we are rendering. Method
1287           * should be called after {@link #setMapArea(Envelope)} has been set to the           * should be called after {@link #setMapArea(Envelope)} has been set to the
1288           * new mapArea and transform has been reset.<br/>           * new mapArea and transform has been reset.<br/>
          * This method does nothing if the {@link #lastRenderingDuration} is smaller  
          * then a trashhold.  
1289           *           *
1290           * @param g           * @param g
1291           *            Graphics2D to paint the preview into           *            Graphics2D to paint the preview into
# Line 1300  public class XMapPane extends JPanel imp Line 1330  public class XMapPane extends JPanel imp
1330                  SwingUtil.clearAround(graphics, painedArea, visibleArea);                  SwingUtil.clearAround(graphics, painedArea, visibleArea);
1331    
1332                  addGadgets(graphics);                  addGadgets(graphics);
                 // graphics.drawImage(getGadgetsImage(), 0,0, (int)  
                 // visibleArea.getMaxX(), (int) visibleArea.getMaxY(),null);  
1333    
1334                  quickPreviewHint = 0;                  quickPreviewHint = 0;
1335    
1336                  graphics.dispose();                  graphics.dispose();
1337    
1338                  // Something has been drawn                  // Something has been drawn
1339                  return true;                  return true;
1340          }          }
# Line 1321  public class XMapPane extends JPanel imp Line 1350  public class XMapPane extends JPanel imp
1350                  if (mapImage != null)                  if (mapImage != null)
1351                          graphics.drawImage(mapImage, getBounds().width                          graphics.drawImage(mapImage, getBounds().width
1352                                          - mapImage.getWidth() - 10, getBounds().height                                          - mapImage.getWidth() - 10, getBounds().height
1353                                          - mapImage.getHeight() - 10, this);                                          - mapImage.getHeight() - 10, null);
1354    
1355                  // If still rendering, paint a gray shadow or so...                  // If still rendering, paint a gray shadow or so...
1356                  if (bgExecuter != null && bgExecuter.isRunning()                  if (bgExecuter != null && bgExecuter.isRunning()
1357                                  || localExecuter != null && localExecuter.isRunning()) {                                  || localExecuter != null && localExecuter.isRunning()) {
1358    
1359                            Color c = graphics.getColor();
1360                          graphics.setColor(Color.BLACK);                          graphics.setColor(Color.BLACK);
1361    
1362                          graphics.setFont(waitFont);                          graphics.setFont(waitFont);
1363                          graphics.drawString("Wait...", 40, 70);                          graphics.drawString("Wait...", 40, 70); // i8n
1364    
1365                            graphics.setColor(c);
1366                  }                  }
1367    
1368          }          }
# Line 1339  public class XMapPane extends JPanel imp Line 1372  public class XMapPane extends JPanel imp
1372           *           *
1373           * @return           * @return
1374           */           */
1375          synchronized protected BufferedImage updateFinalImage() {          synchronized protected Image updateFinalImage() {
   
                 final Graphics2D finalG = (Graphics2D) getFinalImage().getGraphics();  
                 finalG.setBackground(getMapBackgroundColor());  
1376    
1377                  // Render the two map images first, into the preFinalImage                  // Render the two map images first, into the preFinalImage
1378                  {                  if (bgExecuter != null) {
1379                          final Graphics2D preFinalG = (Graphics2D) getPreFinalImage()                          final Graphics2D preFinalG = (Graphics2D) getPreFinalImage()
1380                                          .getGraphics();                                          .getGraphics();
1381                          preFinalG.setBackground(getMapBackgroundColor());                          preFinalG.setBackground(getMapBackgroundColor());
1382    
1383                          preFinalG.drawImage(getBgImage(), 0, 0, getMapBackgroundColor(),                          preFinalG.drawImage(getBgImage(), 0, 0, getMapBackgroundColor(),
1384                                          null);                                          null);
1385    
1386                          // // Draw the local layers image                          // // Draw the local layers image
1387                          preFinalG.drawImage(getLocalImage(), 0, 0, null);                          preFinalG.drawImage(getLocalImage(), 0, 0, null);
1388                          preFinalG.dispose();                          preFinalG.dispose();
1389    
1390                    } else {
1391                            preFinalImage = getLocalImage();
1392                  }                  }
1393    
1394                    final Graphics2D finalG = getFinalImage().createGraphics();
1395                    finalG.setBackground(getMapBackgroundColor());
1396                  finalG.drawImage(getPreFinalImage(), imageOrigin.x, imageOrigin.y,                  finalG.drawImage(getPreFinalImage(), imageOrigin.x, imageOrigin.y,
1397                                  getMapBackgroundColor(), null);                                  getMapBackgroundColor(), null);
1398    
                 // System.out.println(new Date().getTime() - startTime +  
                 // "ms for update");  
                 //  
                 // if (finalImageFrame == null) {  
                 // finalImageFrame = new JFrame();  
                 // finalImageFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
                 // SwingUtil.setRelativeFramePosition(finalImageFrame, 0, 0);  
                 // }  
                 // finalImageFrame.setContentPane(new JLabel(new  
                 // ImageIcon(finalImage)));  
                 // finalImageFrame.pack();  
                 // finalImageFrame.setVisible(true);  
   
1399                  final int finalImageHeight = getFinalImage().getHeight(null);                  final int finalImageHeight = getFinalImage().getHeight(null);
1400                  final int finalImageWidth = getFinalImage().getWidth(null);                  final int finalImageWidth = getFinalImage().getWidth(null);
1401    
# Line 1381  public class XMapPane extends JPanel imp Line 1404  public class XMapPane extends JPanel imp
1404                  SwingUtil.clearAround(finalG, painedArea, getVisibleRect());                  SwingUtil.clearAround(finalG, painedArea, getVisibleRect());
1405    
1406                  addGadgets(finalG);                  addGadgets(finalG);
                 // finalG.drawImage(getGadgetsImage(), 0, 0, null);  
1407    
1408                  finalG.dispose();                  finalG.dispose();
1409    
1410                  return finalImage;                  return finalImage;
1411          }          }
1412    
1413          /*          private BufferedImage getFinalImage() {
1414           * Set alpha composite. For example, pass in 1.0f to have 100% opacity pass                  //
          * in 0.25f to have 25% opacity.  
          */  
         private AlphaComposite makeComposite(float alpha) {  
                 int type = AlphaComposite.SRC_OVER;  
                 return (AlphaComposite.getInstance(type, alpha));  
         }  
   
         private Image getFinalImage() {  
   
1415                  if (finalImage == null) {                  if (finalImage == null) {
1416                          finalImage = null;                          // Rectangle curPaintArea = getVisibleRect();
1417                          Rectangle curPaintArea = getVisibleRect();                          finalImage = new BufferedImage(getBounds().width,
1418                          finalImage = new BufferedImage(curPaintArea.width,                                          getBounds().height, BufferedImage.TYPE_INT_RGB);
                                         curPaintArea.height, BufferedImage.TYPE_INT_RGB);  
1419    
1420                          requestStartRendering();                          requestStartRendering();
1421                  }                  }
# Line 1411  public class XMapPane extends JPanel imp Line 1423  public class XMapPane extends JPanel imp
1423          }          }
1424    
1425          private Image getPreFinalImage() {          private Image getPreFinalImage() {
1426                    // if (preFinalImage == null) {
1427                  if (preFinalImage == null) {                  //                      
1428                          preFinalImage = null;                  // // Rectangle curPaintArea = getVisibleRect();
1429                          Rectangle curPaintArea = getVisibleRect();                  // // preFinalImage = new BufferedImage(curPaintArea.width,
1430                    // // curPaintArea.height, BufferedImage.TYPE_INT_RGB);
1431                          preFinalImage = new BufferedImage(curPaintArea.width,                  //                      
1432                                          curPaintArea.height, BufferedImage.TYPE_INT_RGB);                  // preFinalImage = createImage(getBounds().width, getBounds().height);
1433                    //
1434                          requestStartRendering();                  // requestStartRendering();
1435                  }                  // }
1436                  return preFinalImage;                  return preFinalImage;
1437          }          }
1438    
# Line 1429  public class XMapPane extends JPanel imp Line 1441  public class XMapPane extends JPanel imp
1441           * cached images while setting it together.           * cached images while setting it together.
1442           **/           **/
1443          Point imageOrigin = new Point(0, 0);          Point imageOrigin = new Point(0, 0);
1444            private final GTRenderer localRenderer;
1445    
1446          /**          /**
1447           * Starts rendering on one or two threads           * Starts rendering on one or two threads
# Line 1438  public class XMapPane extends JPanel imp Line 1451  public class XMapPane extends JPanel imp
1451                  if (!isWellDefined())                  if (!isWellDefined())
1452                          return;                          return;
1453    
1454                  if (bgExecuter != null)                  if (bgExecuter != null) {
1455                          // Stop all renderers                          // Stop all renderers
1456                          bgExecuter.cancelTask();                          bgExecuter.cancelTask();
1457                    }
1458    
1459                  if (localExecuter != null)                  if (localExecuter != null) {
1460                          localExecuter.cancelTask();                          localExecuter.cancelTask();
1461                  //                  }
                 //  
                 // LOGGER.debug("stopping any running renderes:");  
                 // int count = 0;  
                 // while (bgExecuter.isRunning() || localExecuter.isRunning()) {  
                 // LOGGER.debug("waiting for threads to stop");  
                 //  
                 // bgExecuter.cancelTask();  
                 // localExecuter.cancelTask();  
                 //  
                 // count++;  
                 //  
                 // try {  
                 // Thread.sleep(100);  
                 // } catch (InterruptedException e) {  
                 // LOGGER.error(e);  
                 // }  
                 //  
                 // if (count > 100) {  
                 // throw new RuntimeException(  
                 // "Unable to stop rendering thread for 10secs");  
                 // }  
                 // }  
                 // LOGGER.debug(" threads stopped after " + count / 10. + "secs");  
1462    
1463                  Rectangle curPaintArea = getVisibleRect();                  Rectangle curPaintArea = getVisibleRect();
1464    
                 // allow a single pixel margin at the right and bottom edges  
                 curPaintArea.width -= 1;  
                 curPaintArea.height -= 1;  
   
                 labelCache.clear();  
   
                 /**  
                  */  
1465    
1466                  /**                  /**
1467                   * We have to set new renderer                   * We have to set new renderer
1468                   */                   */
1469    
1470                  if (getBgContext() != null) {                  if (getBgContext() != null) {
1471                          bgExecuter = new RenderingExecutor(this, 333l);                          // bgExecuter = new RenderingExecutor();
1472                          LOGGER.debug("starting bg renderer:");                          // LOGGER.debug("starting bg renderer:");
1473                          // /* System.out.println("rendering"); */                          // // /* System.out.println("rendering"); */
1474                          final GTRenderer createGTRenderer = GTUtil.createGTRenderer(                          // final GTRenderer createGTRenderer = GTUtil.createGTRenderer(
1475                                          localContext, getRendererHints());                          // bgContext, getRendererHints());
1476                          createGTRenderer.setJava2DHints(getJava2dHints());                          // createGTRenderer.setJava2DHints(getJava2dHints());
1477                          bgExecuter.submit(getBgContext().getAreaOfInterest(), curPaintArea,                          // bgExecuter.submit(getBgContext().getAreaOfInterest(),
1478                                          (Graphics2D) getBgImage().getGraphics(), createGTRenderer);                          // curPaintArea,
1479                            // (Graphics2D) getBgImage().getGraphics(), createGTRenderer);
1480                  }                  }
1481    
1482                  if (getContext() != null) {                  if (getContext() != null) {
1483                          localExecuter = new RenderingExecutor(this, 150l);                          // localExecuter = new RenderingExecutor(this, 150l);
1484                          LOGGER.debug("starting local renderer:");  //                      LOGGER.debug("starting local renderer:");
1485                          final GTRenderer createGTRenderer = GTUtil.createGTRenderer(                          
1486                                          localContext, getRendererHints());                          // Clear label cache
1487                          createGTRenderer.setJava2DHints(getJava2dHints());                          labelCache.clear();
1488                          localExecuter.submit(getContext().getAreaOfInterest(),                          Map<Object, Object> rh = localRenderer.getRendererHints();
1489                            rh.put(StreamingRenderer.LABEL_CACHE_KEY, labelCache);
1490                            localRenderer.setRendererHints(rh);
1491    
1492                            boolean submitted = localExecuter.submit(getContext().getAreaOfInterest(),
1493                                          curPaintArea, (Graphics2D) getLocalImage().getGraphics(),                                          curPaintArea, (Graphics2D) getLocalImage().getGraphics(),
1494                                          createGTRenderer);                                          localRenderer, getWorldToScreenTransform());
1495                            if (submitted) repainterTimer.restart();
1496                            else requestStartRendering = true; // Try to start rendering again in a moment
1497                  }                  }
1498    
1499                  updateCursor();                  updateCursor();
 //  
 //              // start regular repaints until all renderers are done.  
 //              repainterTimer.setRepeats(true);  
 //              repainterTimer.restart();  
1500    
1501          }          }
1502    
1503          /**          /**
1504           * Lazyly initializes a {@link BufferedImage} for the background renderer.           * Lazyly initializes a {@link BufferedImage} for the background renderer.
1505           */           */
1506          private BufferedImage getBgImage() {          private Image getBgImage() {
1507                    //
1508                  if (bgImage == null) {                  // if (bgImage == null) {
1509  //                      LOGGER.debug("creating a new background image");                  // bgImage = createImage(getBounds().width, getBounds().height);
1510                    // }
                         Rectangle curPaintArea = getVisibleRect();  
                         // allow a single pixel margin at the right and bottom edges  
                         curPaintArea.width -= 1;  
                         curPaintArea.height -= 1;  
   
                         bgImage = new BufferedImage(curPaintArea.width + 1,  
                                         curPaintArea.height + 1, BufferedImage.TYPE_INT_ARGB);  
                 }  
1511    
1512                  return bgImage;                  return bgImage;
1513          }          }
# Line 1557  public class XMapPane extends JPanel imp Line 1535  public class XMapPane extends JPanel imp
1535           */           */
1536          public void setMapImage(BufferedImage mapImage) {          public void setMapImage(BufferedImage mapImage) {
1537                  this.mapImage = mapImage;                  this.mapImage = mapImage;
                 // gadgetsImage = null;  
1538          }          }
1539    
1540          /**          /**
# Line 1566  public class XMapPane extends JPanel imp Line 1543  public class XMapPane extends JPanel imp
1543          private BufferedImage getLocalImage() {          private BufferedImage getLocalImage() {
1544    
1545                  if (localImage == null) {                  if (localImage == null) {
1546  //                      LOGGER.debug("creating a new local image");                          localImage = new BufferedImage(getBounds().width,
1547                                            getBounds().height, BufferedImage.TYPE_INT_ARGB);
                         Rectangle curPaintArea = getVisibleRect();  
                         // allow a single pixel margin at the right and bottom edges  
                         curPaintArea.width -= 1;  
                         curPaintArea.height -= 1;  
   
                         localImage = new BufferedImage(curPaintArea.width + 1,  
                                         curPaintArea.height + 1, BufferedImage.TYPE_INT_ARGB);  
1548                  }                  }
1549    
1550                  return localImage;                  return localImage;
1551          }          }
1552    
         //  
         // /**  
         // * Lazyly initializes a {@link BufferedImage} for the background renderer.  
         // */  
         // private BufferedImage getGadgetsImage() {  
         //  
         // if (gadgetsImage == null) {  
         // LOGGER.debug("creating a new gadgets image");  
         //  
         // Rectangle curPaintArea = getVisibleRect();  
         // // allow a single pixel margin at the right and bottom edges  
         // curPaintArea.width -= 1;  
         // curPaintArea.height -= 1;  
         //  
         // gadgetsImage = new BufferedImage(curPaintArea.width + 1,  
         // curPaintArea.height + 1, BufferedImage.TYPE_INT_ARGB);  
         //  
         // if (mapImage != null)  
         // gadgetsImage.getGraphics().drawImage(mapImage,  
         // curPaintArea.width - mapImage.getWidth() - 10,  
         // curPaintArea.height - mapImage.getHeight() - 10, this);  
         //                        
         // }  
         //  
         // return gadgetsImage;  
         // }  
   
1553          /**          /**
1554           * Called by the {@linkplain XMapPane.RenderingTask} when rendering has been           * Called by the {@linkplain XMapPane.RenderingTask} when rendering has been
1555           * completed Publishes a {@linkplain MapPaneEvent} of type {@code           * completed Publishes a {@linkplain MapPaneEvent} of type {@code
# Line 1615  public class XMapPane extends JPanel imp Line 1558  public class XMapPane extends JPanel imp
1558           * @see MapPaneListener#onRenderingStopped(org.geotools.swing.event.MapPaneEvent)           * @see MapPaneListener#onRenderingStopped(org.geotools.swing.event.MapPaneEvent)
1559           */           */
1560          public void onRenderingCompleted() {          public void onRenderingCompleted() {
1561                    repainterTimer.stop();
1562                  updateFinalImage();                  updateFinalImage();
1563                  repaint();                  repaint();
1564          }          }
# Line 1627  public class XMapPane extends JPanel imp Line 1571  public class XMapPane extends JPanel imp
1571           * @see MapPaneListener#onRenderingStopped(org.geotools.swing.event.MapPaneEvent)           * @see MapPaneListener#onRenderingStopped(org.geotools.swing.event.MapPaneEvent)
1572           */           */
1573          public void onRenderingCancelled() {          public void onRenderingCancelled() {
1574                    repainterTimer.stop();
1575                  LOGGER.debug("Rendering cancelled");                  LOGGER.debug("Rendering cancelled");
1576          }          }
1577    
# Line 1641  public class XMapPane extends JPanel imp Line 1586  public class XMapPane extends JPanel imp
1586           * @see MapPaneListener#onRenderingStopped(org.geotools.swing.event.MapPaneEvent)           * @see MapPaneListener#onRenderingStopped(org.geotools.swing.event.MapPaneEvent)
1587           */           */
1588          public void onRenderingFailed(Exception renderingError) {          public void onRenderingFailed(Exception renderingError) {
1589                  // MapPaneEvent ev = new MapPaneEvent(this,                  repainterTimer.stop();
                 // MapPaneEvent.Type.RENDERING_STOPPED);  
                 // publishEvent(ev);  
1590                  LOGGER.warn("Rendering failed", renderingError);                  LOGGER.warn("Rendering failed", renderingError);
1591                  updateFinalImage();                  updateFinalImage();
1592                  repaint();                  repaint();
# Line 1651  public class XMapPane extends JPanel imp Line 1594  public class XMapPane extends JPanel imp
1594          }          }
1595    
1596          /**          /**
          * Called when a rendering request has been rejected. This will be common,  
          * such as when the user pauses during drag-resizing fo the map pane. The  
          * base implementation does nothing. It is provided for sub-classes to  
          * override if required.  
          */  
         public void onRenderingRejected() {  
                 LOGGER.warn("Rendering rejected");  
                 repaint();  
         }  
   
         @Override  
         public void propertyChange(final PropertyChangeEvent evt) {  
                 final String prop = evt.getPropertyName();  
   
                 if (prop.equalsIgnoreCase("crs")) {  
                         localContext.setAreaOfInterest(localContext.getAreaOfInterest(),  
                                         (CoordinateReferenceSystem) evt.getNewValue());  
                 }  
         }  
   
         // xulu.sn  
         /**  
1597           * Korrigiert den {@link Envelope} aka {@code mapArea} auf die beste           * Korrigiert den {@link Envelope} aka {@code mapArea} auf die beste
1598           * erlaubte Flaeche damit die Massstabsbeschaenkungen noch eingehalten           * erlaubte Flaeche damit die Massstabsbeschaenkungen noch eingehalten
1599           * werden, FALLS der uebergeben Envelope nicht schon gueltig sein sollte.<br/>           * werden, FALLS der uebergeben Envelope nicht schon gueltig sein sollte.<br/>
# Line 1840  public class XMapPane extends JPanel imp Line 1761  public class XMapPane extends JPanel imp
1761                                                          false);                                                          false);
1762                                  }                                  }
1763                          }                          }
   
1764                  }                  }
1765    
1766                  return newArea;                  return newArea;
# Line 1991  public class XMapPane extends JPanel imp Line 1911  public class XMapPane extends JPanel imp
1911                  mapPaneListeners.remove(l);                  mapPaneListeners.remove(l);
1912          }          }
1913    
1914          /** Stored the time used for the last real rendering in ms. **/  //      /** Stored the time used for the last real rendering in ms. **/
1915          private long lastRenderingDuration = Long.MAX_VALUE;  //      private long lastRenderingDuration = Long.MAX_VALUE;
1916    
1917          // if null, no quick preview will be shown          // if null, no quick preview will be shown
1918          private int quickPreviewHint = 0;          private int quickPreviewHint = 0;
# Line 2006  public class XMapPane extends JPanel imp Line 1926  public class XMapPane extends JPanel imp
1926           */           */
1927          private RenderingHints java2dHints;          private RenderingHints java2dHints;
1928    
1929          /**  //      /**
1930           * Returns in milli seconds the time the last rending of the  //       * Returns in milli seconds the time the last rending of the
1931           * {@link SelectableXMapPane} took. #Long.MAX_VALUE if the JMapPane has not  //       * {@link SelectableXMapPane} took. #Long.MAX_VALUE if the JMapPane has not
1932           * been rendered yet.  //       * been rendered yet.
1933           */  //       */
1934          public long getLastRenderingDuration() {  //      public long getLastRenderingDuration() {
1935                  return lastRenderingDuration;  //              return lastRenderingDuration;
1936          }  //      }
1937    
1938          /**          /**
1939           * Should be called when the {@link JMapPane} is not needed no more to help           * Should be called when the {@link JMapPane} is not needed no more to help
# Line 2027  public class XMapPane extends JPanel imp Line 1947  public class XMapPane extends JPanel imp
1947          public void dispose() {          public void dispose() {
1948                  if (isDisposed())                  if (isDisposed())
1949                          return;                          return;
1950                    
1951                  setPainting(false);                  setPainting(false);
1952                    
1953                    resizeTimer.stop();
1954                    startRenderThreadsTimer.stop();
1955    
1956                  disposed = true;                  disposed = true;
1957    
1958                  if (bgExecuter != null) {                  if (bgExecuter != null) {
1959                          bgExecuter.cancelTask();                          bgExecuter.cancelTask();
1960                            bgExecuter.dispose();
1961                  }                  }
1962    
1963                  if (localExecuter != null) {                  if (localExecuter != null) {
1964                            int i = 0;
1965                          localExecuter.cancelTask();                          localExecuter.cancelTask();
1966                            while (i++ < 10 && localExecuter.isRunning()){
1967                                    try {
1968                                            Thread.sleep(200);
1969                                    } catch (InterruptedException e) {
1970                                            // TODO Auto-generated catch block
1971                                            e.printStackTrace();
1972                                    }
1973                            }
1974                            if (localExecuter.isRunning()) {
1975                                    System.out.println("BAD BAD BAD... still running the thread....");
1976                            }
1977                            localExecuter.dispose();
1978                  }                  }
1979    
                 startRenderThreadsTimer.stop();  
 //              repainterTimer.stop();  
1980    
1981                  if (bgImage != null)                  if (bgImage != null) {
1982                          bgImage.flush();                          bgImage = dispose(bgImage);
1983                  if (localImage != null)                          bgImage = null;
1984                          localImage.flush();  //                      LangUtil.gcTotal();
1985                  // if (gadgetsImage != null)                  }
1986                  // gadgetsImage.flush();  
1987                  if (finalImage != null)                  if (localImage != null) {
1988                          finalImage.flush();                          localImage = dispose(localImage);
1989                  if (preFinalImage != null)                          localImage = null;
1990                          preFinalImage.flush();  //                      LangUtil.gcTotal();
1991                    }
1992                  // if (dragWaitCursorListener != null)  
1993                  // this.removeMouseListener(dragWaitCursorListener);                  if (finalImage != null) {
1994                  // if (mouseWheelZoomListener != null)                          finalImage = dispose(finalImage);
1995                  // this.removeMouseWheelListener(mouseWheelZoomListener);                          finalImage = null;
1996    //                      LangUtil.gcTotal();
1997                    }
1998    
1999                    if (preFinalImage != null) {
2000                            preFinalImage = dispose(preFinalImage);
2001                            preFinalImage=null;
2002                    }
2003                    
2004    //              LangUtil.gcTotal();
2005    
2006                  // Alle mapPaneListener entfernen                  // Alle mapPaneListener entfernen
2007                  mapPaneListeners.clear();                  mapPaneListeners.clear();
# Line 2073  public class XMapPane extends JPanel imp Line 2017  public class XMapPane extends JPanel imp
2017                  removeAll();                  removeAll();
2018          }          }
2019    
2020            private BufferedImage dispose(BufferedImage bi) {
2021    
2022    //              System.out.println("vorher = "
2023    //                      + new MbDecimalFormatter().format(LangUtil.gcTotal()));
2024                    bi.flush();
2025                    return bi = null;
2026    //              System.out.println("nacher = "
2027    //                              + new MbDecimalFormatter().format(LangUtil.gcTotal()));
2028    //
2029    //              System.out.println("\n");
2030            }
2031    
2032          /**          /**
2033           * A flag indicating if dispose() has already been called. If true, then           * A flag indicating if dispose() has already been called. If true, then
2034           * further use of this {@link SelectableXMapPane} is undefined.           * further use of this {@link SelectableXMapPane} is undefined.
# Line 2215  public class XMapPane extends JPanel imp Line 2171  public class XMapPane extends JPanel imp
2171                                  drawRectangle(graphics, startPos, lastPos);                                  drawRectangle(graphics, startPos, lastPos);
2172                          }                          }
2173    
2174                            graphics.dispose();
2175    
2176                  }                  }
2177    
2178          }          }
# Line 2225  public class XMapPane extends JPanel imp Line 2183  public class XMapPane extends JPanel imp
2183           */           */
2184          protected void drawRectangle(final Graphics graphics, Point startPos,          protected void drawRectangle(final Graphics graphics, Point startPos,
2185                          Point e) {                          Point e) {
2186    
2187                    if (!isWellDefined())
2188                            return;
2189    
2190                  // undraw last box/draw new box                  // undraw last box/draw new box
2191                  final int left = Math.min(startPos.x, e.x);                  final int left = Math.min(startPos.x, e.x);
2192                  final int right = Math.max(startPos.x, e.x);                  final int right = Math.max(startPos.x, e.x);
# Line 2262  public class XMapPane extends JPanel imp Line 2224  public class XMapPane extends JPanel imp
2224          }          }
2225    
2226          public void onRenderingPending() {          public void onRenderingPending() {
2227                  LOGGER.debug("Pending rendering updates the preview...");                  // LOGGER.debug("Pending rendering updates the preview...");
2228                  updateFinalImage();                  updateFinalImage();
2229                  XMapPane.this.repaint();                  repaint();
2230          }          }
2231    
2232  }  }

Legend:
Removed from v.516  
changed lines
  Added in v.529

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26