001: /*
002: * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
003: * reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: *
017: * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
018: * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
019: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
020: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
021: * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
022: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
023: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
024: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
025: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
027: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: */
030:
031: package rcm.awt;
032:
033: import java.awt.*;
034: import java.awt.event.*;
035: import java.beans.*;
036:
037: public class OkCancelDialog extends Dialog {
038:
039: Panel controlsPane;
040: Button okButton;
041: Button cancelButton;
042:
043: public OkCancelDialog(Frame owner, String title) {
044: super (owner, title, true);
045:
046: add(controlsPane = new Panel(), BorderLayout.CENTER);
047:
048: Panel panel;
049: add(panel = new Panel(), BorderLayout.SOUTH);
050: panel.setLayout(new FlowLayout());
051:
052: Panel subpanel;
053: panel.add(subpanel = new Panel());
054: subpanel.setLayout(new GridLayout(1, 0, 4, 0));
055:
056: subpanel.add(okButton = new Button("OK"));
057: okButton.addActionListener(new ActionListener() {
058: public void actionPerformed(ActionEvent event) {
059: ok();
060: }
061: });
062:
063: subpanel.add(cancelButton = new Button("Cancel"));
064: cancelButton.addActionListener(new ActionListener() {
065: public void actionPerformed(ActionEvent event) {
066: cancel();
067: }
068: });
069:
070: addWindowListener(new WindowAdapter() {
071: public void windowClosing(WindowEvent event) {
072: cancel();
073: }
074: });
075:
076: }
077:
078: public static void centerWindow(Window window, Component ref) {
079: Dimension size = window.getSize();
080: Dimension refSize = (ref != null) ? ref.getSize() : Toolkit
081: .getDefaultToolkit().getScreenSize();
082: Point origin = (ref != null) ? ref.getLocationOnScreen()
083: : new Point(0, 0);
084:
085: if (refSize != null) {
086: int x = Math.max(0, origin.x + (refSize.width - size.width)
087: / 2);
088: int y = Math.max(0, origin.y
089: + (refSize.height - size.height) / 2);
090: window.setLocation(x, y);
091: }
092: }
093:
094: public void show() {
095: Container parent = getParent();
096: if (parent != null)
097: centerWindow(this , parent);
098:
099: registerKeystrokes();
100:
101: super .show();
102: }
103:
104: public Panel getControlsPane() {
105: return controlsPane;
106: }
107:
108: public void registerKeystrokes() {
109: // NIY: bind Enter to OK, Esc to Cancel
110: }
111:
112: public void ok() {
113: dispose();
114: }
115:
116: public void cancel() {
117: dispose();
118: }
119: }
|