Including the TaskProxy (when available) in TaskEditPlace so we do not do an extra round trip to the server to lookup the task.
Also fixing a bug where the "Task List" menu item isn't selected when the task list is visible because TaskListPlace is no longer a singleton. We now do an instanceof check instead of an == check to compare the TaskListPlace in the menu item to the TaskListPlace from the place change event.
Review at http://gwt-code-reviews.appspot.com/1428810
Review by: rchandia@google.com
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@10134 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/samples/mobilewebapp/src/main/com/google/gwt/sample/mobilewebapp/client/activity/TaskEditActivity.java b/samples/mobilewebapp/src/main/com/google/gwt/sample/mobilewebapp/client/activity/TaskEditActivity.java
index 9035240..1a01ac8 100644
--- a/samples/mobilewebapp/src/main/com/google/gwt/sample/mobilewebapp/client/activity/TaskEditActivity.java
+++ b/samples/mobilewebapp/src/main/com/google/gwt/sample/mobilewebapp/client/activity/TaskEditActivity.java
@@ -78,6 +78,7 @@
*/
public TaskEditActivity(TaskEditPlace place, ClientFactory clientFactory) {
this.taskId = place.getTaskId();
+ this.task = place.getTask();
this.clientFactory = clientFactory;
}
@@ -163,40 +164,45 @@
// Lock the display until the task is loaded.
isEditing = true;
view.setEditing(true);
- view.setLocked(true);
- // Load the existing task.
- clientFactory.getRequestFactory().taskRequest().findTask(this.taskId).fire(
- new Receiver<TaskProxy>() {
- @Override
- public void onFailure(ServerFailure error) {
- Window.alert("An error occurred on the server while loading this task."
- + " Please select a different task from the task list.");
- doCancelTask();
- }
-
- @Override
- public void onSuccess(TaskProxy response) {
- // Early exit if this activity has already been cancelled.
- if (isDead) {
- return;
- }
-
- // Task not found.
- if (response == null) {
- Window.alert("The task with id '" + taskId + "' could not be found."
+ if (task == null) {
+ // Load the existing task.
+ view.setLocked(true);
+ clientFactory.getRequestFactory().taskRequest().findTask(this.taskId).fire(
+ new Receiver<TaskProxy>() {
+ @Override
+ public void onFailure(ServerFailure error) {
+ Window.alert("An error occurred on the server while loading this task."
+ " Please select a different task from the task list.");
doCancelTask();
- return;
}
- // Show the task.
- task = response;
- view.getEditorDriver()
- .edit(response, clientFactory.getRequestFactory().taskRequest());
- view.setLocked(false);
- }
- });
+ @Override
+ public void onSuccess(TaskProxy response) {
+ // Early exit if this activity has already been cancelled.
+ if (isDead) {
+ return;
+ }
+
+ // Task not found.
+ if (response == null) {
+ Window.alert("The task with id '" + taskId + "' could not be found."
+ + " Please select a different task from the task list.");
+ doCancelTask();
+ return;
+ }
+
+ // Show the task.
+ task = response;
+ view.getEditorDriver().edit(response,
+ clientFactory.getRequestFactory().taskRequest());
+ view.setLocked(false);
+ }
+ });
+ } else {
+ // Use the task that was passed with the place.
+ view.getEditorDriver().edit(task, clientFactory.getRequestFactory().taskRequest());
+ }
}
// Display the view.
diff --git a/samples/mobilewebapp/src/main/com/google/gwt/sample/mobilewebapp/client/activity/TaskListActivity.java b/samples/mobilewebapp/src/main/com/google/gwt/sample/mobilewebapp/client/activity/TaskListActivity.java
index 832931a..5e66f02 100644
--- a/samples/mobilewebapp/src/main/com/google/gwt/sample/mobilewebapp/client/activity/TaskListActivity.java
+++ b/samples/mobilewebapp/src/main/com/google/gwt/sample/mobilewebapp/client/activity/TaskListActivity.java
@@ -221,7 +221,8 @@
public void selectTask(TaskProxy selected) {
// Go into edit mode when a task is selected.
- clientFactory.getPlaceController().goTo(TaskEditPlace.createTaskEditPlace(selected.getId()));
+ clientFactory.getPlaceController().goTo(
+ TaskEditPlace.createTaskEditPlace(selected.getId(), selected));
}
public void start(AcceptsOneWidget container, EventBus eventBus) {
diff --git a/samples/mobilewebapp/src/main/com/google/gwt/sample/mobilewebapp/client/desktop/MobileWebAppShellDesktop.java b/samples/mobilewebapp/src/main/com/google/gwt/sample/mobilewebapp/client/desktop/MobileWebAppShellDesktop.java
index bbcfadb..49d7647 100644
--- a/samples/mobilewebapp/src/main/com/google/gwt/sample/mobilewebapp/client/desktop/MobileWebAppShellDesktop.java
+++ b/samples/mobilewebapp/src/main/com/google/gwt/sample/mobilewebapp/client/desktop/MobileWebAppShellDesktop.java
@@ -18,9 +18,8 @@
import com.google.gwt.canvas.dom.client.CssColor;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.GWT;
-import com.google.gwt.dom.client.MediaElement;
-import com.google.gwt.dom.client.VideoElement;
import com.google.gwt.dom.client.Style.Unit;
+import com.google.gwt.dom.client.VideoElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.media.client.Video;
@@ -111,6 +110,17 @@
public Place getPlace() {
return place;
}
+
+ /**
+ * Check whether or not this {@link MainMenuItem} maps to the specified
+ * place.
+ *
+ * @param p a {@link Place}
+ * @return true if this menu item maps to the place, false if not
+ */
+ public boolean mapsToPlace(Place p) {
+ return place == p;
+ }
}
/**
@@ -202,7 +212,13 @@
// Add items to the main menu.
final List<MainMenuItem> menuItems = new ArrayList<MainMenuItem>();
- menuItems.add(new MainMenuItem("Task List", new TaskListPlace(false)));
+ menuItems.add(new MainMenuItem("Task List", new TaskListPlace(false)) {
+ @Override
+ public boolean mapsToPlace(Place p) {
+ // Map to all TaskListPlace instances.
+ return p instanceof TaskListPlace;
+ }
+ });
menuItems.add(new MainMenuItem("Add Task", TaskEditPlace.getTaskCreatePlace()));
mainMenu.setRowData(menuItems);
@@ -224,7 +240,7 @@
public void onPlaceChange(PlaceChangeEvent event) {
Place place = event.getNewPlace();
for (MainMenuItem menuItem : menuItems) {
- if (place == menuItem.getPlace()) {
+ if (menuItem.mapsToPlace(place)) {
// We found a match in the main menu.
selectionModel.setSelected(menuItem, true);
return;
diff --git a/samples/mobilewebapp/src/main/com/google/gwt/sample/mobilewebapp/client/place/TaskEditPlace.java b/samples/mobilewebapp/src/main/com/google/gwt/sample/mobilewebapp/client/place/TaskEditPlace.java
index 025654f..3d9fb2b 100644
--- a/samples/mobilewebapp/src/main/com/google/gwt/sample/mobilewebapp/client/place/TaskEditPlace.java
+++ b/samples/mobilewebapp/src/main/com/google/gwt/sample/mobilewebapp/client/place/TaskEditPlace.java
@@ -1,12 +1,12 @@
/*
* Copyright 2011 Google Inc.
- *
+ *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -17,6 +17,7 @@
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceTokenizer;
+import com.google.gwt.sample.mobilewebapp.shared.TaskProxy;
/**
* The place in the app that show a task in an editable view.
@@ -34,10 +35,10 @@
try {
// Parse the task ID from the URL.
Long taskId = Long.parseLong(token);
- return new TaskEditPlace(taskId);
+ return new TaskEditPlace(taskId, null);
} catch (NumberFormatException e) {
// If the ID cannot be parsed, assume we are creating a task.
- return new TaskEditPlace(null);
+ return TaskEditPlace.getTaskCreatePlace();
}
}
@@ -57,10 +58,11 @@
* task ID.
*
* @param taskId the ID of the task to edit
+ * @param task the task to edit, or null if not available
* @return the place
*/
- public static TaskEditPlace createTaskEditPlace(Long taskId) {
- return new TaskEditPlace(taskId);
+ public static TaskEditPlace createTaskEditPlace(Long taskId, TaskProxy task) {
+ return new TaskEditPlace(taskId, task);
}
/**
@@ -71,20 +73,32 @@
*/
public static TaskEditPlace getTaskCreatePlace() {
if (singleton == null) {
- singleton = new TaskEditPlace(null);
+ singleton = new TaskEditPlace(null, null);
}
return singleton;
}
+ private final TaskProxy task;
private final Long taskId;
/**
* Construct a new {@link TaskEditPlace} for the specified task id.
*
* @param taskId the ID of the task to edit
+ * @param task the task to edit, or null if not available
*/
- private TaskEditPlace(Long taskId) {
+ private TaskEditPlace(Long taskId, TaskProxy task) {
this.taskId = taskId;
+ this.task = task;
+ }
+
+ /**
+ * Get the task to edit.
+ *
+ * @return the task to edit, or null if not available
+ */
+ public TaskProxy getTask() {
+ return task;
}
/**