FileDocCategorySizeDatePackage
FamilyFrame.javaAPI DocExample17623Tue Dec 08 01:21:00 GMT 1998HumanInterface

FamilyFrame.java

// Copyright 1997 Object UK Ltd.
// Author Andrew Carmichael
// Updated Tim Shelley
//
// Date
// Version 0.2
// Email: objectuk@objectuk.co.uk
// Web: http://www.objectuk.co.uk

package HumanInterface;

// Generated by Together/J

import java.awt.*;
import java.io.*;
import java.lang.*;
import java.awt.event.*;
import java.util.*;
import ProblemDomain.*;
import DataManagement.FlatFile.*;

/**@note This is the main frame which is shown when the application is 
run.  It consists of a frame, main menu and text area for 
displaying details.
*/
public class FamilyFrame extends Frame implements java.awt.event.ActionListener{

  ListDialog listDlg; 
  AboutBox aboutBox;
  InEntry entryDlg;
  Search searchDlg;  
         
/** @shapeType AggregationLink 
@associates <b>Individual</b> */  
  Hashtable people = null;

/** @shapeType AggregationLink 
@associates <b>Family</b> */ 
  Hashtable families = null;         

  DataManagement.DMControl dmControl;
  
//  Family baseFamily = null;

  int listType = 1;
  FlatFile fileType;

/* GUI components */
  public TextArea textArea = new TextArea();
  private BorderLayout borderLayout = new BorderLayout();

  /** Menu class instantations:
      Main menu bar */
  private MenuBar menuBar = new MenuBar();

  /** Menu class instantations:
      Main menu options: File */
  private Menu menuFile = new Menu();
  /** Menu class instantations:
      Main menu options: Edit */
  private Menu menuEdit = new Menu();
  /** Menu class instantations:
      Main menu options: View */
  private Menu menuView = new Menu();
  /** Menu class instantations:
      Main menu options: Help */
  private Menu menuHelp = new Menu();

  /** Menu class instantations:
      Menu items: Open */
  private MenuItem menuOpen = new MenuItem();
  /** Menu class instantations:
      Menu items: Open */
  private MenuItem menuSave = new MenuItem();
  /** Menu class instantations:
      Menu items: Close */
  private MenuItem menuClose = new MenuItem();
  /** Menu class instantations:
      Menu items: Exit */
  private MenuItem menuExit = new MenuItem();
  /** Menu class instantations:
      Menu items: New Individual */
  private MenuItem menuNewIndividual = new MenuItem();
  /** Menu class instantations:
      Menu items: Search */
  private MenuItem menuSearch = new MenuItem();  
  /** Menu class instantations:
      Menu items: Descendents */
  private MenuItem menuDescendents = new MenuItem();
  /** Menu class instantations:
      Menu items: Ancestors */
  private MenuItem menuAncestors = new MenuItem();
  /** Menu class instantations:
      Menu items: About */
  private MenuItem menuAbout = new MenuItem();

  private CheckboxMenuItem checkShowSpouse = new CheckboxMenuItem();

  /** Construct the frame
  */
  public FamilyFrame(DataManagement.DMControl dmControl) {
    this.dmControl = dmControl;
    dmControl.setTarget(this);
    try {
      setup();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

/** @note Component initialization.*/
  public void setup() throws Exception{
    this.setLayout(borderLayout);
    this.setSize(new Dimension(529, 327));
    this.setTitle("Family Tree");
    menuFile.setLabel("File");
    menuOpen.setLabel("Open");
    menuSave.setLabel("Save as");
    menuClose.setLabel("Close");
    menuExit.setLabel("Exit");
    menuEdit.setLabel("Edit");
    menuNewIndividual.setLabel("New Individual");
    menuView.setLabel("View");
    menuSearch.setLabel("Find");
    menuDescendents.setLabel("Descendants");
    menuAncestors.setLabel("Ancestors");
    checkShowSpouse.setLabel("Show Spouse");
    menuHelp.setLabel("Help");
    menuAbout.setLabel("About");

    menuAbout.addActionListener(this);
    menuOpen.addActionListener(this);
    menuSave.addActionListener(this);
    menuClose.addActionListener(this);
    menuExit.addActionListener(this);
    menuNewIndividual.addActionListener(this);
    menuSearch.addActionListener(this);
    menuDescendents.addActionListener(this);
    menuAncestors.addActionListener(this);

    menuBar.add(menuFile);
    menuBar.add(menuEdit);
    menuBar.add(menuView);
    menuBar.add(menuHelp);

    menuFile.add(menuOpen);
    menuFile.add(menuSave);
    menuFile.add(menuClose);
    menuFile.add(menuExit);
    menuEdit.add(menuNewIndividual);
    menuView.add(menuSearch);
    menuView.add(menuDescendents);
    menuView.add(menuAncestors);
    menuView.add(checkShowSpouse);
    menuHelp.add(menuAbout);

    this.setMenuBar(menuBar);
    this.addWindowListener(new windowEventHandler(this));
    textArea.setEditable(false);
    this.add(textArea, BorderLayout.CENTER);
    testData();
  }
                  
/**@note Creates the Search dialog and displays on the screen.*/
  void find() {    
    searchDlg = new Search();
    searchDlg.setTarget(this);
    searchDlg.setVisible(true);
  }
  
/**@note The Search Dialog directly calls this method once you have 
entered a search string*/
  public void search() { 
    Vector found = new Vector();
    boolean flag;
    Individual person;
    if(searchDlg.getSearchName().length() > 0) {
      String name = searchDlg.getSearchName().toLowerCase();
      Enumeration list = people.keys();
      while(list.hasMoreElements()) {
        person = (Individual)people.get(list.nextElement());
        if(person.getFamilyName().toLowerCase().equals(name) || person.getGivenNames().toLowerCase().equals(name))
          found.addElement(person);
      }
      listDlg = new ListDialog(this);
      listDlg.setTitle("List of Individuals...");
      listDlg.setData(found);
      listDlg.setTarget(this);
      Dimension frmSize = getSize();
      Point loc = getLocation();
      listDlg.setLocation((frmSize.width - 180)/ 2 + loc.x, (frmSize.height - 300) / 2 + loc.y);
      listDlg.setVisible(true);
    }
  }
  
/**@note This method sets up test data by creating Individuals and
placing them into Families.*/
  void testData() {
    Family f;

    people = new Hashtable();
    families = new Hashtable();
    people.put(new Integer(1),Individual.testObject());
    people.put(new Integer(2),Individual.testObject2());
    people.put(new Integer(3),Individual.testObject3());
    people.put(new Integer(4),Individual.testObject4());
    people.put(new Integer(5),Individual.testObject5());
    people.put(new Integer(6),Individual.testObject6());

    f = new Family(1,
    (Individual)people.get(new Integer(1)), (Individual)people.get(new Integer(2)),
      Marriage.testObject(), Divorce.testObject());
    f.addChild((Individual)people.get(new Integer(3)));
    f.addChild((Individual)people.get(new Integer(4)));

    families.put(new Integer(1),f);

    f = new Family(2,
    (Individual)people.get(new Integer(3)), (Individual)people.get(new Integer(5)),
      Marriage.testObject(), Divorce.testObject());
    f.addChild((Individual)people.get(new Integer(6)));

    families.put(new Integer(2),f);
    dmControl.setUpRecords(families,people);
  }

/** @note Exit action performed */
  public void exit() {
    System.exit(0);
  }

/**@note Close and empty the Family and Individual lists, as well as 
resetting the reference numbers.*/
  void close() {
    people = new Hashtable();
    families = new Hashtable();
    Individual temp1 = new Individual();
    temp1.reset();
    Family temp2 = new Family();
    temp2.reset();
//    baseFamily = new Family();
  }

/**@note The method to handle opening the data file, the actual reading
in of the data is left to the FlatFile object created within
this method.*/
  void openFile() {
    FileDialog fDialog = new FileDialog(this,"Open...",FileDialog.LOAD);
    fDialog.setFile("*.ged");
    fDialog.show();
    if(fDialog.getFile() != null) {
      close();
      dmControl.readData(fDialog.getFile());
      families = dmControl.getFamilies();
      people = dmControl.getIndividuals();
    }
  }

/**@note Using the string representation from ther Inidivudal and Family 
objects, this method can write out records and fields to a data 
file.*/
  public void saveFile() {
    FileDialog fDialog = new FileDialog(this,"Save as...",FileDialog.SAVE);
    fDialog.show();
    if(fDialog.getFile() != null) {
/*      try {
        BufferedWriter out = new BufferedWriter(new FileWriter(fDialog.getFile()));
        textArea.setText("Saving as " + fDialog.getFile());
        Enumeration list = people.keys();
        while(list.hasMoreElements()) {
          out.write(((Individual)people.get(list.nextElement())).toString("0"));
        }
        list = families.keys();
        while(list.hasMoreElements()) {
          out.write(((Family)families.get(list.nextElement())).toString("0"));
        }
        out.write("0 TRLR");
        out.close();
        textArea.appendText("\nFinished saving file....");
      }
      catch(FileNotFoundException ex) {}
      catch(IOException exe) {}*/
      dmControl.saveData(fDialog.getFile());
    }
  }

/** @note Help | About action performed */
  public void showAboutBox() {
    aboutBox = new AboutBox(this);
    Dimension frmSize = getSize();
    Point loc = getLocation();
    aboutBox.setLocation((frmSize.width - 216)/ 2 + loc.x, (frmSize.height - 100) / 2 + loc.y);
    aboutBox.setModal(true);
    aboutBox.show();
  }

/*  public void setDMControl(DataManagement.DMControl dmControl) {
    this.dmControl = dmControl;
    dmControl.setTarget(this);
  }*/

/**@note Method to handle the action events for the menu options.*/
  public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand();
//    System.out.println(command);
    if(command.equals("Open")) openFile();
    if(command.equals("Save as")) saveFile();
    if(command.equals("Close")) close();
    if(command.equals("Exit")) exit();
    if(command.equals("About")) showAboutBox();
    if(command.equals("New Individual")) newIndividual();
    if(command.equals("Find")) find();
    if(command.equals("Descendants")) getDescendants();
    if(command.equals("Ancestors")) getAncestors();
  }

/**@note This method is called from ListDialog, and displays in the Text 
area the Individuals family details.*/
  void viewFamily() {
    Individual person;
    Enumeration list;
    Family family;
    person = listDlg.getCurrent();  
    if(person != null) {
      textArea.setText(" ");
      family = person.getChildOf();
      if(family != null) textArea.setText(family.getDetails());
      if(person.isParent() == true) {
        list = person.getSpouseInList();
        if(list != null) {
          while(list.hasMoreElements()) {
            textArea.setText(textArea.getText() + "\n" + ((Family)list.nextElement()).getDetails());
          }   
        }
      }
    }
  }

/**@note This method is called from ListDialog, and displays in the Text 
area the Individuals details.*/  
  void viewIndividual() {
    Individual person;

    person = listDlg.getCurrent();
    if(person != null)
    {
      textArea.setText(person.getDetails());
    }
    else
      textArea.setText("Please select an individual before choosing to view their details or family...");
  }
                     
/**@note This method creates a list of ancestors to be displayed in the Text area.*/  
  void showAncestors() {
    Vector list = null;
    String name;
    String test;

    textArea.setText(" ");
    Individual person = listDlg.getCurrent();
    try {
      list = person.getChildOf().getAncestors(0);
    }catch(NullPointerException ex) {
      textArea.setText(person.getName() + " has no Ancestors");
    }
    if(list != null) {
      Enumeration loop = list.elements();
      textArea.setText("The Ancestors of " + person.getName() + "\n");
      while(loop.hasMoreElements()) {
        name = (String)loop.nextElement();
        textArea.setText(textArea.getText() + "\n" + name);
      }
    }
  }

/**@note This method is called from ListDialog when an element in the
list has been selected and clicked on.*/
  void listClick() {
    Enumeration list;
    String name;
    Individual found;
    Enumeration descendants;
    Vector alist;
    if(listType == 2) showAncestors();
    else {
      Individual person = listDlg.getIndividual();
      if(person.isParent()) {
        list = person.getSpouseInList();
        while(list.hasMoreElements()) {
          alist = ((Family)list.nextElement()).getDescendants(person.getName(),0,checkShowSpouse.getState());
          if(alist != null) {
            textArea.setText(" ");
            descendants = alist.elements();
            while(descendants.hasMoreElements()) {
              name = (String)descendants.nextElement();
              textArea.setText(textArea.getText() + name + "\n");
            }
            name = person.getGivenNames() + " " + person.getFamilyName().toLowerCase();
            textArea.setText("The Descendants of: " + name + "\n\n" + textArea.getText());
          }
        }
      }
      else textArea.setText("No Descendants found.");
    }
  }

/**@note Handle the window closing event by shutting down the entire 
application.*/
  void this_windowClosing(WindowEvent e) {
    System.exit(0);
  }

/**@note Create a list of individuals and pass them to the ListDialog
to be displayed.*/
  void getDescendants() {
    Vector buffer = null;
    Family t;
    Individual temp;
    Enumeration pers;
    String name;
    int count = 0;

    listType = 1;
    if(people != null) {
      listDlg = new ListDialog(this);
      listDlg.setTitle("List of Individuals...");
      pers = people.keys();
      while (pers.hasMoreElements()) {
        temp = (Individual)people.get((Integer)pers.nextElement());
        name = temp.getGivenNames() + " " + temp.getFamilyName();
        if(buffer == null) buffer = new Vector();
        buffer.addElement(temp);
        count++;
      }
      listDlg.setData(buffer);
      listDlg.setTarget(this);
      Dimension frmSize = getSize();
      Point loc = getLocation();
      listDlg.setLocation((frmSize.width - 180)/ 2 + loc.x, (frmSize.height - 300) / 2 + loc.y);
      listDlg.setVisible(true);
    }
  }

/**@note Create a list of individuals and pass them to the ListDialog
to be displayed.*/  
  void getAncestors() {
    Vector buffer = null;
    Enumeration pers;
    Individual temp;
    String name;
    int count = 0;

    listType = 2;
    if(people != null) {
      listDlg = new ListDialog(this);
      listDlg.setTitle("List of Individuals...");
      pers = people.keys();
      while (pers.hasMoreElements()) {
        temp = (Individual)people.get((Integer)pers.nextElement());
        name = temp.getGivenNames() + " " + temp.getFamilyName();
        if(buffer == null) buffer = new Vector();
        buffer.addElement(temp);
        count++;
      }
      listDlg.setData(buffer);
      listDlg.setTarget(this);
      Dimension frmSize = getSize();
      Point loc = getLocation();
      listDlg.setLocation((frmSize.width - 180)/ 2 + loc.x, (frmSize.height - 300) / 2 + loc.y);
      listDlg.setVisible(true);
    }
  }

/**@note When Edit | New Individual is chosen from the main menu, 
display the InEntry dialog for the user to create an individual.*/
  void newIndividual() {
    Individual tempPerson;
    Family tempFamily;
    Hashtable test;

    test = families;
    entryDlg = new InEntry(this,true);
    entryDlg.setList(families);
    entryDlg.setInList(people);
    entryDlg.setVisible(true);
    tempPerson = entryDlg.getIndividual();
    if(tempPerson != null && entryDlg.getCancel() != true) {
      if(entryDlg.isNewIndividual()) {
        people.put(new Integer(tempPerson.getReference()),tempPerson);
        if(entryDlg.isParent()) {
          entryDlg.getParentOf().setWife(tempPerson);
          if(entryDlg.isChild()) {
            entryDlg.getChild().addChild(tempPerson);
          }  
        }    
        else {
          if(entryDlg.isChild()) {
            entryDlg.getChild().addChild(tempPerson);
          }  
          else {
            tempFamily = new Family();
            tempFamily.setHusband(tempPerson);
            int next = families.size() + 1;
            families.put(new Integer(next),tempFamily);     
          }
        }
      }
      else {
        tempFamily = new Family();
        tempFamily.setHusband(tempPerson);
        int next = families.size() + 1;
        families.put(new Integer(next),tempFamily);     
      }
    }
  }
}

/**@note Class to extend WindowAdapter so that we can capture and handle 
the window closing event correctly.*/
class windowEventHandler extends java.awt.event.WindowAdapter {
  FamilyFrame adaptee;

  windowEventHandler(FamilyFrame adaptee) {
    this.adaptee = adaptee;
  }

  public void windowClosing(WindowEvent e) {
    adaptee.this_windowClosing(e);
  }
}