Package de.chris_soft.fyllgen.widget.dialog

Source Code of de.chris_soft.fyllgen.widget.dialog.MergePersonDialog

/**
* 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.Enumeration;
import java.util.Properties;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
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.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

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.utilities.SwtConsts;
import de.chris_soft.fyllgen.utilities.SwtUtilities;
import de.chris_soft.fyllgen.widget.listener.CurrentPersonChangeListener;
import de.chris_soft.fyllgen.widget.listener.CurrentPersonChanger;

/**
* Dialog, um die Daten einer "neuen" zu mergenden Person einer "alten" Person
* zuzuordnen bzw. zu �bernehmen. Er kann auch nur zum Editieren einer einzelnen
* neuen Person genutzt werden. In diesem Fall ist "oldPerson" im Konstruktor
* auf null zu setzen.
* @author Christian Packenius, Juli 2008.
*/
public class MergePersonDialog extends Dialog implements SelectionListener, CurrentPersonChanger {
  /**
   * Das eigentliche Fenster-Objekt.
   */
  private final Shell shell;

  /**
   * Push-Buttons.
   */
  private final Button buttonSet;

  private final Button buttonCancel;

  /**
   * Daten zur neuen und alten Person.
   */
  private final Person newPerson;

  /**
   * Entweder die schon bestehende Person oder jene, die neu angelegt wurde.
   */
  public final Person oldPerson;

  /**
   * Virtuelle Person, die nur f�r die Datenaufnahme da ist.
   */
  private final Person person;

  /**
   * true, sobald der Dialog mit "�bernehmen" abgeschlossen wurde.
   */
  public boolean finished = false;

  private final Control firstCtrl;

  /**
   * true, wenn eine echt neue Person eingegeben wird, sonst false.
   */
  public final boolean bEditNewPerson;

  /**
   * Konstruktor. Erzeugt das Fenster, ohne es anzuzeigen.
   * @param parent Parent-Shell, �ber der dieser Dialog angezeigt wird.
   * @param oldPerson Person, die editiert werden soll. Darf auch null sein, um
   *          eine neue Person zu erzeugen.
   * @param newPerson Person, die mit angezeigt werden soll.
   */
  public MergePersonDialog(Shell parent, Person oldPerson, Person newPerson) {
    super(parent, 0);
    bEditNewPerson = oldPerson == null;

    // Anzuzeigende Zusatzdaten festlegen.
    Properties newPersonProperties = newPerson.cloneProperties();
    Properties[] viewData = new Properties[] { newPersonProperties };

    // Falls oldPerson == null ist, wird sie neu angelegt.
    if (oldPerson == null) {
      oldPerson = newPerson.clone();
      viewData = new Properties[0];
    }

    this.newPerson = newPerson;
    this.oldPerson = oldPerson;

    // Alte Person kopieren, da sich erstmal darin die Daten �ndern.
    person = oldPerson.clone();

    shell = new Shell(parent, SWT.CLOSE | SWT.TITLE | SWT.APPLICATION_MODAL);
    shell.setImage(GUI.instance.shellIcon);
    shell.setText("Personendaten �bernehmen");
    shell.setSize(700, 500);

    // Noch mittig/rechts unten ins vorhandene Fenster setzen.
    if (OptionData.instance.getBoolean(OptionData.MERGE_DIALOG_IN_CORNER)) {
      SwtUtilities.setRightBottom(shell);
    }
    else {
      SwtUtilities.centerInParent(shell, parent);
    }

    // Shell hat innen einen Rahmen.
    FormLayout shellLayout = new FormLayout();
    shellLayout.marginBottom = shellLayout.marginLeft = shellLayout.marginRight = shellLayout.marginTop = 5;
    shell.setLayout(shellLayout);

    // Innen ein Composite.
    Composite inner = new Composite(shell, 0);
    FormLayout innerLayout = new FormLayout();
    inner.setLayout(innerLayout);
    FormData fd = new FormData();
    fd.top = new FormAttachment(0, 0);
    fd.left = new FormAttachment(0, 0);
    fd.right = new FormAttachment(100, 0);
    fd.bottom = new FormAttachment(100, 0);
    inner.setLayoutData(fd);

    // Hinweis oben.
    Label label1 = new Label(inner, SWT.WRAP);
    String label1text = "Sie k�nnen nun die Fremddaten in die bestehende Person �bertragen.";
    label1.setText(label1text);
    fd = new FormData();
    fd.top = new FormAttachment(0, 0);
    fd.left = new FormAttachment(0, 0);
    fd.right = new FormAttachment(100, 0);
    label1.setLayoutData(fd);

    // Unten die Buttonleiste.
    Composite compButtons = new Composite(inner, 0);
    compButtons.setLayout(new GridLayout(2, true));
    fd = new FormData();
    fd.right = new FormAttachment(100, 0);
    fd.bottom = new FormAttachment(100, 0);
    compButtons.setLayoutData(fd);

    buttonSet = new Button(compButtons, SWT.PUSH);
    buttonSet.setText("�bernehmen");
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    buttonSet.setLayoutData(gd);
    buttonSet.addSelectionListener(this);
    shell.setDefaultButton(buttonSet);

    buttonCancel = new Button(compButtons, SWT.PUSH);
    buttonCancel.setText("Abbrechen");
    gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    buttonCancel.setLayoutData(gd);
    buttonCancel.addSelectionListener(this);

    ScrolledComposite scrolledData = new ScrolledComposite(inner, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    fd = new FormData();
    fd.top = new FormAttachment(label1, 5, SWT.BOTTOM);
    fd.bottom = new FormAttachment(compButtons, -5, SWT.TOP);
    fd.left = new FormAttachment(0, 0);
    fd.right = new FormAttachment(100, 0);
    scrolledData.setLayoutData(fd);

    Composite inScrollComposite = new Composite(scrolledData, 0);
    GridLayout gridLayout = new GridLayout(2, false);
    inScrollComposite.setLayout(gridLayout);

    firstCtrl = GUI.instance.createAtomWidgets(Person.atomData, inScrollComposite, null, this, null, viewData);
    GUI.instance.createAtomWidgets(Person.atomDataNotes, inScrollComposite, null, this, null, viewData);
    GUI.instance.createAtomWidgets(Person.atomDataSources, inScrollComposite, null, this, null, viewData);

    scrolledData.setContent(inScrollComposite);
    int width = Math.max(660, inScrollComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
    int height = inScrollComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y + 5;
    inScrollComposite.setSize(width, height);
  }

  /**
   * Shell sichtbar machen.
   */
  public void open() {
    shell.open();
    firstCtrl.forceFocus();

    // Ereignis-Liste abarbeiten.
    while (!shell.isDisposed()) {
      if (!SwtConsts.display.readAndDispatch()) {
        SwtConsts.display.sleep();
      }
    }
  }

  /**
   * @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 == buttonCancel) {
      shell.dispose();
      return;
    }
    else if (e.widget == buttonSet) {
      // Alle eingegebenen Daten in die Person der Familie �bernehmen.
      Properties cloneProps = person.cloneProperties();
      Enumeration<Object> en = cloneProps.keys();
      while (en.hasMoreElements()) {
        String key = (String) en.nextElement();
        if (!key.equals(Person.XREFID)) {
          String val = cloneProps.getProperty(key);
          oldPerson.setValue(key, val);
        }
      }
      shell.dispose();
      finished = true;

      newPerson.setValue(Person.XREFID, oldPerson.getValue(Person.XREFID));
      newPerson.setFinishedMerge(true);
      Family.instance.addNewPerson(oldPerson);
      Family.instance.setCurrentPerson(oldPerson, 2);
    }
  }

  /**
   * @see de.chris_soft.fyllgen.widget.listener.CurrentPersonChanger#addCurrentPersonChangeListener(de.chris_soft.fyllgen.widget.listener.CurrentPersonChangeListener)
   */
  public void addCurrentPersonChangeListener(CurrentPersonChangeListener listener) {
    // Ignorieren, da sich die aktuelle Person nie �ndern wird.
  }

  /**
   * @see de.chris_soft.fyllgen.widget.listener.CurrentPersonChanger#getCurrentPerson()
   */
  public Person getCurrentPerson() {
    return person;
  }

  /**
   * @see de.chris_soft.fyllgen.widget.listener.CurrentPersonChanger#removeCurrentPersonChangeListener(de.chris_soft.fyllgen.widget.listener.CurrentPersonChangeListener)
   */
  public void removeCurrentPersonChangeListener(CurrentPersonChangeListener listener) {
    // Ignorieren, der Dialog ist in sich geschlossen.
  }
}
TOP

Related Classes of de.chris_soft.fyllgen.widget.dialog.MergePersonDialog

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.