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.HomePlace; import com.appspot.socialinquirer.client.util.UiUtils; import com.appspot.socialinquirer.client.view.HomeView; 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.Message; import com.appspot.socialinquirer.shared.dto.Question; import com.appspot.socialinquirer.shared.dto.Quiz; import com.google.gwt.activity.shared.AbstractActivity; import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.JsArray; 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.regexp.shared.MatchResult; import com.google.gwt.regexp.shared.RegExp; import com.google.gwt.search.client.Result; import com.google.gwt.search.client.SearchControl; import com.google.gwt.search.client.SearchControlOptions; import com.google.gwt.search.client.SearchResultsHandler; import com.google.gwt.search.client.WebResult; import com.google.gwt.search.client.WebSearch; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.ui.AcceptsOneWidget; /** * The Class HomeActivity. */ public class HomeActivity extends AbstractActivity implements HomeView.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 site pattern. */ RegExp sitePattern = RegExp.compile("http://(.*)/questions/(\\d+)/?.*"); /** The home view. */ private HomeView homeView; /** * Instantiates a new home activity. * * @param place the place * @param clientFactory the client factory */ public HomeActivity(HomePlace place, ClientFactory clientFactory) { this.name = place.getName(); 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() { homeView = clientFactory.getHomeView(); homeView.setPresenter(HomeActivity.this); containerWidget.setWidget(homeView.asWidget()); 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) { homeView.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) { homeView.setClassifiers(result); } }); clientFactory.getUserService().getUserMessages(new AsyncCallback<ArrayList<Message>>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(ArrayList<Message> result) { homeView.setMessages(result); } }); if (clientFactory.getUser() != null) { clientFactory.getUserService().getUserMessages(new AsyncCallback<ArrayList<Message>>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(ArrayList<Message> result) { homeView.setMessages(result); } }); } } }); } /* (non-Javadoc) * @see com.appspot.linkedhub.client.view.TasksView.Presenter#onQuestionsSearchPerformed(java.lang.String) */ @Override public void onQuestionsSearchPerformed(final String text) { GWT.runAsync(new RunAsyncCallback() { public void onFailure(Throwable caught) { Window.alert("Code download failed"); } public void onSuccess() { SearchControlOptions options = new SearchControlOptions(); WebSearch ws = new WebSearch(); ws.setSiteRestriction("015203706368483719477:w4zebhpqxyu"); options.add(ws); SearchControl searchControl = new SearchControl(options); searchControl.addSearchResultsHandler(new SearchResultsHandler() { @Override public void onSearchResults(SearchResultsEvent event) { JsArray<? extends Result> results = event.getResults(); ArrayList<Question> questions = new ArrayList<Question>(); for (int i = 0; i < results.length(); i++) { Result result = results.get(i); if (result instanceof WebResult) { Question question = createQuestion((WebResult) result); question.setKey(getQuestionId(((WebResult) result).getUnescapedUrl())); if (question.getKey() != null) { questions.add(question); } } } homeView.setQuestions(questions); } }); searchControl.execute(text); } }); } /* * (non-Javadoc) * * @see com.appspot.linkedhub.client.view.TaskView.Presenter#goTo(com.google.gwt.place.shared.Place) */ public void goTo(Place place) { clientFactory.getPlaceController().goTo(place); } /** * Creates the question. * * @param result the result * @return the question */ private Question createQuestion(WebResult result) { Question task = new Question(); task.setTitle(result.getTitle()); task.setContent(result.getContent()); return task; } /** * Gets the question id. * * @param url the url * @return the question id */ protected String getQuestionId(String url) { MatchResult matcher = sitePattern.exec(url); if (sitePattern.test(url)) { return matcher.getGroup(2); } return null; } /* (non-Javadoc) * @see com.appspot.socialinquirer.client.view.HomeView.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.HomeView.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.HomeView.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.HomeView.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.HomeView.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.HomeView.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.HomeView.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.HomeView.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.HomeView.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.HomeView.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.HomeView.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()); } }); } }