001    package com.google.gwt.maps.client.adsense;
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.controls.ControlPosition;
009    import com.google.gwt.maps.client.events.MapEventType;
010    import com.google.gwt.maps.client.events.MapHandlerRegistration;
011    import com.google.gwt.maps.client.events.channelnumber.ChannelNumberChangeEventFormatter;
012    import com.google.gwt.maps.client.events.channelnumber.ChannelNumberChangeMapHandler;
013    import com.google.gwt.maps.client.events.format.FormatChangeEventFormatter;
014    import com.google.gwt.maps.client.events.format.FormatChangeMapHandler;
015    import com.google.gwt.maps.client.events.mapchange.MapChangeEventFormatter;
016    import com.google.gwt.maps.client.events.mapchange.MapChangeMapHandler;
017    import com.google.gwt.maps.client.events.position.PositionChangeEventFormatter;
018    import com.google.gwt.maps.client.events.position.PositionChangeMapHandler;
019    import com.google.gwt.maps.client.mvc.MVCObject;
020    
021    /**
022     * Implements AdSense for Content advertising on an associated map. To use an
023     * AdUnit, you must obtain and specify an AdSense for Content publisher ID
024     * within the AdUnit's constructor options. This class extends MVCObject.
025     * <br><br>
026     * See <a href="https://developers.google.com/maps/documentation/javascript/reference#AdUnit">AdUnit API Doc</a>
027     */
028    public class AdUnitImpl extends MVCObject<AdUnitImpl> {
029    
030      private final static native JavaScriptObject createJso(Element container, AdUnitOptions options) /*-{
031                    return new $wnd.google.maps.adsense.AdUnit(container, options);
032      }-*/;
033    
034      /**
035       * Creates an AdSense for Content display ad on the associated map.
036       * 
037       * @param options
038       */
039      public final static AdUnitImpl newInstance(Element container, AdUnitOptions options) {
040        return createJso(container, options).cast();
041      }
042    
043      /**
044       * use newInstance();
045       */
046      protected AdUnitImpl() {}
047    
048      /**
049       * This event is fired when the AdUnit's channelNumber property changes.
050       * @param handler
051       */
052      public final HandlerRegistration addChannelNumberChangeHandler(ChannelNumberChangeMapHandler handler) {
053        return MapHandlerRegistration.addHandler(this, MapEventType.CHANNELNUMBER_CHANGED, handler, new ChannelNumberChangeEventFormatter());
054      }
055    
056      /**
057       * This event is fired when the AdUnit's format property changes.
058       * @param handler
059       */
060      public final HandlerRegistration addFormatChangeHandler(FormatChangeMapHandler handler) {
061        return MapHandlerRegistration.addHandler(this, MapEventType.FORMAT_CHANGED, handler, new FormatChangeEventFormatter());
062      }
063    
064      /**
065       * This event is fired when the AdUnit's map property changes.
066       * @param handler
067       */
068      public final HandlerRegistration addMapChangeHandler(MapChangeMapHandler handler) {
069        return MapHandlerRegistration.addHandler(this, MapEventType.MAP_CHANGED, handler, new MapChangeEventFormatter());
070      }
071    
072      /**
073       * This event is fired when the AdUnit's position property changes.
074       * @param handler
075       */
076      public final HandlerRegistration addPositionChangeHandler(PositionChangeMapHandler handler) {
077        return MapHandlerRegistration.addHandler(this, MapEventType.POSITION_CHANGED, handler, new PositionChangeEventFormatter());
078      }
079    
080      /**
081       * Returns the channel number in use by this AdUnit.
082       */
083      public final native String getChannelNumber() /*-{
084                    return this.getChannelNumber();
085      }-*/;
086    
087      /**
088       * Returns the containing element of the AdUnit.
089       */
090      public final native Element getContainer() /*-{
091                    return this.getContainer();
092      }-*/;
093    
094      /**
095       * Returns the format in use by this AdUnit.
096       */
097      public final native AdFormat getFormat() /*-{
098                    return this.getFormat();
099      }-*/;
100    
101      /**
102       * Returns the map to which this AdUnit's ads are targeted.
103       */
104      public final MapWidget getMap() {
105        return MapWidget.newInstance(getMapImpl());
106      }
107    
108      private final native MapImpl getMapImpl() /*-{
109                    return this.getMap();
110      }-*/;
111    
112      /**
113       * Returns the ControlPosition at which this AdUnit is displayed on the map.
114       */
115      public final ControlPosition getPosition() {
116        return ControlPosition.fromValue(getPositionImpl());
117      }
118    
119      private final native int getPositionImpl() /*-{
120        return this.getPosition();
121      }-*/;
122      
123      /**
124       * Returns the specified AdSense For Content publisher ID.
125       */
126      public final native String getPublisherId() /*-{
127                    return this.getPublisherId();
128      }-*/;
129      
130      /**
131       * Specifies the channel number for this AdUnit. Channel numbers are optional and can be created for Google AdSense tracking.
132       * @param channelNumber
133       */
134      public final native void setChannelNumber(String channelNumber) /*-{
135                    this.channelNumber = channelNumber;
136      }-*/;
137    
138      /**
139       * Specifies the display format for this AdUnit.
140       * @param format
141       */
142      public final native void setFormat(AdFormat format) /*-{
143                    this.setFormat(format);
144      }-*/;
145      
146      /**
147       * Associates this AdUnit with the specified map. Ads will be targeted to the map's viewport. The map must be specified in order to display ads.
148       * @param mapWidget
149       */
150      public final void setMap(MapWidget mapWidget) {
151        setMapImpl(mapWidget.getJso());
152      }
153      
154      private final native void setMapImpl(MapImpl map) /*-{
155                    this.setMap(map);
156      }-*/;
157      
158      /**
159       * Sets the ControlPosition at which to display the AdUnit on the map. If the position is set to null, the AdUnit is removed from the map.
160       * @param position {@link ControlPosition}
161       */
162      public final void setPosition(ControlPosition position) {
163        setPositionImpl(position.value());
164      }
165    
166      private final native void setPositionImpl(int position) /*-{
167        this.setPosition(position);
168      }-*/;
169      
170    }