Skip to content

Commit 427ff67

Browse files
committed
Part 5 - UiBinder
1 parent 6a34c2d commit 427ff67

File tree

6 files changed

+219
-185
lines changed

6 files changed

+219
-185
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
hellogwt.db
12
target/

ReadMe.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
This project is a step-by-step Spring-GWT tutorial.
2-
It is inspired from the article written by Alex Tretyakov on 2011-11-14
3-
[Spring and GWT tutorial. Part 4 - MyBatis and MySQL](http://alextretyakov.blogspot.fr/2011/11/developing-simple-web-application-using.html).
2+
It is inspired from the article written by Alex Tretyakov on 2012-08-30
3+
[Spring and GWT tutorial. Part 5 - UiBinder](http://alextretyakov.blogspot.fr/2012/08/using-uibinder.html).
44

55
The project was created as a "Maven Project" in Eclipse.
66
Then files from hellogwt were added, commented and modified to run with SQLite instead of MySQL.
Lines changed: 11 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/**
22
* GWT entry point.
33
*
4+
* Content of class HelloGWT does not contain UI creation and application logic code.
5+
* All this stuff is helloGWTWidget object's responsibility.
6+
*
47
* Licensed under the Apache License, Version 2.0 (the "License");
58
* you may not use this file except in compliance with the License.
69
* You may obtain a copy of the License at
@@ -17,199 +20,25 @@
1720

1821
import com.google.gwt.core.client.EntryPoint;
1922
import com.google.gwt.core.client.GWT;
20-
import com.google.gwt.event.dom.client.KeyUpEvent;
21-
import com.google.gwt.event.dom.client.KeyUpHandler;
22-
import com.google.gwt.event.dom.client.ClickEvent;
23-
import com.google.gwt.event.dom.client.ClickHandler;
24-
import com.google.gwt.user.client.Window;
25-
import com.google.gwt.user.client.rpc.AsyncCallback;
26-
import com.google.gwt.user.client.ui.Button;
27-
import com.google.gwt.user.client.ui.FlexTable;
28-
import com.google.gwt.user.client.ui.HorizontalPanel;
29-
import com.google.gwt.user.client.ui.Label;
3023
import com.google.gwt.user.client.ui.RootPanel;
31-
import com.google.gwt.user.client.ui.TextBox;
32-
import com.hellogwt.shared.domain.Greeting;
33-
import java.util.List;
3424

3525
/**
3626
* @author Alex Tretyakov
3727
*/
3828
public class HelloGWT implements EntryPoint {
3929

40-
private GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
41-
42-
private HorizontalPanel greetingPanel = new HorizontalPanel();
43-
private TextBox nameTextBox = new TextBox();
44-
private Label greetingLabel = new Label("Hello, GWT!");
45-
46-
private HorizontalPanel authorPanel = new HorizontalPanel();
47-
private HorizontalPanel textPanel = new HorizontalPanel();
48-
private HorizontalPanel editPanel = new HorizontalPanel();
49-
50-
private Label authorLabel = new Label("Author:");
51-
private Label textLabel = new Label("Text:");
52-
private TextBox authorTextBox = new TextBox();
53-
private TextBox textTextBox = new TextBox();
54-
55-
private Button addButton = new Button("Add");
56-
private Button updateButton = new Button("Update");
57-
private Button deleteButton = new Button("Delete");
58-
59-
private FlexTable greetingsFlexTable = new FlexTable();
60-
6130
/**
6231
* Use our service GreetingService in application.
63-
*
64-
* We will invoke greet() method every time any symbol is entered into the text field.
65-
* If greet() completes successfully then label text will change to String that method returns.
66-
* Otherwise label text will tell us about error.
32+
*
33+
* We will invoke greet() method every time any symbol is entered into the
34+
* text field. If greet() completes successfully then label text will change
35+
* to String that method returns. Otherwise label text will tell us about
36+
* error.
6737
*/
38+
@Override
6839
public void onModuleLoad() {
69-
initWidgets();
70-
initHandlers();
71-
72-
refreshGreetingsTable();
73-
}
74-
75-
private void initWidgets() {
76-
// label
77-
greetingPanel.add(nameTextBox);
78-
greetingPanel.add(greetingLabel);
79-
RootPanel.get().add(greetingPanel);
80-
81-
// table
82-
authorLabel.setWidth("50");
83-
authorTextBox.setWidth("100");
84-
authorPanel.add(authorLabel);
85-
authorPanel.add(authorTextBox);
86-
87-
textLabel.setWidth("50");
88-
textTextBox.setWidth("100");
89-
textPanel.add(textLabel);
90-
textPanel.add(textTextBox);
91-
92-
addButton.setWidth("50");
93-
updateButton.setWidth("50");
94-
deleteButton.setWidth("50");
95-
editPanel.add(addButton);
96-
editPanel.add(updateButton);
97-
editPanel.add(deleteButton);
98-
99-
RootPanel.get().add(authorPanel);
100-
RootPanel.get().add(textPanel);
101-
RootPanel.get().add(editPanel);
102-
RootPanel.get().add(greetingsFlexTable);
103-
}
104-
105-
private void initHandlers() {
106-
// label
107-
final AsyncCallback<String> textbox_callback = new AsyncCallback<String>() {
108-
@Override
109-
public void onFailure(Throwable caught) {
110-
greetingLabel.setText("ERROR!");
111-
}
112-
113-
@Override
114-
public void onSuccess(String result) {
115-
greetingLabel.setText(result);
116-
}
117-
};
118-
119-
nameTextBox.addKeyUpHandler(new KeyUpHandler() {
120-
@Override
121-
public void onKeyUp(KeyUpEvent keyUpEvent) {
122-
greetingService.greet(nameTextBox.getText(), textbox_callback);
123-
}
124-
});
125-
126-
// table
127-
final AsyncCallback<Void> btn_callback = new AsyncCallback<Void>() {
128-
@Override
129-
public void onFailure(Throwable caught) {
130-
Window.alert("ERROR: Cannot edit greetings!");
131-
}
132-
133-
@Override
134-
public void onSuccess(Void result) {
135-
refreshGreetingsTable();
136-
}
137-
};
138-
139-
addButton.addClickHandler(new ClickHandler() {
140-
@Override
141-
public void onClick(ClickEvent clickEvent) {
142-
if (!authorTextBox.getText().isEmpty()
143-
&& !textTextBox.getText().isEmpty()) {
144-
greetingService.getGreeting(textTextBox.getText(),
145-
new AsyncCallback<Greeting>() {
146-
@Override
147-
public void onFailure(Throwable caught) {
148-
Window.alert("ERROR: Cannot find greeting!");
149-
}
150-
151-
@Override
152-
public void onSuccess(Greeting result) {
153-
if (result == null) {
154-
greetingService.addGreeting(authorTextBox.getText(),
155-
textTextBox.getText(), btn_callback);
156-
} else {
157-
Window.alert("Greeting already exists!");
158-
}
159-
}
160-
});
161-
} else {
162-
Window.alert("\"Author\" and \"Text\" fields cannot be empty!");
163-
}
164-
}
165-
});
166-
167-
updateButton.addClickHandler(new ClickHandler() {
168-
@Override
169-
public void onClick(ClickEvent event) {
170-
if (!authorTextBox.getText().isEmpty()
171-
&& !textTextBox.getText().isEmpty()) {
172-
greetingService.updateGreeting(authorTextBox.getText(),
173-
textTextBox.getText(), btn_callback);
174-
} else {
175-
Window.alert("\"Author\" and \"Text\" fields cannot be empty!");
176-
}
177-
}
178-
});
179-
180-
deleteButton.addClickHandler(new ClickHandler() {
181-
@Override
182-
public void onClick(ClickEvent event) {
183-
greetingService.deleteGreeting(textTextBox.getText(), btn_callback);
184-
}
185-
});
186-
}
187-
188-
private void refreshGreetingsTable() {
189-
greetingService.getGreetings(new AsyncCallback<List<Greeting>>() {
190-
@Override
191-
public void onFailure(Throwable throwable) {
192-
Window.alert("ERROR: Cannot load greetings!");
193-
}
194-
195-
@Override
196-
public void onSuccess(List<Greeting> greetings) {
197-
fillGreetingsTable(greetings);
198-
}
199-
});
200-
}
201-
202-
private void fillGreetingsTable(List<Greeting> greetings) {
203-
greetingsFlexTable.removeAllRows();
204-
205-
greetingsFlexTable.setText(0, 0, "Author");
206-
greetingsFlexTable.setText(0, 1, "Text");
207-
208-
for (Greeting greeting : greetings) {
209-
int row = greetingsFlexTable.getRowCount();
40+
HelloGWTWidget helloGWTWidget = GWT.create(HelloGWTWidget.class);
21041

211-
greetingsFlexTable.setText(row, 0, greeting.getAuthor());
212-
greetingsFlexTable.setText(row, 1, greeting.getText());
213-
}
42+
RootPanel.get().add(helloGWTWidget);
21443
}
21544
}

0 commit comments

Comments
 (0)