1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  *
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  *
28  * pmMessageDialog.java
29  * Common info message dialog
30  */
31 
32 package com.sun.admin.pm.client;
33 
34 import javax.swing.*;
35 import java.util.*;
36 import java.awt.*;
37 import java.awt.event.*;
38 import com.sun.admin.pm.server.*;
39 
40 public class pmMessageDialog extends pmDialog {
41 
42     pmButton helpButton = null;
43     pmButton okButton = null;
44     JList theText = null;
45     pmTop theTop = null;
46     String helpTag = null;
47 
48 
pmMessageDialog(String title, String msg)49     public pmMessageDialog(String title, String msg) {
50         this(null, title, msg, null, null);
51     }
52 
pmMessageDialog(Frame f, String title, String msg)53     public pmMessageDialog(Frame f, String title, String msg) {
54         this(f, title, msg, null, null);
55     }
56 
pmMessageDialog(Frame f, String title, String msg, pmTop top, String h)57     public pmMessageDialog(Frame f,
58 			    String title,
59 			    String msg,
60 			    pmTop top,
61 			    String h) {
62 
63 	super(f, title, true);	// modal
64 
65         theTop = top;
66         helpTag = h;
67 
68         // initialize constraints
69         GridBagConstraints c = new GridBagConstraints();
70         c.gridx = 0;
71         c.gridy = GridBagConstraints.RELATIVE;
72         c.gridwidth = 1;
73         c.gridheight = 1;
74         c.insets = new Insets(10, 10, 10, 10);
75         c.anchor = GridBagConstraints.EAST;
76 
77         // top panel
78         JPanel p = new JPanel();
79         p.setLayout(new GridBagLayout());
80         // p.setLayout(new BoxLayout(BoxLayout.X_AXIS));
81 
82         // JLabel label = new JLabel(msg, SwingConstants.CENTER);
83         JList theText = new JList() {
84             public boolean isFocusable() {
85                 return false;
86             }
87         };
88 
89         Vector v = new Vector();
90 
91         Debug.message("CLNT:  MessageDialog: " + title + " , " + msg);
92 
93         if (msg != null) {
94             StringTokenizer st = new StringTokenizer(msg, "\n", false);
95             try {
96                 while (st.hasMoreTokens())
97                     v.addElement(st.nextToken());
98             } catch (Exception x) {
99                 Debug.warning("CLNT:  pmMessageDialog caught " + x);
100             }
101             theText.setListData(v);
102         }
103 
104         theText.setBackground(p.getBackground());
105 
106         // p.add(theText, "Center");
107         p.add(theText, c);
108 
109         this.getContentPane().add(p, "Center");
110 
111         okButton = new pmButton(
112             pmUtility.getResource("Dismiss"));
113         okButton.setMnemonic(
114             pmUtility.getIntResource("Dismiss.mnemonic"));
115         okButton.addActionListener(new ActionListener() {
116             public void actionPerformed(ActionEvent evt) {
117                 actionOKButton();
118             }
119         });
120 
121         // handle Esc as dismiss in any case
122         this.getRootPane().registerKeyboardAction(new ActionListener() {
123             public void actionPerformed(ActionEvent e) {
124                 Debug.message("CLNT:  default cancel action");
125                 actionOKButton();
126             }},
127             KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false),
128             JComponent.WHEN_IN_FOCUSED_WINDOW);
129 
130         p = new JPanel();
131         p.add(okButton);
132 
133         if (theTop != null && helpTag != null) {
134             helpButton = new pmButton(
135                 pmUtility.getResource("Help"));
136             helpButton.setMnemonic(
137                 pmUtility.getIntResource("Help.mnemonic"));
138             p.add(helpButton);
139             helpButton.addActionListener(new ActionListener() {
140                 public void actionPerformed(ActionEvent evt) {
141                     theTop.showHelpItem(helpTag);
142                 }
143             });
144         }
145 
146         this.getContentPane().add(p, "South");
147         this.addWindowListener(new WindowAdapter() {
148             public void windowClosing(WindowEvent evt) {
149                 actionOKButton();
150             }
151         });
152 
153         this.pack();
154 
155         // this.getRootPane().setDefaultButton(okButton);
156         okButton.setAsDefaultButton();
157 
158         // okButton.requestFocus();
159         okButton.grabFocus();
160 
161     }
162 
163 
actionOKButton()164     protected void actionOKButton() {
165         returnValue = JOptionPane.OK_OPTION;
166         pmMessageDialog.this.setVisible(false);
167     }
168 
169 
getValue()170     public int getValue() {
171         return returnValue;
172     }
173 
174 
main(String[] args)175     public static void main(String[] args) {
176         JFrame f = new JFrame("Test Dialog");
177         f.setSize(300, 100);
178 
179         f.addWindowListener(new WindowAdapter() {
180             public void windowClosing(WindowEvent evt) {
181                 System.exit(0);
182             }
183         });
184 
185         f.setVisible(true);
186 
187         while (true) {
188             System.out.println("creating a new dialog instance...");
189             pmMessageDialog d =
190                 new pmMessageDialog(null,
191                                     "Dialog Test",
192                                     "Dumb test message.",
193 				    null,
194                                     null);
195             d.setVisible(true);
196             System.out.println("Dialog returns " + d.getValue());
197 
198             d.dispose();
199 
200         }
201 
202     }
203 
204 
205     protected int returnValue = JOptionPane.CLOSED_OPTION;
206 }
207