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