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.util; import java.util.ArrayList; import java.util.List; import com.appspot.socialinquirer.client.ClientFactory; import com.appspot.socialinquirer.client.constant.EverScribeConstants; import com.appspot.socialinquirer.client.view.NamedBeanForm; import com.appspot.socialinquirer.client.view.QuizView; import com.appspot.socialinquirer.client.view.QuizViewImpl; import com.appspot.socialinquirer.client.view.TaskForm; import com.appspot.socialinquirer.shared.dto.Classification; import com.appspot.socialinquirer.shared.dto.ContentAnalysis; import com.appspot.socialinquirer.shared.dto.Message; import com.appspot.socialinquirer.shared.dto.Poll; import com.appspot.socialinquirer.shared.dto.Quiz; import com.appspot.socialinquirer.shared.dto.Task; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.user.client.Command; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.DialogBox; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.HasAlignment; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.VerticalPanel; /** * The Class UiUtils. */ public final class UiUtils { /** * Instantiates a new ui utils. */ private UiUtils() { } /** * Show settings saved dialog. * * @param constants the constants */ public static void showSettingsSavedDialog(EverScribeConstants constants) { final DialogBox dialogBox = new DialogBox(); dialogBox.setText(constants.titleDialogBoxOperationSuccessful()); dialogBox.setAnimationEnabled(true); VerticalPanel dialogVPanel = new VerticalPanel(); dialogVPanel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER); dialogVPanel.add(new Label(constants.settingsSaved())); final Button closeButton = new Button(constants.closeButton()); closeButton.getElement().setId("closeButton"); dialogVPanel.add(closeButton); dialogBox.setWidget(dialogVPanel); closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { dialogBox.hide(); } }); dialogBox.center(); } /** * Show error dialog. * * @param constants the constants * @param message the message */ public static void showErrorDialog(EverScribeConstants constants, String message) { final DialogBox dialogBox = new DialogBox(); dialogBox.setText(constants.titleDialogBoxError()); dialogBox.setAnimationEnabled(true); VerticalPanel dialogVPanel = new VerticalPanel(); dialogVPanel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER); dialogVPanel.add(new Label(message)); final Button closeButton = new Button(constants.closeButton()); closeButton.getElement().setId("closeButton"); dialogVPanel.add(closeButton); dialogBox.setWidget(dialogVPanel); closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { dialogBox.hide(); } }); dialogBox.center(); } /** * Show help dialog. * * @param clientFactory the client factory * @param name the name */ public static void showHelpDialog(ClientFactory clientFactory, String name) { final EverScribeConstants constants = clientFactory.getConstants(); clientFactory.getContentService().getHelpText(name, new AsyncCallback<String>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(constants, caught.getLocalizedMessage()); } @Override public void onSuccess(String result) { final DialogBox dialogBox = new DialogBox(); dialogBox.setText(constants.titleDialogBoxHelp()); dialogBox.setAnimationEnabled(true); dialogBox.setWidth("400px"); dialogBox.setHeight("200px"); VerticalPanel dialogVPanel = new VerticalPanel(); dialogVPanel.add(new HTML(result)); final Button closeButton = new Button(constants.closeButton()); // We can set the id of a widget by accessing its // Element closeButton.getElement().setId("closeButton"); dialogVPanel.add(closeButton); dialogBox.setWidget(dialogVPanel); closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { dialogBox.hide(); } }); dialogBox.center(); } }); } /** * Show task dialog. * * @param task the task * @param clientFactory the client factory */ public static void showTaskDialog(Task task, final ClientFactory clientFactory) { final DialogBox dialogBox = new DialogBox(); dialogBox.setText("Task"); dialogBox.setAnimationEnabled(true); VerticalPanel dialogVPanel = new VerticalPanel(); dialogVPanel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER); final TaskForm form = new TaskForm(); form.setTask(task); form.setCommand(new Command() { @Override public void execute() { clientFactory.getUserService().addUserTask(form.getTask(), new AsyncCallback<Void>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(Void result) { } }); } }); dialogVPanel.add(form); final Button closeButton = new Button(clientFactory.getConstants().closeButton()); closeButton.getElement().setId("closeButton"); dialogVPanel.add(closeButton); dialogBox.setWidget(dialogVPanel); closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { dialogBox.hide(); } }); dialogBox.center(); } /** * Show poll dialog. * * @param poll the poll * @param clientFactory the client factory */ public static void showPollDialog(Poll poll, final ClientFactory clientFactory) { final DialogBox dialogBox = new DialogBox(); dialogBox.setText("Poll"); dialogBox.setAnimationEnabled(true); VerticalPanel dialogVPanel = new VerticalPanel(); dialogVPanel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER); final NamedBeanForm form = new NamedBeanForm(); form.setBean(poll); form.setCommand(new Command() { @Override public void execute() { clientFactory.getContentService().addPoll((Poll) form.getBean(), new AsyncCallback<Void>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(Void result) { } }); } }); dialogVPanel.add(form); final Button closeButton = new Button(clientFactory.getConstants().closeButton()); closeButton.getElement().setId("closeButton"); dialogVPanel.add(closeButton); dialogBox.setWidget(dialogVPanel); closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { dialogBox.hide(); } }); dialogBox.center(); } /** * Show quiz dialog. * * @param quiz the quiz * @param clientFactory the client factory */ public static void showQuizDialog(Quiz quiz, final ClientFactory clientFactory) { final DialogBox dialogBox = new DialogBox(); dialogBox.setText("Quiz"); dialogBox.setAnimationEnabled(true); VerticalPanel dialogVPanel = new VerticalPanel(); dialogVPanel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER); if (quiz == null) { quiz = new Quiz(); final NamedBeanForm form = new NamedBeanForm(); form.setBean(quiz); form.setCommand(new Command() { @Override public void execute() { clientFactory.getContentService().addQuiz((Quiz) form.getBean(), new AsyncCallback<Void>() { @Override public void onFailure(Throwable caught) { UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage()); } @Override public void onSuccess(Void result) { } }); } }); dialogVPanel.add(form); } else if (!quiz.getQuestions().isEmpty()) { QuizView quizView = new QuizViewImpl(clientFactory.getConstants()); quizView.setQuestion(quiz.getQuestions().get((int) Math.random() * quiz.getQuestions().size())); dialogVPanel.add(quizView); } final Button closeButton = new Button(clientFactory.getConstants().closeButton()); closeButton.getElement().setId("closeButton"); dialogVPanel.add(closeButton); dialogBox.setWidget(dialogVPanel); closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { dialogBox.hide(); } }); dialogBox.center(); } /** * Show message dialog. * * @param message the message * @param clientFactory the client factory */ public static void showMessageDialog(Message message, ClientFactory clientFactory) { // TODO-NM: implement this method. final DialogBox dialogBox = new DialogBox(); dialogBox.setText("Message"); dialogBox.setAnimationEnabled(true); VerticalPanel dialogVPanel = new VerticalPanel(); dialogVPanel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER); dialogVPanel.add(new Label(clientFactory.getConstants().featureNotImplemented())); final Button closeButton = new Button(clientFactory.getConstants().closeButton()); closeButton.getElement().setId("closeButton"); dialogVPanel.add(closeButton); dialogBox.setWidget(dialogVPanel); closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { dialogBox.hide(); } }); dialogBox.center(); } /** * Show content analysis dialog. * * @param analysis the analysis * @param constants the constants */ public static void showContentAnalysisDialog(ContentAnalysis analysis, EverScribeConstants constants) { // TODO-NM: implement this method. final DialogBox dialogBox = new DialogBox(); dialogBox.setText("Content Analysis"); dialogBox.setAnimationEnabled(true); VerticalPanel dialogVPanel = new VerticalPanel(); dialogVPanel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER); dialogVPanel.add(new Label(constants.settingsSaved())); final Button closeButton = new Button(constants.closeButton()); closeButton.getElement().setId("closeButton"); dialogVPanel.add(closeButton); dialogBox.setWidget(dialogVPanel); closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { dialogBox.hide(); } }); dialogBox.center(); } /** * Show question summary dialog. * * @param result the result * @param constants the constants */ public static void showQuestionSummaryDialog(String result, EverScribeConstants constants) { final DialogBox dialogBox = new DialogBox(); dialogBox.setText("Summary"); dialogBox.setAnimationEnabled(true); dialogBox.setWidth("400px"); dialogBox.setHeight("200px"); VerticalPanel dialogVPanel = new VerticalPanel(); dialogVPanel.add(new HTML(result)); final Button closeButton = new Button(constants.closeButton()); // We can set the id of a widget by accessing its // Element closeButton.getElement().setId("closeButton"); dialogVPanel.add(closeButton); dialogBox.setWidget(dialogVPanel); closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { dialogBox.hide(); } }); dialogBox.center(); } /** * Show question answer dialog. * * @param result the result * @param constants the constants */ public static void showQuestionAnswerDialog(String result, EverScribeConstants constants) { final DialogBox dialogBox = new DialogBox(); dialogBox.setText("Suggested Answer"); dialogBox.setAnimationEnabled(true); dialogBox.setWidth("400px"); dialogBox.setHeight("200px"); VerticalPanel dialogVPanel = new VerticalPanel(); dialogVPanel.add(new HTML(result)); final Button closeButton = new Button(constants.closeButton()); // We can set the id of a widget by accessing its // Element closeButton.getElement().setId("closeButton"); dialogVPanel.add(closeButton); dialogBox.setWidget(dialogVPanel); closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { dialogBox.hide(); } }); dialogBox.center(); } /** * Show spell check dialog. * * @param result the result * @param constants the constants */ public static void showSpellCheckDialog(String result, EverScribeConstants constants) { final DialogBox dialogBox = new DialogBox(); dialogBox.setText("Spell Checker"); dialogBox.setAnimationEnabled(true); dialogBox.setWidth("400px"); dialogBox.setHeight("200px"); VerticalPanel dialogVPanel = new VerticalPanel(); dialogVPanel.add(new HTML(result)); final Button closeButton = new Button(constants.closeButton()); // We can set the id of a widget by accessing its // Element closeButton.getElement().setId("closeButton"); dialogVPanel.add(closeButton); dialogBox.setWidget(dialogVPanel); closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { dialogBox.hide(); } }); dialogBox.center(); } /** * Show run code dialog. * * @param result the result * @param constants the constants */ public static void showRunCodeDialog(String result, EverScribeConstants constants) { final DialogBox dialogBox = new DialogBox(); dialogBox.setText("Code Evaluation"); dialogBox.setAnimationEnabled(true); dialogBox.setWidth("400px"); dialogBox.setHeight("200px"); VerticalPanel dialogVPanel = new VerticalPanel(); dialogVPanel.add(new HTML(result)); final Button closeButton = new Button(constants.closeButton()); // We can set the id of a widget by accessing its // Element closeButton.getElement().setId("closeButton"); dialogVPanel.add(closeButton); dialogBox.setWidget(dialogVPanel); closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { dialogBox.hide(); } }); dialogBox.center(); } /** * Show speak dialog. * * @param result the result * @param constants the constants */ public static void showSpeakDialog(ArrayList<String> result, EverScribeConstants constants) { final DialogBox dialogBox = new DialogBox(); dialogBox.setText("Speak"); dialogBox.setAnimationEnabled(true); dialogBox.setWidth("400px"); dialogBox.setHeight("500px"); VerticalPanel dialogVPanel = new VerticalPanel(); StringBuilder builder = new StringBuilder(); for (String url : result) { builder.append("<embed src='" + url + "' controller='true' type='audio/wav' height='80px' width='400px' pluginspage='http://www.apple.com/quicktime/download/' />"); builder.append("<br/>"); } dialogVPanel.add(new HTML(builder.toString())); final Button closeButton = new Button(constants.closeButton()); // We can set the id of a widget by accessing its // Element closeButton.getElement().setId("closeButton"); dialogVPanel.add(closeButton); dialogBox.setWidget(dialogVPanel); closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { dialogBox.hide(); } }); dialogBox.center(); } /** * Show classification dialog. * * @param result the result * @param constants the constants */ public static void showClassificationDialog(List<Classification> result, EverScribeConstants constants) { final DialogBox dialogBox = new DialogBox(); dialogBox.setText("Classification"); dialogBox.setAnimationEnabled(true); VerticalPanel dialogVPanel = new VerticalPanel(); dialogVPanel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER); StringBuilder builder = new StringBuilder(); builder.append("<table style='text-align: left; width: 100%;' border='1' cellpadding='2' cellspacing='2'>"); builder.append("<tbody>"); builder.append("<tr><th nowrap='nowrap'>Class</th><th nowrap='nowrap'>Probability</th></tr>"); for (Classification classification : result) { builder.append("<tr><td nowrap='nowrap'>" + classification.getCategory() + "</td><td nowrap='nowrap'>" + classification.getProbability() + "</td></tr>"); } builder.append("</tbody>"); builder.append("</table>"); dialogVPanel.add(new HTML(builder.toString())); final Button closeButton = new Button(constants.closeButton()); closeButton.getElement().setId("closeButton"); dialogVPanel.add(closeButton); dialogBox.setWidget(dialogVPanel); closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { dialogBox.hide(); } }); dialogBox.center(); } /** * Show translation dialog. * * @param result the result * @param constants the constants */ public static void showTranslationDialog(String result, EverScribeConstants constants) { final DialogBox dialogBox = new DialogBox(); dialogBox.setText("Translation"); dialogBox.setAnimationEnabled(true); dialogBox.setWidth("400px"); dialogBox.setHeight("200px"); VerticalPanel dialogVPanel = new VerticalPanel(); dialogVPanel.add(new HTML(result)); final Button closeButton = new Button(constants.closeButton()); // We can set the id of a widget by accessing its // Element closeButton.getElement().setId("closeButton"); dialogVPanel.add(closeButton); dialogBox.setWidget(dialogVPanel); closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { dialogBox.hide(); } }); dialogBox.center(); } }