/**
* 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.utilities;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import de.chris_soft.fyllgen.widget.dialog.TextInputDialog;
/**
* Kleinere Hilfsmethoden f�r SWT.
* @author Christian Packenius, Juli 2008.
*/
public class SwtUtilities {
static String textinput;
/**
* Verschiebt die angegebene Shell soweit, dass sie mittig im Parent steht.
* @param shell Zu zentrierendes Fenster.
* @param parent Parent-Fenster, in dem zentriert werden soll.
*/
public static void centerInParent(Shell shell, Shell parent) {
Rectangle pParent = parent.getBounds();
Rectangle pShell = shell.getBounds();
shell.setLocation(pParent.x + pParent.width / 2 - pShell.width / 2, pParent.y + pParent.height / 2 - pShell.height
/ 2);
}
/**
* Verschiebt die angegebene Shell soweit, dass sie rechts unten im Desktop
* steht.
* @param shell Zu setzendes Fenster.
*/
public static void setRightBottom(Shell shell) {
Rectangle pDesktop = shell.getDisplay().getClientArea();
Rectangle pShell = shell.getBounds();
shell.setLocation(pDesktop.width - pShell.width, pDesktop.height - pShell.height);
}
/**
* Fragt den Anwender etwas, auf das er mit Ja/Nein antworten kann.
* @param shell �bergeordnetes Fenster.
* @param question Fragetext.
* @param title Titel des Fensters.
* @return true, wenn der Anwender mit Ja geantwortet hat, sonst false.
*/
public static boolean askYesNo(Shell shell, String question, String title) {
MessageBox mb = new MessageBox(shell, SWT.YES | SWT.NO | SWT.ICON_QUESTION);
mb.setText(title);
mb.setMessage(question);
return mb.open() == SWT.YES;
}
/**
* Informiert den Anwender durch ein Hinweisfenster �ber etwas.
* @param shell �bergeordnetes Fenster.
* @param infoMessage Meldung.
*/
public static void sayInfo(final Shell shell, final String infoMessage) {
Display.getDefault().syncExec(new Runnable() {
public void run() {
MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_INFORMATION);
mb.setText("Hinweis");
mb.setMessage(infoMessage);
mb.open();
}
});
}
/**
* Informiert den Anwender durch ein Hinweisfenster �ber einen Fehler.
* @param shell �bergeordnetes Fenster.
* @param errMessage Meldung.
*/
public static void sayError(final Shell shell, final String errMessage) {
Display.getDefault().syncExec(new Runnable() {
public void run() {
MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
mb.setText("Fehler");
mb.setMessage(errMessage);
mb.open();
}
});
}
/**
* Stellt dem Anwender eine Ja/Nein-Frage, die er auch abbrechen kann.
* @param shell Parent-Fenster.
* @param question Zu stellende Frage.
* @return Antwort des Users (SWT.YES oder SWT.NO oder SWT.CANCEL).
*/
public static int askYesNoCancel(Shell shell, String question) {
MessageBox mb = new MessageBox(shell, SWT.YES | SWT.NO | SWT.CANCEL | SWT.ICON_QUESTION);
mb.setText("Frage");
mb.setMessage(question);
return mb.open();
}
/**
* L�sst den Anwender einen einzeiligen Text eingeben.
* @param parent Parent-Shell.
* @param msg Meldung.
* @return Eingegebener Text.
*/
public static String askForTextInput(final Shell parent, final String msg) {
Display.getDefault().syncExec(new Runnable() {
public void run() {
TextInputDialog tid = new TextInputDialog(parent, msg, false);
tid.open();
textinput = tid.getUserInput();
}
});
return textinput;
}
/**
* L�sst den Anwender einen einzeiligen Passwort-Text eingeben.
* @param parent Parent-Shell.
* @param msg Meldung.
* @return Eingegebener Text.
*/
public static String askForPasswordInput(final Shell parent, final String msg) {
Display.getDefault().syncExec(new Runnable() {
public void run() {
TextInputDialog tid = new TextInputDialog(parent, msg, true);
tid.open();
textinput = tid.getUserInput();
}
});
return textinput;
}
}