package ch.werner_randelshofer.gui;/*Copyright (C) 1998-99 Werner Randelshoferwerner.randelshofer@mythen.chhttp://www.mythen.ch/w.randelshofer/    Permission to use this release of MessageDialog is herebygranted without fee provided that the complete copyrightnotice and this permission notice appear in all copiesand in supporting documentation.*/import java.awt.*;/**Message dialog conforming to AWT 1.0.2 specification.@author	Werner Randelshofer, Staldenmattweg 2, CH-6405 Immensee, Switzerland@version	0.2	1999-01-01	Created.*/public class MessageDialogextends Dialog	{    /**      * Type meaning look and feel should not supply any options -- only     * use the options from the JOptionPane.     */    public static final int         DEFAULT_OPTION = -1;    /** Type used for showConfirmDialog. */    public static final int         YES_NO_OPTION = 0;    /** Type used for showConfirmDialog. */    public static final int         YES_NO_CANCEL_OPTION = 1;    /** Type used for showConfirmDialog. */    public static final int         OK_CANCEL_OPTION = 2;    //    // Return values.    //    /** Return value from class method if YES is chosen. */    public static final int         YES_OPTION = 0;    /** Return value from class method if NO is chosen. */    public static final int         NO_OPTION = 1;    /** Return value from class method if CANCEL is chosen. */    public static final int         CANCEL_OPTION = 2;    /** Return value form class method if OK is chosen. */    public static final int         OK_OPTION = 0;    /** Return value from class method if user closes window without selecting     * anything, more than likely this should be treated as either a     * CANCEL_OPTION or NO_OPTION. */    public static final int         CLOSED_OPTION = -1;    //    // Message types. Used by the UI to determine what icon to display,    // and possibly what behavior to give based on the type.    //    /** Used for error messages. */    public static final int  ERROR_MESSAGE = 0;    /** Used for information messages. */    public static final int  INFORMATION_MESSAGE = 1;    /** Used for warning messages. */    public static final int  WARNING_MESSAGE = 2;    /** Used for questions. */    public static final int  QUESTION_MESSAGE = 3;    /** No icon is used. */    public static final int   PLAIN_MESSAGE = -1;	private int option_;	public MessageDialog(Frame parent, String message, String title, int optionType, int messageType)		{		super(parent,title,true);		Panel p = new Panel();		p.setLayout(new GridLayout(0,1));				int pos0 = 0;		int pos1 = 0;		while(true)			{			pos1 = message.indexOf('\n',pos0);			if (pos1 == -1)				{				p.add(new Label(message.substring(pos0,message.length())));				break;				}			else				{				p.add(new Label(message.substring(pos0,pos1)));				pos0 = pos1+1;				}			}		add(BorderLayout.NORTH,p);				p = new Panel();		switch (optionType)			{			case DEFAULT_OPTION :				p.add(new Button("OK"));				break;			case YES_NO_OPTION :				p.add(new Button("Yes"));				p.add(new Button("No"));				break;			case YES_NO_CANCEL_OPTION :				p.add(new Button("Yes"));				p.add(new Button("No"));				p.add(new Button("Cancel"));				break;			case OK_CANCEL_OPTION :				p.add(new Button("OK"));				p.add(new Button("Cancel"));				break;			}		add(BorderLayout.SOUTH,p);		setResizable(false);		}	/** Do the AWT 1.0.2 event handling. */	public boolean handleEvent(Event event)		{		switch (event.id)			{			// Dialog closed?			// ...Dispose it.			case Event.WINDOW_DESTROY :				option_ = CLOSED_OPTION;				dispose();				return true;			default :				return super.handleEvent(event);			}		}	/** Do the AWT 1.0.2 event handling. */	public boolean action(Event event, Object what)		{		if (what.equals("OK"))			{			option_ = OK_OPTION;			hide();			return true;			}		else if (what.equals("Cancel"))			{			option_ = CANCEL_OPTION;			hide();			return true;			}		else if (what.equals("Yes"))			{			option_ = YES_OPTION;			hide();			return true;			}		else if (what.equals("No"))			{			option_ = NO_OPTION;			hide();			return true;			}		else			{			return super.action(event,what);			}		}	public static int showMessage(Frame parent, String message, String title)		{		MessageDialog md = new MessageDialog(parent,message,title,OK_OPTION,PLAIN_MESSAGE);		md.pack();		md.show();		return md.option_;		}    public static Object showInputDialog(Frame parentComponent, Object message,		String title, int messageType,		Object[] selectionValues, Object initialSelectionValue)		{		MessageDialog md = new MessageDialog(parentComponent,message.toString(),title,OK_CANCEL_OPTION,messageType);		List list = new List();		md.add(BorderLayout.CENTER,list);		for (int i=0; i < selectionValues.length; i++)			{			list.addItem(selectionValues[i].toString());			if (selectionValues[i].equals(initialSelectionValue))				{ list.select(i); }			}		md.setResizable(true);		md.pack();		md.show();				if (md.option_ == OK_OPTION)			{ return list.getSelectedIndex() == -1 ? null : selectionValues[list.getSelectedIndex()]; }		else			{ return null; }		}	/**	Brings up a modal dialog where the number of choices is determined by the	optionType parameter, where the messageType parameter determines the icon to	display. The messageType parameter is primarily used to supply a default icon from	the look and feel.	@param	parentComponent Determines the Frame in which the dialog is displayed.			If null, or if the parentComponent has no Frame, a default Frame is used.	@param	message The Object to display	@param	title	the title string for the dialog	@param	optionType an int designating the options available on the dialog:			YES_NO_OPTION, or YES_NO_CANCEL_OPTION	@param	messageType an int designating the kind of message this is, primarily used			to determine the icon from the pluggable look and feel: ERROR_MESSAGE,			INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE.	@return	an int indicating the option selected by the user	*/	public static int showConfirmDialog(		Frame parentComponent,		Object message,		String title,		int optionType,		int messageType)		{		MessageDialog md = new MessageDialog(parentComponent,message.toString(),title,optionType,messageType);		md.pack();		md.show();		return md.option_;		}	}