org.xwiki.gwt.user.client
Class BackForwardCache

java.lang.Object
  extended by org.xwiki.gwt.user.client.BackForwardCache
All Implemented Interfaces:
Updatable

public class BackForwardCache
extends Object
implements Updatable

Allows us to preserve the state of the editor against the browser's Back button, the soft page refresh and the browser crash by using the browser's cache mechanism. Modern browsers cache HTML form fields like input and textarea as long as they are generated on the server and not on the client using JavaScript. This class uses the value of such an element to save string properties.

Version:
$Id: fb803524fa4442f7ff594b5dedf1470d9307de92 $

Constructor Summary
BackForwardCache(Element cacheable)
          Creates a new cache based on the given HTML element whose value can be cached by the browser.
 
Method Summary
 boolean canUpdate()
           
protected  Map<String,String> deserialize(String data)
          Parses the given string and extracts a map of key-value pairs.
 String get(String key)
          Retrieves the value of a cached property specified by the given key.
 String get(String key, String defaultValue)
          Retrieves the value of a cached property specified by the given key, falling back on a default value if the key is not found in the cache.
protected  Map<String,String> getMap()
           
 String put(String key, String value)
          Adds a new key in the cache or updates the value of an existing key.
protected  String serialize(Map<String,String> map)
          Converts the given map of key-value pairs to a string from which it can be recomputed.
 void update()
          Updates the state of this object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BackForwardCache

public BackForwardCache(Element cacheable)
Creates a new cache based on the given HTML element whose value can be cached by the browser. Examples of such elements are input and textarea.

The browser doesn't cache the HTML elements added dynamically through JavaScript but only the static ones. Moving an element inside the DOM tree prevents the browser from caching its value even if that element was generated on the server.

Parameters:
cacheable - any HTML element whose value can be cached by the browser
Method Detail

get

public String get(String key)
Retrieves the value of a cached property specified by the given key.

Parameters:
key - a key in the cache
Returns:
the value associated with the given key or null if there's no such key in the cache

get

public String get(String key,
                  String defaultValue)
Retrieves the value of a cached property specified by the given key, falling back on a default value if the key is not found in the cache.

Parameters:
key - a key in the cache
defaultValue - the value returned if the specified key is not found in the cache
Returns:
the value associated with the given key or defaultValue if there's no such key in the cache

put

public String put(String key,
                  String value)
Adds a new key in the cache or updates the value of an existing key.

Parameters:
key - a key in the cache
value - the new value for the given key
Returns:
the previous value associated with the given key

getMap

protected Map<String,String> getMap()
Returns:
the Map used to store the cache for quick access

canUpdate

public boolean canUpdate()
Specified by:
canUpdate in interface Updatable
Returns:
true if this object can be updated at this point, false otherwise

update

public void update()
Description copied from interface: Updatable
Updates the state of this object.

Specified by:
update in interface Updatable

deserialize

protected Map<String,String> deserialize(String data)
Parses the given string and extracts a map of key-value pairs.

Parameters:
data - the string to be deserialized
Returns:
the map whose serialization is the given string
See Also:
serialize(Map)

serialize

protected String serialize(Map<String,String> map)
Converts the given map of key-value pairs to a string from which it can be recomputed.

Parameters:
map - the map to be serialized
Returns:
a string representation of the given map
See Also:
deserialize(String)


Copyright © 2004-2012 XWiki. All Rights Reserved.