001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.layer;
003
004import java.awt.Graphics2D;
005
006import org.openstreetmap.josm.data.Bounds;
007import org.openstreetmap.josm.gui.MapView;
008
009/**
010 * This is a component that can be painted on the map view.
011 * <p>
012 * You might want to extend {@link AbstractMapViewPaintable} to ease implementation of this.
013 * <p>
014 * That class allows you to listen to paintable change events. Those methods may be moved here some time in the future.
015 */
016public interface MapViewPaintable {
017
018    /**
019     * This event is fired whenever the paintable got invalidated and needs repainting some time in the future.
020     * <p>
021     * Note: We might add an area in the future.
022     *
023     * @author Michael Zangl
024     */
025    class PaintableInvalidationEvent {
026        private final MapViewPaintable paintable;
027
028        /**
029         * Creates a new {@link PaintableInvalidationEvent}
030         * @param paintable The paintable that is invalidated.
031         */
032        public PaintableInvalidationEvent(MapViewPaintable paintable) {
033            this.paintable = paintable;
034        }
035
036        /**
037         * Gets the layer that was invalidated.
038         * @return The layer.
039         */
040        public MapViewPaintable getLayer() {
041            return paintable;
042        }
043
044        @Override
045        public String toString() {
046            return "LayerInvalidationEvent [layer=" + paintable + ']';
047        }
048    }
049
050    /**
051     * This is a listener that listens to {@link PaintableInvalidationEvent}s
052     * @author Michael Zangl
053     */
054    interface PaintableInvalidationListener {
055        /**
056         * Called whenever a {@link PaintableInvalidationEvent} is fired. This might be called from any thread.
057         * @param event The event
058         */
059        void paintablInvalidated(PaintableInvalidationEvent event);
060    }
061
062    /**
063     * Gets a new LayerPainter that paints this {@link MapViewPaintable} to the given map view.
064     *
065     * @author Michael Zangl
066     * @since 10458
067     */
068    public interface LayerPainter {
069
070        /**
071         * Paints the given layer.
072         * <p>
073         * This can be called in any thread at any time. You will not receive parallel calls for the same map view but you can receive parallel
074         * calls if you use the same {@link LayerPainter} for different map views.
075         * @param graphics The graphics object of the map view you should use.
076         *                 It provides you with a content pane, the bounds and the view state.
077         */
078        void paint(MapViewGraphics graphics);
079
080        /**
081         * Called when the layer is removed from the map view and this painter is not used any more.
082         * <p>
083         * This method is called once on the painter returned by {@link Layer#attachToMapView}
084         * @param event The event.
085         */
086        void detachFromMapView(MapViewEvent event);
087    }
088
089    /**
090     * A event that is fired whenever the map view is attached or detached from any layer.
091     * @author Michael Zangl
092     * @see Layer#attachToMapView
093     * @since 10458
094     */
095    class MapViewEvent {
096        private final MapView mapView;
097        private final boolean temporaryLayer;
098
099        /**
100         * Create a new {@link MapViewEvent}
101         * @param mapView The map view
102         * @param temporaryLayer <code>true</code> if this layer is in the temporary layer list of the view.
103         */
104        public MapViewEvent(MapView mapView, boolean temporaryLayer) {
105            super();
106            this.mapView = mapView;
107            this.temporaryLayer = temporaryLayer;
108        }
109
110        /**
111         * Gets the map view.
112         * @return The map view.
113         */
114        public MapView getMapView() {
115            return mapView;
116        }
117
118        /**
119         * @return true if this {@link MapViewPaintable} is used as a temporary layer.
120         */
121        public boolean isTemporaryLayer() {
122            return temporaryLayer;
123        }
124
125        @Override
126        public String toString() {
127            return "AttachToMapViewEvent [mapView=" + mapView + ", temporaryLayer=" + temporaryLayer + "]";
128        }
129    }
130
131    /**
132     * Paint the dataset using the engine set.
133     * @param g Graphics
134     * @param mv The object that can translate GeoPoints to screen coordinates.
135     * @param bbox Bounding box
136     */
137    void paint(Graphics2D g, MapView mv, Bounds bbox);
138}