/**
* 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 org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
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.utilities.SwtUtilities;
import de.chris_soft.fyllgen.utilities.TagManagement;
/**
* Tagging some/single person(s).
* @author Christian Packenius, 20110202.
*/
public class DirectLabelChoiceShell extends Dialog implements ShellListener, SelectionListener, ModifyListener {
/**
* Dialog window.
*/
private final Shell shell;
/**
* "OK"-Button.
*/
private Button buttonOK;
/**
* Textfeld f�r neu zu vergebende Tags.
*/
private Text textNewTag;
private ScrolledComposite tagsCheckboxesScroller;
private Composite tagsCheckboxesInner;
/**
* List of all existing tags.
*/
private List<String> tagList;
private List<Button> checkerList = new ArrayList<Button>();
private String directTag = null;
/**
* Constructor.
* @param parent The parent shell for this dialog.
*/
public DirectLabelChoiceShell(Shell parent) {
super(parent, 0);
shell = new Shell(parent, SWT.CLOSE | SWT.TITLE | SWT.APPLICATION_MODAL);
shell.setImage(GUI.instance.shellIcon);
shell.addShellListener(this);
shell.setText("Festlegen einer Direkt-Markierung");
shell.setSize(500, 300);
// 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);
buttonOK.setEnabled(false);
shell.setDefaultButton(buttonOK);
// Textfeld, in dem neue Tags angegeben werden k�nnen �ber dem OK-Button.
textNewTag = new Text(inner, SWT.SINGLE | SWT.BORDER | SWT.SEARCH);
textNewTag.setMessage("Neue Direkt-Markierung");
fd = new FormData();
fd.left = new FormAttachment(0, 0);
fd.right = new FormAttachment(100, 0);
fd.bottom = new FormAttachment(buttonOK, -5);
textNewTag.setLayoutData(fd);
textNewTag.addModifyListener(this);
// 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(0, 0);
fd.bottom = new FormAttachment(textNewTag, -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.RADIO);
checker.setText(tag);
checker.addSelectionListener(this);
checkerList.add(checker);
}
}
/**
* Open this dialog.
*/
public void open() {
tagsCheckboxesInner.setSize(tagsCheckboxesInner.computeSize(SWT.DEFAULT, SWT.DEFAULT));
shell.open();
// Ereignis-Liste abarbeiten.
eventQueue();
}
/**
* SWT event queue.
*/
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) {
TagManagement.instance.setDirectTag(directTag);
// Okay, alles wieder l�schen.
shell.dispose();
}
else if (source instanceof Button) {
setOkEnabled();
directTag = ((Button) source).getText();
buttonOK.setEnabled(true);
}
}
/**
* Enable or disable OK button. Set "directTag" variable.
*/
private void setOkEnabled() {
boolean ok = false;
for (Button button : checkerList) {
if (button.getSelection()) {
directTag = button.getText();
ok = true;
break;
}
}
String s = textNewTag.getText();
s = s == null ? "" : s;
if (s.trim().length() > 0) {
ok = true;
for (Button button : checkerList) {
button.setSelection(false);
}
directTag = s.trim();
}
if (s.indexOf(',') >= 0) {
ok = false; // Komma ist als Zeichen nicht erlaubt.
directTag = null;
}
buttonOK.setEnabled(ok);
}
/**
* @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
*/
public void modifyText(ModifyEvent e) {
if (e.widget instanceof Text) {
setOkEnabled();
}
}
/**
* Open the shell for changing direct tagging.
*/
public static void openShell() {
new DirectLabelChoiceShell(GUI.instance.shell).open();
}
}