| 1 | package livegps;
|
|---|
| 2 |
|
|---|
| 3 | import java.awt.event.ActionEvent;
|
|---|
| 4 | import java.awt.event.ActionListener;
|
|---|
| 5 | import java.awt.event.KeyEvent;
|
|---|
| 6 | import java.beans.PropertyChangeListener;
|
|---|
| 7 | import java.util.ArrayList;
|
|---|
| 8 | import java.util.Collection;
|
|---|
| 9 | import java.util.List;
|
|---|
| 10 |
|
|---|
| 11 | import javax.swing.JCheckBoxMenuItem;
|
|---|
| 12 | import javax.swing.JMenu;
|
|---|
| 13 | import javax.swing.JMenuBar;
|
|---|
| 14 | import javax.swing.JMenuItem;
|
|---|
| 15 | import javax.swing.KeyStroke;
|
|---|
| 16 |
|
|---|
| 17 | import org.openstreetmap.josm.Main;
|
|---|
| 18 | import org.openstreetmap.josm.gui.MapFrame;
|
|---|
| 19 | import org.openstreetmap.josm.gui.layer.RawGpsLayer.GpsPoint;
|
|---|
| 20 | import org.openstreetmap.josm.plugins.Plugin;
|
|---|
| 21 |
|
|---|
| 22 | public class LiveGpsPlugin extends Plugin
|
|---|
| 23 | {
|
|---|
| 24 | private LiveGpsAcquirer acquirer = null;
|
|---|
| 25 | private Thread acquirerThread = null;
|
|---|
| 26 | private JMenu lgpsmenu;
|
|---|
| 27 | private JCheckBoxMenuItem lgpscapture;
|
|---|
| 28 | private JMenuItem lgpscenter;
|
|---|
| 29 | private JCheckBoxMenuItem lgpsautocenter;
|
|---|
| 30 | private LiveGpsDialog lgpsdialog;
|
|---|
| 31 | List<PropertyChangeListener>listenerQueue;
|
|---|
| 32 |
|
|---|
| 33 | private Collection<Collection<GpsPoint>> data = new ArrayList<Collection<GpsPoint>>();
|
|---|
| 34 | private LiveGpsLayer lgpslayer;
|
|---|
| 35 |
|
|---|
| 36 | public LiveGpsPlugin()
|
|---|
| 37 | {
|
|---|
| 38 | JMenuBar menu = Main.main.menu;
|
|---|
| 39 | lgpsmenu = new JMenu("LiveGPS");
|
|---|
| 40 | lgpsmenu.setMnemonic(KeyEvent.VK_G);
|
|---|
| 41 | menu.add(lgpsmenu, 2);
|
|---|
| 42 | lgpscapture = new JCheckBoxMenuItem("Capture GPS Track");
|
|---|
| 43 | lgpscapture.setSelected(false);
|
|---|
| 44 | lgpscapture.setAccelerator(KeyStroke.getKeyStroke("alt R"));
|
|---|
| 45 | lgpscapture.addActionListener(new ActionListener() {
|
|---|
| 46 | public void actionPerformed(ActionEvent ev) {
|
|---|
| 47 | enableTracking(lgpscapture.isSelected());
|
|---|
| 48 | }
|
|---|
| 49 | });
|
|---|
| 50 | lgpsmenu.add(lgpscapture);
|
|---|
| 51 |
|
|---|
| 52 | lgpscenter = new JMenuItem("Center Once", KeyEvent.VK_C);
|
|---|
| 53 | lgpscenter.addActionListener(new ActionListener() {
|
|---|
| 54 | public void actionPerformed(ActionEvent ev) {
|
|---|
| 55 | lgpslayer.center();
|
|---|
| 56 | }
|
|---|
| 57 | });
|
|---|
| 58 | lgpsmenu.add(lgpscenter);
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 | lgpsautocenter = new JCheckBoxMenuItem("Auto-Center on current position");
|
|---|
| 62 | lgpsautocenter.setSelected(false);
|
|---|
| 63 | lgpsautocenter.addActionListener(new ActionListener() {
|
|---|
| 64 | public void actionPerformed(ActionEvent ev) {
|
|---|
| 65 | lgpslayer.setAutoCenter(lgpsautocenter.isSelected());
|
|---|
| 66 | if (lgpsautocenter.isSelected()) lgpslayer.center();
|
|---|
| 67 | }
|
|---|
| 68 | });
|
|---|
| 69 | lgpsmenu.add(lgpsautocenter);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | /**
|
|---|
| 73 | * Enable or disable gps tracking
|
|---|
| 74 | * @param enable if <code>true</code> tracking is started.
|
|---|
| 75 | */
|
|---|
| 76 | public void enableTracking(boolean enable) {
|
|---|
| 77 | if ((acquirer != null) && (!enable))
|
|---|
| 78 | {
|
|---|
| 79 | acquirer.shutdown();
|
|---|
| 80 | acquirerThread = null;
|
|---|
| 81 | }
|
|---|
| 82 | else if(enable)
|
|---|
| 83 | {
|
|---|
| 84 | if (acquirer == null) {
|
|---|
| 85 | acquirer = new LiveGpsAcquirer();
|
|---|
| 86 | if (lgpslayer == null) {
|
|---|
| 87 | lgpslayer = new LiveGpsLayer(data);
|
|---|
| 88 | Main.main.addLayer(lgpslayer);
|
|---|
| 89 | }
|
|---|
| 90 | // connect layer with acquirer:
|
|---|
| 91 | addPropertyChangeListener(lgpslayer);
|
|---|
| 92 | // add all listeners that were added before the acquirer existed:
|
|---|
| 93 | if(listenerQueue != null) {
|
|---|
| 94 | for(PropertyChangeListener listener : listenerQueue) {
|
|---|
| 95 | addPropertyChangeListener(listener);
|
|---|
| 96 | }
|
|---|
| 97 | listenerQueue.clear();
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 | if(acquirerThread == null) {
|
|---|
| 101 | acquirerThread = new Thread(acquirer);
|
|---|
| 102 | acquirerThread.start();
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 | /**
|
|---|
| 109 | * Add a listener for gps events.
|
|---|
| 110 | * @param listener the listener.
|
|---|
| 111 | */
|
|---|
| 112 | public void addPropertyChangeListener(PropertyChangeListener listener) {
|
|---|
| 113 | if(acquirer != null) {
|
|---|
| 114 | acquirer.addPropertyChangeListener(listener);
|
|---|
| 115 | } else {
|
|---|
| 116 | if(listenerQueue == null) {
|
|---|
| 117 | listenerQueue = new ArrayList<PropertyChangeListener>();
|
|---|
| 118 | }
|
|---|
| 119 | listenerQueue.add(listener);
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 | /* (non-Javadoc)
|
|---|
| 125 | * @see org.openstreetmap.josm.plugins.Plugin#mapFrameInitialized(org.openstreetmap.josm.gui.MapFrame, org.openstreetmap.josm.gui.MapFrame)
|
|---|
| 126 | */
|
|---|
| 127 | @Override
|
|---|
| 128 | public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
|
|---|
| 129 | // add dialog
|
|---|
| 130 | newFrame.addToggleDialog(lgpsdialog = new LiveGpsDialog(newFrame));
|
|---|
| 131 | // connect listeners with acquirer:
|
|---|
| 132 | addPropertyChangeListener(lgpsdialog);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 | /**
|
|---|
| 137 | * @return the lgpsmenu
|
|---|
| 138 | */
|
|---|
| 139 | public JMenu getLgpsMenu() {
|
|---|
| 140 | return this.lgpsmenu;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | }
|
|---|