From 2a3025c8337577ea6d187fd0839f493813e3f6e1 Mon Sep 17 00:00:00 2001 From: Ben Konrath Date: Fri, 6 May 2011 16:25:41 +0200 Subject: [PATCH] Use Presenter for app navigation. This is the proper way to deal with Place (URL) changes with the MVP framework. * src/main/java/org/glom/web/client/ClientFactory.java: * src/main/java/org/glom/web/client/ClientFactoryImpl.java: Remove PlaceHistoryMapper and PlaceHistoryHandler. * src/main/java/org/glom/web/client/OnlineGlom.java: Re-add PlaceHistoryMapper and PlaceHistoryHandler. * src/main/java/org/glom/web/client/activity/DocumentSelectionActivity.java: Don't use getHistoryMapper().getToken(place) to create the hyperlinks. * src/main/java/org/glom/web/client/ui/DocumentSelectionView.java: * src/main/java/org/glom/web/client/ui/DocumentSelectionViewImpl.java: Add Presenter interface and setPresenter methods. Rename addHyperLink to addDocumentLink taking only the document title as a parameter. --- ChangeLog | 19 ++++++++++++++++++ .../java/org/glom/web/client/ClientFactory.java | 6 ------ .../org/glom/web/client/ClientFactoryImpl.java | 15 -------------- src/main/java/org/glom/web/client/OnlineGlom.java | 11 +++++++---- .../client/activity/DocumentSelectionActivity.java | 17 +++++++++------- .../glom/web/client/ui/DocumentSelectionView.java | 9 ++++++++- .../web/client/ui/DocumentSelectionViewImpl.java | 23 +++++++++++++++++++--- 7 files changed, 64 insertions(+), 36 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2eaecf8..c964606 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,22 @@ +2011-05-06 Ben Konrath + + Use Presenter for app navigation. + + This is the proper way to deal with Place (URL) changes with the MVP + framework. + + * src/main/java/org/glom/web/client/ClientFactory.java: + * src/main/java/org/glom/web/client/ClientFactoryImpl.java: Remove + PlaceHistoryMapper and PlaceHistoryHandler. + * src/main/java/org/glom/web/client/OnlineGlom.java: Re-add + PlaceHistoryMapper and PlaceHistoryHandler. + * src/main/java/org/glom/web/client/activity/DocumentSelectionActivity.java: + Don't use getHistoryMapper().getToken(place) to create the hyperlinks. + * src/main/java/org/glom/web/client/ui/DocumentSelectionView.java: + * src/main/java/org/glom/web/client/ui/DocumentSelectionViewImpl.java: + Add Presenter interface and setPresenter methods. Rename addHyperLink + to addDocumentLink taking only the document title as a parameter. + 2011-04-14 Ben Konrath Prompt for db username/password if they haven't been set. diff --git a/src/main/java/org/glom/web/client/ClientFactory.java b/src/main/java/org/glom/web/client/ClientFactory.java index e933039..de59a5e 100644 --- a/src/main/java/org/glom/web/client/ClientFactory.java +++ b/src/main/java/org/glom/web/client/ClientFactory.java @@ -24,8 +24,6 @@ import org.glom.web.client.ui.OnlineGlomView; import com.google.gwt.event.shared.EventBus; import com.google.gwt.place.shared.PlaceController; -import com.google.gwt.place.shared.PlaceHistoryHandler; -import com.google.gwt.place.shared.PlaceHistoryMapper; public interface ClientFactory { @@ -33,10 +31,6 @@ public interface ClientFactory { PlaceController getPlaceController(); - PlaceHistoryHandler getHistoryHandler(); - - PlaceHistoryMapper getHistoryMapper(); - OnlineGlomView getOnlineGlomView(); DocumentSelectionView getDocumentSelectionView(); diff --git a/src/main/java/org/glom/web/client/ClientFactoryImpl.java b/src/main/java/org/glom/web/client/ClientFactoryImpl.java index 6eaa5e6..0ec6e28 100644 --- a/src/main/java/org/glom/web/client/ClientFactoryImpl.java +++ b/src/main/java/org/glom/web/client/ClientFactoryImpl.java @@ -19,24 +19,18 @@ package org.glom.web.client; -import org.glom.web.client.mvp.AppPlaceHistoryMapper; import org.glom.web.client.ui.DocumentSelectionView; import org.glom.web.client.ui.DocumentSelectionViewImpl; import org.glom.web.client.ui.OnlineGlomView; import org.glom.web.client.ui.OnlineGlomViewImpl; -import com.google.gwt.core.client.GWT; import com.google.gwt.event.shared.EventBus; import com.google.gwt.event.shared.SimpleEventBus; import com.google.gwt.place.shared.PlaceController; -import com.google.gwt.place.shared.PlaceHistoryHandler; -import com.google.gwt.place.shared.PlaceHistoryMapper; public class ClientFactoryImpl implements ClientFactory { private final EventBus eventBus = new SimpleEventBus(); private final PlaceController placeController = new PlaceController(eventBus); - private final AppPlaceHistoryMapper historyMapper = GWT.create(AppPlaceHistoryMapper.class); - private final PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper); private final OnlineGlomView onlineGlomView = new OnlineGlomViewImpl(); private final DocumentSelectionView documentSelectionView = new DocumentSelectionViewImpl(); @@ -52,15 +46,6 @@ public class ClientFactoryImpl implements ClientFactory { return onlineGlomView; } - public PlaceHistoryHandler getHistoryHandler() { - return historyHandler; - } - - public PlaceHistoryMapper getHistoryMapper() { - return historyMapper; - } - - @Override public DocumentSelectionView getDocumentSelectionView() { return documentSelectionView; } diff --git a/src/main/java/org/glom/web/client/OnlineGlom.java b/src/main/java/org/glom/web/client/OnlineGlom.java index eb27e36..03367b6 100644 --- a/src/main/java/org/glom/web/client/OnlineGlom.java +++ b/src/main/java/org/glom/web/client/OnlineGlom.java @@ -20,6 +20,7 @@ package org.glom.web.client; import org.glom.web.client.mvp.AppActivityMapper; +import org.glom.web.client.mvp.AppPlaceHistoryMapper; import org.glom.web.client.place.DocumentSelectionPlace; import com.google.gwt.activity.shared.ActivityManager; @@ -49,18 +50,20 @@ public class OnlineGlom implements EntryPoint { EventBus eventBus = clientFactory.getEventBus(); PlaceController placeController = clientFactory.getPlaceController(); - // Start ActivityManager for the main widget with our ActivityMapper + // Start the ActivityManager for the main root panel. ActivityMapper activityMapper = new AppActivityMapper(clientFactory); ActivityManager activityManager = new ActivityManager(activityMapper, eventBus); activityManager.setDisplay(panel); - PlaceHistoryHandler historyHandler = clientFactory.getHistoryHandler(); + // Start PlaceHistoryHandler with our PlaceHistoryMapper + AppPlaceHistoryMapper historyMapper = GWT.create(AppPlaceHistoryMapper.class); + PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper); historyHandler.register(placeController, eventBus, defaultPlace); - // Put the container managed by the activity manger in the root panel. + // Put the container that is managed by the activity manager in the root panel. RootPanel.get().add(panel); - // Goes to the place represented on URL else default place + // Goes to the place represented on URL or the default place. historyHandler.handleCurrentHistory(); } diff --git a/src/main/java/org/glom/web/client/activity/DocumentSelectionActivity.java b/src/main/java/org/glom/web/client/activity/DocumentSelectionActivity.java index 0218599..3602f2d 100644 --- a/src/main/java/org/glom/web/client/activity/DocumentSelectionActivity.java +++ b/src/main/java/org/glom/web/client/activity/DocumentSelectionActivity.java @@ -23,20 +23,19 @@ import java.util.ArrayList; import org.glom.web.client.ClientFactory; import org.glom.web.client.OnlineGlomServiceAsync; -import org.glom.web.client.place.OnlineGlomPlace; import org.glom.web.client.ui.DocumentSelectionView; import com.google.gwt.activity.shared.AbstractActivity; import com.google.gwt.event.shared.EventBus; +import com.google.gwt.place.shared.Place; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.ui.AcceptsOneWidget; -public class DocumentSelectionActivity extends AbstractActivity { +public class DocumentSelectionActivity extends AbstractActivity implements DocumentSelectionView.Presenter { // TODO inject with GIN private final ClientFactory clientFactory; - private final ArrayList documentPlaces = new ArrayList(); public DocumentSelectionActivity(ClientFactory clientFactory) { this.clientFactory = clientFactory; @@ -46,6 +45,7 @@ public class DocumentSelectionActivity extends AbstractActivity { public void start(AcceptsOneWidget panel, EventBus eventBus) { Window.setTitle("Online Glom"); final DocumentSelectionView documentSelectionView = clientFactory.getDocumentSelectionView(); + documentSelectionView.setPresenter(this); AsyncCallback> callback = new AsyncCallback>() { public void onFailure(Throwable caught) { @@ -57,10 +57,7 @@ public class DocumentSelectionActivity extends AbstractActivity { documentSelectionView.clearHyperLinks(); if (!documentTitles.isEmpty()) { for (String documentTitle : documentTitles) { - OnlineGlomPlace place = new OnlineGlomPlace(documentTitle); - documentPlaces.add(place); - documentSelectionView.addHyperLink(documentTitle, - clientFactory.getHistoryMapper().getToken(place)); + documentSelectionView.addDocumentLink(documentTitle); } } else { documentSelectionView @@ -72,4 +69,10 @@ public class DocumentSelectionActivity extends AbstractActivity { panel.setWidget(documentSelectionView.asWidget()); } + + @Override + public void goTo(Place place) { + clientFactory.getPlaceController().goTo(place); + } + } diff --git a/src/main/java/org/glom/web/client/ui/DocumentSelectionView.java b/src/main/java/org/glom/web/client/ui/DocumentSelectionView.java index 2c6f2b6..2a58794 100644 --- a/src/main/java/org/glom/web/client/ui/DocumentSelectionView.java +++ b/src/main/java/org/glom/web/client/ui/DocumentSelectionView.java @@ -19,11 +19,18 @@ package org.glom.web.client.ui; +import com.google.gwt.place.shared.Place; import com.google.gwt.user.client.ui.IsWidget; public interface DocumentSelectionView extends IsWidget { - void addHyperLink(String dbTitle, String string); + public interface Presenter { + void goTo(Place place); + } + + void setPresenter(Presenter presenter); + + void addDocumentLink(String documentTitle); void clearHyperLinks(); diff --git a/src/main/java/org/glom/web/client/ui/DocumentSelectionViewImpl.java b/src/main/java/org/glom/web/client/ui/DocumentSelectionViewImpl.java index d6db9e5..acfc8e6 100644 --- a/src/main/java/org/glom/web/client/ui/DocumentSelectionViewImpl.java +++ b/src/main/java/org/glom/web/client/ui/DocumentSelectionViewImpl.java @@ -19,11 +19,15 @@ package org.glom.web.client.ui; +import org.glom.web.client.place.OnlineGlomPlace; + import com.google.gwt.core.client.GWT; +import com.google.gwt.event.dom.client.ClickEvent; +import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.uibinder.client.UiBinder; import com.google.gwt.uibinder.client.UiField; +import com.google.gwt.user.client.ui.Anchor; import com.google.gwt.user.client.ui.Composite; -import com.google.gwt.user.client.ui.Hyperlink; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.user.client.ui.Widget; @@ -36,13 +40,21 @@ public class DocumentSelectionViewImpl extends Composite implements DocumentSele private static DocumentSelectionViewImplUiBinder uiBinder = GWT.create(DocumentSelectionViewImplUiBinder.class); @UiField VerticalPanel documentLinks; + private Presenter presenter; public DocumentSelectionViewImpl() { initWidget(uiBinder.createAndBindUi(this)); } - public void addHyperLink(String dbTitle, String targetHistoryToken) { - documentLinks.add(new Hyperlink(dbTitle, targetHistoryToken)); + public void addDocumentLink(final String documentTitle) { + Anchor link = new Anchor(documentTitle); + link.addClickHandler(new ClickHandler() { + @Override + public void onClick(ClickEvent event) { + presenter.goTo(new OnlineGlomPlace(documentTitle)); + } + }); + documentLinks.add(link); } public void clearHyperLinks() { @@ -60,4 +72,9 @@ public class DocumentSelectionViewImpl extends Composite implements DocumentSele label.getElement().getStyle().setColor("Red"); documentLinks.add(label); } + + @Override + public void setPresenter(Presenter presenter) { + this.presenter = presenter; + } } -- 2.1.4