Una vez que llegue aquí, eso significa que sabe cómo crear una aplicación GWT básica con RPC y Datastore. Ahora es posible que desee agregar control de acceso para su aplicación. Lo que necesitamos aquí es construir otro servicio. El proceso similar se aplicó aquí.
Finalmente, Class Diagram en el directorio del cliente.
NoteWatcher.java es el punto de entrada. Se enumera a continuación, solo para ideas generales. El código fuente completo se exporta a un archivo zip. Puede descargar desde http://hotfile.com/dl/101385897/0cc8202/NoteWatcher.zip.html. No hereda trabajos anteriores en 1 y 2, es otra aplicación.
(El enlace está inactivo. Lamentablemente, no tengo una copia del código porque se desarrolló hace mucho tiempo, antes de que Git se hiciera popular. Lamento las molestias, pero no tengo tiempo para hacerlo de nuevo).
package com.google.gwt.sample.notewatcher.client; import java.util.ArrayList; 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.event.dom.client.KeyCodes; import com.google.gwt.event.dom.client.KeyPressEvent; import com.google.gwt.event.dom.client.KeyPressHandler; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.ui.Anchor; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.FlexTable; import com.google.gwt.user.client.ui.HorizontalPanel; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.TextArea; import com.google.gwt.user.client.ui.VerticalPanel; public class NoteWatcher implements EntryPoint { private VerticalPanel mainPanel = new VerticalPanel(); private FlexTable notesFlexTable = new FlexTable(); private HorizontalPanel addPanel = new HorizontalPanel(); private TextArea newSymbolTextBox = new TextArea(); private Button addNoteButton = new Button("Add"); private Label lastUpdatedLabel = new Label(); private ArrayList<String> NotesNames = new ArrayList<String>(); private LoginInfo loginInfo = null; private VerticalPanel loginPanel = new VerticalPanel(); private Label loginLabel = new Label( "Please sign in to your Google Account to access the NoteWatcher application."); private Anchor signInLink = new Anchor("Sign In"); private Anchor signOutLink = new Anchor("Sign Out"); // (1) Create the client proxy. Note that although you are creating the // service interface properly, you cast the result to the asynchronous // version of the interface. The cast is always safe because the // generated proxy implements the asynchronous interface automatically. private final NoteServiceAsync NoteService = GWT.create(NoteService.class); /** * Entry point method. */ public void onModuleLoad() { // (1) Check login status using login service. LoginServiceAsync loginService = GWT.create(LoginService.class); // (2) Create an asynchronous callback to handle the result. AsyncCallback acb = new AsyncCallback<LoginInfo>() { public void onFailure(Throwable error) { } public void onSuccess(LoginInfo result) { loginInfo = result; if (loginInfo.isLoggedIn()) { loadNoteWatcher(); } else { loadLogin(); } } }; // (3) Make the call. Control flow will continue immediately and later // 'callback' will be invoked when the RPC completes. loginService.login(GWT.getHostPageBaseURL(), acb); } private void loadLogin() { // Assemble login panel. signInLink.setHref(loginInfo.getLoginUrl()); loginPanel.add(loginLabel); loginPanel.add(signInLink); RootPanel.get("noteList").add(loginPanel); } private void loadNoteWatcher() { // Set up sign out hyperlink. signOutLink.setHref(loginInfo.getLogoutUrl()); // Create table for Note data. notesFlexTable.setText(0, 0, "User"); notesFlexTable.setCellSpacing(20); notesFlexTable.setText(0, 1, "Note"); if(loginInfo.getNickname() == "xiaoranlr"){ notesFlexTable.setText(0, 2, "Remove"); } // set button's style addNoteButton.addStyleName("addButton"); // Assemble Add Note panel. addPanel.add(newSymbolTextBox); addPanel.add(addNoteButton); // Assemble Main panel. mainPanel.add(signOutLink); mainPanel.add(notesFlexTable); mainPanel.add(addPanel); mainPanel.add(lastUpdatedLabel); // Associate the Main panel with the HTML host page. RootPanel.get("noteList").add(mainPanel); // Move cursor focus to the input box. newSymbolTextBox.setWidth("300px"); newSymbolTextBox.setFocus(true); // Listen for mouse events on the Add button. addNoteButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { addNote(); } }); // Listen for keyboard events in the input box. newSymbolTextBox.addKeyPressHandler(new KeyPressHandler() { public void onKeyPress(KeyPressEvent event) { if (event.getCharCode() == KeyCodes.KEY_ENTER) { addNote(); } } }); loadNotes(); } /** * Add Note to FlexTable. Executed when the user clicks the addNoteButton or * presses enter in the newSymbolTextBox. */ private void addNote() { final String symbol = newSymbolTextBox.getText().trim(); newSymbolTextBox.setFocus(true); newSymbolTextBox.setText(""); // Don't add the Note if it's already in the table. if (NotesNames.contains(symbol)) return; // displayNote(symbol); addNote(loginInfo.getNickname(), symbol); } private void addNote(final String user, final String symbol) { // (2) Create an asynchronous callback to handle the result. AsyncCallback callback = new AsyncCallback<Void>() { public void onFailure(Throwable error) { //do something, when fail } public void onSuccess(Void ignore) { //when successful, do something, about UI displayNote(user, symbol); } }; // (3) Make the call. Control flow will continue immediately and later // 'callback' will be invoked when the RPC completes. NoteService.addNote(user, symbol, callback); } private void displayNote(final String user, final String symbol) { // Add the Note to the table. int row = notesFlexTable.getRowCount(); NotesNames.add(symbol); notesFlexTable.setText(row, 0, user); notesFlexTable.setText(row, 1, symbol); // Add a button to remove this Note from the table. if (loginInfo.getNickname() == "xiaoranlr") { Button removeNoteButton = new Button("x"); removeNoteButton.addStyleDependentName("remove"); removeNoteButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { removeNote(symbol); } }); notesFlexTable.setWidget(row, 2, removeNoteButton); } } private void removeNote(final String symbol) { NoteService.removeNote(symbol, new AsyncCallback<Void>() { public void onFailure(Throwable error) { } public void onSuccess(Void ignore) { undisplayNote(symbol); } }); } private void undisplayNote(String symbol) { int removedIndex = NotesNames.indexOf(symbol); NotesNames.remove(removedIndex); notesFlexTable.removeRow(removedIndex + 1); } private void loadNotes() { NoteService.getNotes(new AsyncCallback<String[]>() { public void onFailure(Throwable error) { } public void onSuccess(String[] symbols) { displayNotes("anonymous", symbols); } }); } private void displayNotes(String user, String[] symbols) { for (String symbol : symbols) { displayNote(user, symbol); } } } |