/**
* 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.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import de.chris_soft.fyllgen.GUI;
import de.chris_soft.fyllgen.Statics;
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.data.Relationship;
import de.chris_soft.fyllgen.data.RelationshipParentChild;
import de.chris_soft.fyllgen.utilities.FamilyUtilities;
import de.chris_soft.fyllgen.utilities.SwtUtilities;
import de.chris_soft.fyllgen.widget.FamilyComposite;
import de.chris_soft.fyllgen.widget.FamilyModelShowPersonsConnection;
import de.chris_soft.fyllgen.widget.dialog.PersonChoiceShell;
/**
* Ermittelt zu einer beliebigen Person die k�rzeste Verbindung zur aktuellen
* Person.
* @author Christian Packenius, Juli 2008.
*/
public class GetShortestConnection implements Listener {
/**
* Aktuelle und gesuchte Person. Zwischen diesen beiden wird die Verbindung
* bestimmt.
*/
private Person currentPerson;
/**
* Person, zu der die k�rzeste Verbindung gesucht wird.
*/
private Person searchPerson;
/**
* K�rzeste Verbindung suchen.
* @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
*/
public void handleEvent(Event event) {
// Zuerst vom Anwender die zweite Person holen (erste ist die aktuelle).
currentPerson = Family.instance.getCurrentPerson();
PersonChoiceShell pcs = new PersonChoiceShell(GUI.instance.shell, "Verbindung suchen", "Verbindung suchen", null,
1, false, null, false);
pcs.removePerson(currentPerson);
pcs.open();
// Dann die Verbindung suchen.
if (pcs.personChoice != null) {
// Es wurde eine vorhandene Person angew�hlt. Nun die Verbindung zu dieser
// suchen.
searchPerson = pcs.personChoice;
List<Person> listFound = search();
if (listFound != null && listFound.size() > 0) {
// Alle Personen ggf. in einer Datei ausgeben.
if (OptionData.instance.getBoolean(OptionData.WRITE_SHORT_CONNECTIONS_INTO_FILE)) {
writeShortConnectionIntoFile(listFound);
}
FamilyComposite familyComposite = GUI.instance.getFamilyComposite();
familyComposite.setFamilyModel(new FamilyModelShowPersonsConnection(familyComposite, listFound));
}
else {
String msg = "Es besteht keine Verbindung zwischen " + currentPerson.getValue(Person.NAME);
msg += " und " + searchPerson.getValue(Person.NAME) + "!";
SwtUtilities.sayInfo(GUI.instance.shell, msg);
}
}
}
/**
* Schreibt alle Personen dieser "k�rzesten Verbindung" in eine Datei.
*/
private void writeShortConnectionIntoFile(List<Person> listFound) {
try {
new File("Short Connections").mkdir();
PrintStream out = new PrintStream(new FileOutputStream("Short Connections/" + Statics.getUniqueID() + ".txt"));
Person lastPerson = null;
for (Person person : listFound) {
if (lastPerson != null) {
Relationship relship = lastPerson.getRelationship(person);
if (relship instanceof RelationshipParentChild) {
if (relship.partner1 == lastPerson) {
out.println("...ist Elternteil von...");
}
else {
out.println("...ist Kind von...");
}
}
else {
out.println("...ist Partner von...");
}
}
out.println(person.toString());
lastPerson = person;
}
out.close();
}
catch (IOException exception) {
// Egal, dann halt nicht speichern.
}
}
/**
* Sucht anhand der angegebenen Person weiter nach der Verbindung. Falls
* gefunden, wird true zur�ck gegeben, sonst false.
*/
private List<Person> search() {
List<Person> persons = new ArrayList<Person>(Arrays.asList(Family.instance.getPersonsArray()));
Map<Person, Person> map = FamilyUtilities.createNextPersonsMap(currentPerson, persons);
if (map.containsKey(searchPerson)) {
persons.clear();
Person p0 = searchPerson;
while (p0 != currentPerson) {
persons.add(p0);
p0 = map.get(p0);
}
persons.add(p0);
return persons;
}
return null;
}
}