Java tutorial
/* * Copyright 2012 Nabeel Mukhtar * * 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.appspot.socialinquirer.client.activity; import java.util.ArrayList; import com.appspot.socialinquirer.client.ClientFactory; import com.appspot.socialinquirer.client.constant.EverScribeConstants; import com.appspot.socialinquirer.client.place.QuestionPlace; import com.appspot.socialinquirer.client.util.UiUtils; import com.appspot.socialinquirer.client.view.QuestionView; import com.appspot.socialinquirer.shared.ProgrammingLanguage; import com.appspot.socialinquirer.shared.TextLanguage; import com.appspot.socialinquirer.shared.dto.Classification; import com.appspot.socialinquirer.shared.dto.Classifier; import com.appspot.socialinquirer.shared.dto.ContentAnalysis; import com.appspot.socialinquirer.shared.dto.Question; import com.appspot.socialinquirer.shared.dto.Quiz; import com.appspot.socialinquirer.shared.dto.User; import com.google.gwt.activity.shared.AbstractActivity; import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.RunAsyncCallback; 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; /** * The Class QuestionActivity. */ public class QuestionActivity extends AbstractActivity implements QuestionView.Presenter { // Used to obtain views, eventBus, placeController // Alternatively, could be injected via GIN /** The client factory. */ private ClientFactory clientFactory; // Name that will be appended to "Hello," /** The name. */ private String name; /** The user. */ private User user; /** The dashboard view. */ private QuestionView dashboardView; /** * Instantiates a new question activity. * * @param place the place * @param clientFactory the client factory */ public QuestionActivity(QuestionPlace place, ClientFactory clientFactory) { this.name = place.getName(); this.user = clientFactory.getUser(); this.clientFactory = clientFactory; } /* * (non-Javadoc) * * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget, * com.google.gwt.event.shared.EventBus) */ @Override public void start(final AcceptsOneWidget containerWidget, final EventBus eventBus) { GWT.runAsync(new RunAsyncCallback() { public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), clientFactory.getConstants().errorCodeDownloadFailed()); } public void onSuccess() { String questionId = getQuestionId(name); dashboardView = clientFactory.getQuestionView(); final EverScribeConstants constants = clientFactory.getConstants(); dashboardView.setName(name); dashboardView.setPresenter(QuestionActivity.this); containerWidget.setWidget(dashboardView.asWidget()); clientFactory.getContentService().getQuestion(questionId, new AsyncCallback<Question>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(constants, caught.getLocalizedMessage()); } @Override public void onSuccess(Question result) { dashboardView.setQuestion(result); } }); clientFactory.getContentService().getQuizzes(new AsyncCallback<ArrayList<Quiz>>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(ArrayList<Quiz> result) { dashboardView.setQuizzes(result); } }); clientFactory.getContentService().getBuiltinClassifiers(new AsyncCallback<ArrayList<Classifier>>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(ArrayList<Classifier> result) { dashboardView.setClassifiers(result); } }); } private String getQuestionId(String name) { int index = name.indexOf(':'); return (index > 0) ? name.substring(index + 1) : null; } }); } /* * (non-Javadoc) * * @see com.appspot.linkedhub.client.view.CodeSettingsView.Presenter#goTo(com.google.gwt.place.shared.Place) */ public void goTo(Place place) { clientFactory.getPlaceController().goTo(place); } /* (non-Javadoc) * @see com.appspot.socialinquirer.client.view.QuestionView.Presenter#onAnalyzeClicked(com.appspot.socialinquirer.shared.dto.Question) */ @Override public void onAnalyzeClicked(Question question) { if (question == null) { UiUtils.showErrorDialog(clientFactory.getConstants(), "No question selected."); return; } clientFactory.getContentService().analyzeQuestion(question, new AsyncCallback<ContentAnalysis>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(ContentAnalysis result) { UiUtils.showContentAnalysisDialog(result, clientFactory.getConstants()); } }); } /* (non-Javadoc) * @see com.appspot.socialinquirer.client.view.QuestionView.Presenter#onFollowClicked(com.appspot.socialinquirer.shared.dto.Question) */ @Override public void onFollowClicked(Question question) { if (question == null) { UiUtils.showErrorDialog(clientFactory.getConstants(), "No question selected."); return; } // TODO Auto-generated method stub } /* (non-Javadoc) * @see com.appspot.socialinquirer.client.view.QuestionView.Presenter#onSummarizeClicked(com.appspot.socialinquirer.shared.dto.Question) */ @Override public void onSummarizeClicked(Question question) { if (question == null) { UiUtils.showErrorDialog(clientFactory.getConstants(), "No question selected."); return; } clientFactory.getContentService().summarizeQuestion(question, 10, new AsyncCallback<String>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(String result) { UiUtils.showQuestionSummaryDialog(result, clientFactory.getConstants()); } }); } /* (non-Javadoc) * @see com.appspot.socialinquirer.client.view.QuestionView.Presenter#onSuggestAnswerClicked(com.appspot.socialinquirer.shared.dto.Question) */ @Override public void onSuggestAnswerClicked(Question question) { if (question == null) { UiUtils.showErrorDialog(clientFactory.getConstants(), "No question selected."); return; } clientFactory.getContentService().suggestAnswer(question, new AsyncCallback<String>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(String result) { UiUtils.showQuestionAnswerDialog(result, clientFactory.getConstants()); } }); } /* (non-Javadoc) * @see com.appspot.socialinquirer.client.view.QuestionView.Presenter#onAddToQuizClicked(com.appspot.socialinquirer.shared.dto.Question, com.appspot.socialinquirer.shared.dto.Quiz) */ @Override public void onAddToQuizClicked(Question question, Quiz quiz) { if (question == null) { UiUtils.showErrorDialog(clientFactory.getConstants(), "No question selected."); return; } clientFactory.getContentService().addQuestionToQuiz(question, quiz, new AsyncCallback<Void>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(Void result) { } }); } /* (non-Javadoc) * @see com.appspot.socialinquirer.client.view.QuestionView.Presenter#onTranslateClicked(com.appspot.socialinquirer.shared.dto.Question, com.appspot.socialinquirer.shared.TextLanguage) */ @Override public void onTranslateClicked(Question question, TextLanguage language) { if (question == null) { UiUtils.showErrorDialog(clientFactory.getConstants(), "No question selected."); return; } clientFactory.getContentService().translateQuestion(question, language, new AsyncCallback<String>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(String result) { UiUtils.showTranslationDialog(result, clientFactory.getConstants()); } }); } /* (non-Javadoc) * @see com.appspot.socialinquirer.client.view.QuestionView.Presenter#onSpellCheckClicked(com.appspot.socialinquirer.shared.dto.Question) */ @Override public void onSpellCheckClicked(Question question) { if (question == null) { UiUtils.showErrorDialog(clientFactory.getConstants(), "No question selected."); return; } clientFactory.getContentService().spellCheck(question, "en", new AsyncCallback<String>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(String result) { UiUtils.showSpellCheckDialog(result, clientFactory.getConstants()); } }); } /* (non-Javadoc) * @see com.appspot.socialinquirer.client.view.QuestionView.Presenter#onSpeakClicked(com.appspot.socialinquirer.shared.dto.Question) */ @Override public void onSpeakClicked(Question question) { if (question == null) { UiUtils.showErrorDialog(clientFactory.getConstants(), "No question selected."); return; } clientFactory.getContentService().textToSpeech(question, TextLanguage.ENGLISH, new AsyncCallback<ArrayList<String>>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(ArrayList<String> result) { UiUtils.showSpeakDialog(result, clientFactory.getConstants()); } }); } /* (non-Javadoc) * @see com.appspot.socialinquirer.client.view.QuestionView.Presenter#onRunCodeClicked(com.appspot.socialinquirer.shared.dto.Question) */ @Override public void onRunCodeClicked(Question question) { if (question == null) { UiUtils.showErrorDialog(clientFactory.getConstants(), "No question selected."); return; } clientFactory.getContentService().evaluate(question, ProgrammingLanguage.JAVA, new AsyncCallback<String>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(String result) { UiUtils.showRunCodeDialog(result, clientFactory.getConstants()); } }); } /* (non-Javadoc) * @see com.appspot.socialinquirer.client.view.QuestionView.Presenter#onClassifyClicked(com.appspot.socialinquirer.shared.dto.Question, com.appspot.socialinquirer.shared.dto.Classifier) */ @Override public void onClassifyClicked(Question question, Classifier classifier) { if (question == null) { UiUtils.showErrorDialog(clientFactory.getConstants(), "No question selected."); return; } clientFactory.getContentService().classifyQuestion(question, classifier.getName(), classifier.getAuthor(), new AsyncCallback<ArrayList<Classification>>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(ArrayList<Classification> result) { UiUtils.showClassificationDialog(result, clientFactory.getConstants()); } }); } /* (non-Javadoc) * @see com.appspot.socialinquirer.client.view.QuestionView.Presenter#onFollowUserClicked(java.lang.String) */ @Override public void onFollowUserClicked(String userName) { ArrayList<User> friends = new ArrayList<User>(); User user = new User(); friends.add(user); int index = userName.indexOf(':'); if (index > 0) { user.setStackExchangeId(userName.substring(index + 1)); clientFactory.getUserService().addUserFriends(friends, new AsyncCallback<Void>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(Void result) { } }); } } }