/*
* Datei: Ankreuzdialog.java
* Autor(en): Lukas König
* Java-Version: 6.0
* Erstellt: -
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
* author or licensor (but not in any way that suggests that they endorse
* you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
* distribute the resulting work only under the same or a similar license to
* this one.
*
* + Detailed license conditions (Germany):
* http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
* http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/
package eas.miscellaneous.system.windowFrames;
import eas.math.geometry.Vector2D;
import eas.startSetup.marbBuilder.MultiLineLabel;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Checkbox;
import java.util.ArrayList;
import java.util.Iterator;
/**
* Implementierung eines allgemeinen Dialogs.
*
* @author Lukas König
*/
public class CheckboxDialog extends Dialog implements ActionListener {
/**
* Die Serial-Version-ID vom 28. April 2007
*/
private static final long serialVersionUID = 2504188077455833733L;
/**
* Das gewählte Element.
*/
private String result;
/**
* Die Auswahlelemente.
*/
private Checkbox[] choices;
/**
* Enthält die ausgewählten Elemente.
*/
private ArrayList<String> auswahl;
/**
* Der Konstruktor.
*
* @param owner Das Vaterfenster.
* @param msg Anzuzeigende Nachricht.
* @param titel Der Titel des Fensters.
* @param buttons Liste von Strings, die von den anzuzeigenden
* Buttons dargestellt werden.
* @param elemente Liste der ankreuzbaren Elemente.
* @param aktiv Welche Elemente angekreuzt sind.
*/
public CheckboxDialog(final Frame owner,
final String msg,
final String titel,
final ArrayList<String> buttons,
final ArrayList<String> elemente,
final ArrayList<Boolean> aktiv) {
super(owner, titel, true);
final int locationX = 200;
final int locationY = 200;
final int borderX = 4;
final int borderY = 4;
final int lrMargin = 20;
final int tbMargin = 10;
Iterator<String> it;
Button butt;
int i;
this.choices = new Checkbox[elemente.size()];
this.setAlwaysOnTop(true);
//Fenster
this.setLayout(new BorderLayout(borderX, borderY));
this.setResizable(false);
this.setLocation(locationX, locationY);
//Message
MultiLineLabel lab = new MultiLineLabel(msg);
lab.setLeftRightMargin(lrMargin);
lab.setTopBottomMargin(tbMargin);
this.add(lab, BorderLayout.NORTH);
//Buttons
Panel panel = new Panel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
it = buttons.iterator();
while (it.hasNext()) {
butt = new Button(it.next());
butt.addActionListener(this);
panel.add(butt);
}
Panel panelChoice = new Panel();
panelChoice.setLayout(new GridLayout(elemente.size(), 1));
i = 0;
it = elemente.iterator();
while (it.hasNext()) {
this.choices[i] = new Checkbox(it.next());
this.choices[i].setState(aktiv.get(i));
panelChoice.add(this.choices[i]);
i++;
}
this.add(new Panel(), BorderLayout.WEST);
this.add(panelChoice, BorderLayout.CENTER);
this.add(panel, BorderLayout.SOUTH);
this.pack();
}
/**
* Fängt das Klicken auf einen Button ab.
*
* @param event Das ausgelöste Ereignis.
*/
@Override
public void actionPerformed(final ActionEvent event) {
this.result = event.getActionCommand();
this.auswahl = new ArrayList<String>();
for (int i = 0; i < this.choices.length; i++) {
if (this.choices[i].getState()) {
this.auswahl.add((this.choices[i].getLabel()));
}
}
this.setVisible(false);
this.dispose();
}
/**
* Gibt die Wahl des Benutzers zurück.
*
* @return Das Ergebnis des Benutzers.
*/
public String getResult() {
return this.result;
}
/**
* Sets the position of the window.
*
* @param pos Position vector.
*/
public void setPosition(final Vector2D pos) {
this.setBounds((int) pos.x, (int) pos.y,
this.getWidth(), this.getHeight());
}
/**
* Sets the size of the window.
*
* @param size Size vector.
*/
public void setSize(final Vector2D size) {
this.setBounds(this.getX(), this.getY(),
(int) size.x, (int) size.y);
}
/**
* @return Returns the auswahl.
*/
public ArrayList<String> getAuswahl() {
return this.auswahl;
}
}