/**
* FyLLGen - A Java based tool for collecting and distributing family data
*
* Copyright (C) 2007-2011 Christian Packenius
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.chris_soft.fyllgen.menu.edit;
import java.io.File;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import de.chris_soft.fyllgen.GUI;
import de.chris_soft.fyllgen.data.Family;
import de.chris_soft.fyllgen.data.Person;
import de.chris_soft.fyllgen.utilities.SwtUtilities;
import de.chris_soft.fyllgen.widget.dialog.ImageCutDialog;
import de.chris_soft.fyllgen.widget.dialog.PersonChoiceShell;
/**
* F�gt der Person ein Bild hinzu.
* @author Christian Packenius, 20110905.
*/
public class AddImageToPerson implements Listener {
/**
* @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
*/
public void handleEvent(Event event) {
// Erst einmal nach der Bilddatei suchen.
FileDialog fd = new FileDialog(GUI.instance.shell, SWT.OPEN);
fd.setFilterExtensions(new String[] { "*.*", "*.png", "*.jpg", "*.tif" });
fd.setFilterNames(new String[] { "Any Files", "PNG - Portable Network Graphics",
"JPEG - Joint Photographic Experts Group", "TIFF - Tagged Image File Format" });
fd.setFilterPath(".");
fd.setText("Bilddatei �ffnen");
String filename = fd.open();
if (filename != null) {
askForImagePerson(new File(filename));
}
}
/**
* Fragt nach, welcher Person diese Datei als Bilddatei zugeordnet werden
* soll, nachdem die Bilddatei zurechtgestutzt wurde.
*/
private void askForImagePerson(final File file) {
Display.getDefault().syncExec(new Runnable() {
public void run() {
String imagename = file.getName();
try {
while (true) {
imagename = cutImage(imagename);
if (imagename != null) {
Family family = Family.instance;
Person currentPerson = family.getCurrentPerson();
String cpName = currentPerson.getValueView(Person.NAME);
String quest = "Soll das Portrait\r\n" + cpName + "\r\nzugeordnet werden?";
if (SwtUtilities.askYesNo(GUI.instance.shell, quest, "Portraitzuordnung")) {
currentPerson.addImage(imagename);
family.review();
}
else {
PersonChoiceShell pcs = new PersonChoiceShell(GUI.instance.shell, "Bild-Person festlegen",
"Bild zuordnen", null, 1, false, new File("images/" + imagename), true);
// int p = imagename.lastIndexOf('.');
// pcs.setStartString(imagename.substring(0, p).replace('_',
// ' '));
pcs.open();
Person person = pcs.personChoice;
if (person != null) {
person.addImage(imagename);
family.setCurrentPerson(person, 1);
}
else {
new File("images/" + imagename).delete();
break;
}
}
}
else {
break;
}
}
}
catch (IOException e) {
// Ignorieren - dann findet keine Personenzuordnung statt.
}
}
private String cutImage(String imagename) throws IOException {
ImageCutDialog icd = new ImageCutDialog(GUI.instance.shell, file);
icd.open();
return icd.newImageName;
}
});
}
}