Ignore:
Timestamp:
2011-01-02T00:36:28+01:00 (14 years ago)
Author:
pieren
Message:

moved grab action to a thread

Location:
applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr
Files:
2 added
9 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CacheControl.java

    r24907 r24934  
    153153        if (successfulRead && wmsLayer.isRaster()) {
    154154            // serialized raster bufferedImage hangs-up on Java6. Recreate them here
    155             wmsLayer.images.get(0).image = RasterImageModifier.fixRasterImage(wmsLayer.images.get(0).image);
     155            wmsLayer.getImage(0).image = RasterImageModifier.fixRasterImage(wmsLayer.getImage(0).image);
    156156        }
    157157        return successfulRead;
     
    172172        for (;;) {
    173173            imagesLock.lock();
    174             //ArrayList<GeorefImage> images = new ArrayList<GeorefImage>(imagesToSave);
    175174            int size = imagesToSave.size();
    176175            imagesLock.unlock();
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/DownloadWMSPlanImage.java

    r21493 r24934  
    3535            try {
    3636                if (grabber.getWmsInterface().retrieveInterface(wmsLayer)) {
    37                     if (!wmsLayer.images.isEmpty()) {
     37                    if (!wmsLayer.getImages().isEmpty()) {
    3838                        //JOptionPane.showMessageDialog(Main.parent,tr("Image already loaded"));
    3939                        JOptionPane pane = new JOptionPane(
     
    5151                        // first time we grab an image for this layer
    5252                        if (CacheControl.cacheEnabled) {
    53                             if (wmsLayer.getCacheControl().loadCacheIfExist()) {
     53                            if (wmsLayer.grabThread.getCacheControl().loadCacheIfExist()) {
    5454                                dontGeoreference = true;
    5555                                Main.map.mapView.repaint();
     
    6464                            wmsLayer.grab(grabber, bounds);
    6565                            if (grabber.getWmsInterface().downloadCancelled) {
    66                                 wmsLayer.images.clear();
     66                                wmsLayer.clearImages();
    6767                                Main.map.mapView.repaint();
    6868                            } else {
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/DownloadWMSVectorImage.java

    r24913 r24934  
    3333        try {
    3434            if (grabber.getWmsInterface().retrieveInterface(wmsLayer)) {
    35                 if (wmsLayer.images.isEmpty()) {
     35                if (wmsLayer.getImages().isEmpty()) {
    3636                    // first time we grab an image for this layer
    3737                    if (CacheControl.cacheEnabled) {
    38                         if (wmsLayer.getCacheControl().loadCacheIfExist()) {
     38                        if (wmsLayer.grabThread.getCacheControl().loadCacheIfExist()) {
    3939                            Main.map.mapView.zoomTo(wmsLayer.getCommuneBBox().toBounds());
    4040                            //Main.map.mapView.repaint();
     
    6969        grabber.getWmsInterface().cancel();
    7070        if (wmsLayer != null)
    71             wmsLayer.cancelled = true;
     71            wmsLayer.grabThread.setCancelled(true);
    7272    }
    7373
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/GeorefImage.java

    r23190 r24934  
    2323import org.openstreetmap.josm.gui.NavigatableComponent;
    2424
    25 public class GeorefImage implements Serializable, ImageObserver {
     25public class GeorefImage implements Serializable, ImageObserver, Cloneable {
    2626    private static final long serialVersionUID = 1L;
    2727
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/MenuActionGrabPlanImage.java

    r23190 r24934  
    106106        if (loadedFromCache) {
    107107            Main.map.repaint();
    108         } else if (wmsLayer.images.size() == 0) {
     108        } else if (wmsLayer.getImages().size() == 0) {
    109109            // action cancelled or image loaded from cache (and already georeferenced)
    110110            actionInterrupted();
     
    149149            countMouseClicked++;
    150150            // ignore clicks outside the image
    151             if (ea.east() < wmsLayer.images.get(0).min.east() || ea.east() > wmsLayer.images.get(0).max.east()
    152                     || ea.north() < wmsLayer.images.get(0).min.north() || ea.north() > wmsLayer.images.get(0).max.north())
     151            if (ea.east() < wmsLayer.getImage(0).min.east() || ea.east() > wmsLayer.getImage(0).max.east()
     152                    || ea.north() < wmsLayer.getImage(0).min.north() || ea.north() > wmsLayer.getImage(0).max.north())
    153153                return;
    154154            if (mode == cGetCorners) {
     
    259259        Main.map.mapView.removeMouseListener(this);
    260260        affineTransform(ea1, ea2, georefpoint1, georefpoint2);
    261         wmsLayer.saveNewCache();
     261        wmsLayer.grabThread.saveNewCache();
    262262        Main.map.mapView.repaint();
    263263        actionCompleted();
     
    378378        double dx = dst1.getX() - org1.getX();
    379379        double dy = dst1.getY() - org1.getY();
    380         wmsLayer.images.get(0).shear(dx, dy);
     380        wmsLayer.getImage(0).shear(dx, dy);
    381381        org1 = org1.add(dx, dy); // org1=dst1 now
    382382        org2 = org2.add(dx, dy);
    383383        // rotate : org1(=dst1 now) is anchor for rotation and scale
    384         wmsLayer.images.get(0).rotate(dst1, angle);
     384        wmsLayer.getImage(0).rotate(dst1, angle);
    385385        org2 = org2.rotate(dst1, angle);
    386386        // scale image from anchor org1(=dst1 now)
    387         wmsLayer.images.get(0).scale(dst1, proportion);
     387        wmsLayer.getImage(0).scale(dst1, proportion);
    388388    }
    389389
     
    392392        georefpoint2 = new EastNorth(wmsLayer.X0+wmsLayer.fX*wmsLayer.communeBBox.max.getX(),
    393393                wmsLayer.Y0+wmsLayer.fY*wmsLayer.communeBBox.max.getX());
    394         ea1 = new EastNorth(wmsLayer.images.get(0).min.east(), wmsLayer.images.get(0).max.north());
    395         EastNorth ea2 = wmsLayer.images.get(0).max;
     394        ea1 = new EastNorth(wmsLayer.getImage(0).min.east(), wmsLayer.getImage(0).max.north());
     395        EastNorth ea2 = wmsLayer.getImage(0).max;
    396396        affineTransform(ea1, ea2, georefpoint1, georefpoint2);
    397         wmsLayer.saveNewCache();
     397        wmsLayer.grabThread.saveNewCache();
    398398        Main.map.mapView.repaint();
    399399    }
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/MenuActionLoadFromCache.java

    r23190 r24934  
    7575                    // create layer and load cache
    7676                    WMSLayer wmsLayer = new WMSLayer("", "", Integer.parseInt(ext)-1);
    77                     if (wmsLayer.getCacheControl().loadCache(file, layoutZone)) {
     77                    if (wmsLayer.grabThread.getCacheControl().loadCache(file, layoutZone)) {
    7878                        CadastrePlugin.addWMSLayer(wmsLayer);
    7979                    }
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/MenuActionSaveRasterAs.java

    r18544 r24934  
    5656            if (!file.getName().endsWith(".png"))
    5757                file = new File(file.getParent(), file.getName()+".png");
    58             BufferedImage bi = wmsLayer.images.get(0).image;
     58            BufferedImage bi = wmsLayer.getImage(0).image;
    5959            try {
    6060                ImageIO.write(bi, "png", file);
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/WMSAdjustAction.java

    r20281 r24934  
    165165            double endAngle = Math.atan2(end.east()-pivot.east(), end.north()-pivot.north());
    166166            double rotationAngle = endAngle - startAngle;
    167             if (selectedLayer.images.get(0).orgCroppedRaster != null) {
     167            if (selectedLayer.getImage(0).orgCroppedRaster != null) {
    168168                for (int i=0; i<4; i++) {
    169                     croppedRaster[i] = selectedLayer.images.get(0).orgCroppedRaster[i].rotate(pivot, rotationAngle);
     169                    croppedRaster[i] = selectedLayer.getImage(0).orgCroppedRaster[i].rotate(pivot, rotationAngle);
    170170                }
    171171                croppedRaster[4] = croppedRaster[0];
     
    198198    private void saveModifiedLayers() {
    199199        for (WMSLayer wmsLayer : modifiedLayers) {
    200             wmsLayer.saveNewCache();
     200            wmsLayer.grabThread.saveNewCache();
    201201        }
    202202    }
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/WMSLayer.java

    r24913 r24934  
    2020import java.util.HashSet;
    2121import java.util.Vector;
     22import java.util.concurrent.locks.Lock;
     23import java.util.concurrent.locks.ReentrantLock;
    2224
    2325import javax.swing.Action;
     
    3436import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
    3537import org.openstreetmap.josm.gui.layer.Layer;
    36 import org.openstreetmap.josm.io.OsmTransferException;
    3738
    3839/**
     
    4849            CadastrePlugin.class.getResource("/images/cadastre_small.png")));
    4950
    50     protected Vector<GeorefImage> images = new Vector<GeorefImage>();
     51    private Vector<GeorefImage> images = new Vector<GeorefImage>();
     52
     53    public Lock imagesLock = new ReentrantLock();
    5154
    5255    /**
     
    6164    private ArrayList<EastNorthBound> dividedBbox = new ArrayList<EastNorthBound>();
    6265
    63     private CacheControl cacheControl = null;
    64 
    6566    private String location = "";
    6667
     
    7071
    7172    public EastNorthBound communeBBox = new EastNorthBound(new EastNorth(0,0), new EastNorth(0,0));
    72 
    73     public boolean cancelled;
    7473
    7574    private boolean isRaster = false;
     
    8483    private Action saveAsPng;
    8584
     85    private Action cancelGrab;
     86
    8687    public boolean adjustModeEnabled;
    8788
    88 
     89    public GrabThread grabThread;
     90   
    8991    public WMSLayer() {
    9092        this(tr("Blank Layer"), "", -1);
     
    9698        this.codeCommune = codeCommune;
    9799        this.lambertZone = lambertZone;
     100        grabThread = new GrabThread(this);
     101        grabThread.start();
    98102        // enable auto-sourcing option
    99103        CadastrePlugin.pluginUsed = true;
     
    103107    public void destroy() {
    104108        // if the layer is currently saving the images in the cache, wait until it's finished
    105         if (cacheControl != null) {
    106             while (!cacheControl.isCachePipeEmpty()) {
    107                 System.out.println("Try to close a WMSLayer which is currently saving in cache : wait 1 sec.");
    108                 CadastrePlugin.safeSleep(1000);
    109             }
    110         }
     109        grabThread.cancel();
    111110        super.destroy();
    112111        images = null;
     
    127126
    128127    public void grab(CadastreGrabber grabber, Bounds b) throws IOException {
    129         cancelled = false;
     128        grabThread.setCancelled(false);
     129        grabThread.setGrabber(grabber);
    130130        // if it is the first layer, use the communeBBox as grab bbox (and not divided)
    131131        if (Main.map.mapView.getAllLayers().size() == 1 ) {
     
    142142        }
    143143
    144         int lastSavedImage = images.size();
    145         for (EastNorthBound n : dividedBbox) {
    146             if (cancelled)
    147                 return;
    148             GeorefImage newImage;
    149             try {
    150                 newImage = grabber.grab(this, n.min, n.max);
    151             } catch (IOException e) {
    152                 System.out.println("Download action cancelled by user or server did not respond");
    153                 break;
    154             } catch (OsmTransferException e) {
    155                 System.out.println("OSM transfer failed");
    156                 break;
    157             }
    158             if (grabber.getWmsInterface().downloadCancelled) {
    159                 System.out.println("Download action cancelled by user");
    160                 break;
    161             }
    162             if (CadastrePlugin.backgroundTransparent) {
    163                 for (GeorefImage img : images) {
    164                     if (img.overlap(newImage))
    165                         // mask overlapping zone in already grabbed image
    166                         img.withdraw(newImage);
    167                     else
    168                         // mask overlapping zone in new image only when new
    169                         // image covers completely the existing image
    170                         newImage.withdraw(img);
    171                 }
    172             }
    173             images.add(newImage);
    174             Main.map.mapView.repaint();
    175         }
    176         if (!cancelled) {
    177             for (int i=lastSavedImage; i < images.size(); i++)
    178                 saveToCache(images.get(i));
    179         }
     144        grabThread.addImages(dividedBbox);
     145        Main.map.repaint();
    180146    }
    181147
     
    255221            else
    256222                g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
    257             synchronized(this){
    258                 for (GeorefImage img : images)
    259                     img.paint(g, mv, CadastrePlugin.backgroundTransparent,
    260                             CadastrePlugin.transparency, CadastrePlugin.drawBoundaries);
    261             }
     223            imagesLock.lock();
     224            for (GeorefImage img : images)
     225                img.paint(g, mv, CadastrePlugin.backgroundTransparent,
     226                        CadastrePlugin.transparency, CadastrePlugin.drawBoundaries);
     227            imagesLock.unlock();
    262228            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, savedInterpolation);
    263229        }
    264230        if (this.isRaster) {
    265231            paintCrosspieces(g, mv);
     232        }
     233        if (grabThread.getImagesToGrabSize() > 0) {
     234            grabThread.paintBoxesToGrab(g, mv);
    266235        }
    267236        if (this.adjustModeEnabled) {
     
    287256        saveAsPng = new MenuActionSaveRasterAs(this);
    288257        saveAsPng.setEnabled(isRaster);
     258        cancelGrab = new MenuActionCancelGrab(this);
     259        cancelGrab.setEnabled(!isRaster && grabThread.getImagesToGrabSize() > 0);
    289260        return new Action[] {
    290261                LayerListDialog.getInstance().createShowHideLayerAction(),
     
    292263                new MenuActionLoadFromCache(),
    293264                saveAsPng,
     265                cancelGrab,
    294266                new LayerListPopup.InfoAction(this),
    295267
     
    318290        }
    319291        return false;
    320     }
    321 
    322     public void saveToCache(GeorefImage image) {
    323         if (CacheControl.cacheEnabled && !isRaster()) {
    324             getCacheControl().saveCache(image);
    325         }
    326     }
    327 
    328     public void saveNewCache() {
    329         if (CacheControl.cacheEnabled) {
    330             getCacheControl().deleteCacheFile();
    331             for (GeorefImage image : images)
    332                 getCacheControl().saveCache(image);
    333         }
    334     }
    335 
    336     public CacheControl getCacheControl() {
    337         if (cacheControl == null)
    338             cacheControl = new CacheControl(this);
    339         return cacheControl;
    340292    }
    341293
     
    642594    }
    643595
     596    public GeorefImage getImage(int index) {
     597        imagesLock.lock();
     598        GeorefImage img = null;
     599        try {
     600            img = this.images.get(index);
     601        } catch (ArrayIndexOutOfBoundsException e) {
     602            e.printStackTrace(System.out);
     603        }
     604        imagesLock.unlock();
     605        return img;
     606    }
     607   
     608    public Vector<GeorefImage> getImages() {
     609        return this.images;
     610    }
     611   
     612    public void addImage(GeorefImage img) {
     613        imagesLock.lock();
     614        this.images.add(img);
     615        imagesLock.unlock();
     616    }
     617   
     618    public void setImages(Vector<GeorefImage> images) {
     619        imagesLock.lock();
     620        this.images = images;
     621        imagesLock.unlock();
     622    }
     623   
     624    public void clearImages() {
     625        imagesLock.lock();
     626        this.images.clear();
     627        imagesLock.unlock();
     628    }
     629
    644630}
Note: See TracChangeset for help on using the changeset viewer.