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