Changeset 25790 in osm
- Timestamp:
- 2011-04-04T21:42:44+02:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/livegps/src/livegps/LiveGpsPlugin.java
r23191 r25790 28 28 29 29 public class LiveGpsPlugin extends Plugin implements LayerChangeListener { 30 private boolean enabled = false; 30 31 private LiveGpsAcquirer acquirer = null; 31 32 private Thread acquirerThread = null; 32 private JMenu lgpsmenu ;33 private JMenu lgpsmenu = null; 33 34 private JCheckBoxMenuItem lgpscapture; 34 35 private JCheckBoxMenuItem lgpsautocenter; 35 36 private LiveGpsDialog lgpsdialog; 36 List<PropertyChangeListener> listenerQueue;37 37 38 38 private GpxData data = new GpxData(); … … 42 42 * The LiveGpsSuppressor is queried, if an event shall be suppressed. 43 43 */ 44 private LiveGpsSuppressor suppressor ;44 private LiveGpsSuppressor suppressor = null; 45 45 46 46 /** … … 106 106 107 107 public void layerRemoved(Layer oldLayer) { 108 if (oldLayer == lgpslayer) { 109 enableTracking(false); 110 lgpscapture.setSelected(false); 111 removePropertyChangeListener(lgpslayer); 112 MapView.removeLayerChangeListener(this); 113 lgpslayer = null; 114 } 108 assert (oldLayer == lgpslayer); 109 110 enableTracking(false); 111 lgpscapture.setSelected(false); 112 MapView.removeLayerChangeListener(this); 113 lgpslayer = null; 115 114 } 116 115 … … 134 133 lgpsautocenter = new JCheckBoxMenuItem(autoCenterAction); 135 134 lgpsmenu.add(lgpsautocenter); 136 lgpsautocenter.setAccelerator(autoCenterAction.getShortcut() 137 .getKeyStroke()); 135 lgpsautocenter.setAccelerator(autoCenterAction.getShortcut().getKeyStroke()); 138 136 } 139 137 … … 165 163 */ 166 164 public void enableTracking(boolean enable) { 167 if ((acquirer != null) && (!enable)) { 168 acquirer.shutdown(); 169 acquirerThread = null; 170 171 // also stop the suppressor 172 if (suppressor != null) { 173 suppressor.shutdown(); 174 suppressorThread = null; 175 if (lgpslayer != null) { 176 lgpslayer.setSuppressor(null); 177 } 178 } 179 } else if (enable) { 180 // also start the suppressor 181 if (suppressor == null) { 182 suppressor = new LiveGpsSuppressor(); 183 } 184 if (suppressorThread == null) { 185 suppressorThread = new Thread(suppressor); 186 suppressorThread.start(); 187 } 188 189 if (acquirer == null) { 190 acquirer = new LiveGpsAcquirer(); 191 if (lgpslayer == null) { 192 lgpslayer = new LiveGpsLayer(data); 193 Main.main.addLayer(lgpslayer); 194 MapView.addLayerChangeListener(this); 195 lgpslayer.setAutoCenter(isAutoCenter()); 196 } 197 // connect layer with acquirer: 198 addPropertyChangeListener(lgpslayer); 199 200 // connect layer with suppressor: 201 lgpslayer.setSuppressor(suppressor); 202 // add all listeners that were added before the acquirer 203 // existed: 204 if (listenerQueue != null) { 205 for (PropertyChangeListener listener : listenerQueue) { 206 addPropertyChangeListener(listener); 207 } 208 listenerQueue.clear(); 209 } 210 } 211 if (acquirerThread == null) { 212 acquirerThread = new Thread(acquirer); 213 acquirerThread.start(); 214 } 215 216 } 217 } 218 219 /** 220 * Add a listener for gps events. 221 * @param listener the listener. 222 */ 223 public void addPropertyChangeListener(PropertyChangeListener listener) { 224 if (acquirer != null) { 225 acquirer.addPropertyChangeListener(listener); 226 } else { 227 if (listenerQueue == null) { 228 listenerQueue = new ArrayList<PropertyChangeListener>(); 229 } 230 listenerQueue.add(listener); 231 } 232 } 233 234 /** 235 * Remove a listener for gps events. 236 * @param listener the listener. 237 */ 238 public void removePropertyChangeListener(PropertyChangeListener listener) { 239 if (acquirer != null) 240 acquirer.removePropertyChangeListener(listener); 241 else if (listenerQueue != null && listenerQueue.contains(listener)) 242 listenerQueue.remove(listener); 165 166 if (enable && !enabled) { 167 assert (suppressor == null); 168 assert (suppressorThread == null); 169 assert (acquirer == null); 170 assert (acquirerThread == null); 171 172 suppressor = new LiveGpsSuppressor(); 173 suppressorThread = new Thread(suppressor); 174 175 acquirer = new LiveGpsAcquirer(); 176 acquirerThread = new Thread(acquirer); 177 178 if (lgpslayer == null) { 179 lgpslayer = new LiveGpsLayer(data); 180 Main.main.addLayer(lgpslayer); 181 MapView.addLayerChangeListener(this); 182 lgpslayer.setAutoCenter(isAutoCenter()); 183 } 184 185 lgpslayer.setSuppressor(suppressor); 186 acquirer.addPropertyChangeListener(lgpslayer); 187 acquirer.addPropertyChangeListener(lgpsdialog); 188 189 suppressorThread.start(); 190 acquirerThread.start(); 191 192 enabled = true; 193 194 } else if (!enable && enabled) { 195 assert (lgpslayer != null); 196 assert (suppressor != null); 197 assert (suppressorThread != null); 198 assert (acquirer != null); 199 assert (acquirerThread != null); 200 201 acquirer.shutdown(); 202 acquirer = null; 203 acquirerThread = null; 204 205 suppressor.shutdown(); 206 suppressor = null; 207 suppressorThread = null; 208 209 lgpslayer.setSuppressor(null); 210 211 enabled = false; 212 } 243 213 } 244 214 … … 248 218 @Override 249 219 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 250 if (newFrame != null) { 251 // add dialog 220 if (newFrame != null) 252 221 newFrame.addToggleDialog(lgpsdialog = new LiveGpsDialog(newFrame)); 253 // connect listeners with acquirer:254 addPropertyChangeListener(lgpsdialog);255 }256 222 } 257 223 … … 262 228 return this.lgpsmenu; 263 229 } 264 265 230 }
Note:
See TracChangeset
for help on using the changeset viewer.