Changeset 23191 in osm for applications/editors/josm/plugins/livegps/src/livegps/LiveGpsSuppressor.java
- Timestamp:
- 2010-09-15T18:56:19+02:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/livegps/src/livegps/LiveGpsSuppressor.java
r19011 r23191 18 18 public class LiveGpsSuppressor implements Runnable, ILiveGpsSuppressor { 19 19 20 21 22 23 20 /** 21 * Default sleep time is 5 seconds. 22 */ 23 private static final int DEFAULT_SLEEP_TIME = 5; 24 24 25 26 27 28 25 /** 26 * The currently used sleepTime. 27 */ 28 private int sleepTime = DEFAULT_SLEEP_TIME; 29 29 30 31 32 33 30 /** 31 * The flag allowUpdate is enabled once during the sleepTime. 32 */ 33 private boolean allowUpdate = false; 34 34 35 36 37 38 35 /** 36 * Controls if this thread is still in used. 37 */ 38 private boolean shutdownFlag = false; 39 39 40 41 42 43 44 45 40 /** 41 * Run thread enables the allowUpdate flag once during its cycle. 42 * @see java.lang.Runnable#run() 43 */ 44 public void run() { 45 initSleepTime(); 46 46 47 48 49 50 51 47 shutdownFlag = false; 48 // stop the thread, when explicitely shut down or when disabled by 49 // config setting 50 while (!shutdownFlag && isEnabled()) { 51 setAllowUpdate(true); 52 52 53 54 55 56 57 58 59 53 try { 54 Thread.sleep(getSleepTime()); 55 } catch (InterruptedException e) { 56 // TODO I never knew, how to handle this??? Probably just carry 57 // on 58 } 59 } 60 60 61 61 } 62 62 63 64 65 66 67 68 69 70 71 72 73 63 /** 64 * Retrieve the sleepTime from the configuration. 65 * If no such configuration key exists, it will be initialized here. 66 */ 67 private void initSleepTime() { 68 // fetch it from the user setting, or use the default value. 69 int sleepSeconds = 0; 70 sleepSeconds = Main.pref.getInteger("livegps.refreshinterval", 71 DEFAULT_SLEEP_TIME); 72 // creates the setting, if none present. 73 Main.pref.putInteger("livegps.refreshinterval", sleepSeconds); 74 74 75 76 77 75 // convert seconds into milliseconds internally. 76 this.sleepTime = sleepSeconds * 1000; 77 } 78 78 79 80 81 82 83 84 85 79 /** 80 * Set the allowUpdate flag. May only privately accessible! 81 * @param allowUpdate the allowUpdate to set 82 */ 83 private synchronized void setAllowUpdate(boolean allowUpdate) { 84 this.allowUpdate = allowUpdate; 85 } 86 86 87 88 89 90 91 92 93 94 95 87 /** 88 * Query, if an update is currently allowed. 89 * When it is allowed, it will disable the allowUpdate flag as a side effect. 90 * (this means, one thread got to issue an update event) 91 * 92 * @return true, if an update is currently allowed; false, if the update shall be suppressed. 93 * @see livegps.ILiveGpsSuppressor#isAllowUpdate() 94 */ 95 public synchronized boolean isAllowUpdate() { 96 96 97 98 99 100 97 // if disabled, always permit a re-draw. 98 if (!isEnabled()) { 99 return true; 100 } else { 101 101 102 103 104 105 106 107 108 109 102 if (allowUpdate) { 103 allowUpdate = false; 104 return true; 105 } else { 106 return false; 107 } 108 } 109 } 110 110 111 112 113 114 115 * 116 117 118 119 120 111 /** 112 * A value below 1 disables this feature. 113 * This ensures that a small value does not run this thread 114 * in a tight loop. 115 * 116 * @return true, if suppressing is enabled 117 */ 118 private boolean isEnabled() { 119 return this.sleepTime > 0; 120 } 121 121 122 123 124 125 126 127 122 /** 123 * Shut this thread down. 124 */ 125 public void shutdown() { 126 shutdownFlag = true; 127 } 128 128 129 130 131 132 133 134 129 /** 130 * @return the defaultSleepTime 131 */ 132 private int getSleepTime() { 133 return this.sleepTime; 134 } 135 135 136 136 }
Note:
See TracChangeset
for help on using the changeset viewer.