home *** CD-ROM | disk | FTP | other *** search
/ PC World 1998 October / PCWorld_1998-10_cd.bin / software / prehled / inprise / JSAMPLES.Z / PasswordDialog.java < prev    next >
Text File  |  1998-05-08  |  6KB  |  189 lines

  1. /*
  2.  * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
  3.  * 
  4.  * This SOURCE CODE FILE, which has been provided by Borland as part
  5.  * of a Borland product for use ONLY by licensed users of the product,
  6.  * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
  7.  *
  8.  * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
  9.  * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
  10.  * THE PRODUCT.
  11.  *
  12.  * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
  13.  * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
  14.  * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
  15.  * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
  16.  * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
  17.  * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
  18.  * CODE FILE.
  19.  */
  20. package borland.samples.intl.gui;
  21.  
  22. import java.awt.*;
  23. import java.awt.event.*;
  24. import java.util.*;
  25. import borland.jbcl.layout.*;
  26. import borland.jbcl.control.*;
  27. import borland.jbcl.view.*;
  28. import borland.jbcl.model.*;
  29. import borland.jbcl.util.*;
  30.  
  31. import borland.samples.intl.beans.*;
  32.  
  33. public class PasswordDialog extends Dialog {
  34.   BevelPanel mainDialogPanel = new BevelPanel();
  35.   BevelPanel entryPanel = new BevelPanel();
  36.   BevelPanel buttonPanel = new BevelPanel();
  37.   ButtonControl okButton = new ButtonControl();
  38.   ButtonControl cancelButton = new ButtonControl();
  39.   LabelControl enterPasswordLabel = new LabelControl();
  40.   BorderLayout borderLayout1 = new BorderLayout();
  41.   GridLayout gridLayout1 = new GridLayout();
  42.   FlowLayout flowLayout1 = new FlowLayout();
  43.   MaskableTextItemEditor passwordEntryField = new MaskableTextItemEditor();
  44.   private String password = null;
  45.   ResourceBundle textRes = java.util.ResourceBundle.getBundle("borland.samples.intl.gui.resources.TextRes",
  46.                                                               LocaleChangeManager.getLocale());
  47.  
  48.   public PasswordDialog(Frame frame, String title) {
  49.     super(frame, title, true);
  50.     try {
  51.       jbInit();
  52.       passwordEntryField.setEditMask("*aaaaaaaaaaaaaaaaaaaa*", Variant.STRING, Locale.getDefault());
  53.     }
  54.     catch (Exception e) {
  55.       e.printStackTrace();
  56.     }
  57.     add(mainDialogPanel);
  58.     pack();
  59.   }
  60.  
  61.   public PasswordDialog(Frame frame) {
  62.     this(frame, "");
  63.     this.setTitle(textRes.getString("Enter_Password"));
  64.   }
  65.  
  66.   public void jbInit() throws Exception {
  67.  
  68.     enterPasswordLabel.setText(textRes.getString("Enter_Password"));
  69.     enterPasswordLabel.setAlignment(Label.RIGHT);
  70.     passwordEntryField.setColumns(15);
  71.     passwordEntryField.addActionListener(new PasswordDialog_passwordEntryField_actionAdapter(this));
  72.  
  73.     entryPanel.setLayout(flowLayout1);
  74.     entryPanel.add(enterPasswordLabel, null);
  75.     entryPanel.add(passwordEntryField, null);
  76.  
  77.     okButton.setLabel(textRes.getString("OK"));
  78.     okButton.addActionListener(new PasswordDialog_okButton_actionAdapter(this));
  79.     cancelButton.setLabel(textRes.getString("Cancel"));
  80.     cancelButton.addActionListener(new PasswordDialog_cancelButton_actionAdapter(this));
  81.  
  82.     gridLayout1.setRows(1);
  83.     gridLayout1.setColumns(2);
  84.     gridLayout1.setHgap(10);
  85.     buttonPanel.setLayout(gridLayout1);
  86.     buttonPanel.setBevelInner(BevelPanel.FLAT);
  87.     buttonPanel.setMargins(new Insets(10, 10, 10, 10));
  88.     buttonPanel.add(okButton, null);
  89.     buttonPanel.add(cancelButton, null);
  90.  
  91.     mainDialogPanel.setLayout(borderLayout1);
  92.     mainDialogPanel.add(entryPanel, BorderLayout.CENTER);
  93.     mainDialogPanel.add(buttonPanel, BorderLayout.SOUTH);
  94.     this.addWindowListener(new PasswordDialog_this_windowAdapter(this));
  95.   }
  96.  
  97.   //OK
  98.    void okButton_actionPerformed(ActionEvent e) {
  99.     try {
  100.       if (passwordEntryField.getValue() != null) {
  101.         password = passwordEntryField.getValue().toString();
  102.       }
  103.     } catch (IllegalStateException ex) { // will occur if the entered value fails to satisfy editmask pattern
  104.     }
  105.     dispose();
  106.   }
  107.  
  108.   //Cancel
  109.   void cancelButton_actionPerformed(ActionEvent e) {
  110.     dispose();
  111.   }
  112.   
  113.   void this_windowClosing(WindowEvent e) {
  114.     dispose();
  115.   }
  116.  
  117.   // In order to center the dialog, we override its addNotify()
  118.   // method and set its location immediately after the peer is created. 
  119.   public void addNotify() {
  120.     super.addNotify();
  121.     //Center the window
  122.     this.pack();
  123.     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  124.     Dimension frameSize = this.getPreferredSize();
  125.     if (frameSize.height > screenSize.height)
  126.       frameSize.height = screenSize.height;
  127.     if (frameSize.width > screenSize.width)
  128.       frameSize.width = screenSize.width;
  129.     this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
  130.   }
  131.  
  132.   public String getPassword() {
  133.     return password;
  134.   }
  135.   
  136.   void passwordEntryField_actionPerformed(ActionEvent e) {
  137.     okButton_actionPerformed(e);
  138.   }
  139. }
  140.  
  141. class PasswordDialog_okButton_actionAdapter implements ActionListener{
  142.   PasswordDialog adaptee;
  143.  
  144.   PasswordDialog_okButton_actionAdapter(PasswordDialog adaptee) {
  145.     this.adaptee = adaptee;
  146.   }
  147.  
  148.   public void actionPerformed(ActionEvent e) {
  149.     adaptee.okButton_actionPerformed(e);
  150.   }
  151. }
  152.  
  153. class PasswordDialog_cancelButton_actionAdapter implements ActionListener{
  154.   PasswordDialog adaptee;
  155.  
  156.   PasswordDialog_cancelButton_actionAdapter(PasswordDialog adaptee) {
  157.     this.adaptee = adaptee;
  158.   }
  159.  
  160.   public void actionPerformed(ActionEvent e) {
  161.     adaptee.cancelButton_actionPerformed(e);
  162.   }
  163. }
  164.  
  165. class PasswordDialog_this_windowAdapter extends WindowAdapter {
  166.   PasswordDialog adaptee;
  167.  
  168.   PasswordDialog_this_windowAdapter(PasswordDialog adaptee) {
  169.     this.adaptee = adaptee;
  170.   }
  171.  
  172.   public void windowClosing(WindowEvent e) {
  173.     adaptee.this_windowClosing(e);
  174.   }
  175. }
  176.  
  177. class PasswordDialog_passwordEntryField_actionAdapter implements java.awt.event.ActionListener {
  178.   PasswordDialog adaptee;
  179.  
  180.   PasswordDialog_passwordEntryField_actionAdapter(PasswordDialog adaptee) {
  181.     this.adaptee = adaptee;
  182.   }
  183.  
  184.   public void actionPerformed(ActionEvent e) {
  185.     adaptee.passwordEntryField_actionPerformed(e);
  186.   }
  187. }
  188.  
  189.