[OpenMap Users] Mouse Events: example of low-level mouse event handling.

From: Cary O'Brien <cobrien_at_cornet.com>
Date: Fri, 03 Dec 2004 12:06:31 -0500

Here is an example of how to set up a layer to catch low-level
(move, click, press, release, move, drag, enter, leave)
mouse events. The shape layer subclass uses its own MapMouseListener,
which simply prints information from the MapMouseEvent.

I will try to create a similar example with high-level events
(select/highlight)
if time allows.

Comments?

-- cary



// **********************************************************************
//
// <copyright>
//
// BBN Technologies, a Verizon Company
// 10 Moulton Street
// Cambridge, MA 02138
// (617) 873-8000
//
// Copyright (C) BBNT Solutions LLC. All rights reserved.
//
// </copyright>
// **********************************************************************
//
// $Source: /usr2/local/CVS/openmap-4.6/src/openmap/com/bbn/openmap/examples/simple/SimpleMap.java,v $
// $RCSfile: SimpleMap.java,v $
// $Revision: 1.1.1.1 $
// $Date: 2004/07/14 12:06:31 $
// $Author: cary $
//
// **********************************************************************


package com.bbn.openmap.examples.simple;

import java.awt.event.*;
import java.util.Properties;
import javax.swing.JFrame;

import com.bbn.openmap.*;
import com.bbn.openmap.gui.*;
import com.bbn.openmap.event.*;
import com.bbn.openmap.util.Debug;
import com.bbn.openmap.omGraphics.*;
import com.bbn.openmap.omGraphics.event.*;
import com.bbn.openmap.layer.shape.ShapeLayer;


/**
 * This is a simple application that uses the OpenMap MapBean to show
 * a map.
 * <p>
 * This example shows:
 * <ul>
 * <li>MapBean
 * <li>ShapeLayer with political data
 *<li>Low-level map events handled the layer.
 * </ul>
 */
public class SimpleMapWithLowLevelMouseEvents extends JFrame {
    
    public SimpleMapWithLowLevelMouseEvents(String name) {
        super(name); // set title

        
        setSize(640, 480); // and location
        
        // Create a map handler (map, layer, component container)
        MapHandler mapHandler = new MapHandler();
        
        // Create a MapBean
        MapBean mapBean = new MapBean();
        mapHandler.add(mapBean);
        
        // Set Gesture (select) mode for the map.
        MouseDelegator mouseDelegator = new MouseDelegator();
        mapHandler.add(mouseDelegator);
        SelectMouseMode selectMouseMode = new SelectMouseMode();
        mapHandler.add(selectMouseMode);
        mouseDelegator.setActive(selectMouseMode);
        
        //
        // Create a shape layer subclass with our own map mouse listener.
        //
        final MyMapMouseListener myMapMouseListener = new MyMapMouseListener();
        //
        // Subclass ShapeLayer
        //
        ShapeLayer shapeLayer = new ShapeLayer() {
            //
            // When asked for the mouse listener, return my version.
            //
            public synchronized MapMouseListener getMapMouseListener() {
                return myMapMouseListener;
            }
        };
        
        // Configure ShapeLayer to show world political boundaries.
        // Set the properties of the layer. This assumes that the
        // datafiles "dcwpo-browse.shp" and "dcwpo-browse.ssx" are in
        // a path specified in the CLASSPATH variable. These files
        // are distributed with OpenMap and reside in the toplevel
        // "share" subdirectory.
      
        Properties shapeLayerProps = new Properties();
        shapeLayerProps.put("prettyName", "Political Solid");
        shapeLayerProps.put("lineColor", "000000");
        shapeLayerProps.put("fillColor", "BDDE83");
        shapeLayerProps.put("shapeFile", "data/shape/dcwpo-browse.shp");
        shapeLayerProps.put("spatialIndex", "data/shape/dcwpo-browse.ssx");
        shapeLayer.setProperties(shapeLayerProps);
           
        // Add the political layer to the map
        mapBean.add(shapeLayer);
        
        // Add the map to the frame
        getContentPane().add(mapBean);
        
    }
    
    public static void main(String[] argv) {
                
        Debug.init();
        //Debug.put("mousemode");
        
        SimpleMapWithLowLevelMouseEvents simpleMap = new SimpleMapWithLowLevelMouseEvents("Simple Map With Low-Level Mouse Events");
        simpleMap.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }});
            
            // Display the frame
            
            simpleMap.show();
    }
    
    //
    // Create our own MouseMapInterpreter to handle mouse events.
    // In a real application this would be an innner class of
    // some app specific class, and would have access application-specific
    // information.
    //
    public class MyMapMouseListener implements MapMouseListener {
        //
        // We need to tell the system we want events in Select (Gesture)
        // mouse mode.
        //
        public String[] getMouseModeServiceList() {
            return new String[] {
                SelectMouseMode.modeID
            };
        }
        //
        // Print interesting information. THe MapMouseEvent
        // is map-bean and lat-long aware
        //
        protected void printMouseEvent(String msg, MouseEvent e) {
            if(e instanceof MapMouseEvent) {
                MapMouseEvent mme = (MapMouseEvent) e;
                System.out.println(msg + " at " + mme.getLatLon());
            } else {
                System.out.println(msg + " " + e);
            }
        }
        public boolean mousePressed(MouseEvent e) {
            printMouseEvent("mouse pressed ",e);
            return true;
        }
       
        public boolean mouseReleased(MouseEvent e) {
            printMouseEvent("mouse released ", e);
             return true;
        }
        
        public boolean mouseClicked(MouseEvent e) {
            printMouseEvent("mouse clicked " ,e);
             return true;
        }
        
        public void mouseEntered(MouseEvent e) {
            printMouseEvent("mouse entered ", e);
        }
        
        
        public void mouseExited(MouseEvent e) {
            printMouseEvent("mouse exited ", e);
        }
        
        public boolean mouseDragged(MouseEvent e) {
            printMouseEvent("mouse dragged ", e);
            return true;
        }
        
        //
        // Don't print every one of these, but you can use them
        // if you want.
        public boolean mouseMoved(MouseEvent e) {
            //printMouseEvent("mouse pressed ", e);
            return true;
        }
        
        public void mouseMoved() {
            System.out.println("mouse moved - no event");
        }
        

    }
}


--
[To unsubscribe to this list send an email to "majdart_at_bbn.com"
with the following text in the BODY of the message "unsubscribe openmap-users"]
Received on Fri Dec 03 2004 - 12:15:52 EST

This archive was generated by hypermail 2.3.0 : Tue Mar 28 2017 - 23:25:06 EDT