1 30 31 package rcm.awt; 32 33 import java.awt.*; 34 import java.awt.event.*; 35 import java.util.Vector ; 36 import rcm.util.Win; 37 38 42 public class PopupDialog extends Dialog { 43 44 public static final int YES = 0; 45 public static final int OK = 0; 46 public static final int NO = 1; 47 public static final int CANCEL = 2; 48 49 Component parent; 50 int answer = -1; 51 String text; 52 53 TextField textfield; 54 Button okButton, noButton, cancelButton; 55 56 public static String ask (Component comp, String topic, String question, String defaultAnswer) { 57 PopupDialog d = new PopupDialog (comp, topic, true, 58 question, defaultAnswer, 59 "OK", null, "Cancel"); 60 d.show (); 61 switch (d.getAnswer ()) { 62 case OK: 63 return d.getText (); 64 default: 65 return null; 66 } 67 } 68 69 public static String ask (Component comp, String topic, String question) { 70 return ask (comp, topic, question, ""); 71 } 72 73 public static boolean okcancel (Component comp, String topic, String question) { 74 PopupDialog d = new PopupDialog (comp, topic, true, 75 question, null, 76 "OK", null, "Cancel"); 77 d.show (); 78 return (d.getAnswer () == OK); 79 } 80 81 public static boolean yesno (Component comp, String topic, String question) { 82 PopupDialog d = new PopupDialog (comp, topic, true, 83 question, null, 84 "Yes", "No", null); 85 d.show (); 86 return (d.getAnswer () == YES); 87 } 88 89 public static int yesnocancel (Component comp, String topic, String question) { 90 PopupDialog d = new PopupDialog (comp, topic, true, 91 question, null, 92 "Yes", "No", "Cancel"); 93 d.show (); 94 return d.getAnswer (); 95 } 96 97 public static void warn (Component comp, String topic, String message) { 98 PopupDialog d = new PopupDialog (comp, topic, true, 99 message, null, 100 "OK", null, null); 101 d.show (); 102 } 103 104 public static String currentDirectory = ""; 105 106 public static String askFilename (Component comp, String topic, 107 String defaultFilename, boolean loading) { 108 try { 109 FileDialog fd = new FileDialog (Win.findFrame(comp), 110 topic, 111 loading ? FileDialog.LOAD : FileDialog.SAVE); 112 113 if (currentDirectory != null) 114 fd.setDirectory (currentDirectory); 115 if (defaultFilename != null) 116 fd.setFile (defaultFilename); 117 118 fd.show (); 119 120 String dir = fd.getDirectory(); 121 String file = fd.getFile (); 122 123 if (dir == null || file == null) 124 return null; 125 126 currentDirectory = dir; 127 return dir + file; 128 } catch (AWTError e) { 129 return ask (comp, topic, "Filename:", defaultFilename); 130 } 131 } 132 133 public static String askDirectory (Component comp, String topic, 134 String defaultFilename, boolean loading) { 135 try { 136 FileDialog fd = new FileDialog (Win.findFrame(comp), 137 topic, 138 loading ? FileDialog.LOAD : FileDialog.SAVE); 139 140 if (currentDirectory != null) 141 fd.setDirectory (currentDirectory); 142 if (defaultFilename != null) 143 fd.setFile (defaultFilename); 144 145 fd.show (); 146 147 String dir = fd.getDirectory(); 148 149 if (dir != null) 150 currentDirectory = dir; 151 152 return dir; 153 } catch (AWTError e) { 154 return ask (comp, topic, "Directory:", defaultFilename); 155 } 156 } 157 158 159 public PopupDialog (Component parent, String title, boolean modal) { 160 super (Win.findFrameOrMakeFrame (parent), title, modal); 161 this.parent = parent; 162 } 163 164 public PopupDialog (Component parent, String title, boolean modal, 165 String question, String initialEntry, 166 String okOrYes, String no, String cancel) { 167 this (parent, title, modal); 168 169 if (parent != null) 170 setFont (parent.getFont ()); 171 172 Panel middle = new Panel (); 173 add ("Center", BorderPanel.wrap (middle, 10, 10, 10, 5)); 174 middle.setLayout (new BorderLayout ()); 175 MultiLineLabel questionLabel = new MultiLineLabel (question, Label.LEFT); 176 middle.add ("Center", questionLabel); 177 if (initialEntry != null) { 178 textfield = new TextField (Math.max (40, initialEntry.length()+1)); 179 middle.add ("South", textfield); 180 textfield.setText (initialEntry); 181 textfield.selectAll (); 182 textfield.addActionListener (new ActionListener () { 183 public void actionPerformed (ActionEvent event) { 184 answer = OK; 185 close (); 186 } 187 }); 188 } 189 190 Panel bottom = new Panel (); 191 add ("South", bottom); 192 193 if (okOrYes != null) { 194 okButton = new Button (okOrYes); 195 okButton.addActionListener (new ActionListener () { 196 public void actionPerformed (ActionEvent event) { 197 answer = OK; 198 close (); 199 } 200 }); 201 bottom.add (okButton); 202 } 203 204 if (no != null) { 205 noButton = new Button (no); 206 noButton.addActionListener (new ActionListener () { 207 public void actionPerformed (ActionEvent event) { 208 answer = NO; 209 close (); 210 } 211 }); 212 bottom.add (noButton); 213 } 214 215 if (cancel != null) { 216 cancelButton = new Button (cancel); 217 cancelButton.addActionListener (new ActionListener () { 218 public void actionPerformed (ActionEvent event) { 219 answer = CANCEL; 220 close (); 221 } 222 }); 223 bottom.add (cancelButton); 224 } 225 226 addWindowListener (new WindowAdapter () { 227 public void windowClosing (WindowEvent event) { 228 if (cancelButton != null) { 229 answer = CANCEL; 230 close (); 231 } 232 else if (noButton == null && cancelButton == null) { 233 answer = OK; 234 close (); 235 } 236 } 237 }); 238 239 pack (); 246 } 247 248 public static void centerWindow (Window window, Component ref) { 249 Dimension size = window.getSize(); 250 Dimension refSize = (ref != null) ? ref.getSize() : Toolkit.getDefaultToolkit().getScreenSize(); 251 Point origin = (ref != null) ? ref.getLocationOnScreen () : new Point (0, 0); 252 253 if (refSize != null) { 254 int x = Math.max (0, origin.x + (refSize.width - size.width) / 2); 255 int y = Math.max (0, origin.y + (refSize.height - size.height) / 2); 256 window.setLocation (x, y); 257 } 258 } 259 260 public void show () { 261 centerWindow (this, parent); 262 super.show (); 263 if (textfield != null) 264 textfield.requestFocus (); 265 } 266 267 public int getAnswer () { 268 return answer; 269 } 270 271 public void setAnswer (int answer) { 272 this.answer = answer; 273 } 274 275 public String getText () { 276 return text; 277 } 278 279 Vector listeners = new Vector (); 280 281 public synchronized void addPopupListener (PopupListener listener) { 282 listeners.addElement (listener); 283 } 284 285 public synchronized void removePopupListener (PopupListener listener) { 286 listeners.removeElement (listener); 287 } 288 289 public synchronized void close () { 290 text = (answer == OK && textfield != null) 291 ? textfield.getText () : null; 292 293 dispose (); 294 if (parent == null) 295 ((Frame)getParent()).dispose (); 296 else 297 parent.requestFocus (); 298 299 if (answer != -1) { 300 PopupEvent e = new PopupEvent (answer, text); 301 for (int i=0; i<listeners.size (); ++i) { 302 PopupListener p = (PopupListener) (listeners.elementAt (i)); 303 switch (e.getID ()) { 304 case YES: 305 p.yes (e); 306 break; 307 case NO: 308 p.no (e); 309 break; 310 case CANCEL: 311 p.cancel (e); 312 break; 313 } 314 } 315 } 316 317 try { 318 finalize (); 319 } catch (Throwable t) { 320 throw new RuntimeException (t.toString()); 321 } 322 } 323 324 325 329 public static void main (String [] args) { 330 String name = ask (null, "Enter Name", "Enter your full name:"); 331 332 if (name != null) { 333 switch (yesnocancel (null, "Confirm", 334 "Hello, " + name + ".\nIs this your name?")) { 335 case PopupDialog.YES: 336 if (okcancel (null, "Thanks", 337 "Great!\nDo you want to play a game?")) { 338 warn (null, "Sorry", "Too bad, my mommy won't let me out of the house."); 339 } 340 break; 341 342 case PopupDialog.NO: 343 warn (null, "D'oh", "Oops. My bad."); 344 break; 345 } 346 } 347 348 System.exit (0); 349 } 350 351 } 352 | Popular Tags |