Package de.chris_soft.fyllgen

Source Code of de.chris_soft.fyllgen.Main

/**
* 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;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import de.chris_soft.fyllgen.data.Family;
import de.chris_soft.fyllgen.data.LastOpenedFamilyFile;
import de.chris_soft.fyllgen.menu.file.OpenFile;
import de.chris_soft.fyllgen.utilities.SwtConsts;
import de.chris_soft.fyllgen.utilities.SwtUtilities;

/**
* Start-Klasse. Baut das SWT-Fenster auf, liest schon mal eine Familiendatei
* ein und startet somit die Anwendung.
* @author Christian Packenius, Juni 2008.
*/
public class Main {
  /**
   * @param args Ungenutzte Kommandozeilenargumente.
   * @throws IOException
   */
  public static void main(String[] args) throws IOException {
    try {
      new Main(args);
    }
    catch (Throwable exception) {
      // Die Exception auf der Konsole, als Meldungsfenster und in einer Datei
      // ausgeben.
      exception.printStackTrace();
      FileOutputStream out = new FileOutputStream("Exception-" + System.currentTimeMillis() + ".err");
      exception.printStackTrace(new PrintStream(out));
      out.close();
      viewThrowable(exception);
    }
    finally {
      SwtConsts.display.dispose();
    }
  }

  /**
   * Gibt die das Programm beendende Exception in einem Meldungsfenster aus.
   */
  private static void viewThrowable(Throwable exception) {
    Shell shell = new Shell(SwtConsts.display);
    String msg = "Es ist eine Exception aufgetreten!";
    msg += Statics.CRLF + exception.getMessage();
    msg += Statics.CRLF + "Das Programm wird beendet!";
    msg += Statics.CRLF + "Die Exception wird in eine Datei geschrieben...";
    SwtUtilities.sayError(shell, msg);
    shell.dispose();
  }

  /**
   * Konstruktor. Startet das Programm.
   * @param args Argumente von der Kommandozeile (noch ungenutzt).
   * @throws Exception
   */
  public Main(String[] args) throws Exception {
    // Programm initialisieren und Fenster �ffnen.
    Display display = SwtConsts.display;
    Shell shell = GUI.instance.shell;
    shell.open();

    // Ungl�cklich, dies hier aufzurufen, aber aktuell besteht keine bessere
    // Idee (als ansonsten die gesamte GUI zu ver�ndern).
    GUI.instance.saveInputFieldsCompositeHeight();

    // Letzte offene Familiendatei einlesen.
    readLastFamilyFileAndMergeFile();

    // Warten bis TopLevel-Shell geschlossen wird
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }

    // Optionen und aktuelle Familie abspeichern.
    // Consts.getOptionData().saveOptions();
    Family.instance.deleteUnzippedFiles();
  }

  /**
   * Konstruktor. Initialisierungen.
   */
  public void readLastFamilyFileAndMergeFile() {
    // Personendaten einlesen.
    while (Family.instance.getPersonsArray().length == 0) {
      try {
        Family.instance.readPersons(LastOpenedFamilyFile.getLastOpenedFamilyFile());
        break;
      }
      catch (IOException exception) {
        if (!askForFamilyFileName(true)) {
          break;
        }
      }
    }

    // Falls es eine zu verkn�pfende Datei gibt, diese ebenfalls laden.
    if (new File(Statics.FAMILY2MERGE_ZIP).exists()) {
      Statics.mergeFamily = new Family();
      try {
        Statics.mergeFamily.loader.load(Statics.FAMILY2MERGE_ZIP);
      }
      catch (IOException exception) {
        SwtUtilities.sayError(GUI.instance.shell, "Es gab Probleme beim Laden der Merge-Daten!");
      }
    }
  }

  /**
   * Fragt den Anwender, ob er eine neue Familiendatei er�ffnen m�chte oder eine
   * bestehende �ffnen will.
   * @param bPreError Angabe, ob vorher ein Ladefehler stattfand.
   * @return Angabe, ob die Familiendatei schon existiert.
   */
  private boolean askForFamilyFileName(boolean bPreError) {
    String quest = "";
    if (bPreError) {
      quest = "Es gab einen Fehler beim Laden der Familiendatei!\r\n";
    }
    quest += "M�chten Sie eine neue Familiendatei erstellen?";
    if (SwtUtilities.askYesNo(GUI.instance.shell, quest, "Anfrage")) {
      // Eine noch nicht existierende Familiendatei verwenden.
      int id = 0;
      String familyname;
      while (new File(familyname = "family-" + id + ".zip").exists()) {
        id++;
      }
      LastOpenedFamilyFile.setLastOpenedFamilyFile(familyname);

      // Hier wird eine Dummy-Person angelegt.
      Family.instance.setCurrentPerson(Family.instance.getFirstPerson(), 2);
      Family.instance.setChanged(false);

      return false;
    }

    OpenFile.familyFileChoice();
    return true;
  }
}
TOP

Related Classes of de.chris_soft.fyllgen.Main

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.