gwt view activity place clientfactory notes

VIEW

view extends isWidget, defines the widgets it will render.

ClientFactory

ClientFactory clientFactory = GWT.create(ClientFactory.class);

clientfacotry used to manage eventbus, views.

Since view is expensive to create, we can reuse views from this factory. (if not null, return existing instance)

Activities

Activities class usually extends AbstractActivity where we override the start() and mayStop() method.

  • start(containerWidget, eventBus) method is invoked by the ActivityManager to start a new Activity. we init our desired view here. Also event can be register here through the “eventBus” that is passed in. we set our desired view into the “containerWidget” in the end.
  • the mayStop() method ssk user before stopping this activity. we can return confirmation String here. Or null.

PlaceController

PlaceController placeController = new PlaceController(eventBus);

PlaceController’s goto(place) method controls where app goes.

if app is event driven, in the event handler, call the goto() in the end. Or this goto() can lie in specific activity as a coupled behavior.

Places

In order to be accessible via a URL, an Activity needs a corresponding Place. A Place extends com.google.gwt.place.shared.Place.

Places must have an associated PlaceTokenizer which knows how to serialize the Place’s state to a URL token.

often looks like:

public class ButtonPlace extends Place {
	public static class ButtonPlaceTokenizer implements PlaceTokenizer<ButtonPlace> {

		@Override
		public ButtonPlace getPlace(String token) {
			return new ButtonPlace();
		}

		@Override
		public String getToken(ButtonPlace place) {
			return "";
		}

	}
}

PlaceHistoryMapper

PlaceHistoryMapper declares all the Places available in your app. You create an interface that extends PlaceHistoryMapper and uses the annotation @WithTokenizers to list each of your tokenizer classes. Here is the PlaceHistoryMapper in our sample:

@WithTokenizers({ButtonPlace.Tokenizer.class, TextPlace .Tokenizer.class})
public interface AppPlaceHistoryMapper extends PlaceHistoryMapper
{
}

 

ActivityMapper

ActivityMapper maps each Place to its corresponding Activity. It must implement ActivityMapper, and will likely have lots of code like “if (place instanceof SomePlace) return new SomeActivity(place)”.

 

How it all works

The ActivityManager keeps track of all Activities running within the context of one container widget. It listens for PlaceChangeRequestEvents and notifies the current activity when a new Place has been requested. If the current Activity allows the Place change (Activity.onMayStop() returns null) or the user allows it (by clicking OK in the confirmation dialog), the ActivityManager discards the current Activity and starts the new one. In order to find the new one, it uses your app’s ActivityMapper to obtain the Activity associated with the requested Place.

Along with the ActivityManager, two other GWT classes work to keep track of Places in your app. PlaceController initiates navigation to a new Place and is responsible for warning the user before doing so. PlaceHistoryHandler provides bi-directional mapping between Places and the URL. Whenever your app navigates to a new Place, the URL will be updated with the new token representing the Place so it can be bookmarked and saved in browser history. Likewise, when the user clicks the back button or pulls up a bookmark, PlaceHistoryHandler ensures that your application loads the corresponding Place.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s