xref: /titanic_41/usr/src/cmd/krb5/kadmin/gui/util/ChoiceDialog.java (revision e4d060fb4c00d44cd578713eb9a921f594b733b8)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright (c) 1999-2000 by Sun Microsystems, Inc.
26  * All rights reserved.
27  */
28 import java.awt.*;
29 import java.awt.event.*;
30 
31 /**
32  * Returns null when the dialog box is closed through the window closing menu.
33  */
34 public class ChoiceDialog extends java.awt.Dialog {
35 
36     private String result = null;
37 
38     public ChoiceDialog(Frame parent, String title,
39   		        String[] messageLines, String[] buttonText,
40 		        int bottomLeftX, int bottomLeftY) {
41         super(parent, title, true);
42         addLinesAndButtons(messageLines, buttonText);
43         positionDialog(bottomLeftX, bottomLeftY);
44         finishDialog();
45     }
46 
47     public ChoiceDialog(Frame parent, String title,
48     String[] messageLines, String[] buttonText) {
49 
50         super(parent, title, true);
51         addLinesAndButtons(messageLines, buttonText);
52         positionDialog(parent);
53         finishDialog();
54     }
55 
56     public void addLinesAndButtons(String[] messageLines, String[] buttonText) {
57 
58 	Panel panel = new Panel();
59 
60 	panel.setLayout(new GridBagLayout());
61 	GridBagConstraints gbc = new GridBagConstraints();
62 	gbc.gridwidth = GridBagConstraints.REMAINDER;
63 	gbc.weightx = gbc.weighty = 1;
64 	gbc.gridx = gbc.gridy = 1;
65 
66 	for (int i = 0; i < messageLines.length; i++) {
67 	    Label t = new Label(" "+messageLines[i]);
68 	    panel.add(t, gbc);
69 	    gbc.gridy++;
70 	}
71 
72 	add(panel, "Center" /* NOI18N */);
73 
74 	panel = new Panel();
75 	panel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 5));
76 
77 	for (int i = 0; i < buttonText.length; i++) {
78 	    Button b = new Button(buttonText[i]);
79 	    b.addActionListener(new ActionListener() {
80 		public void actionPerformed(ActionEvent e) {
81 		    result = e.getActionCommand();
82 		    dispose();
83 		}
84 	    });
85 	    panel.add(b);
86 	}
87 
88 	add(panel, "South" /* NOI18N */);
89     }
90 
91     public void finishDialog() {
92 
93         Frame parent = (Frame)getParent();
94 
95 	setResizable(false);
96 
97 	setBackground(parent.getBackground());
98 	setForeground(parent.getForeground());
99 	addWindowListener(new WindowCloseListener());
100 	setVisible(true);
101     }
102 
103     public void positionDialog(Frame frame) {
104         Point p = frame.getLocationOnScreen();
105         Dimension s1 = frame.getSize();
106         pack();
107         Dimension s2 = getSize();
108         p.x += s1.width/2 - s2.width/2;
109         p.y += s1.height/2 - s2.height/2;
110         setLocation(p.x, p.y);
111     }
112 
113     public void positionDialog(int bottomLeftX, int bottomLeftY) {
114         Point p = new Point(bottomLeftX, bottomLeftY);
115         pack();
116         Dimension s = getSize();
117         p.y -= s.height;
118         setLocation(p.x, p.y);
119     }
120 
121     // return the name of the selected button.
122     public String getSelection() {
123 	return result;
124     }
125 
126     private  class WindowCloseListener extends  WindowAdapter {
127         public void windowClosing(WindowEvent e) {
128 	    dispose();
129         }
130     }
131 
132     public static void main(String[] args) {
133 
134 	Frame frame = new Frame();
135 	frame.setVisible(true);
136 
137 	String[] lines = {"line one", "line two"};
138 	String[] buttons = {"button one", "button two"};
139 	ChoiceDialog c1 = new ChoiceDialog(frame, "Hi", lines, buttons,
140                                            100, 100);
141 	String s = c1.getSelection();
142 	System.out.println("Returned "+s);
143 
144 	String[] warnlines = {"You are about to lose changes",
145 		 "Press OK to discard changes or"
146 		+" Cancel to continue editing."};
147 	String[] warnbuttons = {"OK", "Cancel"};
148 	c1 = new ChoiceDialog(frame, "Confirm Action",
149 				warnlines, warnbuttons);
150 	s = c1.getSelection();
151 	System.out.println("Returned "+s);
152 
153 	System.exit(0);
154     }
155 }
156