import java.lang.reflect.InvocationTargetException; import javax.swing.SwingUtilities; import org.apache.log4j.Logger; import com.bbn.openmap.MapBean; import com.bbn.openmap.event.LayerEvent; /** * Changes how projectionChange events are propagated - layers no longer * repaint themselves, but instead the projection thread repaints the map once * after all the layers have been notified about the projection. */ public class MyMapBean extends MapBean { /** The Logger for this class */ private static Logger logger = Logger.getLogger( MyMapBean.class.getName() ); protected final static float DEFAULT_MAP_CENTER_LAT = 41.4116897f; protected final static float DEFAULT_MAP_CENTER_LON = -81.8497942f; protected final static int DEFAULT_MAP_WIDTH = 640; protected final static int DEFAULT_MAP_HEIGHT = 480; protected final static float DEFAULT_MAP_SCALE = 1f; /** Constructor */ public MyMapBean() { super(); //this allows a layer's memory to be freed as soon as possible setLayerRemovalDelayed(false); //start the projection support thread which repaints map after notifying layers. projectionSupport = new ProjectionSupportWithThread(this); } /** * Changes the scale. Allows the zoom-in/out operation to * be centered around an arbitrary point. * This allows zooming in/out on where the mouse is pointing. * NOTE: Only works if the current projection is LamAzEqArea * * @param scale The new zoom scale. * @param x Viewport coordinates * @param y Viewport coordinates */ public void setScale( float newScale, int x, int y ) { if(projection instanceof LamAzEqArea) { ((LamAzEqArea) projection).setScale( newScale, x, y ); fireProjectionChanged(); } else { //operation not supported } } /** * Pans the map using a pixel offset from the current location. * * NOTE: Only works if the current projection is LamAzEqArea * * @param dx pixel coords * @param dy pixel coords */ public void pan(int dx, int dy) { if(projection instanceof LamAzEqArea) { ((LamAzEqArea) projection).pan( dx, dy ); fireProjectionChanged(); } else { //operation not supported } } /** Repaints the mapbean. Doesn't return until the repainting is done. */ public void repaintSynchronously() throws InterruptedException { try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { //setBufferDirty(true); // not needed since BufferedMapBean no longer used paintImmediately(0, 0, getWidth(), getHeight() ); } }); } catch(InvocationTargetException e ) { e.printStackTrace(); //thrown if there's an error in the run() method. } } /** * NO LONGER NEEDED SINCE WE'RE NOT USING BUFFERED MAP BEAN * * Set the projection. This overrides the MapBean version of this method so that * the map bean's buffer will only be marked dirty after all Layers * have been notified and given a chance to process the projection changed. * * This makes it possible to repaint each layer only once - when all layers * have finished processing the projection change... until that time, the buffer * can continue to be used. * * @param aProjection Projection * public void setProjection(Projection aProjection) { if (aProjection != null) { projection = (Proj) aProjection; setPreferredSize(new Dimension(projection.getWidth(), projection.getHeight())); fireProjectionChanged(); } } */ /** * LayerListener interface method. A list of layers will be added, * removed, or replaced based on on the type of LayerEvent. * * @param evt a LayerEvent */ public void setLayers(LayerEvent evt) { super.setLayers( evt ); //bufferDirty = true; repaint(); } }