[mozzes] r12 committed - Working on Vaadin example.

11 views
Skip to first unread message

moz...@googlecode.com

unread,
Feb 26, 2010, 12:24:34 PM2/26/10
to mozzes-d...@googlegroups.com
Revision: 12
Author: perica.milosevic
Date: Fri Feb 26 09:23:22 2010
Log: Working on Vaadin example.
http://code.google.com/p/mozzes/source/detail?r=12

Added:

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleVaadin/src/main/java/org/mozzes/application/example/vaadin/gui/TeamEditWindow.java
Modified:

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleCommon/src/main/java/org/mozzes/application/example/common/domain/Team.java

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleServer/src/main/resources/hibernate.cfg.xml

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleServer/src/main/resources/log4j.properties

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleVaadin/pom.xml

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleVaadin/src/main/java/org/mozzes/application/example/vaadin/gui/MainWindow.java

=======================================
--- /dev/null
+++
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleVaadin/src/main/java/org/mozzes/application/example/vaadin/gui/TeamEditWindow.java
Fri Feb 26 09:23:22 2010
@@ -0,0 +1,165 @@
+package org.mozzes.application.example.vaadin.gui;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+
+import org.mozzes.application.example.common.domain.Team;
+import org.mozzes.application.example.common.service.Administration;
+
+import com.vaadin.Application;
+import com.vaadin.terminal.FileResource;
+import com.vaadin.ui.Alignment;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Embedded;
+import com.vaadin.ui.FormLayout;
+import com.vaadin.ui.HorizontalLayout;
+import com.vaadin.ui.Layout;
+import com.vaadin.ui.TextField;
+import com.vaadin.ui.Upload;
+import com.vaadin.ui.VerticalLayout;
+import com.vaadin.ui.Window;
+import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.Button.ClickListener;
+import com.vaadin.ui.Upload.FailedEvent;
+import com.vaadin.ui.Upload.SucceededEvent;
+
+public class TeamEditWindow extends Window {
+
+ private static final long serialVersionUID = 3432589583281572169L;
+
+ private final Application application;
+ private final MainWindow mainWindow;
+ private final Administration<Team> administration;
+ private FormLayout form;
+
+ private Team team;
+ private Upload imageUpload;
+ private TextField nameField = new TextField("Name");
+ private TextField webField = new TextField("Web");
+ private Embedded crestImage;
+ private ImageUploadListener uploadReceiver;
+
+ TeamEditWindow(Application application, MainWindow mainWindow, String
title, Team team,
+ Administration<Team> administration) {
+ super(title);
+ setModal(true);
+ this.application = application;
+ this.mainWindow = mainWindow;
+ this.administration = administration;
+ setTeam(team);
+ setImageUpload(team);
+
+ form = new FormLayout();
+ form.setSizeUndefined();
+ form.addComponent(nameField);
+ form.addComponent(webField);
+ form.addComponent(crestImage);
+ form.addComponent(imageUpload);
+
+ Button saveButton = new Button("Save");
+ saveButton.addListener(new ClickListener() {
+ private static final long serialVersionUID = 8515020970178194064L;
+
+ @Override
+ public void buttonClick(ClickEvent event) {
+ save();
+ }
+ });
+
+ HorizontalLayout actions = new HorizontalLayout();
+ actions.addComponent(saveButton);
+ actions.setComponentAlignment(saveButton, Alignment.MIDDLE_CENTER);
+
+ Layout layout = new VerticalLayout();
+ layout.setStyleName(STYLE_LIGHT);
+ layout.setSizeUndefined();
+ layout.addComponent(form);
+ layout.addComponent(actions);
+ setContent(layout);
+ }
+
+ private void setTeam(Team team) {
+ this.team = team;
+ nameField.setValue(team.getName());
+ webField.setValue(team.getWebAddress());
+ if (team.getCrestImage() != null)
+ crestImage = new Embedded("Crest", new FileResource(new
File(team.getCrestImage()), application));
+ else
+ crestImage = new Embedded("Crest");
+ crestImage.requestRepaint();
+ }
+
+ private void setImageUpload(Team team) {
+ uploadReceiver = new ImageUploadListener(team);
+ imageUpload = new Upload("Upload crest image", uploadReceiver);
+ imageUpload.setButtonCaption("Upload");
+ imageUpload.addListener((Upload.SucceededListener) uploadReceiver);
+ imageUpload.addListener((Upload.FailedListener) uploadReceiver);
+ }
+
+ private void save() {
+ team.setName(String.valueOf(nameField.getValue()));
+ team.setWebAddress(String.valueOf(webField.getValue()));
+ if (uploadReceiver.file != null)
+ team.setCrestImage(uploadReceiver.file.getAbsolutePath());
+ administration.save(team);
+ mainWindow.removeWindow(this);
+ mainWindow.reloadTeams();
+ }
+
+ private static final String IMAGE_PATH = "c:" + File.separatorChar
+ "images" + File.separatorChar;
+
+ private class ImageUploadListener implements Upload.SucceededListener,
Upload.FailedListener, Upload.Receiver {
+
+ private static final long serialVersionUID = 7572703698105220338L;
+
+ private final Team team;
+ private File file;
+
+ public ImageUploadListener(Team team) {
+ this.team = team;
+ }
+
+ @Override
+ public OutputStream receiveUpload(String filename, String MIMEType) {
+ if (MIMEType.indexOf("image") == -1)
+ return null;
+
+ FileOutputStream fos = null; // Output stream to write to
+ if (team.getCrestImage() != null)
+ file = new File(team.getCrestImage());
+ else
+ file = new File(generateImageFileName() + "." +
MIMEType.substring(MIMEType.indexOf('/') + 1));
+ try {
+ // Open the file for writing.
+ fos = new FileOutputStream(file);
+ } catch (final java.io.FileNotFoundException e) {
+ // Error while opening the file. Not reported here.
+ e.printStackTrace();
+ return null;
+ }
+ return fos; // Return the output stream to write to
+ }
+
+ @Override
+ public void uploadSucceeded(SucceededEvent event) {
+ showNotification("File " + event.getFilename() + " of type '" +
event.getMIMEType() + "' uploaded.");
+ form.removeComponent(crestImage);
+ crestImage = new Embedded("Crest", (new FileResource(file,
application)));
+ crestImage.requestRepaint();
+ form.addComponent(crestImage);
+ }
+
+ @Override
+ public void uploadFailed(FailedEvent event) {
+ showNotification("Uploading " + event.getFilename() + " of type '" +
event.getMIMEType() + "' failed.",
+ Notification.TYPE_ERROR_MESSAGE);
+ }
+
+ private String generateImageFileName() {
+ return IMAGE_PATH + "t" + hashCode() + System.currentTimeMillis();
+ }
+ }
+
+}
=======================================
---
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleCommon/src/main/java/org/mozzes/application/example/common/domain/Team.java
Wed Feb 24 05:48:21 2010
+++
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleCommon/src/main/java/org/mozzes/application/example/common/domain/Team.java
Fri Feb 26 09:23:22 2010
@@ -15,6 +15,7 @@
private String name;
private String webAddress;
private byte[] image;
+ private String crestImage;

public Integer getId() {
return id;
@@ -47,7 +48,15 @@
public void setImage(byte[] image) {
this.image = image;
}
-
+
+ public String getCrestImage() {
+ return crestImage;
+ }
+
+ public void setCrestImage(String crestImage) {
+ this.crestImage = crestImage;
+ }
+
@Override
public String toString() {
return name;
=======================================
---
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleServer/src/main/resources/hibernate.cfg.xml
Wed Feb 24 05:48:21 2010
+++
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleServer/src/main/resources/hibernate.cfg.xml
Fri Feb 26 09:23:22 2010
@@ -7,7 +7,7 @@

<!-- Settings for a local HSQL (testing) database. -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
- <property name="connection.url">jdbc:hsqldb:file:db/example</property>
+ <property name="connection.url">jdbc:hsqldb:file:../db/example</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>

=======================================
---
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleServer/src/main/resources/log4j.properties
Tue Feb 23 16:09:19 2010
+++
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleServer/src/main/resources/log4j.properties
Fri Feb 26 09:23:22 2010
@@ -8,7 +8,7 @@

# F1 je apender za logovanje u tekstualni fajl
log4j.appender.F1=org.apache.log4j.RollingFileAppender
-log4j.appender.F1.File=log/server.log
+log4j.appender.F1.File=../log/server.log
log4j.appender.F1.layout=org.apache.log4j.PatternLayout
log4j.appender.F1.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p [%c] from
[%t] %m%n
log4j.appender.F1.MaxFileSize=1000KB
=======================================
---
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleVaadin/pom.xml
Tue Feb 23 16:09:19 2010
+++
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleVaadin/pom.xml
Fri Feb 26 09:23:22 2010
@@ -21,6 +21,11 @@
<artifactId>mozzesApplicationVaadin</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.mozzes.validation</groupId>
+ <artifactId>mozzesValidation</artifactId>
+ <version>${project.version}</version>
+ </dependency>
</dependencies>
<build>
<plugins>
=======================================
---
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleVaadin/src/main/java/org/mozzes/application/example/vaadin/gui/MainWindow.java
Wed Feb 24 08:48:47 2010
+++
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleVaadin/src/main/java/org/mozzes/application/example/vaadin/gui/MainWindow.java
Fri Feb 26 09:23:22 2010
@@ -5,7 +5,6 @@
import org.mozzes.application.common.client.MozzesClient;
import org.mozzes.application.example.common.domain.Match;
import org.mozzes.application.example.common.domain.Team;
-import org.mozzes.application.example.common.service.Administration;
import org.mozzes.application.example.common.service.MatchAdministration;
import org.mozzes.application.example.common.service.TeamAdministration;

@@ -16,10 +15,8 @@
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
-import com.vaadin.ui.Form;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
-import com.vaadin.ui.Layout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
@@ -31,15 +28,20 @@
@SessionScoped
public class MainWindow extends Window {

+ private static final String TITLE = "Mozzes example - Vaadin web client";
+
private static final long serialVersionUID = -1629403228064679292L;

private final MozzesClient client;
private BeanItemContainer<Team> teamSource = new
BeanItemContainer<Team>(Team.class);
private BeanItemContainer<Match> matchSource = new
BeanItemContainer<Match>(Match.class);

+ private Table teamTable;
+ private Table matchTable;
+
@Inject
public MainWindow(MozzesClient client) {
- super("Hello!");
+ super(TITLE);
this.client = client;
setTheme("runo");
initGui();
@@ -54,7 +56,7 @@
teamLayout.setExpandRatio(teamTable, 1f);
teamLayout.setSizeFull();

- Panel teamPanel = new Panel("Timovi");
+ Panel teamPanel = new Panel("Teams");
teamPanel.setContent(teamLayout);
teamPanel.setSizeFull();

@@ -65,7 +67,7 @@
matchLayout.setExpandRatio(matchTable, 1f);
matchLayout.setSizeFull();

- Panel matchPanel = new Panel("Mečevi");
+ Panel matchPanel = new Panel("Matches");
matchPanel.setContent(matchLayout);
matchPanel.setSizeFull();

@@ -74,7 +76,7 @@
mainLayout.addComponent(matchPanel);
mainLayout.setSizeFull();

- Panel mainPanel = new Panel("Primer Vaadin klijenta za Mozzes server");
+ Panel mainPanel = new Panel(TITLE);
mainPanel.setStyleName(STYLE_LIGHT);
mainPanel.setContent(mainLayout);
mainPanel.setSizeFull();
@@ -83,15 +85,16 @@
}

private Table createTeamTable() {
- Table teamTable = new Table(null, teamSource);
- teamTable.setVisibleColumns(new Object[]{"name", "image", "webAddress"});
- teamTable.setColumnHeaders(new String[]{"Naziv", "Grb", "Web"});
+ teamTable = new Table(null, teamSource);
+ teamTable.setVisibleColumns(new
Object[]{"name", "crestImage", "webAddress"});
+ teamTable.setColumnHeaders(new String[]{"Name", "Crest", "Web"});
+ teamTable.setSelectable(true);
teamTable.setSizeFull();
return teamTable;
}

private GridLayout createTeamButtonPanel() {
- Button refreshTeamsButton = new Button("Osveži timove");
+ Button refreshTeamsButton = new Button("Reload teams");
refreshTeamsButton.setIcon(new ThemeResource("icons/32/reload.png"));
refreshTeamsButton.addListener(new ClickListener() {
private static final long serialVersionUID = -4440833664676514934L;
@@ -101,19 +104,34 @@
}
});

- Button newTeamButton = new Button("Kreiraj tim");
+ Button newTeamButton = new Button("New team");
newTeamButton.setIcon(new ThemeResource("icons/32/document-add.png"));
newTeamButton.addListener(new ClickListener() {
private static final long serialVersionUID = -5792165786350656817L;
@Override
public void buttonClick(ClickEvent event) {
- addWindow(new BeanEditWindow<Team>(MainWindow.this, "New team", new
Team(), client.getService(TeamAdministration.class)));
+ addWindow(new TeamEditWindow(getApplication(), MainWindow.this, "New
team", new Team(), client.getService(TeamAdministration.class)));
}
});

- Button editTeamButton = new Button("Izmeni tim");
+ Button editTeamButton = new Button("Edit team");
editTeamButton.setIcon(new ThemeResource("icons/32/document-edit.png"));
- Button deleteTeamButton = new Button("Obriši tim");
+ editTeamButton.addListener(new ClickListener() {
+ private static final long serialVersionUID = -5792165786350656817L;
+ @SuppressWarnings("unchecked")
+ @Override
+ public void buttonClick(ClickEvent event) {
+ Object selected = teamTable.getValue();
+ if (selected != null) {
+ Team selectedTeam =
((BeanItem<Team>)teamTable.getItem(selected)).getBean();
+ addWindow(new TeamEditWindow(getApplication(), MainWindow.this, "Edit
team", selectedTeam, client.getService(TeamAdministration.class)));
+ ;
+ } else
+ showNotification("No team selected!");
+ }
+ });
+
+ Button deleteTeamButton = new Button("Delete team");
deleteTeamButton.setIcon(new
ThemeResource("icons/32/document-delete.png"));

GridLayout teamButtonPanel = new GridLayout(4, 1);
@@ -131,15 +149,15 @@
}

private Table createMatchTable() {
- Table matchTable = new Table(null, matchSource);
+ matchTable = new Table(null, matchSource);
matchTable.setVisibleColumns(new
Object[]{"startTime", "homeTeam", "visitorTeam", "result"});
- matchTable.setColumnHeaders(new
String[]{"Vreme", "Domaćin", "Gost", "Rezultat"});
+ matchTable.setColumnHeaders(new String[]{"Start time", "Home
team", "Visitor team", "Result"});
matchTable.setSizeFull();
return matchTable;
}

private GridLayout createMatchButtonPanel() {
- Button refreshMatchesButton = new Button("Osveži mečeve");
+ Button refreshMatchesButton = new Button("Reload matches");
refreshMatchesButton.setIcon(new ThemeResource("icons/32/reload.png"));
refreshMatchesButton.addListener(new ClickListener() {
private static final long serialVersionUID = -4440833664676514934L;
@@ -149,12 +167,12 @@
}
});

- Button newMatchButton = new Button("Kreiraj meč");
+ Button newMatchButton = new Button("New match");
newMatchButton.setIcon(new ThemeResource("icons/32/document-add.png"));

- Button editMatchButton = new Button("Izmeni meč");
+ Button editMatchButton = new Button("Edit match");
editMatchButton.setIcon(new ThemeResource("icons/32/document-edit.png"));
- Button deleteMatchButton = new Button("Obriši meč");
+ Button deleteMatchButton = new Button("Delete match");
deleteMatchButton.setIcon(new
ThemeResource("icons/32/document-delete.png"));

GridLayout matchButtonPanel = new GridLayout(4, 1);
@@ -176,13 +194,13 @@
reloadMatches();
}

- private void reloadMatches() {
+ void reloadMatches() {
matchSource.removeAllItems();
for (Match match : getMatches())
matchSource.addBean(match);
}

- private void reloadTeams() {
+ void reloadTeams() {
teamSource.removeAllItems();
for (Team team: getTeams())
teamSource.addBean(team);
@@ -197,62 +215,4 @@
return client.getService(MatchAdministration.class).findAll();
}

- private static final class BeanEditWindow<T> extends Window {
-
- private static final long serialVersionUID = 3432589583281572169L;
-
- private final MainWindow mainWindow;
- private final Administration<T> administration;
- private final Form beanForm;
-
- BeanEditWindow(MainWindow mainWindow, String title, T bean,
Administration<T> administration) {
- super(title);
- setModal(true);
- this.mainWindow = mainWindow;
- this.administration = administration;
- this.beanForm = new Form();
-
- beanForm.setItemDataSource(new BeanItem<T>(bean));
- beanForm.setSizeUndefined();
-
- Button saveButton = new Button("Save");
- saveButton.addListener(new ClickListener() {
- private static final long serialVersionUID = 8515020970178194064L;
- @Override
- public void buttonClick(ClickEvent event) {
- save();
- }
- });
- Button cancelButton = new Button("Cancel");
- cancelButton.addListener(new ClickListener() {
- private static final long serialVersionUID = 7606145616074298309L;
- @Override
- public void buttonClick(ClickEvent event) {
- cancel();
- }
- });
-
- Layout buttonsLayout = new HorizontalLayout();
- buttonsLayout.addComponent(saveButton);
- buttonsLayout.addComponent(cancelButton);
-
- Layout layout = new VerticalLayout();
- layout.setStyleName(STYLE_LIGHT);
- layout.setSizeUndefined();
- layout.addComponent(beanForm);
- layout.addComponent(buttonsLayout);
- setContent(layout);
- }
-
- @SuppressWarnings("unchecked")
- private void save() {
-
administration.save(((BeanItem<T>)beanForm.getItemDataSource()).getBean());
- mainWindow.removeWindow(this);
- mainWindow.reloadData();
- }
-
- private void cancel() {
- mainWindow.removeWindow(this);
- }
- }
-}
+}
Reply all
Reply to author
Forward
0 new messages