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 * pmOKCancelDialog.java 29 * Common dialog 30 */ 31 32 package com.sun.admin.pm.client; 33 34 import javax.swing.*; 35 import java.awt.*; 36 import java.awt.event.*; 37 import com.sun.admin.pm.server.Debug; 38 39 40 public class pmOKCancelDialog extends pmDialog { 41 private pmTop theTop; 42 private String theTag; 43 protected boolean defaultIsOK = true; 44 pmOKCancelDialog(Frame f, String title, String msg)45 public pmOKCancelDialog(Frame f, String title, String msg) { 46 this(f, title, msg, null, null, true); 47 } 48 pmOKCancelDialog(Frame f, String title, String msg, boolean ok)49 public pmOKCancelDialog(Frame f, String title, String msg, boolean ok) { 50 this(f, title, msg, null, null, ok); 51 } 52 pmOKCancelDialog(Frame f, String title, String msg, pmTop t, String h)53 public pmOKCancelDialog(Frame f, String title, String msg, 54 pmTop t, String h) { 55 this(f, title, msg, t, h, true); 56 } 57 pmOKCancelDialog(Frame f, String title, String msg, pmTop t, String h, boolean ok)58 public pmOKCancelDialog(Frame f, String title, String msg, 59 pmTop t, String h, boolean ok) { 60 super(f, title, true); // modal 61 62 theTop = t; 63 theTag = h; 64 defaultIsOK = ok; 65 66 // initialize constraints 67 GridBagConstraints c = new GridBagConstraints(); 68 c.gridx = 0; 69 c.gridy = GridBagConstraints.RELATIVE; 70 c.gridwidth = 1; 71 c.gridheight = 1; 72 c.insets = new Insets(10, 10, 10, 10); 73 c.anchor = GridBagConstraints.EAST; 74 75 // top panel 76 JPanel p = new JPanel(); 77 p.setLayout(new GridBagLayout()); 78 79 JLabel label = new JLabel(msg, SwingConstants.CENTER); 80 p.add(label, c); 81 82 this.getContentPane().add(p, "Center"); 83 84 this.getContentPane().add( 85 buttonPanel(defaultIsOK, theTop != null && theTag != null), 86 "South"); 87 88 this.pack(); 89 90 this.addWindowListener(new WindowAdapter() { 91 public void windowClosing(WindowEvent evt) { 92 returnValue = JOptionPane.CLOSED_OPTION; 93 pmOKCancelDialog.this.setVisible(false); 94 } 95 }); 96 97 if (defaultIsOK) { 98 // this.getRootPane().setDefaultButton(okButton); 99 okButton.setAsDefaultButton(); 100 okButton.requestFocus(); 101 } else { 102 // this.getRootPane().setDefaultButton(cancelButton); 103 cancelButton.setAsDefaultButton(); 104 cancelButton.requestFocus(); 105 } 106 107 // handle Esc as cancel in any case 108 this.getRootPane().registerKeyboardAction(new ActionListener() { 109 public void actionPerformed(ActionEvent e) { 110 Debug.message("CLNT: default cancel action"); 111 actionCancelButton(); 112 }}, 113 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false), 114 JComponent.WHEN_IN_FOCUSED_WINDOW); 115 } 116 buttonPanel(boolean okDefault, boolean useHelp)117 public JPanel buttonPanel(boolean okDefault, boolean useHelp) { 118 JPanel panel = new JPanel(); 119 120 panel.setLayout(new GridBagLayout()); 121 GridBagConstraints c = new GridBagConstraints(); 122 123 c.gridheight = 1; 124 c.gridwidth = 1; 125 c.weightx = c.weighty = 0.0; 126 c.anchor = GridBagConstraints.CENTER; 127 c.fill = GridBagConstraints.HORIZONTAL; 128 c.gridy = 0; 129 130 131 if (okDefault) 132 c.gridx = 0; 133 else 134 c.gridx = 1; 135 136 okButton = new pmButton( 137 pmUtility.getResource("OK")); 138 okButton.setMnemonic( 139 pmUtility.getIntResource("OK.mnemonic")); 140 141 if (okDefault) 142 c.gridx = 1; 143 else 144 c.gridx = 0; 145 146 cancelButton = new pmButton( 147 pmUtility.getResource("Cancel")); 148 cancelButton.setMnemonic( 149 pmUtility.getIntResource("Cancel.mnemonic")); 150 151 helpButton = null; 152 153 if (useHelp) { 154 c.gridx = 2; 155 helpButton = new pmButton( 156 pmUtility.getResource("Help")); 157 helpButton.setMnemonic( 158 pmUtility.getIntResource("Help.mnemonic")); 159 } 160 161 okButton.addActionListener(new ActionListener() { 162 public void actionPerformed(ActionEvent evt) { 163 actionOKButton(); 164 } 165 }); 166 167 cancelButton.addActionListener(new ActionListener() { 168 public void actionPerformed(ActionEvent evt) { 169 actionCancelButton(); 170 } 171 }); 172 173 if (helpButton != null) { 174 helpButton.addActionListener(new ActionListener() { 175 public void actionPerformed(ActionEvent evt) { 176 theTop.showHelpItem(theTag); 177 } 178 }); 179 } 180 181 c.insets = new Insets(15, 15, 15, 15); 182 c.gridx = 0; 183 panel.add(okButton, c); 184 c.gridx = 1; 185 panel.add(cancelButton, c); 186 c.gridx = 2; 187 if (helpButton != null) 188 panel.add(helpButton, c); 189 190 return panel; 191 } 192 actionOKButton()193 protected void actionOKButton() { 194 returnValue = JOptionPane.OK_OPTION; 195 pmOKCancelDialog.this.setVisible(false); 196 } 197 actionCancelButton()198 protected void actionCancelButton() { 199 returnValue = JOptionPane.CANCEL_OPTION; 200 pmOKCancelDialog.this.setVisible(false); 201 } 202 203 204 getValue()205 public int getValue() { 206 return returnValue; 207 } 208 209 main(String[] args)210 public static void main(String[] args) { 211 JFrame f = new JFrame("Test Dialog"); 212 f.setSize(300, 100); 213 214 f.addWindowListener(new WindowAdapter() { 215 public void windowClosing(WindowEvent evt) { 216 System.exit(0); 217 } 218 }); 219 220 f.setVisible(true); 221 222 while (true) { 223 System.out.println("creating a new dialog instance..."); 224 pmOKCancelDialog d = 225 new pmOKCancelDialog( 226 null, "Dialog Test", "Some message.", false); 227 d.setVisible(true); 228 System.out.println("Dialog returns " + d.getValue()); 229 230 d.dispose(); 231 232 } 233 234 } 235 236 237 pmButton helpButton = null; 238 pmButton okButton = null; 239 pmButton cancelButton = null; 240 241 protected int returnValue = JOptionPane.CLOSED_OPTION; 242 } 243