/**
* 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.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
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.events.ShellEvent;
import org.eclipse.swt.events.ShellListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;
import de.chris_soft.fyllgen.GUI;
import de.chris_soft.fyllgen.data.Person;
import de.chris_soft.fyllgen.utilities.SwtUtilities;
import de.chris_soft.fyllgen.utilities.TagManagement;
/**
* Tagging some/single person(s).
* @author Christian Packenius, 20110202.
*/
public class PersonTagShell extends Dialog implements ShellListener, SelectionListener {
/**
* List of persons to tag.
*/
private final List<Person> persons;
/**
* Dialog window.
*/
private final Shell shell;
/**
* "OK"-Button.
*/
private Button buttonOK;
/**
* Textfeld f�r neu zu vergebende Tags.
*/
private Text textNewTags;
/**
* Textfeld (nicht editierbar) mit allen Personen, die gekennzeichnet werden
* sollen.
*/
private Text textPersons;
private ScrolledComposite tagsCheckboxesScroller;
private Composite tagsCheckboxesInner;
/**
* List of all existing tags.
*/
private List<String> tagList;
/**
* List of all existing tags.
*/
private List<Boolean> tagOfSomePersons = new ArrayList<Boolean>();
private List<Button> checkerList = new ArrayList<Button>();
/**
* Constructor.
* @param parent The parent shell for this dialog.
* @param list List of persons to tag/un-tag.
*/
public PersonTagShell(Shell parent, List<Person> list) {
super(parent, 0);
persons = list;
shell = new Shell(parent, SWT.CLOSE | SWT.TITLE | SWT.APPLICATION_MODAL);
shell.setImage(GUI.instance.shellIcon);
shell.addShellListener(this);
shell.setText("Tagging von Personen");
shell.setSize(500, 400);
// Noch mittig ins vorhandene Fenster setzen.
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, das die gesamte Shell au�er dem Rahmen umfasst.
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);
// Unten einen Button zur �bernahme der Eingaben anzeigen.
buttonOK = new Button(inner, SWT.PUSH);
buttonOK.setText("OK");
fd = new FormData();
fd.left = new FormAttachment(0, 0);
fd.right = new FormAttachment(100, 0);
fd.bottom = new FormAttachment(100, 0);
buttonOK.setLayoutData(fd);
buttonOK.addSelectionListener(this);
shell.setDefaultButton(buttonOK);
// Oben eine Textbox mit allen Personen.
textPersons = new Text(inner, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
fd = new FormData();
fd.left = new FormAttachment(0, 0);
fd.right = new FormAttachment(100, 0);
fd.top = new FormAttachment(0, 0);
fd.bottom = new FormAttachment(25, 0);
textPersons.setLayoutData(fd);
String p = persons.get(0).getValueView(Person.NAME);
for (int i = 1; i < persons.size(); i++) {
p += ", " + persons.get(i).getValueView(Person.NAME);
}
textPersons.setText(p);
textPersons.setEnabled(true);
textPersons.setEditable(false);
// Textfeld, in dem neue Tags angegeben werden k�nnen �ber dem OK-Button.
textNewTags = new Text(inner, SWT.MULTI | SWT.BORDER | SWT.SEARCH);
textNewTags.setMessage("Neue Tags f�r alle obigen Personen, mit Komma voneinander getrennt.");
fd = new FormData();
fd.left = new FormAttachment(0, 0);
fd.right = new FormAttachment(100, 0);
fd.top = new FormAttachment(70, 0);
fd.bottom = new FormAttachment(buttonOK, -5);
textNewTags.setLayoutData(fd);
// Dazwischen ein Composite, in dem die schon existierenden Tags angezeigt
// werden.
// Composite-Objekte f�r Personen-Checkboxen aufbauen.
tagsCheckboxesScroller = new ScrolledComposite(inner, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
fd = new FormData();
fd.top = new FormAttachment(textPersons, 5, SWT.BOTTOM);
fd.bottom = new FormAttachment(textNewTags, -5, SWT.TOP);
fd.left = new FormAttachment(0, 0);
fd.right = new FormAttachment(100, 0);
tagsCheckboxesScroller.setLayoutData(fd);
tagsCheckboxesInner = new Composite(tagsCheckboxesScroller, 0);
tagsCheckboxesScroller.setContent(tagsCheckboxesInner);
GridLayout gridLayout = new GridLayout(1, true);
gridLayout.marginHeight = 1;
tagsCheckboxesInner.setLayout(gridLayout);
tagList = TagManagement.instance.getTagList();
for (String tag : tagList) {
Button checker = new Button(tagsCheckboxesInner, SWT.CHECK);
checker.setText(tag);
checkerList.add(checker);
checker.addSelectionListener(this);
int count = getPersonPerTagCount(tag);
if (count == 0) {
checker.setSelection(false);
tagOfSomePersons.add(false);
}
else if (count == persons.size()) {
checker.setSelection(true);
checker.setGrayed(false);
tagOfSomePersons.add(false);
}
else {
checker.setSelection(true);
checker.setGrayed(true);
tagOfSomePersons.add(true);
}
}
}
/**
* Get the number of persons (in the persons list) having this tag.
*/
private int getPersonPerTagCount(String tag) {
int count = 0;
tag = TagManagement.instance.addTag(tag); // Korrektes Tag ermitteln.
for (Person person : persons) {
if (person.hasTagInList(tag)) {
count++;
}
}
return count;
}
/**
* Open this dialog.
*/
public void open() {
if (persons.size() > 0) {
tagsCheckboxesInner.setSize(tagsCheckboxesInner.computeSize(SWT.DEFAULT, SWT.DEFAULT));
shell.open();
// Ereignis-Liste abarbeiten.
eventQueue();
}
}
/**
*
*/
private void eventQueue() {
Display display = Display.getDefault();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* @see org.eclipse.swt.events.ShellListener#shellActivated(org.eclipse.swt.events.ShellEvent)
*/
public void shellActivated(ShellEvent e) {
// Ignore.
}
/**
* @see org.eclipse.swt.events.ShellListener#shellClosed(org.eclipse.swt.events.ShellEvent)
*/
public void shellClosed(ShellEvent e) {
shell.removeShellListener(this);
}
/**
* @see org.eclipse.swt.events.ShellListener#shellDeactivated(org.eclipse.swt.events.ShellEvent)
*/
public void shellDeactivated(ShellEvent e) {
// Ignore.
}
/**
* @see org.eclipse.swt.events.ShellListener#shellDeiconified(org.eclipse.swt.events.ShellEvent)
*/
public void shellDeiconified(ShellEvent e) {
// Ignore.
}
/**
* @see org.eclipse.swt.events.ShellListener#shellIconified(org.eclipse.swt.events.ShellEvent)
*/
public void shellIconified(ShellEvent e) {
// Ignore.
}
/**
* @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) {
Widget source = e.widget;
if (source == buttonOK) {
// Tags in allen Personen setzen oder l�schen.
for (Button checker : checkerList) {
if (!checker.getGrayed()) {
String tag = checker.getText();
if (checker.getSelection()) {
for (Person person : persons) {
person.addTag(tag);
}
}
else {
for (Person person : persons) {
person.removeTag(tag);
}
}
}
}
// Neue Tags vergeben.
StringTokenizer st = new StringTokenizer(textNewTags.getText(), ",");
while (st.hasMoreTokens()) {
String token = st.nextToken();
token = TagManagement.instance.addTag(token);
for (Person person : persons) {
person.addTag(token);
}
}
// Okay, alles wieder l�schen.
shell.dispose();
}
else if (checkerList.contains(source)) {
int k = checkerList.indexOf(source);
// Nur handeln, wenn die CheckBox von vornherein grau war (ansonsten
// normales Verhalten).
if (tagOfSomePersons.get(k)) {
Button checker = (Button) source;
boolean grayed = checker.getGrayed();
boolean selected = checker.getSelection();
if (grayed && !selected) {
checker.setSelection(true);
checker.setGrayed(false);
}
else if (!grayed && selected) {
checker.setGrayed(true);
}
}
}
}
}