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.HistoryToken; import com.appspot.socialinquirer.client.place.QuestionsPlace; import com.appspot.socialinquirer.client.util.UiUtils; import com.appspot.socialinquirer.client.view.QuestionsView; 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 QuestionsActivity. */ public class QuestionsActivity extends AbstractActivity implements QuestionsView.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 QuestionsView dashboardView; /** * Instantiates a new questions activity. * * @param place the place * @param clientFactory the client factory */ public QuestionsActivity(QuestionsPlace 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() { dashboardView = clientFactory.getQuestionsView(); dashboardView.setName(name); // dashboardView.setBlogSettings(user.getBlogSettings()); dashboardView.setPresenter(QuestionsActivity.this); containerWidget.setWidget(dashboardView.asWidget()); if (name.equals(HistoryToken.Questions_Answered.token())) { clientFactory.getUserService().getAnsweredQuestions(new AsyncCallback<ArrayList<Question>>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(ArrayList<Question> result) { dashboardView.setQuestions(result); } }); } else if (name.equals(HistoryToken.Questions_Asked.token())) { clientFactory.getUserService().getAskedQuestions(new AsyncCallback<ArrayList<Question>>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(ArrayList<Question> result) { dashboardView.setQuestions(result); } }); } else if (name.equals(HistoryToken.Questions_Following.token())) { clientFactory.getUserService().getFollowingQuestions(new AsyncCallback<ArrayList<Question>>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(ArrayList<Question> result) { dashboardView.setQuestions(result); } }); } else if (name.equals(HistoryToken.Questions_Recommended.token())) { clientFactory.getUserService() .getRecommendedQuestions(new AsyncCallback<ArrayList<Question>>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(ArrayList<Question> result) { dashboardView.setQuestions(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); } }); } }); } /* * (non-Javadoc) * * @see com.appspot.linkedhub.client.view.BlogSettingsView.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.QuestionsView.Presenter#showHelp(java.lang.String) */ @Override public void showHelp(String name) { UiUtils.showHelpDialog(clientFactory, name); } /* (non-Javadoc) * @see com.appspot.socialinquirer.client.view.QuestionsView.Presenter#onViewClicked(com.appspot.socialinquirer.shared.dto.Question) */ @Override public void onViewClicked(Question question) { if (question == null) { UiUtils.showErrorDialog(clientFactory.getConstants(), "No question selected."); return; } HistoryToken.Question.fire(question.getKey()); } /* (non-Javadoc) * @see com.appspot.socialinquirer.client.view.QuestionsView.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.QuestionsView.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.QuestionsView.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.QuestionsView.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.QuestionsView.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.QuestionsView.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.QuestionsView.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.QuestionsView.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.QuestionsView.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.QuestionsView.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()); } }); } }