nl.cloudfarming.client.geoviewer.jxmap.map.MapMouseHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of geoviewer-jxmap Show documentation
Show all versions of geoviewer-jxmap Show documentation
AgroSense geoviewer JXMap implementation. Contains a map/geoviewer TopComponent based on the JXMap classes from swingx.
/**
* Copyright (C) 2010-2012 Agrosense [email protected]
*
* Licensed under the Eclipse Public License - v 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.eclipse.org/legal/epl-v10.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.cloudfarming.client.geoviewer.jxmap.map;
import java.awt.event.*;
import java.util.Comparator;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Timon Veenstra
*/
public class MapMouseHandler implements MouseListener, MouseMotionListener, MouseWheelListener{
private static final Logger LOGGER = Logger.getAnonymousLogger();
private boolean trace = false;
// static{
// LOGGER.setLevel(Level.ALL);
// LOGGER.addHandler(SystemLogController.getInstance());
// }
private final Comparator comparator = new Comparator() {
@Override
public int compare(MapMouseListener o1, MapMouseListener o2) {
if (o1 == null || o2 == null){
return 0;
}
if (o1.getGroup().equals(o2.getGroup())){
return (o1.getId() == o2.getId())?0:(o1.getId() < o2.getId()) ? -1 : 1;
}else{
return (o1.getGroup().getValue() == o2.getGroup().getValue()) ? 0 : (o1.getGroup().getValue() > o2.getGroup().getValue()) ? - 1 : 1;
}
}
};
private final MapMouseListener root;
private final SortedSet listeners = new TreeSet(comparator);
public MapMouseHandler(MapMouseListener root) {
this.root = root;
}
public void addMapMouseListener(MapMouseListener mapMouseListener){
listeners.add(mapMouseListener);
}
public void removeMapMouseListener(MapMouseListener mapMouseListener){
listeners.remove(mapMouseListener);
}
/**
* dispatch the event to the listeners and the map panel
*
* @param e
*/
private void dispatch(MouseEvent e){
MapMouseEvent event = new MapMouseEvent(e);
LISTENERS:
for (MapMouseListener listener:listeners){
trace("Dispatching to:");
trace("id:"+listener.getId()+"");
trace("group:"+listener.getGroup().name());
listener.onMapMouseEvent(event);
if (event.isConsumed()){
trace("is consumed");
break LISTENERS;
}
}
if (!event.isConsumed() ){
trace("dispatching to the root panel");
root.onMapMouseEvent(event);
}
}
@Override
public void mouseClicked(MouseEvent e) {
trace =true;
trace("==============================================================");
trace("mouseClicked");
dispatch(e);
trace =false;
}
@Override
public void mousePressed(MouseEvent e) {
trace =true;
trace("==============================================================");
trace("mousePressed");
dispatch(e);
trace =false;
}
@Override
public void mouseReleased(MouseEvent e) {
trace =true;
trace("==============================================================");
trace("mouseReleased");
dispatch(e);
trace =false;
}
@Override
public void mouseEntered(MouseEvent e) {
// trace("mouseEntered");
dispatch(e);
}
@Override
public void mouseExited(MouseEvent e) {
// trace("mouseExited");
dispatch(e);
}
@Override
public void mouseDragged(MouseEvent e) {
// trace("mouseDragged");
dispatch(e);
}
@Override
public void mouseMoved(MouseEvent e) {
// trace("mouseMoved");
dispatch(e);
}
private void trace(String msg){
if (trace && LOGGER.isLoggable(Level.FINEST)){
LOGGER.finest(msg);
}
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
// trace("mouseWheelMoved");
dispatch(e);
}
}