001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.plugins.streetside.mode; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.Color; 007import java.awt.Cursor; 008import java.awt.Graphics2D; 009import java.awt.Point; 010import java.awt.event.MouseEvent; 011import java.util.Arrays; 012 013import org.openstreetmap.josm.data.Bounds; 014import org.openstreetmap.josm.gui.MainApplication; 015import org.openstreetmap.josm.gui.MapView; 016import org.openstreetmap.josm.plugins.streetside.StreetsideAbstractImage; 017import org.openstreetmap.josm.plugins.streetside.StreetsideImportedImage; 018import org.openstreetmap.josm.plugins.streetside.StreetsideLayer; 019import org.openstreetmap.josm.plugins.streetside.history.StreetsideRecord; 020import org.openstreetmap.josm.plugins.streetside.history.commands.CommandJoin; 021import org.openstreetmap.josm.plugins.streetside.history.commands.CommandUnjoin; 022 023/** 024 * In this mode the user can join pictures to make sequences or unjoin them. 025 * 026 * @author nokutu 027 * 028 */ 029public class JoinMode extends AbstractMode { 030 031 private StreetsideImportedImage lastClick; 032 private MouseEvent lastPos; 033 034 /** 035 * Main constructor. 036 */ 037 public JoinMode() { 038 this.cursor = Cursor.CROSSHAIR_CURSOR; 039 } 040 041 @Override 042 public void mousePressed(MouseEvent e) { 043 final StreetsideAbstractImage highlighted = StreetsideLayer.getInstance().getData().getHighlightedImage(); 044 if (highlighted == null) { 045 return; 046 } 047 if (this.lastClick == null && highlighted instanceof StreetsideImportedImage) { 048 this.lastClick = (StreetsideImportedImage) highlighted; 049 } else if (this.lastClick != null 050 && highlighted instanceof StreetsideImportedImage) { 051 if ( 052 ( 053 (highlighted.previous() == null && this.lastClick.next() == null) || 054 (highlighted.next() == null && this.lastClick.previous() == null) 055 ) 056 && highlighted.getSequence() != this.lastClick.getSequence() 057 ) { 058 StreetsideRecord.getInstance().addCommand(new CommandJoin(this.lastClick, highlighted)); 059 } else if (this.lastClick.next() == highlighted || this.lastClick.previous() == highlighted) { 060 StreetsideRecord.getInstance().addCommand( 061 new CommandUnjoin(Arrays.asList(this.lastClick, highlighted)) 062 ); 063 } 064 this.lastClick = null; 065 } 066 StreetsideLayer.invalidateInstance(); 067 } 068 069 @Override 070 public void mouseMoved(MouseEvent e) { 071 this.lastPos = e; 072 if (!(MainApplication.getLayerManager().getActiveLayer() instanceof StreetsideLayer)) 073 return; 074 StreetsideAbstractImage closestTemp = getClosest(e.getPoint()); 075 StreetsideLayer.getInstance().getData().setHighlightedImage(closestTemp); 076 StreetsideLayer.invalidateInstance(); 077 } 078 079 @Override 080 public void paint(Graphics2D g, MapView mv, Bounds box) { 081 if (this.lastClick != null) { 082 g.setColor(Color.WHITE); 083 Point p0 = mv.getPoint(this.lastClick.getMovingLatLon()); 084 Point p1 = this.lastPos.getPoint(); 085 g.drawLine(p0.x, p0.y, p1.x, p1.y); 086 } 087 } 088 089 @Override 090 public String toString() { 091 return tr("Join mode"); 092 } 093}