001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.web.client; 017 018import com.google.gwt.event.logical.shared.HasValueChangeHandlers; 019import com.google.gwt.event.logical.shared.ValueChangeEvent; 020import com.google.gwt.event.logical.shared.ValueChangeHandler; 021import com.google.gwt.event.shared.GwtEvent; 022import com.google.gwt.event.shared.HandlerRegistration; 023import com.google.gwt.event.shared.SimpleEventBus; 024import com.google.gwt.place.shared.PlaceHistoryHandler.Historian; 025import com.google.gwt.user.client.History; 026 027/** 028 * 029 * @author kulikov 030 */ 031public class HtmlHistory implements Historian, 032 // allows the use of ValueChangeEvent.fire() 033 HasValueChangeHandlers<String> { 034 035 private final SimpleEventBus handlers = new SimpleEventBus(); 036 private String token; 037 038 public HtmlHistory() { 039 initEvent(); 040 this.token = History.getToken(); 041 } 042 043 @Override 044 public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> valueChangeHandler) { 045 return this.handlers.addHandler(ValueChangeEvent.getType(), valueChangeHandler); 046 } 047 048 @Override 049 public String getToken() { 050 return token; 051 } 052 053 @Override 054 public void newItem(String token, boolean issueEvent) { 055 this.token = token; 056 pushState(token); 057 } 058 059 @Override 060 public void fireEvent(GwtEvent<?> event) { 061 this.handlers.fireEvent(event); 062 } 063 064 private native void initEvent() /*-{ 065 var that = this; 066 var oldHandler = $wnd.onpopstate; 067 $wnd.onpopstate = $entry(function(e) { 068 var uri = $wnd.location 069 that.@org.modeshape.web.client.HtmlHistory::onPopState(Ljava/lang/String;)(uri); 070 if (oldHandler) { 071 oldHandler(); 072 } 073 }); 074 }-*/; 075 076 private void onPopState(String url) { 077 StringBuilder b = new StringBuilder(); 078 b.append(url); 079 ValueChangeEvent.fire(this, b.toString()); 080 } 081 082 private native void pushState(String url) /*-{ 083 $wnd.history.pushState(null, $doc.title, url); 084 }-*/; 085}