1 | /* Copyright (c) 2008, Henrik Niehaus
|
---|
2 | * All rights reserved.
|
---|
3 | *
|
---|
4 | * Redistribution and use in source and binary forms, with or without
|
---|
5 | * modification, are permitted provided that the following conditions are met:
|
---|
6 | *
|
---|
7 | * 1. Redistributions of source code must retain the above copyright notice,
|
---|
8 | * this list of conditions and the following disclaimer.
|
---|
9 | * 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
10 | * this list of conditions and the following disclaimer in the documentation
|
---|
11 | * and/or other materials provided with the distribution.
|
---|
12 | * 3. Neither the name of the project nor the names of its
|
---|
13 | * contributors may be used to endorse or promote products derived from this
|
---|
14 | * software without specific prior written permission.
|
---|
15 | *
|
---|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
---|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
---|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
---|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
---|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
---|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
---|
26 | * POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 | package org.openstreetmap.josm.plugins.osb;
|
---|
29 |
|
---|
30 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
31 |
|
---|
32 | import java.awt.geom.Point2D;
|
---|
33 | import java.util.concurrent.TimeUnit;
|
---|
34 |
|
---|
35 | import org.openstreetmap.josm.Main;
|
---|
36 |
|
---|
37 | public class OsbDownloadLoop extends Thread {
|
---|
38 |
|
---|
39 | private static OsbDownloadLoop instance;
|
---|
40 |
|
---|
41 | private long countdown = TimeUnit.SECONDS.toMillis(1);
|
---|
42 |
|
---|
43 | private boolean downloadDone = false;
|
---|
44 |
|
---|
45 | private final int INTERVAL = 100;
|
---|
46 |
|
---|
47 | private OsbPlugin plugin;
|
---|
48 |
|
---|
49 | private Point2D lastCenter;
|
---|
50 |
|
---|
51 | public OsbDownloadLoop() {
|
---|
52 | setName(tr("OpenStreetBugs download loop"));
|
---|
53 | start();
|
---|
54 | }
|
---|
55 |
|
---|
56 | public static synchronized OsbDownloadLoop getInstance() {
|
---|
57 | if(instance == null) {
|
---|
58 | instance = new OsbDownloadLoop();
|
---|
59 | }
|
---|
60 | return instance;
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | public void run() {
|
---|
65 | try {
|
---|
66 | while(true) {
|
---|
67 | countdown -= INTERVAL;
|
---|
68 |
|
---|
69 | // if the center of the map has changed, the user has dragged or
|
---|
70 | // zoomed the map
|
---|
71 | if(Main.map != null && Main.map.mapView != null) {
|
---|
72 | Point2D currentCenter = Main.map.mapView.getCenter();
|
---|
73 | if(currentCenter != null && !currentCenter.equals(lastCenter)) {
|
---|
74 | resetCountdown();
|
---|
75 | lastCenter = currentCenter;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | // auto download if configured
|
---|
80 | if( Main.pref.getBoolean(ConfigKeys.OSB_AUTO_DOWNLOAD) && OsbPlugin.active ) {
|
---|
81 | if(countdown < 0) {
|
---|
82 | if(!downloadDone) {
|
---|
83 | if(plugin != null) {
|
---|
84 | plugin.updateData();
|
---|
85 | downloadDone = true;
|
---|
86 | }
|
---|
87 | } else {
|
---|
88 | countdown = -1;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | Thread.sleep(INTERVAL);
|
---|
94 | }
|
---|
95 | } catch (InterruptedException e) {
|
---|
96 | e.printStackTrace();
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | public void resetCountdown() {
|
---|
101 | downloadDone = false;
|
---|
102 | countdown = TimeUnit.SECONDS.toMillis(1);
|
---|
103 | }
|
---|
104 |
|
---|
105 | public void setPlugin(OsbPlugin plugin) {
|
---|
106 | this.plugin = plugin;
|
---|
107 | }
|
---|
108 | }
|
---|