/* Copyright (c) 2009 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 License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.appengine.demos.sticky.client.model;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.google.appengine.demos.sticky.client.model.Service.CreateObjectResult;
import com.google.appengine.demos.sticky.client.model.Service.UserInfoResult;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
/**
* Encapsulates the entire application data controller for the application. The
* model controls all RPC to the server and is responsible for keeping client
* side copies of data synchronized with the server.
*
*/
public class Model {
/**
* An observer interface to deliver all data change events.
*/
public interface DataObserver {
/**
* Called when a new {@link Note} is created.
*
* @param note
* the note that was created
*/
void onNoteCreated(Note note);
/**
* Called when a new {@link Surface} is created.
*
* @param surface
* the surface that was created
*/
void onSurfaceCreated(Surface surface);
/**
* Called when an initial list of {@link Note}s is returned from the server.
*
* @param notes
* all the {@link Note}s on the currently selected {@link Surface}.
*/
void onSurfaceNotesReceived(Note[] notes);
/**
* Called when the selected {@link Surface} changes.
*
* @param nowSelected
* the surface that is now selected
* @param wasSelected
* the surface that was previously selected
*/
void onSurfaceSelected(Surface nowSelected, Surface wasSelected);
/**
* Called when the initial list of {@link Surface}s is returned from the
* server.
*
* @param surfaces
* all the surfaces where the current author is a member
*/
void onSurfacesReceived(Surface[] surfaces);
}
/**
* An observer interface used to get callbacks during the initial load of the
* {@link Model}.
*/
public interface LoadObserver {
/**
* Invoked when the {@link Model} loads successfully.
*
* @param model
* the newly loaded model
*/
void onModelLoaded(Model model);
/**
* Invoked when the model fails to load.
*/
void onModelLoadFailed();
}
/**
* An observer interface that provides callbacks useful for giving the user
* feedback about calls to the server.
*/
public interface StatusObserver {
/**
* Invoked when RPC calls begin to succeed again after a failure was
* reported.
*/
void onServerCameBack();
/**
* Invoked when RPC calls to the server are failing.
*/
void onServerWentAway();
/**
* Invoked when current task has finished. This is often used to stop
* displaying status Ui that was made visible in the
* {@link StatusObserver#onTaskStarted(String)} callback.
*/
void onTaskFinished();
/**
* Invoked when a task that requires user feedback starts.
*
* @param description
* a description of the task that is starting
*/
void onTaskStarted(String description);
}
/**
* A simple callback for reporting success to the caller asynchronously. This
* is used for call sites where the caller needs to know the result of an RPC.
*/
public static interface SuccessCallback {
void onResponse(boolean success);
}
/**
* A task that manages the call to the server to add an author to a surface.
*/
private class AddAuthorToSurfaceTask extends Task implements
AsyncCallback<Service.AddAuthorToSurfaceResult> {
private final Surface surface;
private final String email;
private final SuccessCallback callback;
public AddAuthorToSurfaceTask(Surface surface, String email,
SuccessCallback callback) {
this.surface = surface;
this.email = email;
this.callback = callback;
}
public void onFailure(Throwable caught) {
getQueue().taskFailed(this,
caught instanceof Service.AccessDeniedException);
}
public void onSuccess(Service.AddAuthorToSurfaceResult result) {
final boolean success = result != null;
callback.onResponse(success);
if (success) {
surface.update(Model.this, result.getAuthorName(), result
.getUpdatedAt());
}
getQueue().taskSucceeded(this);
}
@Override
void execute() {
api.addAuthorToSurface(surface.getKey(), email, this);
}
}
/**
* A task that manages the call to the server to create a new note.
*/
private class CreateNoteTask extends Task implements
AsyncCallback<CreateObjectResult> {
private final Note note;
private final Surface surface;
public CreateNoteTask(Surface surface, Note note) {
this.note = note;
this.surface = surface;
}
@Override
public void execute() {
api.createNote(surface.getKey(), note.getX(), note.getY(), note
.getWidth(), note.getHeight(), this);
}
public void onFailure(Throwable caught) {
getQueue().taskFailed(this,
caught instanceof Service.AccessDeniedException);
}
public void onSuccess(CreateObjectResult result) {
if (surface == selectedSurface) {
noteLoader.cacheNote(result.getKey(), note);
}
note.update(result.getKey(), result.getUpdateTime());
getQueue().taskSucceeded(this);
}
}
/**
* A task to manages the call to the server to create a new surface.
*/
private class CreateSurfaceTask extends Task implements
AsyncCallback<Service.CreateObjectResult> {
private final Surface surface;
public CreateSurfaceTask(Surface surface) {
this.surface = surface;
}
public void execute() {
api.createSurface(surface.getTitle(), this);
}
public void onFailure(Throwable caught) {
getQueue().taskFailed(this,
caught instanceof Service.AccessDeniedException);
}
public void onSuccess(Service.CreateObjectResult result) {
surfaceLoader.cacheSurface(result.getKey(), surface);
surface.update(result.getKey(), result.getUpdateTime());
getQueue().taskSucceeded(this);
}
}
/**
* Encapsulates a linked list node that is used by {@link TaskQueue} to keep
* an ordered list of pending {@link Task}s.
*/
private static class Node {
private final Task task;
private Node next;
Node(Task task) {
this.task = task;
}
void execute(TaskQueue queue) {
task.execute(queue);
}
}
/**
* Encapsulates a task for writing data to the server. The tasks are managed
* by the {@link TaskQueue} and are auto-retried on failure.
*/
private abstract static class Task {
private TaskQueue queue;
abstract void execute();
void execute(TaskQueue queue) {
this.queue = queue;
execute();
}
TaskQueue getQueue() {
return queue;
}
}
/**
* Provides a mechanism to perform write tasks sequentially and retry tasks
* that fail.
*/
private class TaskQueue extends RetryTimer {
private Node head, tail;
public void post(Task task) {
final Node node = new Node(task);
if (isIdle()) {
head = tail = node;
executeHead();
} else {
enqueueTail(node);
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
appengine-java-sdk-1.9.24 (1334个子文件)
ABOUT 1KB
SignGuestbookServlet.class 3KB
GuestbookServlet.class 2KB
.classpath 489B
.classpath 489B
.classpath 489B
.classpath 489B
.classpath 489B
.classpath 489B
.classpath 489B
.classpath 489B
.classpath 489B
endpoints.cmd 729B
dev_appserver.cmd 368B
appcfg.cmd 286B
google_sql.cmd 196B
COPYING 9KB
COPYING 9KB
COPYING 9KB
COPYING 9KB
COPYING 9KB
COPYING 9KB
COPYING 9KB
COPYING 9KB
COPYING 9KB
COPYING 9KB
COPYING 9KB
COPYING 9KB
COPYING 6KB
stylesheet.css 11KB
stylesheet.css 11KB
Sticky.css 3KB
dev_javadoc.css 3KB
dev_javadoc.css 3KB
dev_javadoc.css 3KB
dev_javadoc.css 3KB
LabelMatrix.css 2KB
TaskList.css 1024B
TaskDetails.css 543B
ControlBar.css 273B
main.css 89B
appengine-web.dtd 5KB
queue.dtd 739B
datastore-indexes.dtd 709B
backends.dtd 370B
cron.dtd 233B
dos.dtd 145B
mandelbrot256.dzi 216B
titlebar.gif 10KB
titlebar.gif 10KB
titlebar.gif 10KB
titlebar.gif 10KB
titlebar.gif 10KB
titlebar.gif 10KB
background.gif 2KB
background.gif 2KB
background.gif 2KB
background.gif 2KB
background.gif 2KB
background.gif 2KB
header-surfaces-button-dn.gif 1KB
header-add-button-dn.gif 1KB
add-surface-button-dn.gif 1KB
titlebar_end.gif 849B
titlebar_end.gif 849B
titlebar_end.gif 849B
titlebar_end.gif 849B
titlebar_end.gif 849B
titlebar_end.gif 849B
header-surfaces-button-hv.gif 836B
header-surfaces-button-up.gif 834B
error.gif 799B
header-add-button-hv.gif 797B
header-add-button-up.gif 797B
add-surface-button-hv.gif 796B
add-surface-button-up.gif 794B
surface-list-add-up.gif 622B
surface-list-add-hv.gif 622B
button.gif 526B
header-add-author-button-up.gif 339B
header-add-author-button-hv.gif 339B
tab.gif 291B
tab.gif 291B
tab.gif 291B
tab.gif 291B
tab.gif 291B
tab.gif 291B
index-all.html 1.15MB
index-all.html 423KB
index-all.html 144KB
overview-tree.html 107KB
serialized-form.html 93KB
Queue.html 89KB
allclasses-frame.html 73KB
MemcacheService.html 73KB
AdminDatastoreService.html 71KB
ImagesService.html 65KB
allclasses-noframe.html 65KB
DatastoreService.html 60KB
constant-values.html 59KB
共 1334 条
- 1
- 2
- 3
- 4
- 5
- 6
- 14
资源评论
- liu5841299142017-10-29很好,找了很久了
- hao1220651752016-02-18很好,找了很久了
BrillianceRen
- 粉丝: 21
- 资源: 22
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功