All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.cloudfarming.client.geoviewer.jxmap.map.AbstractLocationPicker Maven / Gradle / Ivy

Go to download

AgroSense geoviewer JXMap implementation. Contains a map/geoviewer TopComponent based on the JXMap classes from swingx.

The newest version!
/**
 * Copyright (C) 2008-2012 AgroSense Foundation.
 *
 * AgroSense is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * There are special exceptions to the terms and conditions of the GPLv3 as it is applied to
 * this software, see the FLOSS License Exception
 * .
 *
 * AgroSense is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with AgroSense.  If not, see .
 */
package nl.cloudfarming.client.geoviewer.jxmap.map;

import java.awt.Cursor;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import nl.cloudfarming.client.geoviewer.jxmap.render.JXMapGeoTranslator;
import nl.cloudfarming.client.geoviewer.render.GeoTranslator;
import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.mapviewer.GeoPosition;

/**
 * Pick a location on the map.
 * Clicking on the map selects a location, pressing the escape key cancels the action.
 * 
 * @author johan
 */
public abstract class AbstractLocationPicker implements MouseListener, KeyListener {
    public static final String PROP_ACTIVE = "active";
    private final JXMapViewer mapViewer;
    private final boolean multiple;
    private boolean active = false;
    private PropertyChangeSupport propertyChangeSupport;

    public AbstractLocationPicker(JXMapViewer mapViewer) {
        this(mapViewer, false);
    }

    public AbstractLocationPicker(JXMapViewer mapViewer, boolean multiple) {
        this.mapViewer = mapViewer;
        this.multiple = multiple;
    }

    public void start() {
        // TODO: set cursor on topPanel instead of container
        mapViewer.getParent().setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
        mapViewer.addMouseListener(this);
        mapViewer.addKeyListener(this);
        setActive(true);
    }

    /**
        * Removes itself from the map viewer and resets the map viewer's
        * cursor to the default one.
        */
    public void stop() {
        mapViewer.removeMouseListener(this);
        mapViewer.removeKeyListener(this);
        mapViewer.getParent().setCursor(Cursor.getDefaultCursor());
        setActive(false);
    }

    public boolean isActive() {
        return active;
    }
    
    private void setActive(boolean active) {
        boolean oldActive = this.active;
        this.active = active;
        getPropertyChangeSupport().firePropertyChange(PROP_ACTIVE, oldActive, active);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        Point point = new Point(e.getX(), e.getY());
        GeoTranslator translator = new JXMapGeoTranslator(mapViewer);
        GeoPosition position = translator.pixelToGeo(null, null, point);

        if (select(position) && !multiple) {
            stop();   
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
            cancel();
            stop();
        }
    }

    /**
        * Handle the selected location
        * 
        * @param position
        * @return true if the selected location was accepted
        */
    public abstract boolean select(GeoPosition position);

    /**
        * Optional actions when the location picking is canceled.
        * Do nothing by default.
        */
    public void cancel() {
    }
    
    PropertyChangeSupport getPropertyChangeSupport(){
        if (propertyChangeSupport == null){
            propertyChangeSupport = new PropertyChangeSupport(this);
        }
        return propertyChangeSupport;
    }

    public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
        getPropertyChangeSupport().addPropertyChangeListener(propertyName, listener);
    }

    public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
        getPropertyChangeSupport().removePropertyChangeListener(propertyName, listener);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy