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

moved grab action to a thread

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.