/**
* 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.search;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Event;
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.Person;
import de.chris_soft.fyllgen.utilities.PersonListSort;
import de.chris_soft.fyllgen.widget.dialog.PersonChoiceShell;
/**
* Sucht die n�chstgelegenen Personen, die eine eingetragene Mail-Adresse haben.
* @author Christian Packenius, Juli 2008.
*/
public class SearchNextMailPersons implements Listener {
/**
* Zeigt in einem Dialog diejenigen Personen an, die nahe gelegen sind und
* eine Mail-Adresse eingetragen haben.
* @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
*/
public void handleEvent(Event event) {
// Liste aller Personen, zu denen wir schon eine Verbindung aufgebaut haben.
List<Person> allConnections = new ArrayList<Person>();
Person currentPerson = Family.instance.getCurrentPerson();
allConnections.add(currentPerson);
// Liste aller Personen, die auch eine Mail-Adresse besitzen.
List<Person> mailPersons = new ArrayList<Person>();
List<String> mailAddresses = new ArrayList<String>();
// Alle Personen durchsuchen.
int i = 0;
while (i < allConnections.size()) {
Person person = allConnections.get(i++);
String mailAddress = person.getValueView(Person.MAIL).trim();
if (mailAddress.length() > 0) {
mailPersons.add(person);
mailAddresses.add(mailAddress);
}
addPersonsToList(allConnections, person.getChildren());
addPersonsToList(allConnections, person.getParents());
addPersonsToList(allConnections, person.getPartner());
}
saveMailAddressList(mailAddresses);
if (mailPersons.size() == 0) {
MessageBox mb = new MessageBox(GUI.instance.shell, SWT.ICON_INFORMATION | SWT.OK);
mb.setMessage("Leider konnte keine Person mit eingetragener E-Mail-Adresse\r\nund einer Verbindung zu "
+ currentPerson.getValue(Person.NAME) + " gefunden werden.");
mb.setText("Nicht gefunden");
mb.open();
return;
}
PersonListSort.sort(mailPersons);
// Shell erzeugen und anzeigen.
PersonChoiceShell pcs = new PersonChoiceShell(GUI.instance.shell, "N�chste Person mit E-Mail-Adresse", "Anzeigen",
null, 1, true, null, false);
for (int k = 0; k < mailPersons.size(); k++) {
Person person = mailPersons.get(k);
String mailAddress = person.getValueView(Person.MAIL);
pcs.addPerson(person, "(" + mailAddress + ")");
}
pcs.personChoice = currentPerson;
pcs.open();
if (pcs.personChoice != null) {
Family.instance.setCurrentPerson(pcs.personChoice, 1);
}
}
private void saveMailAddressList(List<String> mailAddresses) {
String mailAddressListFileName = "extra/mail-address-list.txt";
try {
PrintStream out = new PrintStream(new File(mailAddressListFileName));
for (String mail : mailAddresses) {
out.println(mail);
}
out.close();
}
catch (IOException e) {
// Ignorieren.
new File(mailAddressListFileName).delete();
e.printStackTrace();
}
}
/**
* F�gt alle angegebenen Personen der Liste hinzu.
* @param allConnections
* @param persons
*/
private void addPersonsToList(List<Person> allConnections, Person[] persons) {
for (Person person : persons) {
if (!allConnections.contains(person)) {
allConnections.add(person);
}
}
}
}