Changeset 23189 in osm for applications/editors/josm/plugins/editgpx/src/org/openstreetmap
- Timestamp:
- 2010-09-15T18:53:09+02:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/editgpx/src/org/openstreetmap/josm/plugins/editgpx
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/editgpx/src/org/openstreetmap/josm/plugins/editgpx/EditGpxLayer.java
r22548 r23189 35 35 public class EditGpxLayer extends Layer { 36 36 37 38 39 37 private static Icon icon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(EditGpxPlugin.class.getResource("/images/editgpx_layer.png"))); 38 public final EditGpxData data; 39 private GPXLayerImportAction layerImport; 40 40 41 42 43 44 45 41 public EditGpxLayer(String str, EditGpxData gpxData) { 42 super(str); 43 data = gpxData; 44 layerImport = new GPXLayerImportAction(data); 45 } 46 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 47 /** 48 * check if dataSet is empty 49 * if so show import dialog to user 50 */ 51 public void initializeImport() { 52 try { 53 if(data.isEmpty()) { 54 layerImport.activateImport(); 55 } 56 } catch (Exception e) { 57 System.out.println(e.getMessage()); 58 e.printStackTrace(); 59 } 60 } 61 61 62 63 64 65 62 @Override 63 public Icon getIcon() { 64 return icon; 65 } 66 66 67 68 69 70 67 @Override 68 public Object getInfoComponent() { 69 return getToolTipText(); 70 } 71 71 72 73 74 75 76 77 78 79 80 81 82 83 72 @Override 73 public Action[] getMenuEntries() { 74 return new Action[] { 75 LayerListDialog.getInstance().createShowHideLayerAction(), 76 LayerListDialog.getInstance().createDeleteLayerAction(), 77 SeparatorLayerAction.INSTANCE, 78 layerImport, 79 new ConvertToGpxLayerAction(), 80 new ConvertToAnonTimeGpxLayerAction(), 81 SeparatorLayerAction.INSTANCE, 82 new LayerListPopup.InfoAction(this)}; 83 } 84 84 85 86 87 88 85 @Override 86 public String getToolTipText() { 87 return tr("Layer for editing GPX tracks"); 88 } 89 89 90 91 92 93 94 90 @Override 91 public boolean isMergable(Layer other) { 92 // TODO 93 return false; 94 } 95 95 96 97 98 99 96 @Override 97 public void mergeFrom(Layer from) { 98 // TODO 99 } 100 100 101 102 103 101 @Override 102 public void paint(Graphics2D g, MapView mv, Bounds bounds) { 103 g.setColor(Color.yellow); 104 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 105 //don't iterate through dataSet whiling making changes 106 synchronized(layerImport.importing) { 107 for (EditGpxTrack track: data.getTracks()) { 108 for (EditGpxTrackSegment segment: track.getSegments()) { 109 for (EditGpxWayPoint wayPoint: segment.getWayPoints()) { 110 if (!wayPoint.isDeleted()) { 111 Point pnt = Main.map.mapView.getPoint(wayPoint.getCoor().getEastNorth()); 112 g.drawOval(pnt.x - 2, pnt.y - 2, 4, 4); 113 } 114 } 115 } 116 } 117 } 118 } 119 119 120 120 121 122 123 121 public void reset(){ 122 //TODO implement a reset 123 } 124 124 125 125 126 127 128 129 126 @Override 127 public void visitBoundingBox(BoundingXYVisitor v) { 128 // TODO Auto-generated method stub 129 } 130 130 131 131 132 133 134 135 136 137 138 139 140 132 /** 133 * convert a DataSet to GPX 134 * 135 * @param boolean anonTime If true set all time and date in GPX to 01/01/1970 00:00 ? 136 * @return GPXData 137 */ 138 private GpxData toGpxData(boolean anonTime) { 139 return data.createGpxData(); 140 } 141 141 142 143 144 145 146 147 148 149 150 151 152 153 142 //context item "Convert to GPX layer" 143 public class ConvertToGpxLayerAction extends AbstractAction { 144 public ConvertToGpxLayerAction() { 145 super(tr("Convert to GPX layer"), ImageProvider.get("converttogpx")); 146 } 147 public void actionPerformed(ActionEvent e) { 148 Main.main.addLayer(new GpxLayer(toGpxData(false), tr("Converted from: {0}", getName()))); 149 Main.main.removeLayer(EditGpxLayer.this); 150 if(Main.map.mapMode instanceof EditGpxMode) 151 Main.map.selectSelectTool(false); 152 } 153 } 154 154 155 156 157 158 159 160 161 162 163 164 165 166 155 //context item "Convert to GPX layer with anonymised time" 156 public class ConvertToAnonTimeGpxLayerAction extends AbstractAction { 157 public ConvertToAnonTimeGpxLayerAction() { 158 super(tr("Convert to GPX layer with anonymised time"), ImageProvider.get("converttogpx")); 159 } 160 public void actionPerformed(ActionEvent e) { 161 Main.main.addLayer(new GpxLayer(toGpxData(true), tr("Converted from: {0}", getName()))); 162 Main.main.removeLayer(EditGpxLayer.this); 163 if(Main.map.mapMode instanceof EditGpxMode) 164 Main.map.selectSelectTool(false); 165 } 166 } 167 167 } -
applications/editors/josm/plugins/editgpx/src/org/openstreetmap/josm/plugins/editgpx/EditGpxMode.java
r21582 r23189 29 29 public class EditGpxMode extends MapMode implements LayerChangeListener { 30 30 31 32 33 34 35 36 31 private static final long serialVersionUID = 7940589057093872411L; 32 Point pointPressed; 33 MapFrame mapFrame; 34 Rectangle oldRect; 35 MapFrame frame; 36 EditGpxLayer currentEditLayer; 37 37 38 39 40 38 public EditGpxMode(MapFrame mapFrame, String name, String desc) { 39 super(name, "editgpx_mode.png", desc, mapFrame, Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); 40 } 41 41 42 43 44 45 46 47 48 42 @Override public void enterMode() { 43 super.enterMode(); 44 Main.map.mapView.addMouseListener(this); 45 Main.map.mapView.addMouseMotionListener(this); 46 MapView.addLayerChangeListener(this); 47 updateLayer(); 48 } 49 49 50 51 52 53 54 50 @Override public void exitMode() { 51 super.exitMode(); 52 Main.map.mapView.removeMouseListener(this); 53 Main.map.mapView.removeMouseMotionListener(this); 54 } 55 55 56 56 57 58 59 57 @Override public void mousePressed(MouseEvent e) { 58 pointPressed = new Point(e.getPoint()); 59 } 60 60 61 61 62 63 64 65 66 67 62 @Override public void mouseDragged(MouseEvent e) { 63 if ( (e.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) == InputEvent.BUTTON1_DOWN_MASK) { 64 //if button1 is hold, draw the rectangle. 65 paintRect(pointPressed, e.getPoint()); 66 } 67 } 68 68 69 70 71 72 69 @Override public void mouseReleased(MouseEvent e) { 70 if (e.getButton() != MouseEvent.BUTTON1) { 71 return; 72 } 73 73 74 74 Point pointReleased = e.getPoint(); 75 75 76 76 Rectangle r = createRect(pointReleased, pointPressed); 77 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 78 //go through nodes and mark the ones in the selection rect as deleted 79 if (currentEditLayer != null) { 80 for (EditGpxTrack track: currentEditLayer.data.getTracks()) { 81 for (EditGpxTrackSegment segment: track.getSegments()) { 82 for (EditGpxWayPoint wayPoint: segment.getWayPoints()) { 83 Point p = Main.map.mapView.getPoint(wayPoint.getCoor().getEastNorth()); 84 if (r.contains(p)) { 85 wayPoint.setDeleted(true); 86 } 87 } 88 } 89 } 90 } 91 oldRect = null; 92 Main.map.mapView.repaint(); 93 93 94 94 } 95 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 96 /** 97 * create rectangle out of two given corners 98 */ 99 public Rectangle createRect(Point p1, Point p2) { 100 int x,y,w,h; 101 if (p1.x == p2.x && p1.y == p2.y) { 102 //if p1 and p2 same points draw a small rectangle around them 103 x = p1.x -1; 104 y = p1.y -1; 105 w = 3; 106 h = 3; 107 } else { 108 if (p1.x < p2.x){ 109 x = p1.x; 110 w = p2.x-p1.x; 111 } else { 112 x = p2.x; 113 w = p1.x-p2.x; 114 } 115 if (p1.y < p2.y) { 116 y = p1.y; 117 h = p2.y-p1.y; 118 } else { 119 y = p2.y; 120 h = p1.y-p2.y; 121 } 122 } 123 return new Rectangle(x,y,w,h); 124 } 125 125 126 127 128 129 130 126 /** 127 * Draw a selection rectangle on screen. 128 */ 129 private void paintRect(Point p1, Point p2) { 130 Graphics g = frame.getGraphics();//Main.map.mapView.getGraphics(); 131 131 132 133 134 135 136 137 138 132 Rectangle r = oldRect; 133 if (r != null) { 134 //overwrite old rct 135 g.setXORMode(Color.BLACK); 136 g.setColor(Color.WHITE); 137 g.drawRect(r.x,r.y,r.width,r.height); 138 } 139 139 140 141 142 143 144 145 140 g.setXORMode(Color.BLACK); 141 g.setColor(Color.WHITE); 142 r = createRect(p1,p2); 143 g.drawRect(r.x,r.y,r.width,r.height); 144 oldRect = r; 145 } 146 146 147 147 148 149 150 148 public void setFrame(MapFrame mapFrame) { 149 frame = mapFrame; 150 } 151 151 152 153 154 155 152 /** 153 * create new layer, add listeners and try importing gpx data. 154 */ 155 private void updateLayer() { 156 156 157 158 157 List<EditGpxLayer> layers = Main.map.mapView.getLayersOfType(EditGpxLayer.class); 158 currentEditLayer = layers.isEmpty()?null:layers.get(0); 159 159 160 161 162 163 164 165 166 160 if(currentEditLayer == null) { 161 currentEditLayer = new EditGpxLayer(tr("EditGpx"), new EditGpxData()); 162 Main.main.addLayer(currentEditLayer); 163 currentEditLayer.initializeImport(); 164 } 165 Main.map.mapView.repaint(); 166 } 167 167 168 168 public void activeLayerChange(Layer oldLayer, Layer newLayer) { } 169 169 170 170 public void layerAdded(Layer newLayer) { } 171 171 172 173 174 175 176 177 178 172 public void layerRemoved(Layer oldLayer) { 173 if (oldLayer instanceof EditGpxLayer) { 174 currentEditLayer = null; 175 if(Main.map.mapMode instanceof EditGpxMode) 176 Main.map.selectSelectTool(false); 177 } 178 } 179 179 180 180 } -
applications/editors/josm/plugins/editgpx/src/org/openstreetmap/josm/plugins/editgpx/EditGpxPlugin.java
r22548 r23189 31 31 public class EditGpxPlugin extends Plugin { 32 32 33 34 33 private IconToggleButton btn; 34 private EditGpxMode mode; 35 35 36 37 38 36 public EditGpxPlugin(PluginInformation info) { 37 super(info); 38 mode = new EditGpxMode(Main.map, "editgpx", tr("edit gpx tracks")); 39 39 40 41 42 40 btn = new IconToggleButton(mode); 41 btn.setVisible(true); 42 } 43 43 44 45 46 47 48 49 50 44 /** 45 * initialize button. if button is pressed create new layer. 46 */ 47 @Override 48 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 49 if(oldFrame == null && newFrame != null) { 50 mode.setFrame(newFrame); 51 51 52 53 54 55 52 if(Main.map != null) 53 Main.map.addMapMode(btn); 54 } 55 } 56 56 57 58 59 60 57 public static ImageIcon loadIcon(String name) { 58 URL url = EditGpxPlugin.class.getResource("/images/editgpx.png"); 59 return new ImageIcon(url); 60 } 61 61 } -
applications/editors/josm/plugins/editgpx/src/org/openstreetmap/josm/plugins/editgpx/GPXLayerImportAction.java
r21582 r23189 38 38 39 39 40 41 42 40 private static final long serialVersionUID = 5794897888911798168L; 41 private EditGpxData data; 42 public Object importing = new Object(); //used for synchronization 43 43 44 45 46 47 48 44 public GPXLayerImportAction(EditGpxData data) { 45 //TODO what is icon at the end? 46 super(tr("Import path from GPX layer"), ImageProvider.get("dialogs", "edit")); 47 this.data = data; 48 } 49 49 50 51 52 53 54 55 56 50 /** 51 * shows a list of GPX layers. if user selects one the data from this layer is 52 * imported. 53 */ 54 public void activateImport() { 55 Box panel = Box.createVerticalBox(); 56 DefaultListModel dModel= new DefaultListModel(); 57 57 58 59 60 61 58 final JList layerList = new JList(dModel); 59 Collection<Layer> data = Main.map.mapView.getAllLayers(); 60 Layer lastLayer = null; 61 int layerCnt = 0; 62 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 63 for (Layer l : data){ 64 if(l instanceof GpxLayer){ 65 dModel.addElement(l); 66 lastLayer = l; 67 layerCnt++; 68 } 69 } 70 if(layerCnt == 1){ 71 layerList.setSelectedValue(lastLayer, true); 72 } 73 if(layerCnt > 0){ 74 layerList.setCellRenderer(new DefaultListCellRenderer(){ 75 @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 76 Layer layer = (Layer)value; 77 JLabel label = (JLabel)super.getListCellRendererComponent(list, 78 layer.getName(), index, isSelected, cellHasFocus); 79 Icon icon = layer.getIcon(); 80 label.setIcon(icon); 81 label.setToolTipText(layer.getToolTipText()); 82 return label; 83 } 84 }); 85 85 86 86 JCheckBox dropFirst = new JCheckBox(tr("Drop existing path")); 87 87 88 89 88 panel.add(layerList); 89 panel.add(dropFirst); 90 90 91 92 93 94 95 96 97 91 final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION){ 92 @Override public void selectInitialValue() { 93 layerList.requestFocusInWindow(); 94 } 95 }; 96 final JDialog dlg = optionPane.createDialog(Main.parent, tr("Import path from GPX layer")); 97 dlg.setVisible(true); 98 98 99 100 101 102 103 99 Object answer = optionPane.getValue(); 100 if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE || 101 (answer instanceof Integer && (Integer)answer != JOptionPane.OK_OPTION)) { 102 return; 103 } 104 104 105 105 GpxLayer gpx = (GpxLayer)layerList.getSelectedValue(); 106 106 107 108 109 110 107 synchronized(importing) { 108 this.data.load(gpx.data, dropFirst.isSelected()); 109 } 110 Main.map.mapView.repaint(); 111 111 112 113 114 115 116 112 } else { 113 // no gps layer 114 JOptionPane.showMessageDialog(Main.parent,tr("No GPX data layer found.")); 115 } 116 } 117 117 118 119 120 121 122 123 124 118 /** 119 * called when pressing "Import.." from context menu of EditGpx layer 120 * 121 */ 122 public void actionPerformed(ActionEvent arg0) { 123 activateImport(); 124 } 125 125 } -
applications/editors/josm/plugins/editgpx/src/org/openstreetmap/josm/plugins/editgpx/data/EditGpxData.java
r21582 r23189 11 11 public class EditGpxData { 12 12 13 14 15 16 13 private final List<EditGpxTrack> tracks = new ArrayList<EditGpxTrack>(); 14 // Only copy of routes and waypoints to preserve all info when converting back to gpx track 15 private final List<GpxRoute> routes = new ArrayList<GpxRoute>(); 16 private final List<WayPoint> waypoints = new ArrayList<WayPoint>(); 17 17 18 19 20 21 22 23 24 25 26 27 28 18 public void load(GpxData data, boolean drop) { 19 if(drop) 20 tracks.clear(); 21 for (GpxTrack track: data.tracks) { 22 tracks.add(new EditGpxTrack(track)); 23 } 24 routes.clear(); 25 routes.addAll(data.routes); 26 waypoints.clear(); 27 waypoints.addAll(data.waypoints); 28 } 29 29 30 31 32 33 34 35 36 37 38 39 30 public boolean isEmpty() { 31 for (EditGpxTrack track: tracks) { 32 for (EditGpxTrackSegment segment: track.getSegments()) { 33 if (!segment.getWayPoints().isEmpty()) { 34 return false; 35 } 36 } 37 } 38 return true; 39 } 40 40 41 42 43 41 public List<EditGpxTrack> getTracks() { 42 return tracks; 43 } 44 44 45 46 45 public GpxData createGpxData() { 46 GpxData result = new GpxData(); 47 47 48 49 50 51 52 53 54 55 48 for (EditGpxTrack track: tracks) { 49 if (!track.isDeleted()) { 50 GpxTrack newTrack = track.createGpxTrack(); 51 if (!newTrack.getSegments().isEmpty()) { 52 result.tracks.add(newTrack); 53 } 54 } 55 } 56 56 57 58 59 60 57 result.routes.addAll(routes); 58 result.waypoints.addAll(waypoints); 59 return result; 60 } 61 61 62 62 } -
applications/editors/josm/plugins/editgpx/src/org/openstreetmap/josm/plugins/editgpx/data/EditGpxTrack.java
r21472 r23189 14 14 public class EditGpxTrack { 15 15 16 17 18 16 private final List<EditGpxTrackSegment> segments = new ArrayList<EditGpxTrackSegment>(); 17 private final Map<String, Object> attributes = new HashMap<String, Object>(); 18 private boolean isDeleted; 19 19 20 21 22 23 24 25 20 public EditGpxTrack(GpxTrack track) { 21 attributes.putAll(track.getAttributes()); 22 for (GpxTrackSegment segment: track.getSegments()) { 23 segments.add(new EditGpxTrackSegment(segment)); 24 } 25 } 26 26 27 28 29 30 31 32 27 public List<EditGpxTrackSegment> getSegments() { 28 return segments; 29 } 30 public Map<String, Object> getAttributes() { 31 return attributes; 32 } 33 33 34 34 public GpxTrack createGpxTrack() { 35 35 36 36 Collection<Collection<WayPoint>> wayPoints = new ArrayList<Collection<WayPoint>>(); 37 37 38 39 40 41 42 43 44 45 38 for (EditGpxTrackSegment segment: segments) { 39 if (!segment.isDeleted()) { 40 List<WayPoint> points = segment.getNonDeletedWaypoints(); 41 if (!points.isEmpty()) { 42 wayPoints.add(points); 43 } 44 } 45 } 46 46 47 48 47 return new ImmutableGpxTrack(wayPoints, attributes); 48 } 49 49 50 51 52 50 public void setDeleted(boolean isDeleted) { 51 this.isDeleted = isDeleted; 52 } 53 53 54 55 56 54 public boolean isDeleted() { 55 return isDeleted; 56 } 57 57 } -
applications/editors/josm/plugins/editgpx/src/org/openstreetmap/josm/plugins/editgpx/data/EditGpxTrackSegment.java
r21472 r23189 9 9 public class EditGpxTrackSegment { 10 10 11 12 11 private final List<EditGpxWayPoint> wayPoints = new ArrayList<EditGpxWayPoint>(); 12 private boolean deleted; 13 13 14 15 16 17 18 14 public EditGpxTrackSegment(GpxTrackSegment segment) { 15 for (WayPoint wayPoint: segment.getWayPoints()) { 16 wayPoints.add(new EditGpxWayPoint(wayPoint)); 17 } 18 } 19 19 20 21 22 20 public List<EditGpxWayPoint> getWayPoints() { 21 return wayPoints; 22 } 23 23 24 25 24 public List<WayPoint> getNonDeletedWaypoints() { 25 List<WayPoint> result = new ArrayList<WayPoint>(); 26 26 27 28 29 30 31 27 for (EditGpxWayPoint wp: wayPoints) { 28 if (!wp.isDeleted()) { 29 result.add(wp.createWayPoint()); 30 } 31 } 32 32 33 34 33 return result; 34 } 35 35 36 37 38 36 public void setDeleted(boolean deleted) { 37 this.deleted = deleted; 38 } 39 39 40 41 42 40 public boolean isDeleted() { 41 return deleted; 42 } 43 43 44 44 } -
applications/editors/josm/plugins/editgpx/src/org/openstreetmap/josm/plugins/editgpx/data/EditGpxWayPoint.java
r21472 r23189 8 8 9 9 public class EditGpxWayPoint { 10 11 12 13 10 private final double time; 11 private final CachedLatLon coor; 12 private boolean deleted; 13 private Map<String, Object> attributes; 14 14 15 16 17 18 19 15 public EditGpxWayPoint(WayPoint wayPoint) { 16 this.time = wayPoint.time; 17 this.coor = new CachedLatLon(wayPoint.getCoor()); 18 this.attributes = new HashMap<String, Object>(wayPoint.attr); 19 } 20 20 21 22 23 24 25 26 21 public WayPoint createWayPoint() { 22 WayPoint result = new WayPoint(getCoor()); 23 result.time = time; 24 result.attr = attributes; 25 return result; 26 } 27 27 28 29 30 28 public void setDeleted(boolean deleted) { 29 this.deleted = deleted; 30 } 31 31 32 33 34 32 public boolean isDeleted() { 33 return deleted; 34 } 35 35 36 37 38 36 public CachedLatLon getCoor() { 37 return coor; 38 } 39 39 }
Note:
See TracChangeset
for help on using the changeset viewer.