Ejemplo n.º 1
0
  public void onModuleLoad() {

    History.addValueChangeHandler(
        new ValueChangeHandler<String>() {
          @Override
          public void onValueChange(ValueChangeEvent<String> event) {
            final String token = event.getValue();
            handleHistoryChange(token);
          }
        });

    taskListsPage = new Page("Task Lists");
    tasksPage = new Page("Tasks");

    _taskService.getLogoutUrl(
        "/",
        new AsyncCallback<String>() {
          @Override
          public void onFailure(Throwable caught) {
            // do nothing
          }

          @Override
          public void onSuccess(String result) {
            RootPanel navigation = RootPanel.get("navigation");
            Anchor logoutLink = new Anchor("Logout", result);
            navigation.add(logoutLink);
          }
        });

    taskListsPage.addCreateItemHandler(
        new CreateItemHandler() {
          @Override
          public void onItemCreated(CreateItemEvent event) {
            AsyncCallback<TaskList> callback =
                new AsyncCallback<TaskList>() {
                  @Override
                  public void onFailure(final Throwable caught) {
                    Window.alert("creating task list failed: " + caught.getLocalizedMessage());
                  }

                  @Override
                  public void onSuccess(final TaskList taskList) {
                    final PageItem item =
                        new PageItem(
                            taskList.getName(),
                            HistoryTokens.LIST + HistoryTokens.SEP + taskList.getId());
                    item.setDeleteCommand(
                        new Command() {
                          @Override
                          public void execute() {
                            _taskService.deleteTaskList(
                                taskList.getId(),
                                new AsyncCallback<Void>() {
                                  @Override
                                  public void onFailure(Throwable caught) {
                                    // TODO inform user
                                  }

                                  @Override
                                  public void onSuccess(Void result) {
                                    taskListsPage.remove(item);
                                  }
                                });
                          }
                        });
                    taskListsPage.add(item);
                  }
                };
            _taskService.createTaskList(event.getItemName(), callback);
          }
        });

    tasksPage.addCreateItemHandler(
        new CreateItemHandler() {
          @Override
          public void onItemCreated(CreateItemEvent event) {
            AsyncCallback<Task> callback =
                new AsyncCallback<Task>() {
                  @Override
                  public void onFailure(final Throwable caught) {
                    Window.alert("creating task failed: " + caught.getLocalizedMessage());
                  }

                  @Override
                  public void onSuccess(final Task task) {
                    final PageItem item =
                        new PageItem(
                            task.getName(), HistoryTokens.TASK + HistoryTokens.SEP + task.getId());
                    item.setDeleteCommand(
                        new Command() {
                          @Override
                          public void execute() {
                            _taskService.deleteTask(
                                task.getId(),
                                new AsyncCallback<Void>() {
                                  @Override
                                  public void onFailure(Throwable caught) {
                                    GWT.log("remove failed");
                                    // TODO inform user
                                  }

                                  @Override
                                  public void onSuccess(Void result) {
                                    tasksPage.remove(item);
                                  }
                                });
                          }
                        });
                    tasksPage.add(item);
                  }
                };
            _taskService.createTask(event.getItemName(), _currentTaskListKey, callback);
          }
        });

    mainPanel.add(new Label("Loading..."));
    mainPanel.add(taskListsPage);
    mainPanel.add(tasksPage);
    RootPanel.get("application").add(mainPanel);
    mainPanel.showWidget(0);
    handleInitialHistoryToken();
  }
Ejemplo n.º 2
0
  private void handleHistoryChange(final String token) {
    final String[] parts = token.split("/", 2);
    if ((token == null) || (token.length() == 0)) {
      History.newItem(HistoryTokens.ALL_LISTS);
    } else if (token.equals(HistoryTokens.ALL_LISTS)) {
      taskListsPage.clear();
      _taskService.getTaskLists(
          new AsyncCallback<List<TaskList>>() {
            @Override
            public void onFailure(Throwable caught) {
              // TODO Auto-generated method stub
            }

            @Override
            public void onSuccess(List<TaskList> result) {
              for (final TaskList taskList : result) {
                final PageItem item =
                    new PageItem(
                        taskList.getName(),
                        HistoryTokens.LIST + HistoryTokens.SEP + taskList.getId());
                item.setDeleteCommand(
                    new Command() {
                      @Override
                      public void execute() {
                        _taskService.deleteTaskList(
                            taskList.getId(),
                            new AsyncCallback<Void>() {
                              @Override
                              public void onFailure(Throwable caught) {
                                // TODO inform user
                              }

                              @Override
                              public void onSuccess(Void result) {
                                taskListsPage.remove(item);
                              }
                            });
                      }
                    });
                taskListsPage.add(item);
              }
              mainPanel.showWidget(1);
            }
          });
    } else if (parts[0].equals(HistoryTokens.LIST)) {
      _taskService.getTaskList(
          parts[1],
          new AsyncCallback<ListOfTasks>() {
            @Override
            public void onFailure(Throwable caught) {
              Window.alert(caught.getLocalizedMessage());
            }

            @Override
            public void onSuccess(ListOfTasks result) {
              _currentTaskListKey = parts[1];
              tasksPage.clear();
              for (final Task task : result.getTasks()) {
                final PageItem item =
                    new PageItem(
                        task.getName(), HistoryTokens.TASK + HistoryTokens.SEP + task.getId());
                item.setDeleteCommand(
                    new Command() {
                      @Override
                      public void execute() {
                        _taskService.deleteTask(
                            task.getId(),
                            new AsyncCallback<Void>() {
                              @Override
                              public void onFailure(Throwable caught) {
                                // TODO inform user
                              }

                              @Override
                              public void onSuccess(Void result) {
                                tasksPage.remove(item);
                              }
                            });
                      }
                    });
                final Image icon = new Image();
                if (task.getCompleted()) {
                  icon.setResource(IconBundle.INSTANCE.checkMark());
                  item.addStyleName("complete");
                } else {
                  icon.setResource(IconBundle.INSTANCE.checkBox());
                  item.removeStyleName("complete");
                }
                icon.addClickHandler(
                    new ClickHandler() {
                      @Override
                      public void onClick(ClickEvent event) {
                        _taskService.switchComplete(
                            task.getId(),
                            new AsyncCallback<Task>() {
                              @Override
                              public void onFailure(Throwable caught) {
                                // TODO inform user
                              }

                              @Override
                              public void onSuccess(Task result) {
                                if (result.getCompleted()) {
                                  icon.setResource(IconBundle.INSTANCE.checkMark());
                                  item.addStyleName("complete");
                                } else {
                                  icon.setResource(IconBundle.INSTANCE.checkBox());
                                  item.removeStyleName("complete");
                                }
                              }
                            });
                      }
                    });
                item.addAction(icon);
                tasksPage.add(item);
              }
              tasksPage.setTitle(result.getListName());
              mainPanel.showWidget(2);
            }
          });
    }
  }