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