home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / PasswordDialog.java < prev    next >
Encoding:
Java Source  |  1997-07-30  |  5.0 KB  |  165 lines

  1. package borland.samples.intl.gui;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.util.*;
  6. import borland.jbcl.layout.*;
  7. import borland.jbcl.control.*;
  8. import borland.jbcl.view.*;
  9. import borland.jbcl.model.*;
  10. import borland.jbcl.util.*;
  11.  
  12. public class PasswordDialog extends Dialog {
  13.   BevelPanel mainDialogPanel = new BevelPanel();
  14.   BevelPanel entryPanel = new BevelPanel();
  15.   BevelPanel buttonPanel = new BevelPanel();
  16.   ButtonControl okButton = new ButtonControl();
  17.   ButtonControl cancelButton = new ButtonControl();
  18.   LabelControl enterPasswordLabel = new LabelControl();
  19.   BorderLayout borderLayout1 = new BorderLayout();
  20.   GridLayout gridLayout1 = new GridLayout();
  21.   FlowLayout flowLayout1 = new FlowLayout();
  22.   MaskableTextItemEditor passwordEntryField = new MaskableTextItemEditor();
  23.   private String password = null;
  24.   ResourceBundle textRes = java.util.ResourceBundle.getBundle("borland.samples.intl.gui.resources.TextRes");
  25.  
  26.   public PasswordDialog(Frame frame, String title) {
  27.     super(frame, title, true);
  28.     try {
  29.       jbInit();
  30.       passwordEntryField.setEditMask("*aaaaaaaaaaaaaaaaaaaa*", Variant.STRING, Locale.getDefault());
  31.     }
  32.     catch (Exception e) {
  33.       e.printStackTrace();
  34.     }
  35.     add(mainDialogPanel);
  36.     pack();
  37.   }
  38.  
  39.   public PasswordDialog(Frame frame) {
  40.     this(frame, "");
  41.     this.setTitle(textRes.getString("Enter_Password"));
  42.   }
  43.  
  44.   public void jbInit() throws Exception {
  45.  
  46.     enterPasswordLabel.setText(textRes.getString("Enter_Password"));
  47.     enterPasswordLabel.setAlignment(Label.RIGHT);
  48.     passwordEntryField.setColumns(15);
  49.     passwordEntryField.addActionListener(new PasswordDialog_passwordEntryField_actionAdapter(this));
  50.  
  51.     entryPanel.setLayout(flowLayout1);
  52.     entryPanel.add(enterPasswordLabel, null);
  53.     entryPanel.add(passwordEntryField, null);
  54.  
  55.     okButton.setLabel(textRes.getString("OK"));
  56.     okButton.addActionListener(new PasswordDialog_okButton_actionAdapter(this));
  57.     cancelButton.setLabel(textRes.getString("Cancel"));
  58.     cancelButton.addActionListener(new PasswordDialog_cancelButton_actionAdapter(this));
  59.  
  60.     gridLayout1.setRows(1);
  61.     gridLayout1.setColumns(2);
  62.     gridLayout1.setHgap(10);
  63.     buttonPanel.setLayout(gridLayout1);
  64.     buttonPanel.setBevelInner(BevelPanel.FLAT);
  65.     buttonPanel.setMargins(new Insets(10, 10, 10, 10));
  66.     buttonPanel.add(okButton, null);
  67.     buttonPanel.add(cancelButton, null);
  68.  
  69.     mainDialogPanel.setLayout(borderLayout1);
  70.     mainDialogPanel.add(entryPanel, BorderLayout.CENTER);
  71.     mainDialogPanel.add(buttonPanel, BorderLayout.SOUTH);
  72.     this.addWindowListener(new PasswordDialog_this_windowAdapter(this));
  73.   }
  74.  
  75.   //OK
  76.    void okButton_actionPerformed(ActionEvent e) {
  77.     try {
  78.       if (passwordEntryField.getValue() != null) {
  79.     password = passwordEntryField.getValue().toString();
  80.       }
  81.     } catch (IllegalStateException ex) { // will occur if the entered value fails to satisfy editmask pattern
  82.     }
  83.     dispose();
  84.   }
  85.  
  86.   //Cancel
  87.   void cancelButton_actionPerformed(ActionEvent e) {
  88.     dispose();
  89.   }
  90.   
  91.   void this_windowClosing(WindowEvent e) {
  92.     dispose();
  93.   }
  94.  
  95.   public void show() {
  96.     //Center the window
  97.     this.pack();
  98.     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  99.     Dimension frameSize = this.getPreferredSize();
  100.     if (frameSize.height > screenSize.height)
  101.       frameSize.height = screenSize.height;
  102.     if (frameSize.width > screenSize.width)
  103.       frameSize.width = screenSize.width;
  104.     this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
  105.     super.show();
  106.   }
  107.  
  108.   public String getPassword() {
  109.     return password;
  110.   }
  111.   
  112.   void passwordEntryField_actionPerformed(ActionEvent e) {
  113.     okButton_actionPerformed(e);
  114.   }
  115. }
  116.  
  117. class PasswordDialog_okButton_actionAdapter implements ActionListener{
  118.   PasswordDialog adaptee;
  119.  
  120.   PasswordDialog_okButton_actionAdapter(PasswordDialog adaptee) {
  121.     this.adaptee = adaptee;
  122.   }
  123.  
  124.   public void actionPerformed(ActionEvent e) {
  125.     adaptee.okButton_actionPerformed(e);
  126.   }
  127. }
  128.  
  129. class PasswordDialog_cancelButton_actionAdapter implements ActionListener{
  130.   PasswordDialog adaptee;
  131.  
  132.   PasswordDialog_cancelButton_actionAdapter(PasswordDialog adaptee) {
  133.     this.adaptee = adaptee;
  134.   }
  135.  
  136.   public void actionPerformed(ActionEvent e) {
  137.     adaptee.cancelButton_actionPerformed(e);
  138.   }
  139. }
  140.  
  141. class PasswordDialog_this_windowAdapter extends WindowAdapter {
  142.   PasswordDialog adaptee;
  143.  
  144.   PasswordDialog_this_windowAdapter(PasswordDialog adaptee) {
  145.     this.adaptee = adaptee;
  146.   }
  147.  
  148.   public void windowClosing(WindowEvent e) {
  149.     adaptee.this_windowClosing(e);
  150.   }
  151. }
  152.  
  153. class PasswordDialog_passwordEntryField_actionAdapter implements java.awt.event.ActionListener {
  154.   PasswordDialog adaptee;
  155.  
  156.   PasswordDialog_passwordEntryField_actionAdapter(PasswordDialog adaptee) {
  157.     this.adaptee = adaptee;
  158.   }
  159.  
  160.   public void actionPerformed(ActionEvent e) {
  161.     adaptee.passwordEntryField_actionPerformed(e);
  162.   }
  163. }
  164.  
  165.