1 /*
2 J-Bird net/sourceforge/jbird/awt/BlankDialog.java
3
4 Copyright 2001, 2002, 2003 Dick Repasky
5 */
6 package net.sourceforge.jbird.awt;
7
8 import java.awt.Button;
9 import java.awt.Dialog;
10 import java.awt.Frame;
11 import java.awt.GridBagConstraints;
12 import java.awt.GridBagLayout;
13 import java.awt.Insets;
14 import java.awt.event.ActionEvent;
15 import java.awt.event.ActionListener;
16 import java.awt.event.WindowAdapter;
17 import java.awt.event.WindowEvent;
18
19 /**
20 * A Dialog that contains only a Commit and Cancel
21 * button. The idea is that the developer will
22 * instantiate the class, add single panel of
23 * content, pack and show. Upon return, the
24 * developer can use the isCommitted or isCanceled
25 * methods to determine whether the user pushed the
26 * commit or cancel button.
27 * <p>
28 * The dialog is built to be modal when shown.
29 * <p>
30 * Build your panel, then use the getGridBagConstraints
31 * method to obtain a GridBagConstraints to add your data
32 * panel to the dialog.
33 * <p>
34 * Nitty gritty. The GridBagConstraints returned by
35 * the GridBag constraints has
36 * <ul>
37 * <li>gridx = 1</li>
38 * <li>gridy = 1</li>
39 * <li>gridwidth = 3</li>
40 * <li>gridheight = 1.</li>
41 * </ul>
42 * FYI the commit button is at
43 * <ul>
44 * <li>gridx = 1</li>
45 * <li>gridy = 3</li>
46 * <li>gridwidth = 1</li>
47 * <li>gridheight = 1.</li>
48 * </ul>
49 * FYI the cancel button is at
50 * <ul>
51 * <li>gridx = 3</li>
52 * <li>gridy = 3</li>
53 * <li>gridwidth = 1</li>
54 * <li>gridheight = 1.</li>
55 * </ul>
56 *
57 */
58
59 public class BlankDialog extends Dialog {
60 protected Button commitbutton = new Button("Commit");
61 protected Button cancelbutton = new Button("Cancel");
62
63 protected boolean commit = false;
64
65 // make all but owner into assignment statements
66 public BlankDialog (Frame owner) {
67 super(owner, "", true);
68 setup();
69 }
70 public BlankDialog (Frame owner, String title) {
71 super(owner, title, true);
72 setup();
73 }
74
75 private final void setup() {
76 setLayout(new GridBagLayout());
77 GridBagConstraints c = new GridBagConstraints();
78 Insets insets = new Insets(5,8,5,8);
79 c.insets = insets;
80
81 c.gridx = 1;
82 c.gridy = 3;
83 c.gridwidth = 1;
84 c.gridheight = 1;
85 c.anchor = GridBagConstraints.WEST;
86 add(commitbutton, c);
87
88 c.gridx = 3;
89 c.anchor = GridBagConstraints.EAST;
90 add(cancelbutton, c);
91 listenerBuilder();
92 }
93
94 private void listenerBuilder () {
95 class CommitBAction implements ActionListener {
96 public void actionPerformed(ActionEvent e) {
97 commit = true;
98 hide();
99 }
100 }
101 class CancelBAction implements ActionListener {
102 public void actionPerformed(ActionEvent e) {
103 commit = false;
104 hide();
105 }
106 }
107 class SimpleDWindowCloseListener extends WindowAdapter {
108 public void windowClosing(WindowEvent e) {
109 commit = false;
110 hide();
111 }
112 }
113
114 // Add handlers
115 CommitBAction commitbaction = new CommitBAction();
116 commitbutton.addActionListener(commitbaction);
117 CancelBAction cancelbaction = new CancelBAction();
118 cancelbutton.addActionListener(cancelbaction);
119 SimpleDWindowCloseListener windowkilled = new
120 SimpleDWindowCloseListener();
121 addWindowListener(windowkilled);
122 }
123
124 public final void setCommitLabel(String text) {
125 commitbutton.setLabel(text);
126 }
127
128 public final void setCancelLabel(String text) {
129 cancelbutton.setLabel(text);
130 }
131
132 /**
133 * Returns true if the commit button was pressed,
134 * and false if the cancel button was pressed.
135 *
136 */
137
138 public final boolean isCommitted() {
139 return commit;
140 }
141
142 /**
143 * Returns true if the cancel button was pressed,
144 * and false if the commit button was pressed.
145 *
146 */
147
148 public final boolean isCancelled() {
149 return ! commit;
150 }
151
152 /**
153 * Returns a GridBagConstraints object that
154 * is suitable for adding a single panel to
155 * a BlankDialog.
156 *
157 */
158
159 public static GridBagConstraints getContentConstraints() {
160 GridBagConstraints answer = new GridBagConstraints();
161 answer.gridx = 1;
162 answer.gridy = 1;
163 answer.gridwidth = 3;
164 answer.gridheight = 1;
165 return answer;
166 }
167
168 }