litesoft
@ 546
litesoft / trunk / GWT_Sandbox / MultiModule / common / src / org / litesoft / sandbox / infrastructure / client / CopyPlaceHistoryHandler.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
package org.litesoft.sandbox.infrastructure.client; import com.google.gwt.core.client.*; import com.google.gwt.event.logical.shared.*; import com.google.gwt.event.shared.*; import com.google.gwt.place.shared.*; public class CopyPlaceHistoryHandler { private final Historian historian; private final PlaceHistoryMapper mapper; private PlaceChanger mPlaceChanger; private Place defaultPlace = Place.NOWHERE; /** * Create a new PlaceHistoryHandler with a {@link HistorianImpl}. The * HistorianImpl is created via a call to GWT.create(), so an alternative * default implementation can be provided through <replace-with> rules * in a {@code gwt.xml} file. * * @param mapper a {@link PlaceHistoryMapper} instance */ public CopyPlaceHistoryHandler( PlaceHistoryMapper mapper ) { this( mapper, (Historian) GWT.create( HistorianImpl.class ) ); } /** * Create a new PlaceHistoryHandler. * * @param mapper a {@link PlaceHistoryMapper} instance * @param historian a {@link Historian} instance */ public CopyPlaceHistoryHandler( PlaceHistoryMapper mapper, Historian historian ) { this.mapper = mapper; this.historian = historian; } /** * Handle the current history token. Typically called at application start, to * ensure bookmark launches work. */ public void handleCurrentHistory() { handleHistoryToken( historian.getToken() ); } /** * Initialize this place history handler. * * @return a registration object to de-register the handler */ public HandlerRegistration register( PlaceChanger pPlaceChanger, EventBus eventBus, Place defaultPlace ) { this.mPlaceChanger = pPlaceChanger; this.defaultPlace = defaultPlace; final HandlerRegistration placeReg = eventBus.addHandler( PlaceChangeEvent.TYPE, new PlaceChangeEvent.Handler() { @Override public void onPlaceChange( PlaceChangeEvent event ) { Place newPlace = event.getNewPlace(); historian.newItem( tokenForPlace( newPlace ), false ); } } ); historian.addValueChangeHandler( new ValueChangeHandler<String>() { @Override public void onValueChange( ValueChangeEvent<String> event ) { String token = event.getValue(); handleHistoryToken( token ); } } ); return new HandlerRegistration() { @Override public void removeHandler() { CopyPlaceHistoryHandler.this.defaultPlace = Place.NOWHERE; CopyPlaceHistoryHandler.this.mPlaceChanger = null; placeReg.removeHandler(); } }; } private void handleHistoryToken( String token ) { Place newPlace = null; if ( "".equals( token ) ) { newPlace = defaultPlace; } if ( newPlace == null ) { newPlace = mapper.getPlace( token ); } if ( newPlace == null ) { // log().warning("Unrecognized history token: " + token); newPlace = defaultPlace; } mPlaceChanger.goTo( newPlace ); } private String tokenForPlace( Place newPlace ) { if ( defaultPlace.equals( newPlace ) ) { return ""; } String token = mapper.getToken( newPlace ); if ( token != null ) { return token; } // log().warning("Place not mapped to a token: " + newPlace); return ""; } } |