Java tutorial
/* * Copyright 2015 Piotr Morgwai Kotarbinski * * 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 pl.morgwai.sample.gwtGuiceHibernate.gwtPresentation; import java.util.List; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.DialogBox; import com.google.gwt.user.client.ui.FlexTable; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.ValueBoxBase.TextAlignment; import com.google.web.bindery.event.shared.EventBus; import com.google.web.bindery.event.shared.SimpleEventBus; import com.google.web.bindery.requestfactory.shared.Receiver; import com.google.web.bindery.requestfactory.shared.ServerFailure; import pl.morgwai.sample.gwtGuiceHibernate.entityProxies.UserProxy; import pl.morgwai.sample.gwtGuiceHibernate.gwtRequestBuilders.RequestBuilderFactory; public class UserSearchEntryPoint implements EntryPoint { public static final String CREATE_USER_PANEL_ID = "createUserPanel"; public static final String USER_SEARCH_PANEL_ID = "userSearchPanel"; public static final String SEARCH_RESULTS_PANEL_ID = "searchResultsPanel"; RequestBuilderFactory serviceFactory; Messages messages = GWT.create(Messages.class); DialogBox createUserDialogBox; FlexTable resultsFlexTable; Button searchButton; @Override public void onModuleLoad() { serviceFactory = GWT.create(RequestBuilderFactory.class); EventBus eventBus = new SimpleEventBus(); serviceFactory.initialize(eventBus); RootPanel createUserPanel = RootPanel.get(CREATE_USER_PANEL_ID); final TextBox newUserNameTextBox = new TextBox(); newUserNameTextBox.setName("newUserName"); newUserNameTextBox.setMaxLength(64); newUserNameTextBox.setText(messages.newUserNamePrompt()); createUserPanel.add(newUserNameTextBox); final Button createUserButton = new Button(messages.createUserButtonLabel()); createUserPanel.add(createUserButton); createUserButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { createUserButton.setEnabled(false); createUser(newUserNameTextBox.getText()); newUserNameTextBox.setText(""); } }); createUserDialogBox = new DialogBox(); Button closeButton = new Button(messages.closeButtonLabel()); createUserDialogBox.add(closeButton); closeButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { createUserDialogBox.hide(); createUserButton.setEnabled(true); } }); RootPanel userSearchPanel = RootPanel.get(USER_SEARCH_PANEL_ID); final TextBox searchNameTextBox = new TextBox(); searchNameTextBox.setName("searchUserName"); searchNameTextBox.setAlignment(TextAlignment.LEFT); searchNameTextBox.setMaxLength(64); searchNameTextBox.setText(messages.searchUserNamePrompt()); userSearchPanel.add(searchNameTextBox); searchButton = new Button(messages.searchButtonLabel()); userSearchPanel.add(searchButton); searchButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { searchButton.setEnabled(false); findUsersByName(searchNameTextBox.getText()); } }); RootPanel searchResultsPanel = RootPanel.get(SEARCH_RESULTS_PANEL_ID); resultsFlexTable = new FlexTable(); searchResultsPanel.add(resultsFlexTable); resultsFlexTable.setText(0, 0, messages.greeting()); } void createUser(String name) { serviceFactory.getUserManagementRequestBuilder().createUser(name).fire(createResultReceiver); } Receiver<Long> createResultReceiver = new Receiver<Long>() { @Override public void onSuccess(Long newUserId) { createUserDialogBox.setText(messages.userCreatedMessage() + newUserId); createUserDialogBox.center(); } @Override public void onFailure(ServerFailure error) { createUserDialogBox.setText(messages.errorInfo() + error.getMessage()); createUserDialogBox.center(); } }; void findUsersByName(String name) { resultsFlexTable.removeAllRows(); resultsFlexTable.setText(0, 0, messages.searchingInfo()); serviceFactory.getUserManagementRequestBuilder().findByName(name).fire(userListReceiver); } Receiver<List<UserProxy>> userListReceiver = new Receiver<List<UserProxy>>() { @Override public void onSuccess(List<UserProxy> users) { searchButton.setEnabled(true); resultsFlexTable.removeAllRows(); if (users == null || users.isEmpty()) { resultsFlexTable.setText(0, 0, messages.noResultsInfo()); return; } for (UserProxy user : users) { resultsFlexTable.setText(resultsFlexTable.getRowCount(), 0, user.getFullName()); } } @Override public void onFailure(ServerFailure error) { searchButton.setEnabled(true); resultsFlexTable.removeAllRows(); resultsFlexTable.setText(0, 0, messages.errorInfo() + error.getMessage()); } }; }