KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > echo2example > chatclient > MessageDialog


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package echo2example.chatclient;
31
32 import java.util.EventListener JavaDoc;
33
34 import nextapp.echo2.app.Button;
35 import nextapp.echo2.app.Extent;
36 import nextapp.echo2.app.Label;
37 import nextapp.echo2.app.Row;
38 import nextapp.echo2.app.SplitPane;
39 import nextapp.echo2.app.WindowPane;
40 import nextapp.echo2.app.event.ActionEvent;
41 import nextapp.echo2.app.event.ActionListener;
42
43 /**
44  * A generic modal dialog that displays a message.
45  */

46 public class MessageDialog extends WindowPane {
47
48     public static final int TYPE_ERROR = 1;
49     public static final int TYPE_CONFIRM = 1;
50     
51     public static final int CONTROLS_OK = 1;
52     public static final int CONTROLS_YES_NO = 2;
53     
54     public static final String JavaDoc COMMAND_OK = "ok";
55     public static final String JavaDoc COMMAND_CANCEL = "cancel";
56     
57     private ActionListener actionProcessor = new ActionListener() {
58
59         /**
60          * @see nextapp.echo2.app.event.ActionListener#actionPerformed(nextapp.echo2.app.event.ActionEvent)
61          */

62         public void actionPerformed(ActionEvent e) {
63             getParent().remove(MessageDialog.this);
64             EventListener JavaDoc[] listeners = getEventListenerList().getListeners(ActionListener.class);
65             ActionEvent outgoingEvent = new ActionEvent(this, e.getActionCommand());
66             for (int i = 0; i < listeners.length; ++i) {
67                 ((ActionListener) listeners[i]).actionPerformed(outgoingEvent);
68             }
69         }
70     };
71     
72     /**
73      * Creates a new <code>MessageDialog</code>.
74      *
75      * @param title the dialog title
76      * @param message the message to display
77      * @param type the type of dialog, one of the following values:
78      * <ul>
79      * <li><code>TYPE_ERROR</code></li>
80      * <li><code>TYPE_CONFIRM</code></li>
81      * </ul>
82      * @param controlConfiguration the control configuration, one of the
83      * following values:
84      * <ul>
85      * <li><code>CONTROLS_OK</code></li>
86      * <li><code>CONTROLS_YES_NO</code></li>
87      * </ul>
88      */

89     public MessageDialog(String JavaDoc title, String JavaDoc message, int type, int controlConfiguration) {
90         super(title, new Extent(320), new Extent(240));
91         setStyleName("Default");
92         setClosable(false);
93         setModal(true);
94
95         SplitPane splitPane = new SplitPane(SplitPane.ORIENTATION_VERTICAL_BOTTOM_TOP, new Extent(32));
96         add(splitPane);
97         
98         Row controlsRow = new Row();
99         controlsRow.setStyleName("ControlPane");
100         splitPane.add(controlsRow);
101
102         Button button;
103         switch (controlConfiguration) {
104         case CONTROLS_OK:
105             button = new Button(Messages.getString("Generic.Ok"), Styles.ICON_24_YES);
106             button.setStyleName("ControlPane.Button");
107             button.setActionCommand(COMMAND_OK);
108             button.addActionListener(actionProcessor);
109             controlsRow.add(button);
110             break;
111         case CONTROLS_YES_NO:
112             button = new Button(Messages.getString("Generic.Yes"), Styles.ICON_24_YES);
113             button.setStyleName("ControlPane.Button");
114             button.setActionCommand(COMMAND_OK);
115             button.addActionListener(actionProcessor);
116             controlsRow.add(button);
117             button = new Button(Messages.getString("Generic.No"), Styles.ICON_24_NO);
118             button.setStyleName("ControlPane.Button");
119             button.setActionCommand(COMMAND_CANCEL);
120             button.addActionListener(actionProcessor);
121             controlsRow.add(button);
122             break;
123         }
124         
125         Label contentLabel = new Label(message);
126         contentLabel.setStyleName("MessageDialog.ContentLabel");
127         splitPane.add(contentLabel);
128         
129         setModal(true);
130     }
131     
132     /**
133      * Adds an <code>ActionListener</code> to receive notification when the
134      * user selects a choice. The fired <code>command</code> of the fired
135      * <code>ActionEvent</code> will contain be one of the
136      * <code>COMMAND_XXX</code> constants.
137      *
138      * @param l the <code>ActionListener</code> to add
139      */

140     public void addActionListener(ActionListener l) {
141         getEventListenerList().addListener(ActionListener.class, l);
142     }
143     
144     /**
145      * Removes an <code>ActionListener</code> from receiving notification
146      * when the user selects a choice.
147      *
148      * @param l the <code>ActionListener</code> to remove
149      */

150     public void removeActionListener(ActionListener l) {
151         getEventListenerList().removeListener(ActionListener.class, l);
152     }
153 }
154
Popular Tags