/**
* 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.widget.dialog;
import java.util.Arrays;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import de.chris_soft.fyllgen.GUI;
import de.chris_soft.fyllgen.data.Family;
import de.chris_soft.fyllgen.data.OptionData;
import de.chris_soft.fyllgen.data.Person;
import de.chris_soft.fyllgen.export.DataExporter;
import de.chris_soft.fyllgen.export.PdfExporter;
import de.chris_soft.fyllgen.export.PerMailExporter;
import de.chris_soft.fyllgen.export.TextExporter;
import de.chris_soft.fyllgen.utilities.SwtConsts;
import de.chris_soft.fyllgen.utilities.SwtUtilities;
/**
* Dialog zum Exportieren von Daten in eine Datei und eventuellem anschlie�enden
* Mailversand.
* @author Christian Packenius, 20110209.
*/
public class ExportDataAsFileDialog extends Dialog implements SelectionListener {
/**
* Exakte Breite des Dialog-Fensters.
*/
private static final int SHELL_WIDTH = 330;
/**
* Liste aller Exporter, die Dateien aus den Familienstammdaten erzeugen.
*/
private static final DataExporter[] exporters = new DataExporter[] { new PdfExporter(), new TextExporter() };
/**
* Das interne Dialog-Fensterobjekt.
*/
final Shell shell;
/**
* OK-Button.
*/
private Button buttonOK;
/**
* Abbruch-Button.
*/
private Button buttonCancel;
/**
* Text-Feld f�r Dateiname.
*/
private Text textFilename;
/**
* ComboBox f�r das zu exportierende Format.
*/
private Combo comboFormat;
/**
* CheckBox, ob die Datei per E-Mail verschickt werden soll.
*/
private Button checkboxSendMail;
/**
* CheckBox, ob der Packenius/Vietor-Filter verwendet werden soll.
*/
private Button checkboxPVFilter;
/**
* CheckBox, ob eine Druckversion in schwarz/wei� und mit Personen-Indizes
* erstellt werden soll.
*/
private Button checkboxPrintVersion;
/**
* Angabe, ob die Daten zum Mailversand gezippt werden sollen oder nicht.
*/
private Button checkboxZipData;
/**
* Konstruktor. Aufbau des Dialog-Fensters.
* @param parent
*/
public ExportDataAsFileDialog(Shell parent) {
super(parent, 0);
shell = new Shell(parent, SWT.CLOSE | SWT.TITLE | SWT.APPLICATION_MODAL);
shell.setImage(GUI.instance.shellIcon);
shell.setText("Exportieren und Mailversand");
// Shell hat innen einen Rahmen.
FormLayout shellLayout = new FormLayout();
shellLayout.marginBottom = shellLayout.marginLeft = shellLayout.marginRight = shellLayout.marginTop = 5;
shell.setLayout(shellLayout);
shell.setSize(SHELL_WIDTH, 500);
// Zuerst die Controls in der richtigen Reihenfolge erzeugen.
// In diesem Composite liegt alles au�er den Buttons.
Composite comp2 = new Composite(shell, SWT.BORDER);
GridLayout comp2Layout = new GridLayout(2, false);
comp2.setLayout(comp2Layout);
Label label = new Label(comp2, SWT.NONE);
label.setText("Format: ");
comboFormat = new Combo(comp2, SWT.DROP_DOWN | SWT.READ_ONLY);
for (DataExporter exporter : exporters) {
comboFormat.add(exporter.getLongTitle());
}
comboFormat.select(0);
label = new Label(comp2, SWT.NONE);
label.setText("Dateiname: ");
textFilename = new Text(comp2, SWT.BORDER | SWT.SINGLE);
textFilename.setText("familienstammbaum");
label = new Label(comp2, SWT.NONE);
label.setText("");
checkboxSendMail = new Button(comp2, SWT.CHECK);
checkboxSendMail.setText(" Mailversand");
checkboxSendMail.setSelection(false);
label = new Label(comp2, SWT.NONE);
label.setText("");
checkboxPVFilter = new Button(comp2, SWT.CHECK);
checkboxPVFilter.setText(" Packenius/Vietor-Filter");
checkboxPVFilter.setSelection(true);
label = new Label(comp2, SWT.NONE);
label.setText("");
checkboxPrintVersion = new Button(comp2, SWT.CHECK);
checkboxPrintVersion.setText(" Druckversion (s/w) mit Indizes (nur PDF)");
checkboxPrintVersion.setSelection(false);
label = new Label(comp2, SWT.NONE);
label.setText("");
checkboxZipData = new Button(comp2, SWT.CHECK);
checkboxZipData.setText(" Komprimieren (ZIP)");
checkboxZipData.setSelection(true);
buttonOK = new Button(shell, SWT.PUSH);
buttonOK.addSelectionListener(this);
buttonOK.setText("OK");
shell.setDefaultButton(buttonOK);
buttonCancel = new Button(shell, SWT.PUSH);
buttonCancel.setText("Abbrechen");
buttonCancel.addSelectionListener(this);
// Dann die Layout-Daten zu den Controlls.
FormData fdOK = new FormData(80, SWT.DEFAULT);
fdOK.bottom = new FormAttachment(100, 0);
fdOK.right = new FormAttachment(buttonCancel, -5);
buttonOK.setLayoutData(fdOK);
FormData fdCancel = new FormData(80, SWT.DEFAULT);
fdCancel.bottom = new FormAttachment(100, 0);
fdCancel.right = new FormAttachment(100, 0);
buttonCancel.setLayoutData(fdCancel);
FormData fdComp2 = new FormData();
fdComp2.top = new FormAttachment(0, 0);
fdComp2.left = new FormAttachment(0, 0);
fdComp2.right = new FormAttachment(100, 0);
fdComp2.bottom = new FormAttachment(buttonCancel, -5);
comp2.setLayoutData(fdComp2);
GridData lgd = new GridData(SWT.LEFT, SWT.CENTER, false, false);
GridData cgd = new GridData(SWT.FILL, SWT.CENTER, true, false);
label.setLayoutData(lgd);
comboFormat.setLayoutData(cgd);
label.setLayoutData(lgd);
textFilename.setLayoutData(cgd);
label.setLayoutData(lgd);
checkboxSendMail.setLayoutData(cgd);
label.setLayoutData(lgd);
checkboxPVFilter.setLayoutData(cgd);
label.setLayoutData(lgd);
checkboxPrintVersion.setLayoutData(cgd);
label.setLayoutData(lgd);
checkboxZipData.setLayoutData(cgd);
shell.pack();
Point size = shell.getSize();
size.x = SHELL_WIDTH;
size.y += 30;
shell.setSize(size);
// Noch mittig ins vorhandene Fenster setzen.
SwtUtilities.centerInParent(shell, parent);
}
/**
* @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
*/
public void widgetDefaultSelected(SelectionEvent e) {
widgetSelected(e);
}
/**
* @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
*/
public void widgetSelected(SelectionEvent e) {
if (e.widget == buttonOK) {
export();
shell.close();
shell.dispose();
}
else if (e.widget == buttonCancel) {
shell.close();
shell.dispose();
}
}
/**
* �ffnen des Dialog-Fensters.
*/
public void open() {
List<Person> personList = Arrays.asList(Family.instance.getCurrentPersonsFamilyArray(true));
if (personList.isEmpty()) {
MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR | SWT.APPLICATION_MODAL);
mb.setText("Fehler!");
mb.setMessage("Es gibt keine zu exportierende Familie!");
mb.open();
}
else {
Person p0 = personList.get(0);
String mail = p0.getValue(Person.MAIL);
String mailer = OptionData.instance.getString(OptionData.EXPORT_MAIL);
if (mail == null || mail.trim().length() == 0 || mail.trim().contains(" ") || !mail.contains("@")
|| mailer == null || mailer.trim().contains(" ") || !mailer.contains("@")) {
checkboxSendMail.setSelection(false);
checkboxSendMail.setEnabled(false);
}
shell.open();
// Ereignis-Liste abarbeiten.
while (!shell.isDisposed()) {
if (!SwtConsts.display.readAndDispatch()) {
SwtConsts.display.sleep();
}
}
}
}
/**
* Erzeugt die Export-Datei.
*/
private void export() {
boolean shallUsePVFilter = checkboxPVFilter.getSelection();
boolean createPrintVersion = checkboxPrintVersion.getSelection();
boolean zipPDF = checkboxZipData.getSelection();
List<Person> personList = Arrays.asList(Family.instance.getCurrentPersonsFamilyArray(shallUsePVFilter));
String simpleFilename = textFilename.getText();
if (simpleFilename == null || simpleFilename.trim().length() == 0) {
simpleFilename = "familienstammbaum";
}
simpleFilename = simpleFilename.replace('*', 'x').replace('?', '-');
DataExporter exporter = exporters[comboFormat.getSelectionIndex()];
exporter.setCreatePrintVersion(createPrintVersion);
boolean shallSendMail = checkboxSendMail.getSelection();
// Mal nach dem Mailpasswort fragen, damit es direkt eingegeben wird.
// Ohne ist kein Mailversand m�glich.
if (shallSendMail) {
String mailPW = OptionData.instance.getExportPassword(true);
if (mailPW == null || mailPW.trim().length() == 0 || mailPW.trim().contains(" ")) {
SwtUtilities.sayInfo(GUI.instance.shell, "Ohne Mailpasswort ist kein Mailexport m�glich!");
}
}
PerMailExporter pme = new PerMailExporter(shallUsePVFilter, createPrintVersion, zipPDF, shallSendMail, personList,
simpleFilename, exporter, GUI.instance.shell, false);
pme.export();
}
}