package NET.worlds.console; import NET.worlds.scape.EventQueue; import java.awt.Button; import java.awt.Choice; import java.awt.Color; import java.awt.Event; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Label; import java.awt.Panel; import java.awt.Point; import java.util.Vector; public class AvatarDialog extends PolledDialog { private static final long serialVersionUID = 8661992825430619335L; private Button okButton = new Button(Console.message("Close")); private AvatarDialogCallback callback; private Vector choices = new Vector(); private Vector changes = new Vector(); private static Font font = new Font(Console.message("MenuFont"), 0, 12); private static Font bfont = new Font(Console.message("ButtonFont"), 0, 12); private int checkChanges; static Point lastWindowLocation = null; public AvatarDialog(java.awt.Window parent, DialogReceiver receiver, String title, AvatarDialogCallback callback) { super(parent, receiver, title, false); this.callback = callback; this.setResizable(false); this.setAlignment(3); this.ready(); } public void setChangeCheck() { this.checkChanges = 2; } @Override protected void build() { Vector headings = this.callback.getComponents(); int rows = headings.size(); Panel top = new Panel(new GridLayout(rows, 2, 2, 2)); top.setFont(font); top.setBackground(Color.black); for (int i = 0; i < rows; i++) { Label label = new Label(headings.elementAt(i), 2); label.setForeground(Color.white); label.setFont(font); top.add(label); Choice choice = new Choice(); choice.setForeground(Color.white); choice.setBackground(Color.black); choice.setFont(font); top.add(choice); this.choices.addElement(choice); Vector items = this.callback.getChoices(i); int count = items.size(); for (int j = 0; j < count; j++) { choice.add(items.elementAt(j)); } choice.select(this.callback.getCurrentSelection(i)); } GridBagLayout gbag = new GridBagLayout(); this.setLayout(gbag); GridBagConstraints c = new GridBagConstraints(); c.weightx = 1.0; c.weighty = 1.0; c.gridheight = rows; c.gridwidth = 0; c.fill = 0; this.add(gbag, top, c); Panel bottom = new Panel(); this.okButton.setFont(bfont); bottom.add(this.okButton); bottom.setBackground(Color.black); c.gridheight = 1; c.weightx = 0.0; c.weighty = 0.0; c.fill = 1; this.add(gbag, bottom, c); this.okButton.setBackground(Color.black); this.okButton.setForeground(Color.white); } @Override public boolean handleEvent(Event event) { if (EventQueue.redirectDrivingKeys(event)) { return true; } else { return event.id == 201 ? this.done(false) : super.handleEvent(event); } } @Override public boolean action(Event event, Object what) { Object target = event.target; if (target == this.okButton) { return this.done(true); } else { int count = this.choices.size(); for (int i = 0; i < count; i++) { Choice choice = this.choices.elementAt(i); if (target == choice) { int[] change = new int[]{i, choice.getSelectedIndex()}; this.changes.addElement(change); return true; } } return false; } } @Override public boolean done(boolean confirmed) { lastWindowLocation = this.getLocation(); return super.done(confirmed); } public void closeWin() { if (lastWindowLocation == null) { this.done(true); } } @Override protected void initialSize(int width, int height) { if (lastWindowLocation == null) { super.initialSize(width, height); } else { this.setLocation(lastWindowLocation); lastWindowLocation = null; this.setSize(width, height); } } @Override protected void activeCallback() { if (this.checkChanges > 0 && --this.checkChanges == 0) { for (int i = 0; i < this.choices.size(); i++) { this.choices.elementAt(i).select(this.callback.getCurrentSelection(i)); } } while (this.changes.size() != 0) { int[] change = this.changes.elementAt(0); this.callback.setCurrentSelection(change[0], change[1]); this.checkChanges = 1; this.changes.removeElementAt(0); } } @Override public boolean keyDown(Event event, int key) { return key == 27 ? this.done(false) : super.keyDown(event, key); } }