/**
* 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.file;
import java.io.File;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import de.chris_soft.fyllgen.GUI;
import de.chris_soft.fyllgen.data.Family;
import de.chris_soft.fyllgen.data.LastOpenedFamilyFile;
/**
* Speichert die Familiendatei unter einem anderen Namen ab.
* @author Christian Packenius, Juli 2008.
*/
public class SaveFamilyFileAs implements Listener {
/**
* Zuerst nach neuem Namen fragen, dann darunter abspeichern.
* @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
*/
public void handleEvent(Event arg0) {
FileDialog fd = new FileDialog(GUI.instance.shell, SWT.SAVE);
fd.setFilterExtensions(new String[] { "*.zip" }); // fgf == FyLLGenFamily
fd.setFilterNames(new String[] { "Gepackte Familiendatei" });
fd.setFilterPath(".");
fd.setText("Unter welchem Dateinamen soll die Familiendatei gespeichert werden?");
String result = fd.open();
if (result != null) {
File f = new File(result);
if (f.exists()) {
MessageBox mb = new MessageBox(GUI.instance.shell, SWT.YES | SWT.NO | SWT.ICON_QUESTION);
mb.setMessage("Die angegebene Datei existiert schon.\r\nSoll sie �berschrieben werden?");
mb.setText("Datei existiert");
int answer = mb.open();
if (answer == SWT.YES) {
save(result);
}
}
else {
save(result);
}
}
}
/**
* Speichert die Datei unter dem angegebenen Namen ab.
* @param filename
*/
private void save(String filename) {
try {
Family.instance.writePersons(filename);
Family.saveMergeData();
LastOpenedFamilyFile.setLastOpenedFamilyFile(filename);
GUI.instance.setShellTitle();
}
catch (IOException exception) {
MessageBox mb = new MessageBox(GUI.instance.shell, SWT.OK | SWT.ICON_ERROR);
mb.setMessage("Beim Speichern hat es einen Fehler gegeben!\r\n" + exception.getMessage());
mb.setText("Dateifehler");
mb.open();
}
}
}