|
OkCancelDialog |
|
1 /* 2 * Copyright (c) 1998-2001, The University of Sheffield. 3 * 4 * This file is part of GATE (see http://gate.ac.uk/), and is free 5 * software, licenced under the GNU Library General Public License, 6 * Version 2, June 1991 (in the distribution as file licence.html, 7 * and also available at http://gate.ac.uk/gate/licence.html). 8 * 9 * Valentin Tablan 16/07/2001 10 * 11 * $Id: OkCancelDialog.java,v 1.8 2002/02/27 17:01:24 valyt Exp $ 12 * 13 */ 14 15 package gate.gui; 16 17 import javax.swing.*; 18 import javax.swing.event.*; 19 import java.awt.*; 20 import java.awt.event.*; 21 22 /** 23 * A simple modal dialog that displays a component provided by the user along 24 * with two buttons ("OK" and "Cancel"). 25 */ 26 public class OkCancelDialog extends JDialog { 27 28 protected OkCancelDialog(Frame owner, String title, Component contents){ 29 super(owner, title); 30 init(contents); 31 } 32 33 protected OkCancelDialog(Dialog owner, String title, Component contents){ 34 super(owner, title); 35 init(contents); 36 } 37 38 protected OkCancelDialog(String title, Component contents){ 39 super(); 40 setTitle(title); 41 init(contents); 42 } 43 44 protected void init(Component contents){ 45 MainFrame.getGuiRoots().add(this); 46 47 getContentPane().setLayout(new BorderLayout()); 48 49 // //fill in the contents 50 // JPanel vBox = new JPanel(); 51 // vBox.setLayout(new BoxLayout(vBox, BoxLayout.Y_AXIS)); 52 // 53 // JPanel contentsPanel = new JPanel(); 54 // contentsPanel.add(contents); 55 // contentsPanel.setAlignmentX(Component.CENTER_ALIGNMENT); 56 // 57 // vBox.add(contentsPanel); 58 59 getContentPane().add(contents, BorderLayout.CENTER); 60 61 JPanel buttonsBox = new JPanel(); 62 buttonsBox.setLayout(new BoxLayout(buttonsBox, BoxLayout.X_AXIS)); 63 buttonsBox.setAlignmentX(Component.CENTER_ALIGNMENT); 64 okButton = new JButton("OK"); 65 cancelButton = new JButton("Cancel"); 66 buttonsBox.add(Box.createHorizontalGlue()); 67 buttonsBox.add(okButton); 68 buttonsBox.add(Box.createHorizontalStrut(20)); 69 buttonsBox.add(cancelButton); 70 buttonsBox.add(Box.createHorizontalGlue()); 71 72 Box vBox = Box.createVerticalBox(); 73 vBox.add(Box.createVerticalStrut(10)); 74 vBox.add(buttonsBox); 75 vBox.add(Box.createVerticalStrut(10)); 76 77 getContentPane().add(vBox, BorderLayout.SOUTH); 78 79 80 okButton.addActionListener(new ActionListener() { 81 public void actionPerformed(ActionEvent e) { 82 userHasPressedOK = true; 83 hide(); 84 } 85 }); 86 87 cancelButton.addActionListener(new ActionListener() { 88 public void actionPerformed(ActionEvent e) { 89 userHasPressedCancel = true; 90 hide(); 91 } 92 }); 93 } 94 95 public void dispose(){ 96 MainFrame.getGuiRoots().remove(this); 97 super.dispose(); 98 } 99 100 101 public void show(){ 102 setModal(true); 103 userHasPressedOK = false; 104 userHasPressedCancel = false; 105 super.show(); 106 } 107 108 /** 109 * @returns true if the user has selected the "OK" button. 110 */ 111 public static boolean showDialog(Component parentComponent, 112 Component contents, 113 String title){ 114 //construct the dialog 115 Window parent = null; 116 if(parentComponent != null){ 117 parent = SwingUtilities.getWindowAncestor(parentComponent); 118 } 119 OkCancelDialog dialog; 120 if(parent == null) dialog = new OkCancelDialog(title, contents); 121 else if(parent instanceof Frame){ 122 dialog = new OkCancelDialog((Frame)parent, title, contents); 123 } else{ 124 dialog = new OkCancelDialog((Dialog)parent, title, contents); 125 } 126 127 //position the dialog 128 dialog.pack(); 129 dialog.setLocationRelativeTo(parentComponent); 130 131 //kalina: make it fit the screen 132 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 133 Dimension dialogSize = dialog.getSize(); 134 if (dialogSize.height > screenSize.height) 135 dialogSize.height = screenSize.height; 136 if (dialogSize.width > screenSize.width) 137 dialogSize.width = screenSize.width; 138 dialog.setSize(dialogSize); 139 //end kalina 140 141 //show the dialog 142 dialog.show(); 143 return dialog.userHasPressedOK; 144 } 145 146 protected JButton okButton; 147 protected JButton cancelButton; 148 protected boolean userHasPressedOK; 149 protected static boolean userHasPressedCancel; 150 }
|
OkCancelDialog |
|