package Scorch.sWindows; /* Class: MessageBox Author: Mikhail Kruk Description: generic tool for providing small dialog boxes with a few buttons; callbacks can be supplied to the constructor and associated with buttons. */ import java.awt.*; import java.lang.reflect.*; import java.util.*; public class MessageBox extends sWindow { protected String[] buttons, callbacks; protected Object[] args; protected Container peer; protected int textAlign = Label.CENTER; public MessageBox(String title, String message, ^I^I String[] buttons, String[] callbacks, ^I^I Container owner, Container peer) { ^Ithis(title, message, buttons, callbacks, owner); ^Ithis.peer = peer; ^Ipeer.setEnabled(false); } public MessageBox(String title, String message, ^I^I String[] buttons, String[] callbacks, ^I^I Container owner) { ^Ithis(title, message, buttons, callbacks, (Object[])null, ^I Label.CENTER, owner); } public MessageBox(String title, String message, ^I^I String[] buttons, String[] callbacks, ^I^I int textAlign, ^I^I Container owner) { ^Ithis(title, message, buttons, callbacks, (Object[])null, ^I textAlign, owner); } public MessageBox(String title, String message, ^I^I String[] buttons, String[] callbacks, Object[] args, ^I^I Container owner) { ^Ithis(title, message, buttons, callbacks, args, Label.CENTER, owner); } public MessageBox(String title, String message, ^I^I String[] buttons, String[] callbacks, Object[] args, ^I^I int textAlign, ^I^I Container owner) { ^Isuper(0,0,0,0, title, owner); ^Ithis.buttons = buttons; ^Ithis.callbacks = callbacks; ^Ithis.args = args; ^Ithis.textAlign = textAlign; ^IPanel pb = new Panel(); ^Ipb.setLayout(new FlowLayout(FlowLayout.CENTER)); ^I ^Ifor(int i = 0; i < buttons.length; i++) ^I pb.add(new Button(buttons[i])); ^I/* build the text (multiline) panel */ ^IPanel pl = new Panel(); ^IStringTokenizer st = new StringTokenizer(message, ""+'\n'); ^Ipl.setLayout(new GridLayout(st.countTokens(),1,0,0)); ^Iwhile( st.hasMoreTokens() ) ^I pl.add(new Label(st.nextToken(), textAlign)); ^IsetLayout(new BorderLayout()); ^Iadd(pl, BorderLayout.CENTER); ^Iadd(pb, BorderLayout.SOUTH); ^Ivalidate(); } public void close() { ^Iif( peer != null ) ^I peer.setEnabled(true); ^Isuper.close(); } public boolean handleEvent(Event evt) { ^IMethod m = null; ^Iif(evt.id == Event.ACTION_EVENT && evt.target instanceof Button) ^I { ^I^Iif( peer != null ) ^I^I peer.setEnabled(true); ^I^Iif( callbacks == null ) ^I^I { ^I^I^Iclose(); ^I^I^Ireturn true; ^I^I } ^I^Ifor(int i = 0; i < buttons.length; i++) ^I^I { ^I^I^Iif(buttons[i].equals((String)evt.arg)) ^I^I^I { ^I^I^I^Iclose(); ^I^I^I^Iif( callbacks[i] == null ) return true; ^I^I^I^Itry ^I^I^I^I { ^I^I^I^I^IClass[] type; ^I^I^I^I^IObject[] arg; ^I^I^I^I^Iif( args == null || args[i] == null) ^I^I^I^I^I { ^I^I^I^I^I^Itype = new Class[0]; ^I^I^I^I^I^Iarg = new Object[0]; ^I^I^I^I^I } ^I^I^I^I^Ielse ^I^I^I^I^I { ^I^I^I^I^I^Itype = new Class[1]; ^I^I^I^I^I^Iarg = new Object[1]; ^I^I^I^I^I^Itype[0] = args[i].getClass(); ^I^I^I^I^I^Iarg[0] = args[i]; ^I^I^I^I^I } ^I^I^I^I^Im = owner.getClass().getMethod ^I^I^I^I^I (callbacks[i], type); ^I^I^I^I^Im.invoke(owner, arg); ^I^I^I^I } ^I^I^I^Icatch(Throwable t) ^I^I^I^I { ^I^I^I^I^ISystem.err.println("MessageBox: "+t); ^I^I^I^I^ISystem.err.println("method: "+callbacks[i]); ^I^I^I^I } ^I^I^I^Ireturn true; ^I^I^I } ^I^I } ^I } ^Ireturn super.handleEvent(evt); } }