Java tutorial
/* * 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 org.mklab.taskit.client.activity; import org.mklab.taskit.client.ClientFactory; import org.mklab.taskit.client.LocalDatabase; import org.mklab.taskit.client.event.GlobalEventListener; import org.mklab.taskit.client.event.TimeoutRecoverable; import org.mklab.taskit.client.event.TimeoutRecoveryFailureException; import org.mklab.taskit.client.place.Login; import org.mklab.taskit.client.ui.HelpCallDisplayable; import org.mklab.taskit.client.ui.PageLayout; import org.mklab.taskit.client.ui.TaskitView; import org.mklab.taskit.shared.CheckMapProxy; import org.mklab.taskit.shared.HelpCallProxy; import org.mklab.taskit.shared.UserProxy; import org.mklab.taskit.shared.UserType; import org.mklab.taskit.shared.event.CheckMapEvent; import org.mklab.taskit.shared.event.HelpCallEvent; import java.util.List; import com.google.gwt.activity.shared.AbstractActivity; import com.google.gwt.event.shared.EventBus; import com.google.gwt.user.client.Cookies; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.AcceptsOneWidget; import com.google.gwt.user.client.ui.Widget; import com.google.web.bindery.requestfactory.shared.Receiver; import com.google.web.bindery.requestfactory.shared.ServerFailure; import de.novanic.eventservice.client.event.Event; import de.novanic.eventservice.client.event.listener.RemoteEventListener; /** * ????????? * <p> * ?????????? * * @author Yuhi Ishikura * @version $Revision$, Jan 25, 2011 */ public abstract class TaskitActivity extends AbstractActivity implements PageLayout.Presenter, RemoteEventListener, TimeoutRecoverable { private ClientFactory clientFactory; private AcceptsOneWidget container; private UserProxy loginUser; private TaskitView view; private PageLayout layout; /** * {@link TaskitActivity}??? * * @param clientFactory */ public TaskitActivity(ClientFactory clientFactory) { if (clientFactory == null) throw new NullPointerException(); this.clientFactory = clientFactory; this.layout = clientFactory.getPageLayout(); this.layout.setPresenter(this); } /** * {@inheritDoc} */ @Override public final void start(AcceptsOneWidget panel, @SuppressWarnings("unused") EventBus eventBus) { this.container = panel; updateLoginUserInfoAsync(); } /** * ???? * * @return */ protected final UserProxy getLoginUser() { return this.loginUser; } /** * ??????????????? * <p> * ????????????????? */ private void updateLoginUserInfoAsync() { getClientFactory().getLocalDatabase().getCacheOrExecute(LocalDatabase.LOGIN_USER, new Receiver<UserProxy>() { @SuppressWarnings("synthetic-access") @Override public void onSuccess(UserProxy user) { if (user == null) { logout(); return; } initViewWith(user); } /** * {@inheritDoc} */ @Override public void onFailure(ServerFailure error) { showErrorDialog(error.getMessage()); logout(); } }); } /** * ??????? * * @param user */ private void initViewWith(UserProxy user) { if (user == null) throw new NullPointerException(); this.loginUser = user; // ????????????? final GlobalEventListener globalEventListener = this.clientFactory.getGlobalEventListener(); if (globalEventListener.isListening() == false) { globalEventListener.listenWith(user); } getClientFactory().getGlobalEventListener().setImplementation(this); this.view = createTaskitView(this.clientFactory); updateHelpCallDisplayAsync(); final Widget page = this.layout.layout(this.view, user); this.container.setWidget(page); onViewShown(); } /** * ????????? * <p> * {@link LocalDatabase}??????????????????????? */ private void updateHelpCallDisplayAsync() { if (this.view instanceof HelpCallDisplayable) { final HelpCallDisplayable helpCallDisplay = (HelpCallDisplayable) this.view; final boolean isDisplayable = getLoginUser().getType() != UserType.STUDENT; helpCallDisplay.setHelpCallDisplayEnabled(isDisplayable); if (isDisplayable) { getClientFactory().getLocalDatabase().getCacheOrExecute(LocalDatabase.CALL_LIST, new Receiver<List<HelpCallProxy>>() { @Override public void onSuccess(List<HelpCallProxy> response) { helpCallDisplay.showHelpCallCount(response.size()); } }); } } } /** * ???? * * @param clientFactory * @return */ protected abstract TaskitView createTaskitView(@SuppressWarnings("hiding") ClientFactory clientFactory); /** * ??????????? * <p> * ???????????????????? <br> * ??????????? */ protected void onViewShown() { // do nothing } /** * ?????? * * @return ??????????null */ protected final TaskitView getTaskitView() { return this.view; } /** * {@inheritDoc} */ @Override public final void logout() { if (getClientFactory().getGlobalEventListener().isListening()) { getClientFactory().getGlobalEventListener().unlisten(); } /* * ????????Activity#onStop()???????????????? * ?? * * ?????? */ getClientFactory().getPlaceController().goTo(Login.INSTANCE); try { getClientFactory().getRequestFactory().accountRequest().logout().fire(); } catch (Throwable e) { showErrorDialog(e); } finally { Cookies.removeCookie(LoginActivity.COOKIE_AUTO_LOGIN_KEY); } this.clientFactory.initialize(); } /** * {@link ClientFactory}???? * * @return */ protected final ClientFactory getClientFactory() { return this.clientFactory; } /** * ??? * * @param errorMessage */ protected final void showErrorDialog(String errorMessage) { if (this.view == null) { Window.alert(errorMessage); } else { this.view.showErrorDialog(errorMessage); } } /** * {@inheritDoc} */ @Override public void apply(Event evt) { if (evt instanceof HelpCallEvent) { if (this.view instanceof HelpCallDisplayable) { ((HelpCallDisplayable) this.view).setHelpCallDisplayEnabled(true); ((HelpCallDisplayable) this.view).showHelpCallCount(((HelpCallEvent) evt).getHelpCallCount()); } updateHelpCallListAsync(); } else if (evt instanceof CheckMapEvent) { updateChecksAsync(); } } private void updateHelpCallListAsync() { getClientFactory().getLocalDatabase().execute(LocalDatabase.CALL_LIST, new Receiver<List<HelpCallProxy>>() { @Override public void onSuccess(List<HelpCallProxy> response) { onHelpCallListChanged(response); } }); } private void updateChecksAsync() { getClientFactory().getLocalDatabase().execute(LocalDatabase.CHECKS, new Receiver<List<CheckMapProxy>>() { @Override public void onSuccess(List<CheckMapProxy> response) { onCheckMapChanged(response); } }); } /** * {@inheritDoc} */ @Override public void recoverFromTimeout() throws TimeoutRecoveryFailureException { final UserType loginUserType = getLoginUser().getType(); if (loginUserType == UserType.TA || loginUserType == UserType.TEACHER) { updateHelpCallListAsync(); updateChecksAsync(); } } /** * ?????????? * * @param helpCalls ? */ protected void onHelpCallListChanged(List<HelpCallProxy> helpCalls) { if (this.view instanceof HelpCallDisplayable) { ((HelpCallDisplayable) this.view).setHelpCallDisplayEnabled(true); ((HelpCallDisplayable) this.view).showHelpCallCount(helpCalls.size()); } } /** * ?????????????? * * @param checks TA??? */ protected void onCheckMapChanged(@SuppressWarnings("unused") List<CheckMapProxy> checks) { // do nothing } /** * ??? * * @param e */ protected final void showErrorDialog(Throwable e) { showErrorDialog(e.toString()); } /** * ??? * * @param message */ protected final void showInformationDialog(String message) { if (this.view == null) { Window.alert(message); } else { this.view.showInformationDialog(message); } } /** * ??? * * @param message */ protected final void showErrorMessage(String message) { if (this.view == null) showErrorDialog(message); this.view.showErrorMessage(message); } /** * ??? * * @param message */ protected final void showInformationMessage(String message) { if (this.view == null) showInformationDialog(message); this.view.showInformationMessage(message); } }