Deleted:
/trunk/mozzesApplication/mozzesApplicationExample/images
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleServer/src/main/java/org/mozzes/application/example/server/service/ImageUtils.java
Modified:
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleCommon/src/main/java/org/mozzes/application/example/common/domain/Team.java
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleVaadin/src/main/java/org/mozzes/application/example/vaadin/gui/MainWindow.java
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleVaadin/src/main/java/org/mozzes/application/example/vaadin/gui/TeamEditWindow.java
=======================================
---
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleServer/src/main/java/org/mozzes/application/example/server/service/ImageUtils.java
Sat Feb 27 09:32:16 2010
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * Copyright 2010 Mozzart
- *
- *
- * This file is part of mozzes.
- *
- * mozzes is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as
published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * mozzes is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with mozzes. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-package org.mozzes.application.example.server.service;
-
-import java.io.File;
-
-public class ImageUtils {
-
- private static final String IMAGE_PATH = ".." + File.separatorChar
+ "images" + File.separatorChar;
-
- public static String generateImageFile() {
- return IMAGE_PATH + "t" + Thread.currentThread().hashCode() +
System.currentTimeMillis();
- }
-
-}
=======================================
---
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleCommon/src/main/java/org/mozzes/application/example/common/domain/Team.java
Sat Feb 27 06:56:25 2010
+++
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleCommon/src/main/java/org/mozzes/application/example/common/domain/Team.java
Wed Mar 3 13:56:07 2010
@@ -35,7 +35,6 @@
private String name;
private String webAddress;
private byte[] image;
- private String crestImage;
public Integer getId() {
return id;
@@ -68,14 +67,6 @@
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() {
=======================================
---
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleVaadin/src/main/java/org/mozzes/application/example/vaadin/gui/MainWindow.java
Sat Feb 27 09:32:16 2010
+++
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleVaadin/src/main/java/org/mozzes/application/example/vaadin/gui/MainWindow.java
Wed Mar 3 13:56:07 2010
@@ -20,8 +20,10 @@
*/
package org.mozzes.application.example.vaadin.gui;
+import java.io.IOException;
import java.util.List;
+import org.apache.log4j.Logger;
import org.mozzes.application.common.client.MozzesClient;
import org.mozzes.application.example.common.domain.Match;
import org.mozzes.application.example.common.domain.Team;
@@ -47,10 +49,10 @@
@SessionScoped
public class MainWindow extends Window {
- private static final String TITLE = "Mozzes example - Vaadin web client";
-
private static final long serialVersionUID = -1629403228064679292L;
-
+ private static final String TITLE = "Mozzes example - Vaadin web client";
+ private static final Logger log = Logger.getLogger(MainWindow.class);
+
private final MozzesClient client;
private BeanItemContainer<Team> teamSource = new
BeanItemContainer<Team>(Team.class);
private BeanItemContainer<Match> matchSource = new
BeanItemContainer<Match>(Match.class);
@@ -105,7 +107,7 @@
private Table createTeamTable() {
teamTable = new Table(null, teamSource);
- teamTable.setVisibleColumns(new Object[]
{ "name", "crestImage", "webAddress" });
+ teamTable.setVisibleColumns(new Object[] { "name", "image", "webAddress"
});
teamTable.setColumnHeaders(new String[] { "Name", "Crest", "Web" });
teamTable.setSelectable(true);
teamTable.setSizeFull();
@@ -131,8 +133,13 @@
@Override
public void buttonClick(ClickEvent event) {
- addWindow(new TeamEditWindow(getApplication(), MainWindow.this, "New
team", new Team(), client
- .getService(TeamAdministration.class)));
+ try {
+ addWindow(new TeamEditWindow(getApplication(), MainWindow.this, "New
team", new Team(), client
+ .getService(TeamAdministration.class)));
+ } catch (IOException e) {
+ log.error(e.getMessage(), e);
+ showNotification("Unable to create file for team crest image.",
Notification.TYPE_ERROR_MESSAGE);
+ }
}
});
@@ -147,9 +154,13 @@
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)));
- ;
+ try {
+ addWindow(new TeamEditWindow(getApplication(),
MainWindow.this, "Edit team", selectedTeam, client
+ .getService(TeamAdministration.class)));
+ } catch (IOException e) {
+ log.error(e.getMessage(), e);
+ showNotification("Unable to create file for team crest image.",
Notification.TYPE_ERROR_MESSAGE);
+ }
} else
showNotification("No team selected!");
}
=======================================
---
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleVaadin/src/main/java/org/mozzes/application/example/vaadin/gui/TeamEditWindow.java
Sat Feb 27 09:32:16 2010
+++
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleVaadin/src/main/java/org/mozzes/application/example/vaadin/gui/TeamEditWindow.java
Wed Mar 3 13:56:07 2010
@@ -21,12 +21,14 @@
package org.mozzes.application.example.vaadin.gui;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileOutputStream;
+import java.io.IOException;
import java.io.OutputStream;
+import org.apache.log4j.Logger;
import org.mozzes.application.example.common.domain.Team;
import org.mozzes.application.example.common.service.Administration;
-import org.mozzes.application.example.server.service.ImageUtils;
import com.vaadin.Application;
import com.vaadin.terminal.FileResource;
@@ -48,35 +50,36 @@
public class TeamEditWindow extends Window {
private static final long serialVersionUID = 3432589583281572169L;
+ private static final Logger log = Logger.getLogger(TeamEditWindow.class);
+ private static final String IMAGE_PATH = ".." + File.separatorChar
+ "images" + File.separatorChar;
+ private static final String IMAGE_FILE_TYPE = "png";
private final Application application;
private final MainWindow mainWindow;
private final Administration<Team> administration;
- private FormLayout form;
-
- private Team team;
- private Upload imageUpload;
+ private final Team team;
+
private TextField nameField = new TextField("Name");
private TextField webField = new TextField("Web");
private Embedded crestImage;
- private ImageUploadListener uploadReceiver;
+ private String crestFileName;
TeamEditWindow(Application application, MainWindow mainWindow, String
title, Team team,
- Administration<Team> administration) {
+ Administration<Team> administration) throws IOException {
super(title);
setModal(true);
this.application = application;
this.mainWindow = mainWindow;
this.administration = administration;
+ this.team = team;
setTeam(team);
- setImageUpload(team);
-
- form = new FormLayout();
+
+ FormLayout form = new FormLayout();
form.setSizeUndefined();
form.addComponent(nameField);
form.addComponent(webField);
form.addComponent(crestImage);
- form.addComponent(imageUpload);
+ form.addComponent(createImageUpload());
Button saveButton = new Button("Save");
saveButton.addListener(new ClickListener() {
@@ -84,7 +87,12 @@
@Override
public void buttonClick(ClickEvent event) {
- save();
+ try {
+ save();
+ } catch (IOException e) {
+ log.error(e.getMessage(), e);
+ showNotification("Unable to save team!",
Notification.TYPE_ERROR_MESSAGE);
+ }
}
});
@@ -101,65 +109,99 @@
setContent(layout);
}
- private void setTeam(Team team) {
- this.team = team;
+ private void setTeam(Team team) throws IOException {
nameField.setValue(team.getName());
webField.setValue(team.getWebAddress());
- if (team.getCrestImage() != null)
- crestImage = new Embedded("Crest", new FileResource(new
File(team.getCrestImage()), application));
+
+ crestFileName = IMAGE_PATH + "t" + Thread.currentThread().hashCode()
+ + System.currentTimeMillis() + "." + IMAGE_FILE_TYPE;
+ if (team.getImage() != null) {
+ saveImage(team.getImage());
+ crestImage = new Embedded("Crest", new FileResource(new
File(crestFileName), application));
+ }
else
crestImage = new Embedded("Crest");
crestImage.requestRepaint();
}
- private void setImageUpload(Team team) {
- uploadReceiver = new ImageUploadListener(team);
- imageUpload = new Upload(null, uploadReceiver);
+ private Upload createImageUpload() {
+ ImageUploadListener uploadReceiver = new ImageUploadListener();
+ Upload imageUpload = new Upload(null, uploadReceiver);
imageUpload.addListener((Upload.SucceededListener) uploadReceiver);
imageUpload.addListener((Upload.FailedListener) uploadReceiver);
+ return imageUpload;
}
- private void save() {
+ private void save() throws IOException {
team.setName(String.valueOf(nameField.getValue()));
team.setWebAddress(String.valueOf(webField.getValue()));
- if (uploadReceiver.currentFileName != null)
- team.setCrestImage(uploadReceiver.currentFileName);
+ team.setImage(loadImage());
administration.save(team);
mainWindow.removeWindow(this);
mainWindow.reloadTeams();
}
+
+ private void saveImage(byte[] image) throws IOException {
+ File file = new File(crestFileName);
+ FileOutputStream fos = null;
+ try {
+ fos = new FileOutputStream(file);
+ fos.write(image);
+ fos.flush();
+ } finally {
+ if (fos != null)
+ try {
+ fos.close();
+ } catch (IOException ignore) {}
+ }
+ }
+
+ private byte[] loadImage() throws IOException {
+ File file = new File(crestFileName);
+ if (!file.exists())
+ return null;
+ byte[] returnValue = new byte[(int) file.length()];
+ FileInputStream fos = null;
+ try {
+ fos = new FileInputStream(file);
+ fos.read(returnValue);
+ return returnValue;
+ } finally {
+ if (fos != null)
+ try {
+ fos.close();
+ } catch (IOException ignore) {}
+ }
+
+ }
private class ImageUploadListener implements Upload.SucceededListener,
Upload.FailedListener, Upload.Receiver {
private static final long serialVersionUID = 7572703698105220338L;
- private final Team team;
private File file;
- private String currentFileName;
-
- public ImageUploadListener(Team team) {
- this.team = team;
- }
@Override
- public OutputStream receiveUpload(String filename, String MIMEType) {
- if (MIMEType.indexOf("image") == -1)
+ public OutputStream receiveUpload(String filename, String MIMEType) {
+ if (MIMEType.indexOf("image") == -1) {
+ showNotification("Invalid image type, only " + IMAGE_FILE_TYPE + "
images are allowed!", Notification.TYPE_ERROR_MESSAGE);
return null;
-
+ }
+
+ if (!MIMEType.substring(MIMEType.indexOf('/') +
1).equalsIgnoreCase(IMAGE_FILE_TYPE)) {
+ showNotification("Invalid image type, only " + IMAGE_FILE_TYPE + "
images are allowed!", Notification.TYPE_ERROR_MESSAGE);
+ return null;
+ }
+
+ file = new File(crestFileName);
FileOutputStream fos = null; // Output stream to write to
- if (team.getCrestImage() != null)
- currentFileName = team.getCrestImage();
- else
- currentFileName = ImageUtils.generateImageFile() + "." +
MIMEType.substring(MIMEType.indexOf('/') + 1);
- file = new File(currentFileName);
-
try {
- // Open the file for writing.
+ file.createNewFile();
fos = new FileOutputStream(file);
- } catch (final java.io.FileNotFoundException e) {
+ } catch (IOException e) {
// Error while opening the file. Not reported here.
- e.printStackTrace();
+ log.error("Unable to open team crest file", e);
return null;
}
return fos; // Return the output stream to write to