1 package com.quikj.client.beans; 2 import java.awt.*; 3 import java.awt.event.*; 4 5 public class YesNoDialog extends Dialog 6 { 7 public YesNoDialog(Frame parent, String title, 8 String text, 9 String ok_text, String cancel_text, 10 boolean modal, 11 long timeout) 12 { 13 super(parent, title, modal); 14 15 GridBagLayout gbl = new GridBagLayout(); 16 GridBagConstraints gbc = new GridBagConstraints(); 17 setLayout(gbl); 18 19 Panel panel1 = new Panel(); 20 panel1.setLayout(gbl); 21 22 addToContainer(panel1, 23 gbl, 24 gbc, 25 new Label(text), 26 0, 27 0, 28 1, 29 1, 30 GridBagConstraints.NONE, 31 GridBagConstraints.NORTHWEST, 32 0, 0); 33 34 addToContainer(this, 36 gbl, 37 gbc, 38 panel1, 39 0, 40 0, 41 5, 42 1, 43 GridBagConstraints.BOTH, 44 GridBagConstraints.NORTHWEST, 45 1, 0, 46 new Insets(5, 5, 5, 5)); 47 48 Panel panel2 = new Panel(); 49 panel2.setLayout(gbl); 50 51 okButton = new Button(ok_text); 52 okButton.addActionListener(new OkListener()); 53 addToContainer(panel2, 54 gbl, 55 gbc, 56 okButton, 57 0, 58 0, 59 1, 60 1, 61 GridBagConstraints.NONE, 62 GridBagConstraints.CENTER, 63 0, 0, 64 new Insets(0, 0, 0, 5)); 65 66 if (cancel_text != null) 67 { 68 cancelButton = new Button(cancel_text); 69 cancelButton.addActionListener(new CancelListener()); 70 addToContainer(panel2, 71 gbl, 72 gbc, 73 cancelButton, 74 1, 75 0, 76 1, 77 1, 78 GridBagConstraints.NONE, 79 GridBagConstraints.CENTER, 80 0, 0); 81 } 82 83 addToContainer(this, 85 gbl, 86 gbc, 87 panel2, 88 0, 89 1, 90 1, 91 1, 92 GridBagConstraints.HORIZONTAL, 93 GridBagConstraints.CENTER, 94 1, 0, 95 new Insets(0, 5, 5, 5)); 96 97 pack(); 98 Rectangle pbounds = parent.getBounds(); 99 Point mid = new Point(pbounds.x + (pbounds.width/2), pbounds.y + (pbounds.height/2)); 100 Rectangle bounds = getBounds(); 101 int x = mid.x - (bounds.width/2); 102 int y = mid.y - (bounds.height/2); 103 if (x < 0) x = pbounds.x; 104 if (y < 0) y = pbounds.y; 105 setBounds(x, y, bounds.width, bounds.height); 106 107 if (timeout > 0) 108 { 109 timer = new YesNoDialog.DialogTimer(timeout); 110 timer.start(); 111 } 112 113 show(); 114 if (modal == true) 115 { 116 if (timer != null) 117 { 118 if (timer.isExpired() == false) 119 { 120 timer.dispose(); 121 } 122 timer = null; 123 } 124 } 125 } 126 127 public YesNoDialog(Frame parent, String title, 128 String text, 129 String ok_text, String cancel_text, 130 boolean modal) 131 { 132 this (parent, title, text, ok_text, cancel_text, modal, 0); 133 } 134 135 public YesNoDialog(Frame parent, String title, String text, 136 String ok_text, String cancel_text) 137 { 138 this (parent, title, text, ok_text, cancel_text, true); 139 } 140 141 public boolean okSelected() 142 { 143 return selection; 144 } 145 146 private void addToContainer(Container cont, 147 GridBagLayout gbl, 148 GridBagConstraints gbc, 149 Component comp, 150 int gridx, 151 int gridy, 152 int gridwidth, 153 int gridheight, 154 int fill, 155 int anchor, 156 int weightx, 157 int weighty, 158 Insets insets) 159 { 160 gbc.gridx = gridx; 161 gbc.gridy = gridy; 162 gbc.gridwidth = gridwidth; 163 gbc.gridheight = gridheight; 164 gbc.fill = fill; 165 gbc.anchor = anchor; 166 gbc.weightx = weightx; 167 gbc.weighty = weighty; 168 169 if (insets != null) 170 { 171 gbc.insets = insets; 172 } 173 174 gbl.setConstraints(comp, gbc); 175 cont.add(comp); 176 } 177 178 private void addToContainer(Container cont, 179 GridBagLayout gbl, 180 GridBagConstraints gbc, 181 Component comp, 182 int gridx, 183 int gridy, 184 int gridwidth, 185 int gridheight, 186 int fill, 187 int anchor, 188 int weightx, 189 int weighty) 190 { 191 addToContainer(cont, gbl, gbc, comp, gridx, gridy, gridwidth, gridheight, 192 fill, anchor, weightx, weighty, null); 193 } 194 195 class CancelListener implements ActionListener 196 { 197 public void actionPerformed(ActionEvent e) 198 { 199 if (timer != null) 200 { 201 if (timer.isExpired() == false) 202 { 203 timer.dispose(); 204 } 205 } 206 207 YesNoDialog.this.dispose(); 208 } 209 } 210 211 class OkListener implements ActionListener 212 { 213 public void actionPerformed(ActionEvent e) 214 { 215 selection = true; 216 217 if (timer != null) 218 { 219 if (timer.isExpired() == false) 220 { 221 timer.dispose(); 222 } 223 } 224 225 YesNoDialog.this.dispose(); 226 } 227 } 228 229 class DialogTimer extends Thread 230 { 231 public DialogTimer(long time) 232 { 233 this.time = time; 234 } 235 236 public void dispose() 237 { 238 if (isAlive() == true) 239 { 240 interrupt(); 241 } 242 } 243 244 public void run() 245 { 246 try 247 { 248 sleep(time); 249 expired = true; 250 YesNoDialog.this.dispose(); 251 } 252 catch (InterruptedException ex) 253 { 254 } 255 } 256 257 public boolean isExpired() 258 { 259 return expired; 260 } 261 262 private long time; 263 private boolean expired = false; 264 } 265 266 private Button okButton; 267 private Button cancelButton; 268 269 private boolean selection = false; 270 private DialogTimer timer = null; 271 } 272 273 274 275 276 277 | Popular Tags |