home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / CustomerLookupDialog.java < prev    next >
Text File  |  1997-07-30  |  8KB  |  230 lines

  1. package borland.samples.intl.gui;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.text.*;
  6. import java.util.*;
  7. import borland.jbcl.layout.*;
  8. import borland.jbcl.control.*;
  9. import borland.jbcl.dataset.*;
  10. import borland.jbcl.view.*;
  11. import borland.jbcl.model.*;
  12.  
  13. import borland.samples.intl.application.*;
  14.  
  15. public class CustomerLookupDialog extends Dialog {
  16.   private AppDataModule appDataModule = AppDataModule.getDataModule();
  17.   private boolean validCustomer = false;
  18.   ResourceBundle textRes = java.util.ResourceBundle.getBundle("borland.samples.intl.gui.resources.TextRes");
  19.   private int captionMargin = 10;
  20.   MessageFormat formattedMessage;
  21.   Object [] messageArgs;
  22.   String messageString;
  23.  
  24.   BevelPanel mainDialogPanel = new BevelPanel();
  25.   LabelControl searchNameLabel = new LabelControl();
  26.   GridControl gridControl1 = new GridControl();
  27.   LocatorControl locatorControl1 = new LocatorControl();
  28.   BevelPanel buttonPanel = new BevelPanel();
  29.   ButtonControl okButton = new ButtonControl();
  30.   ButtonControl cancelButton = new ButtonControl();
  31.   GridLayout gridLayout1 = new GridLayout();
  32.   GridBagLayout gridBagLayout1 = new GridBagLayout();
  33.  
  34.  
  35.   public CustomerLookupDialog(Frame frame, boolean modal) {
  36.     super(frame, modal);
  37.     try {
  38.       jbInit();
  39.  
  40.       formattedMessage = new MessageFormat(textRes.getString("Search_by_fieldName"));
  41.       messageArgs = new Object [] { gridControl1.getColumnView(0).getCaption() };
  42.       messageString = formattedMessage.format(messageArgs);
  43.       searchNameLabel.setText(messageString);
  44.  
  45.     }
  46.     catch (Exception e) {
  47.       borland.jbcl.util.Diagnostic.printStackTrace(e);
  48.     }
  49.     add(mainDialogPanel);
  50.  
  51.     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  52.     mainDialogPanel.setSize(screenSize.width * 2 / 3, screenSize.height / 2);
  53.   }
  54.  
  55.   public void jbInit() throws Exception{
  56.     mainDialogPanel.setLayout(gridBagLayout1);
  57.     searchNameLabel.setAlignment(Label.LEFT);
  58.     locatorControl1.setDataSet(appDataModule.getCustomerDataSetView());
  59.     gridControl1.setDataSet(appDataModule.getCustomerDataSetView());
  60.     gridControl1.setShowPopup(false);
  61.     gridControl1.addMouseListener(new CustomerLookupDialog_gridControl1_mouseAdapter(this));
  62.     gridControl1.addSubfocusListener(new CustomerLookupDialog_gridControl1_subfocusAdapter(this));
  63.     gridControl1.setReadOnly(true);
  64.     okButton.setLabel(textRes.getString("OK"));
  65.     okButton.addActionListener(new CustomerLookupDialog_okButton_actionAdapter(this));
  66.     cancelButton.setLabel(textRes.getString("Cancel"));
  67.     cancelButton.addActionListener(new CustomerLookupDialog_cancelButton_actionAdapter(this));
  68.  
  69.     gridLayout1.setRows(1);
  70.     gridLayout1.setColumns(2);
  71.     gridLayout1.setHgap(10);
  72.     buttonPanel.setLayout(gridLayout1);
  73.     buttonPanel.setBevelInner(BevelPanel.FLAT);
  74.     buttonPanel.add(okButton, null);
  75.     buttonPanel.add(cancelButton, null);
  76.  
  77.     this.setTitle(textRes.getString("Find_customer"));
  78.  
  79.     mainDialogPanel.add(searchNameLabel, new GridBagConstraints2(0, 0, 5, 1, 100.0, 0.0
  80.             ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 5, 10), 0, 0));
  81.     mainDialogPanel.add(locatorControl1, new GridBagConstraints2(0, 1, 5, 1, 100.0, 0.0
  82.             ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 5, 10), 0, 0));
  83.     mainDialogPanel.add(gridControl1, new GridBagConstraints2(0, 2, 5, 4, 100.0, 100.0
  84.             ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 10, 5, 10), 0, 0));
  85.     mainDialogPanel.add(buttonPanel, new GridBagConstraints2(0, 6, 5, 1, 0.0, 0.0
  86.             ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 10, 10, 10), 0, 0));
  87.     this.addWindowListener(new CustomerLookupDialog_this_windowAdapter(this));
  88.   }
  89.  
  90.   // if a customer was selected and the password was valid, returns true
  91.   // otherwise, returns false
  92.   public boolean isValidCustomer() {
  93.     return validCustomer;
  94.   }
  95.  
  96.   //OK
  97.   void okButton_actionPerformed(ActionEvent e) {
  98.     try {
  99.       Component component = this;
  100.       Frame frame = null;
  101.  
  102.       while ((component = component.getParent()) != null) {
  103.     if (component instanceof Frame) {
  104.       frame = (Frame) component;
  105.       break;
  106.     }
  107.       }
  108.  
  109.       if (frame == null) {
  110.     frame = new Frame();
  111.       }
  112.       PasswordDialog passwordDialog = new PasswordDialog(frame, textRes.getString("Password"));
  113.       passwordDialog.show();
  114.       String password;
  115.       if ((password = passwordDialog.getPassword()) != null) {
  116.     if (!appDataModule.isValidCustomerDataViewPassword(password)) {
  117.       Message message = new Message();
  118.       message.setFrame(frame);
  119.       message.setLabels(new String [] { textRes.getString("OK") });
  120.       message.setMessage(textRes.getString("Invalid_password"));
  121.       message.setVisible(true);
  122.       return;
  123.     }
  124.     validCustomer = true;
  125.       } else {
  126.     return;
  127.       }
  128.     } catch (Exception ex) {
  129.       borland.jbcl.util.Diagnostic.printStackTrace(ex);
  130.     }
  131.     dispose();
  132.   }
  133.  
  134.   //Cancel
  135.   void cancelButton_actionPerformed(ActionEvent e) {
  136.     dispose();
  137.   }
  138.  
  139.   public void show() {
  140.     //Center the window
  141.     this.pack();
  142.     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  143.     Dimension frameSize = this.getPreferredSize();
  144.     if (frameSize.height > screenSize.height)
  145.       frameSize.height = screenSize.height;
  146.     if (frameSize.width > screenSize.width)
  147.       frameSize.width = screenSize.width;
  148.     this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
  149.     super.show();
  150.   }
  151.  
  152.   void this_windowClosing(WindowEvent e) {
  153.     System.out.println("CustomerLookupDialog() window closing...");
  154.     dispose();
  155.   }
  156.  
  157.   void gridControl1_subfocusChanged(MatrixSubfocusEvent e) {
  158. //    messageArgs = new Object [] { textRes.getString(gridControl1.getColumnView(e.getLocation().column).getName()) };
  159.     messageArgs = new Object [] { gridControl1.getColumnView(e.getLocation().column).getCaption() };
  160.     messageString = formattedMessage.format(messageArgs);
  161.     searchNameLabel.setText(messageString);
  162.   }
  163.  
  164.   void gridControl1_mouseClicked(MouseEvent e) {
  165.     if (e.getClickCount() >= 2) {
  166.        okButton_actionPerformed(new ActionEvent(gridControl1, ActionEvent.ACTION_PERFORMED, ""));
  167.     }
  168.   }
  169. }
  170.  
  171. class CustomerLookupDialog_okButton_actionAdapter implements ActionListener{
  172.   CustomerLookupDialog adaptee;
  173.  
  174.   CustomerLookupDialog_okButton_actionAdapter(CustomerLookupDialog adaptee) {
  175.     this.adaptee = adaptee;
  176.   }
  177.  
  178.   public void actionPerformed(ActionEvent e) {
  179.     adaptee.okButton_actionPerformed(e);
  180.   }
  181. }
  182.  
  183. class CustomerLookupDialog_cancelButton_actionAdapter implements ActionListener{
  184.   CustomerLookupDialog adaptee;
  185.  
  186.   CustomerLookupDialog_cancelButton_actionAdapter(CustomerLookupDialog adaptee) {
  187.     this.adaptee = adaptee;
  188.   }
  189.  
  190.   public void actionPerformed(ActionEvent e) {
  191.     adaptee.cancelButton_actionPerformed(e);
  192.   }
  193. }
  194.  
  195. class CustomerLookupDialog_this_windowAdapter extends WindowAdapter {
  196.   CustomerLookupDialog adaptee;
  197.  
  198.   CustomerLookupDialog_this_windowAdapter(CustomerLookupDialog adaptee) {
  199.     this.adaptee = adaptee;
  200.   }
  201.  
  202.   public void windowClosing(WindowEvent e) {
  203.     adaptee.this_windowClosing(e);
  204.   }
  205. }
  206.  
  207. class CustomerLookupDialog_gridControl1_subfocusAdapter extends borland.jbcl.model.MatrixSubfocusAdapter {
  208.   CustomerLookupDialog adaptee;
  209.  
  210.   CustomerLookupDialog_gridControl1_subfocusAdapter(CustomerLookupDialog adaptee) {
  211.     this.adaptee = adaptee;
  212.   }
  213.  
  214.   public void subfocusChanged(MatrixSubfocusEvent e) {
  215.     adaptee.gridControl1_subfocusChanged(e);
  216.   }
  217. }
  218.  
  219. class CustomerLookupDialog_gridControl1_mouseAdapter extends java.awt.event.MouseAdapter {
  220.   CustomerLookupDialog adaptee;
  221.  
  222.   CustomerLookupDialog_gridControl1_mouseAdapter(CustomerLookupDialog adaptee) {
  223.     this.adaptee = adaptee;
  224.   }
  225.  
  226.   public void mouseClicked(MouseEvent e) {
  227.     adaptee.gridControl1_mouseClicked(e);
  228.   }
  229. }
  230.