Ignore:
Timestamp:
2011-01-12T23:33:13+01:00 (15 years ago)
Author:
pieren
Message:

change the way we grab the small squares with a spiral algorithm to visualize the first square in the middle of the screen

File:
1 edited

Legend:

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

    r24934 r25033  
    2222    private WMSLayer wmsLayer;
    2323
    24     private Lock lock = new ReentrantLock();
     24    private Lock lockImagesToGrag = new ReentrantLock();
    2525
    2626    private ArrayList<EastNorthBound> imagesToGrab = new ArrayList<EastNorthBound>();
     
    3030    private EastNorthBound currentGrabImage;
    3131
     32    private Lock lockCurrentGrabImage = new ReentrantLock();
     33
     34    /**
     35     * Call directly grabber for raster images or prepare thread for vector images
     36     * @param moreImages
     37     */
    3238    public void addImages(ArrayList<EastNorthBound> moreImages) {
    33         lock.lock();
     39        lockImagesToGrag.lock();
    3440        imagesToGrab.addAll(moreImages);
    35         lock.unlock();
     41        lockImagesToGrag.unlock();
    3642        synchronized(this) {
    3743            this.notify();
    3844        }
    3945        System.out.println("Added " + moreImages.size() + " to the grab thread");
     46        if (wmsLayer.isRaster()) {
     47            waitNotification();
     48        }
    4049    }
    4150
    4251    public int getImagesToGrabSize() {
    43         lock.lock();
     52        lockImagesToGrag.lock();
    4453        int size = imagesToGrab.size();
    45         lock.unlock();
     54        lockImagesToGrag.unlock();
    4655        return size;
    4756    }
     
    4958    public ArrayList<EastNorthBound> getImagesToGrabCopy() {
    5059        ArrayList<EastNorthBound> copyList = new ArrayList<EastNorthBound>();
    51         lock.lock();
     60        lockImagesToGrag.lock();
    5261        for (EastNorthBound img : imagesToGrab) {
    5362            EastNorthBound imgCpy = new EastNorthBound(img.min, img.max);
    5463            copyList.add(imgCpy);
    5564        }
    56         lock.unlock();
     65        lockImagesToGrag.unlock();
    5766        return copyList;
    5867    }
    5968   
    6069    public void clearImagesToGrab() {       
    61         lock.lock();
     70        lockImagesToGrag.lock();
    6271        imagesToGrab.clear();
    63         lock.unlock();
     72        lockImagesToGrag.unlock();
    6473    }
    6574   
     
    6877        for (;;) {
    6978            while (getImagesToGrabSize() > 0) {
    70                 lock.lock();
     79                lockImagesToGrag.lock();
     80                lockCurrentGrabImage.lock();
    7181                currentGrabImage = imagesToGrab.get(0);
     82                lockCurrentGrabImage.unlock();
    7283                imagesToGrab.remove(0);
    73                 lock.unlock();
     84                lockImagesToGrag.unlock();
    7485                if (cancelled) {
    7586                    break;
     
    7788                    GeorefImage newImage;
    7889                    try {
     90                        Main.map.repaint(); // paint the current grab box
    7991                        newImage = grabber.grab(wmsLayer, currentGrabImage.min, currentGrabImage.max);
    8092                    } catch (IOException e) {
     
    93105                        break;
    94106                    }
     107                    try {
    95108                    if (CadastrePlugin.backgroundTransparent) {
    96109                        wmsLayer.imagesLock.lock();
     
    109122                    Main.map.mapView.repaint();
    110123                    saveToCache(newImage);
     124                    } catch (NullPointerException e) {
     125                        System.out.println("Layer destroyed. Cancel grab thread");
     126                        setCancelled(true);
     127                    }
    111128                }
    112129            }
    113130            System.out.println("grab thread list empty");
     131            lockCurrentGrabImage.lock();
    114132            currentGrabImage = null;
     133            lockCurrentGrabImage.unlock();
    115134            if (cancelled) {
    116135                clearImagesToGrab();
    117136                cancelled = false;
    118137            }
    119             synchronized(this) {
    120                 try {
    121                     wait();
    122                 } catch (InterruptedException e) {
    123                     e.printStackTrace(System.out);
    124                 }
    125             }
    126         }
     138            if (wmsLayer.isRaster()) {
     139                notifyWaiter();
     140            }
     141            waitNotification();        }
    127142    }
    128143
     
    165180
    166181    public void paintBoxesToGrab(Graphics g, MapView mv) {
    167         ArrayList<EastNorthBound> imagesToGrab = getImagesToGrabCopy();
    168         for (EastNorthBound img : imagesToGrab) {
    169             paintBox(g, mv, img, Color.red);
    170         }
     182        if (getImagesToGrabSize() > 0) {
     183            ArrayList<EastNorthBound> imagesToGrab = getImagesToGrabCopy();
     184            for (EastNorthBound img : imagesToGrab) {
     185                paintBox(g, mv, img, Color.red);
     186            }
     187        }
     188        lockCurrentGrabImage.lock();
    171189        if (currentGrabImage != null) {
    172190            paintBox(g, mv, currentGrabImage, Color.orange);
    173191        }
     192        lockCurrentGrabImage.unlock();
    174193    }
    175194   
     
    203222    }
    204223
     224    private synchronized void notifyWaiter() {
     225        this.notify();
     226    }
     227
     228    private synchronized void waitNotification() {
     229        try {
     230            wait();
     231        } catch (InterruptedException e) {
     232            e.printStackTrace(System.out);
     233        }
     234    }
     235
    205236}
Note: See TracChangeset for help on using the changeset viewer.