1 package com.quikj.application.utilities.sqlc; 2 3 4 import java.awt.*; 5 import java.awt.event.*; 6 import java.util.*; 7 8 public class LoginDialog extends Dialog 9 { 10 private TextField userWidget; 11 private TextField passwordWidget; 12 private TextField hostWidget; 13 private TextField useWidget; 14 private Button okButton; 15 private Button resetButton; 16 private Button exitButton; 17 18 private String host = ""; 19 private String user = ""; 20 private String password = ""; 21 private String use = ""; 22 23 private static Vector userList = new Vector(); 24 private static Vector hostList = new Vector(); 25 private static Vector useList = new Vector(); 26 27 private int userIndex = userList.size(); 28 private int hostIndex = hostList.size(); 29 private int useIndex = useList.size(); 30 31 private SpecialKeyListener spKeyListener = new SpecialKeyListener(); 32 33 public LoginDialog(Frame parent, String text, boolean exit_button, String user_name) 34 { 35 super(parent, "Login", true); 36 37 GridBagLayout gbl = new GridBagLayout(); 38 GridBagConstraints gbc = new GridBagConstraints(); 39 40 setLayout(gbl); 41 42 int index = 0; 43 if (text.length() != 0) 44 { 45 Panel panel0 = new Panel(); 46 panel0.setLayout(gbl); 47 48 addToContainer(panel0, 49 gbl, 50 gbc, 51 new Label(text), 52 0, 53 0, 54 1, 55 1, 56 GridBagConstraints.NONE, 57 GridBagConstraints.NORTHWEST, 58 0, 0); 59 60 addToContainer(this, 61 gbl, 62 gbc, 63 panel0, 64 0, 65 index++, 66 1, 67 1, 68 GridBagConstraints.NONE, 69 GridBagConstraints.NORTHWEST, 70 0, 0); 71 } 72 73 Panel panel1 = new Panel(); 74 panel1.setLayout(gbl); 75 76 addToContainer(panel1, 77 gbl, 78 gbc, 79 new Label("User"), 80 0, 81 0, 82 1, 83 1, 84 GridBagConstraints.NONE, 85 GridBagConstraints.NORTHWEST, 86 0, 0); 87 88 userWidget = new TextField(user_name, 15); 89 if (user_name.length() > 0) 90 { 91 userWidget.setEditable(false); 92 } 93 addToContainer(panel1, 94 gbl, 95 gbc, 96 userWidget, 97 1, 98 0, 99 1, 100 1, 101 GridBagConstraints.NONE, 102 GridBagConstraints.NORTHWEST, 103 0, 0); 104 userWidget.addKeyListener(spKeyListener); 105 106 addToContainer(panel1, 107 gbl, 108 gbc, 109 new Label("Password"), 110 0, 111 1, 112 1, 113 1, 114 GridBagConstraints.NONE, 115 GridBagConstraints.NORTHWEST, 116 0, 0); 117 118 passwordWidget = new TextField(15); 119 passwordWidget.setEchoChar('*'); 120 addToContainer(panel1, 121 gbl, 122 gbc, 123 passwordWidget, 124 1, 125 1, 126 1, 127 1, 128 GridBagConstraints.NONE, 129 GridBagConstraints.NORTHWEST, 130 0, 0, 131 new Insets(5, 5, 0, 5)); 132 passwordWidget.addKeyListener(spKeyListener); 133 134 addToContainer(panel1, 135 gbl, 136 gbc, 137 new Label("Host"), 138 0, 139 2, 140 1, 141 1, 142 GridBagConstraints.NONE, 143 GridBagConstraints.NORTHWEST, 144 0, 0); 145 146 hostWidget = new TextField(15); 147 addToContainer(panel1, 148 gbl, 149 gbc, 150 hostWidget, 151 1, 152 2, 153 1, 154 1, 155 GridBagConstraints.NONE, 156 GridBagConstraints.NORTHWEST, 157 0, 0); 158 hostWidget.addKeyListener(spKeyListener); 159 160 addToContainer(panel1, 161 gbl, 162 gbc, 163 new Label("Use"), 164 0, 165 3, 166 1, 167 1, 168 GridBagConstraints.NONE, 169 GridBagConstraints.NORTHWEST, 170 0, 0); 171 172 useWidget = new TextField(15); 173 addToContainer(panel1, 174 gbl, 175 gbc, 176 useWidget, 177 1, 178 3, 179 1, 180 1, 181 GridBagConstraints.NONE, 182 GridBagConstraints.NORTHWEST, 183 0, 0); 184 useWidget.addKeyListener(spKeyListener); 185 186 187 addToContainer(this, 189 gbl, 190 gbc, 191 panel1, 192 0, 193 index++, 194 1, 195 1, 196 GridBagConstraints.NONE, 197 GridBagConstraints.NORTHWEST, 198 0, 0); 199 200 Panel panel2 = new Panel(); 201 panel2.setLayout(gbl); 202 203 okButton = new Button(" OK "); 204 okButton.addActionListener(new OkListener()); 205 addToContainer(panel2, 206 gbl, 207 gbc, 208 okButton, 209 0, 210 0, 211 1, 212 1, 213 GridBagConstraints.NONE, 214 GridBagConstraints.CENTER, 215 0, 0); 216 okButton.addKeyListener(spKeyListener); 217 218 resetButton = new Button(" Reset "); 219 resetButton.addActionListener(new ResetListener()); 220 addToContainer(panel2, 221 gbl, 222 gbc, 223 resetButton, 224 1, 225 0, 226 1, 227 1, 228 GridBagConstraints.NONE, 229 GridBagConstraints.CENTER, 230 0, 0, 231 new Insets(0, 10, 0, 0)); 232 233 if (exit_button == true) 234 { 235 exitButton = new Button(" Exit "); 236 exitButton.addActionListener(new ExitListener()); 237 addToContainer(panel2, 238 gbl, 239 gbc, 240 exitButton, 241 2, 242 0, 243 1, 244 1, 245 GridBagConstraints.NONE, 246 GridBagConstraints.CENTER, 247 0, 0, 248 new Insets(0, 10, 0, 0)); 249 } 250 251 addToContainer(this, 253 gbl, 254 gbc, 255 panel2, 256 0, 257 index++, 258 1, 259 1, 260 GridBagConstraints.HORIZONTAL, 261 GridBagConstraints.NORTHWEST, 262 0, 0, 263 new Insets(5, 0, 5, 0)); 264 pack(); 265 Rectangle pbounds = parent.getBounds(); 266 Point mid = new Point(pbounds.x + (pbounds.width/2), pbounds.y + (pbounds.height/2)); 267 Rectangle bounds = getBounds(); 268 int x = mid.x - (bounds.width/2); 269 int y = mid.y - (bounds.height/2); 270 if (x < 0) x = pbounds.x; 271 if (y < 0) y = pbounds.y; 272 setBounds(x, y, bounds.width, bounds.height); 273 274 show(); 275 } 276 277 public LoginDialog(Frame parent) 278 { 279 this(parent, "", true, ""); 280 } 281 282 public String getUser() 283 { 284 return new String (user); 285 } 286 287 public String getPassword() 288 { 289 return new String (password); 290 } 291 292 public String getHost() 293 { 294 return new String (host); 295 } 296 297 public String getDb() 298 { 299 return new String (use); 300 } 301 302 private void addToContainer(Container cont, 303 GridBagLayout gbl, 304 GridBagConstraints gbc, 305 Component comp, 306 int gridx, 307 int gridy, 308 int gridwidth, 309 int gridheight, 310 int fill, 311 int anchor, 312 int weightx, 313 int weighty, 314 Insets insets) 315 { 316 gbc.gridx = gridx; 317 gbc.gridy = gridy; 318 gbc.gridwidth = gridwidth; 319 gbc.gridheight = gridheight; 320 gbc.fill = fill; 321 gbc.anchor = anchor; 322 gbc.weightx = weightx; 323 gbc.weighty = weighty; 324 325 if (insets != null) 326 { 327 gbc.insets = insets; 328 } 329 330 gbl.setConstraints(comp, gbc); 331 cont.add(comp); 332 } 333 334 private void addToContainer(Container cont, 335 GridBagLayout gbl, 336 GridBagConstraints gbc, 337 Component comp, 338 int gridx, 339 int gridy, 340 int gridwidth, 341 int gridheight, 342 int fill, 343 int anchor, 344 int weightx, 345 int weighty) 346 { 347 addToContainer(cont, gbl, gbc, comp, gridx, gridy, gridwidth, gridheight, 348 fill, anchor, weightx, weighty, null); 349 } 350 351 private void reset() 352 { 353 userWidget.setText(""); 354 passwordWidget.setText(""); 355 hostWidget.setText(""); 356 useWidget.setText(""); 357 } 358 359 private void readFromWidgetsAndClose() 360 { 361 user = userWidget.getText().trim(); 362 if ((user.length() > 0) && (userList.contains(user) == false)) 363 { 364 userList.addElement(user); 365 } 366 367 password = passwordWidget.getText().trim(); 368 369 host = hostWidget.getText().trim(); 370 if ((host.length() > 0) && (hostList.contains(host) == false)) 371 { 372 hostList.addElement(host); 373 } 374 375 use = useWidget.getText().trim(); 376 if ((use.length() > 0) && (useList.contains(use) == false)) 377 { 378 useList.addElement(use); 379 } 380 381 dispose(); 383 } 384 385 class SpecialKeyListener extends KeyAdapter 386 { 387 public void keyPressed(KeyEvent e) 388 { 389 int key = e.getKeyCode(); 390 Component parent = e.getComponent(); 391 392 if (key == KeyEvent.VK_ENTER) 393 { 394 if (parent == userWidget) 395 { 396 passwordWidget.requestFocus(); 397 } 398 else if (parent == passwordWidget) 399 { 400 hostWidget.requestFocus(); 401 } 402 else if (parent == hostWidget) 403 { 404 useWidget.requestFocus(); 405 } 406 else if (parent == useWidget) 407 { 408 okButton.requestFocus(); 409 } 410 else if (parent == okButton) 411 { 412 readFromWidgetsAndClose(); 413 } 414 } 415 else if (key == KeyEvent.VK_UP) 416 { 417 if (parent == userWidget) 418 { 419 userIndex--; 420 421 if (userIndex < 0) 422 { 423 userIndex = 0; 424 } 425 426 if (userList.size() > 0) 427 { 428 userWidget.setText((String )userList.elementAt(userIndex)); 429 } 430 else 431 { 432 userWidget.setText(""); 433 } 434 435 } 436 else if (parent == hostWidget) 437 { 438 hostIndex--; 439 440 if (hostIndex < 0) 441 { 442 hostIndex = 0; 443 } 444 445 if (hostList.size() > 0) 446 { 447 hostWidget.setText((String )hostList.elementAt(hostIndex)); 448 } 449 else 450 { 451 hostWidget.setText(""); 452 } 453 454 } 455 else if (parent == useWidget) 456 { 457 useIndex--; 458 459 if (useIndex < 0) 460 { 461 useIndex = 0; 462 } 463 464 if (useList.size() > 0) 465 { 466 useWidget.setText((String )useList.elementAt(useIndex)); 467 } 468 else 469 { 470 useWidget.setText(""); 471 } 472 } 473 } 474 else if (key == KeyEvent.VK_DOWN) 475 { 476 if (parent == userWidget) 477 { 478 userIndex++; 479 480 if (userIndex >= userList.size()) 481 { 482 userIndex = userList.size(); 483 userWidget.setText(""); 484 } 485 else 486 { 487 if (userList.size() > 0) 488 { 489 userWidget.setText((String )userList.elementAt(userIndex)); 490 } 491 else 492 { 493 userWidget.setText(""); 494 } 495 } 496 } 497 else if (parent == hostWidget) 498 { 499 hostIndex++; 500 501 if (hostIndex >= hostList.size()) 502 { 503 hostIndex = hostList.size(); 504 hostWidget.setText(""); 505 } 506 else 507 { 508 if (hostList.size() > 0) 509 { 510 hostWidget.setText((String )hostList.elementAt(hostIndex)); 511 } 512 else 513 { 514 hostWidget.setText(""); 515 } 516 } 517 } 518 else if (parent == useWidget) 519 { 520 useIndex++; 521 522 if (useIndex >= useList.size()) 523 { 524 useIndex = useList.size(); 525 useWidget.setText(""); 526 } 527 else 528 { 529 if (useList.size() > 0) 530 { 531 useWidget.setText((String )useList.elementAt(useIndex)); 532 } 533 else 534 { 535 useWidget.setText(""); 536 } 537 } 538 } 539 } 540 } 541 }; 542 543 class ResetListener implements ActionListener 544 { 545 public void actionPerformed(ActionEvent e) 546 { 547 reset(); 548 } 549 }; 550 551 class ExitListener implements ActionListener 552 { 553 public void actionPerformed(ActionEvent e) 554 { 555 System.exit(0); 556 } 557 }; 558 559 class OkListener implements ActionListener 560 { 561 public void actionPerformed(ActionEvent e) 562 { 563 readFromWidgetsAndClose(); 564 } 565 }; 566 567 public static void main(String args[]) 569 { 570 Frame frame = new Frame("Test Frame"); 571 frame.setBounds( 10, 100, 600, 400); 572 frame.show(); 573 574 LoginDialog ld = new LoginDialog(frame); 575 System.out.println("User : " + ld.getUser()); 576 System.out.println("Password : " + ld.getPassword()); 577 System.out.println("Host : " + ld.getHost()); 578 System.out.println("Use : " + ld.getDb()); 579 System.exit(0); 580 } 581 } 582 583 584 585 586 587 588 589 | Popular Tags |