001    package com.google.gwt.maps.client.services;
002    
003    import com.google.gwt.core.client.JavaScriptObject;
004    import com.google.gwt.dom.client.Element;
005    import com.google.gwt.event.shared.HandlerRegistration;
006    import com.google.gwt.maps.client.MapImpl;
007    import com.google.gwt.maps.client.MapWidget;
008    import com.google.gwt.maps.client.events.MapEventType;
009    import com.google.gwt.maps.client.events.MapHandlerRegistration;
010    import com.google.gwt.maps.client.events.directions.DirectionsChangeEventFormatter;
011    import com.google.gwt.maps.client.events.directions.DirectionsChangeMapHandler;
012    import com.google.gwt.maps.client.mvc.MVCObject;
013    
014    /**
015     * Renders directions retrieved in the form of a DirectionsResult object retrieved from the DirectionsService. This class extends MVCObject. 
016     * <br><br>
017     * See <a href="https://developers.google.com/maps/documentation/javascript/reference#DirectionsRenderer">DirectionsRenderer API Doc</a>
018     */
019    public class DirectionsRenderer extends MVCObject<DirectionsRenderer> {
020      
021      /**
022       * use newInstance();
023       */
024      protected DirectionsRenderer() {}
025      
026      /**
027       * Renders directions retrieved in the form of a DirectionsResult object retrieved from the DirectionsService. This class extends MVCObject.
028       * @param options
029       */
030      public static final DirectionsRenderer newInstance(DirectionsRendererOptions options) {
031        return createJso(options).cast();
032      }
033    
034      private static final native JavaScriptObject createJso(DirectionsRendererOptions options) /*-{
035        return new $wnd.google.maps.DirectionsRenderer(options);
036      }-*/;
037      
038      /**
039       * Returns the renderer's current set of directions.
040       */
041      public final native DirectionsResult getDirections() /*-{
042        return this.getDirections();
043      }-*/;
044      
045      /**
046       * This method specifies the map on which directions will be rendered. Pass null to remove the directions from the map.
047       * @param mapWidget
048       */
049      public final void setMap(MapWidget mapWidget) {
050        setMapImpl(mapWidget.getJso());
051      }
052    
053      private final native void setMapImpl(MapImpl map) /*-{
054        this.setMap(map);
055      }-*/;
056    
057      /**
058       * Returns the map on which the DirectionsResult is rendered.
059       */
060      public final MapWidget getMap() {
061        return MapWidget.newInstance(getMapImpl());
062      }
063    
064      private final native MapImpl getMapImpl() /*-{
065        return this.getMap();
066      }-*/;
067    
068      /**
069       * Returns the panel <code>&lt;div&gt;</code> in which the DirectionsResult is rendered.
070       */
071      public final native Element getPanel() /*-{
072        return this.getPanel();
073      }-*/;
074      
075      /**
076       * Returns the current (zero-based) route index in use by this DirectionsRenderer object.
077       */
078      public final native int getRouteIndex() /*-{
079        return this.getRouteIndex();
080      }-*/;
081      
082      /**
083       * Set the renderer to use the result from the DirectionsService. Setting a valid set of directions in this manner will display the directions on the renderer's designated map and panel.
084       * @param directions
085       */
086      public final native void setDirections(DirectionsResult directions) /*-{
087        this.setDirections(directions);
088      }-*/;
089      
090      /**
091       * Change the options settings of this DirectionsRenderer after initialization.
092       * @param options
093       */
094      public final native void setOptions(DirectionsRendererOptions options) /*-{
095        this.setOptions(options);
096      }-*/;
097      
098      /**
099       * This method renders the directions in a <code>&lt;div&gt;</code>. Pass null to remove the content from the panel.
100       * @param element
101       */
102      public final native void setPanel(Element element) /*-{
103        this.setPanel(element);
104      }-*/;
105      
106      /**
107       * Set the (zero-based) index of the route in the DirectionsResult object to render. By default, the first route in the array will be rendered.
108       * @param routeIndex
109       */
110      public final native void setRouteIndex(int routeIndex) /*-{
111        this.setRouteIndex(routeIndex);
112      }-*/;
113      
114      /**
115       * This event is fired when the rendered directions change, either when a new DirectionsResult is set or when the user finishes dragging a change to the directions path.
116       * @param handler
117       */
118      public final HandlerRegistration addDirectionsChangeHandler(DirectionsChangeMapHandler handler) {
119        return MapHandlerRegistration.addHandler(this, MapEventType.DIRECTIONS_CHANGED, handler, new DirectionsChangeEventFormatter());
120      }
121      
122    }