com.mailme.client.activity.MailActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.mailme.client.activity.MailActivity.java

Source

/*******************************************************************************
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * 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.mailme.client.activity;

import java.util.List;

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.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.mailme.client.ClientFactory;
import com.mailme.client.beans.objects.IPicObject;
import com.mailme.client.beans.objects.users.AutisteUserData;
import com.mailme.client.beans.objects.util.Mail;
import com.mailme.client.place.LoginPlace;
import com.mailme.client.place.MailPlace;
import com.mailme.client.services.MailMeServices;
import com.mailme.client.ui.MailView;

/**
 * Activities are started and stopped by an ActivityManager associated with a container Widget.
 */
public class MailActivity extends AbstractActivity implements MailView.Presenter {
    /**
     * Used to obtain views, eventBus, placeController.
     * Alternatively, could be injected via GIN.
     */
    private ClientFactory clientFactory;

    public MailActivity(MailPlace place, ClientFactory clientFactory) {
        this.clientFactory = clientFactory;
    }

    @Override
    public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
        MailView view = clientFactory.getMailView();
        view.setPresenter(this);
        containerWidget.setWidget(view.asWidget());
    }

    @Override
    public String mayStop() {
        return null;
    }

    /**
     * @see MailView.Presenter#goTo(Place)
     */
    public void goTo(Place place) {
        clientFactory.getPlaceController().goTo(place);
    }

    @Override
    public void getUserData() {
        MailMeServices.Util.getInstance().getUserData(new AsyncCallback<AutisteUserData>() {
            @Override
            public void onSuccess(AutisteUserData result) {
                clientFactory.getMailView().loadWords(result.getWords());
                clientFactory.getMailView().loadFriends(result.getFriends());
            }

            @Override
            public void onFailure(Throwable caught) {
            }
        });
    }

    @Override
    public void getUserMails() {
        MailMeServices.Util.getInstance().getUserMails(new AsyncCallback<List<Mail>>() {
            @Override
            public void onSuccess(List<Mail> result) {
                clientFactory.getMailView().loadMails(result);
            }

            @Override
            public void onFailure(Throwable caught) {
            }
        });
    }

    @Override
    public void sendMail(Long receiver, IPicObject[] items) {
        MailMeServices.Util.getInstance().userSendMail(receiver, items, new AsyncCallback<Void>() {
            @Override
            public void onSuccess(Void result) {
                clientFactory.getMailView().mailSended();
            }

            @Override
            public void onFailure(Throwable caught) {
            }
        });
    }

    @Override
    public void isLoggedIn() {
        MailMeServices.Util.getInstance().isLoggedIn(new AsyncCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean result) {
                if (!result)
                    goTo(new LoginPlace(""));
                else
                    clientFactory.getMailView().connected();
            }

            @Override
            public void onFailure(Throwable caught) {
            }
        });
    }

}