001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.cleverphl.gui;
025:
026: import jacareto.cleverphl.CleverPHL;
027:
028: import java.awt.BorderLayout;
029: import java.awt.Dialog;
030: import java.awt.Frame;
031: import java.awt.Point;
032: import java.awt.Window;
033: import java.awt.event.ActionEvent;
034: import java.awt.event.ActionListener;
035:
036: import javax.swing.JButton;
037: import javax.swing.JDialog;
038: import javax.swing.JLabel;
039: import javax.swing.JPanel;
040: import javax.swing.border.EmptyBorder;
041:
042: /**
043: * A Message Dialog with a line of text (the message) and two buttons, a "yes" and a "no" button.
044: *
045: * <p>
046: * This dialog is modal and not resizable. It is set to the middle of the screen.
047: * </p>
048: *
049: * <p>
050: * After the dialog has been closed, the method {@link #getValue()} returns the value
051: * <code>YES</code> or <code>NO</code>.
052: * </p>
053: *
054: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
055: * @version 0.9
056: */
057: public class CleverPHLConfirmDialog extends JDialog implements
058: ActionListener {
059: /** The value for the pressed <code>YES</code> button. */
060: public static final int YES = 0;
061:
062: /** The value for the pressed <code>NO</code> button. */
063: public static final int NO = 1;
064:
065: /** The CleverPHL instance. */
066: private CleverPHL cleverPHL;
067:
068: /** The chosen value. */
069: private int value;
070:
071: /** The "yes"-button. */
072: private JButton yesButton;
073:
074: /** The "no"-button. */
075: private JButton noButton;
076:
077: /**
078: * Creates a new confirm dialog with a specified title, owner frame, message, "yes"-button text
079: * and "no"-button text. The dialog is set visible in this constructor.
080: *
081: * @param cleverPHL the CleverPHL instance
082: * @param owner The frame which is the owner of this dialog
083: * @param title The title of the dialog
084: * @param message The message
085: * @param yesButtonText The text of the "yes"-button
086: * @param noButtonText The text of the "no"-button
087: */
088: public CleverPHLConfirmDialog(CleverPHL cleverPHL, Frame owner,
089: String title, String message, String yesButtonText,
090: String noButtonText) {
091: super (owner, title, true);
092: this .cleverPHL = cleverPHL;
093: setLocation(owner);
094: create(message, yesButtonText, noButtonText);
095: }
096:
097: /**
098: * Creates a new confirm dialog with a specified title, owner dialog, message, "yes"-button
099: * text and "no"-button text. The dialog is set visible in this constructor.
100: *
101: * @param cleverPHL the CleverPHL instance
102: * @param owner The frame which is the owner of this dialog
103: * @param title The title of the frame
104: * @param message The message
105: * @param yesButtonText The text of the "yes"-button
106: * @param noButtonText The text of the "no"-button
107: */
108: public CleverPHLConfirmDialog(CleverPHL cleverPHL, Dialog owner,
109: String title, String message, String yesButtonText,
110: String noButtonText) {
111: super (owner, title, true);
112: this .cleverPHL = cleverPHL;
113: setLocation(owner);
114: create(message, yesButtonText, noButtonText);
115: }
116:
117: /**
118: * Creates the components and the layout of the dialog.
119: *
120: * @param message the message to be displayed
121: * @param yesButtonText The text of the "yes"-button
122: * @param noButtonText The text of the "no"-button
123: */
124: private void create(String message, String yesButtonText,
125: String noButtonText) {
126: setName(cleverPHL.getCustomization().getString(
127: "Components.JacaretoComponent", "JACARETO_COMPONENT"));
128:
129: // creating the buttons
130: yesButton = new JButton(yesButtonText);
131: yesButton.addActionListener(this );
132: noButton = new JButton(noButtonText);
133: noButton.addActionListener(this );
134:
135: getContentPane().setLayout(new BorderLayout());
136:
137: JPanel labelPanel = new JPanel();
138: labelPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
139: labelPanel.add(new JLabel(message));
140: getContentPane().add(labelPanel, BorderLayout.NORTH);
141:
142: JPanel buttonPanel = new JPanel();
143: buttonPanel.add(yesButton);
144: buttonPanel.add(noButton);
145: buttonPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
146: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
147: pack();
148: setResizable(false);
149: setVisible(true);
150: }
151:
152: /**
153: * Sets the location of the message dialog relativ to the owners position
154: *
155: * @param owner the owner
156: */
157: public void setLocation(Window owner) {
158: Point ownerLoc = owner.getLocation();
159: setLocation(((int) ownerLoc.getX()) + 20, ((int) ownerLoc
160: .getY()) + 20);
161: }
162:
163: /**
164: * Returns the value of the confirm dialog.
165: *
166: * @return <code>YES</code> if the "yes"-button has been pressed, <code>NO</code> if the
167: * "no"-button has been pressed.
168: */
169: public int getValue() {
170: return value;
171: }
172:
173: /**
174: * Invoked when the button was pressed.
175: *
176: * @param e The action event
177: */
178: public void actionPerformed(ActionEvent e) {
179: if (e.getSource() == yesButton) {
180: value = YES;
181: } else {
182: value = NO;
183: }
184:
185: dispose();
186: }
187: }
|