home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / TRIAL / JBUILDER / JREFRNCE.Z / ProductFrame.java < prev    next >
Encoding:
Java Source  |  1998-05-08  |  14.7 KB  |  352 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.  
  21. //Title:        Cliffhanger Adventure Gear
  22. //Version:      
  23. //Copyright:    Copyright (c) 1997 Borland International, Inc.
  24. //Author:       Application Methods, Inc.
  25. //Description:  Cliffhanger Adventure Gear order entry system
  26. //
  27.  
  28. package borland.reference.cliffhanger;
  29.  
  30. import java.awt.*;
  31. import java.awt.event.*;
  32. import borland.jbcl.layout.*;
  33. import borland.jbcl.control.*;
  34. import borland.sql.dataset.*;
  35. import borland.jbcl.dataset.*;
  36. import java.lang.Exception;
  37. import java.util.*;
  38.  
  39. /**
  40.  * ProductFrame implements the Product Form that is used to maintain
  41.  * Product records. This form allows users to view or update
  42.  * existing products, or enter new products. A standard navigation control
  43.  * is used to allow for record browsing.
  44.  *
  45.  * <P>
  46.  * This class employs the Singleton pattern to ensure that the class has only
  47.  * one instance. You can access the singleton instance by calling the
  48.  * getProductFrame method.
  49.  */
  50. public class ProductFrame extends DecoratedFrame {
  51.   private static DataModule1 dm = DataModule1.getDataModule();
  52.   private static ProductFrame myProductFrame;
  53.   private QueryDataSet qdsProduct;
  54.   private QueryDataSet qdsCategory;
  55.   BevelPanel pnlToolbar = new BevelPanel();
  56.   BevelPanel pnlProductInfo = new BevelPanel();
  57.   FieldControl txtProductID = new FieldControl();
  58.   FieldControl txtName = new FieldControl();
  59.   ChoiceControl cboCategory = new ChoiceControl();
  60.   FieldControl txtBasePrice = new FieldControl();
  61.   FieldControl txtDiscountPct = new FieldControl();
  62.   FieldControl txtStockQty = new FieldControl();
  63.   FieldControl txtMinReorderQty = new FieldControl();
  64.   CheckboxControl chkIsActive = new CheckboxControl();
  65.   LabelControl lblName = new LabelControl();
  66.   LabelControl lblCategory = new LabelControl();
  67.   LabelControl lblBasePrice = new LabelControl();
  68.   LabelControl lblDiscountPct = new LabelControl();
  69.   LabelControl lblStockQty = new LabelControl();
  70.   LabelControl lblMinReorderQty = new LabelControl();
  71.   LabelControl lblProductID = new LabelControl();
  72.   BorderLayout borderLayout1 = new BorderLayout();
  73.   GridBagLayout gridBagLayout1 = new GridBagLayout();
  74.   NavigatorControl navigatorControl1 = new NavigatorControl();
  75.   StatusBar statusBar1 = new StatusBar();
  76.   ButtonControl btnClose = new ButtonControl();
  77.   ResourceBundle res = Res.getBundle("borland.reference.cliffhanger.Res");
  78.   ImageControl imageControl1 = new ImageControl();
  79.   GridBagLayout gridBagLayout2 = new GridBagLayout();
  80.  
  81.   /**
  82.    * The constructor is protected. Use the getProductFrame method to get an
  83.    * instance of this class.
  84.    */
  85.   protected ProductFrame() {
  86.     try {
  87.       initData(); // initialize dataset and data-aware components.
  88.       jbInit();   // initialize frame's controls (JBuilder designer)
  89.     }
  90.     catch (Exception e) {
  91.       e.printStackTrace();
  92.     };
  93.   }
  94.  
  95.   /**
  96.    * Method generated and maintained by JBuilder designer to initialize
  97.    * control properties.
  98.    */
  99.   void jbInit() throws Exception{
  100.     this.setSize(new Dimension(400, 350));
  101.     this.setTitle(res.getString("PF_Products"));
  102.     this.addWindowListener(new ProductFrame_this_windowAdapter(this));
  103.     this.setExitOnClose(false);
  104.     pnlProductInfo.setBevelInner(BevelPanel.FLAT);
  105.     chkIsActive.setColumnName("ISACTIVE");
  106.     chkIsActive.setDataSet(dm.getProductDataSet());
  107.     chkIsActive.setLabel(res.getString("PF_Active"));
  108.     txtProductID.setColumnName("ID");
  109.     txtProductID.setBackground(SystemColor.control);
  110.     txtProductID.setDataSet(dm.getProductDataSet());
  111.     txtProductID.setPostOnEndEdit(false);
  112.     txtProductID.setPostOnFocusLost(false);
  113.     txtProductID.setReadOnly(true);
  114.     txtName.setColumnName("NAME");
  115.     txtName.setDataSet(dm.getProductDataSet());
  116.     cboCategory.setColumnName("CATEGORY");
  117.     cboCategory.setDataSet(dm.getProductDataSet());
  118.     txtBasePrice.setColumnName("BASEPRICE");
  119.     txtBasePrice.setDataSet(dm.getProductDataSet());
  120.     txtDiscountPct.setColumnName("DISCOUNTPCT");
  121.     txtDiscountPct.setDataSet(dm.getProductDataSet());
  122.     txtStockQty.setColumnName("STOCKQTY");
  123.     txtStockQty.setDataSet(dm.getProductDataSet());
  124.     txtMinReorderQty.setColumnName("MINREORDERQTY");
  125.     txtMinReorderQty.setDataSet(dm.getProductDataSet());
  126.     pnlToolbar.setLayout(gridBagLayout2);
  127.     pnlProductInfo.setLayout(gridBagLayout1);
  128.     lblName.setText(res.getString("PF_Name"));
  129.     lblCategory.setText(res.getString("PF_Category"));
  130.     lblBasePrice.setText(res.getString("PF_Base_Price"));
  131.     lblDiscountPct.setText(res.getString("PF_Discount_%"));
  132.     lblStockQty.setText(res.getString("PF_Stock_Qty"));
  133.     lblMinReorderQty.setText(res.getString("PF_Min_Reorder_Qty"));
  134.     lblProductID.setText(res.getString("PF_Product_ID"));
  135.     navigatorControl1.setDataSet(dm.getProductDataSet());
  136.     statusBar1.setDataSet(dm.getProductDataSet());
  137.     statusBar1.setBevelOuter(BevelPanel.LOWERED);
  138.     statusBar1.setFont(new Font("Dialog", 0, 10));
  139.     btnClose.setLabel(res.getString("PF_Close"));
  140.     imageControl1.setBackground(SystemColor.control);
  141.     imageControl1.setAlignment(borland.jbcl.util.Alignment.LEFT | borland.jbcl.util.Alignment.TOP);
  142.     imageControl1.setImageName(".\\graphics\\productBanner.jpg");
  143.     btnClose.addActionListener(new ProductFrame_btnClose_actionAdapter(this));
  144.     this.setLayout(borderLayout1);
  145.     this.add(pnlToolbar, BorderLayout.NORTH);
  146.     pnlToolbar.add(navigatorControl1, new GridBagConstraints2(0, 1, 1, 1, 1.0, 0.0
  147.             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 2, 2, 2), 0, 0));
  148.     pnlToolbar.add(btnClose, new GridBagConstraints2(1, 1, 1, 1, 0.0, 0.0
  149.             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 40, 2, 2), 0, 7));
  150.     pnlToolbar.add(imageControl1, new GridBagConstraints2(0, 0, 2, 1, 1.0, 1.0
  151.             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, -57));
  152.     this.add(pnlProductInfo, BorderLayout.CENTER);
  153.     pnlProductInfo.add(txtProductID, new GridBagConstraints2(4, 0, 1, 1, 1.0, 0.0
  154.             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(7, 0, 0, 0), 46, 0));
  155.     pnlProductInfo.add(txtName, new GridBagConstraints2(4, 1, 2, 1, 1.0, 0.0
  156.             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 0, 0, 15), 223, 0));
  157.     pnlProductInfo.add(cboCategory, new GridBagConstraints2(4, 2, 2, 1, 1.0, 0.0
  158.             ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 15), 218, 0));
  159.     pnlProductInfo.add(txtBasePrice, new GridBagConstraints2(4, 3, 1, 1, 1.0, 0.0
  160.             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 46, 0));
  161.     pnlProductInfo.add(txtDiscountPct, new GridBagConstraints2(4, 4, 1, 1, 1.0, 0.0
  162.             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 46, 0));
  163.     pnlProductInfo.add(txtStockQty, new GridBagConstraints2(4, 5, 1, 1, 1.0, 0.0
  164.             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 46, 0));
  165.     pnlProductInfo.add(txtMinReorderQty, new GridBagConstraints2(4, 6, 1, 1, 1.0, 0.0
  166.             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 7, 0), 46, 0));
  167.     pnlProductInfo.add(lblName, new GridBagConstraints2(0, 1, 1, 1, 0.0, 0.0
  168.             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(6, 15, 0, 0), 2, 2));
  169.     pnlProductInfo.add(lblCategory, new GridBagConstraints2(0, 2, 2, 1, 0.0, 0.0
  170.             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 15, 0, 0), -3, 0));
  171.     pnlProductInfo.add(lblBasePrice, new GridBagConstraints2(0, 3, 3, 1, 0.0, 0.0
  172.             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 15, 0, 0), 0, 0));
  173.     pnlProductInfo.add(lblDiscountPct, new GridBagConstraints2(0, 4, 3, 1, 0.0, 0.0
  174.             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 15, 0, 0), 0, 0));
  175.     pnlProductInfo.add(lblStockQty, new GridBagConstraints2(0, 5, 2, 1, 0.0, 0.0
  176.             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 15, 0, 0), 0, 0));
  177.     pnlProductInfo.add(lblMinReorderQty, new GridBagConstraints2(0, 6, 4, 1, 0.0, 0.0
  178.             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 15, 7, 0), 0, 0));
  179.     pnlProductInfo.add(chkIsActive, new GridBagConstraints2(5, 6, 1, 1, 0.0, 0.0
  180.             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 110, 7, 15), 4, 0));
  181.     pnlProductInfo.add(lblProductID, new GridBagConstraints2(0, 0, 3, 1, 0.0, 0.0
  182.             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(7, 15, 0, 0), 1, 0));
  183.     this.add(statusBar1, BorderLayout.SOUTH);
  184.   }
  185.  
  186.   /**
  187.    * We created this method to separate code to initialize database and
  188.    * data-aware controls, which otherwise would go in the jbInit() method.
  189.    * Note that by doing so, the properties set here are not visible to the
  190.    * property inspector. And setting any properties via the inspector will
  191.    * add code to jbInit() instead of here.
  192.    */
  193.   private void initData() throws Exception {
  194.     qdsProduct = dm.getProductDataSet();
  195.     qdsCategory = dm.getCategoryDataSet();
  196.  
  197.     qdsProduct.open();
  198.     qdsCategory.open();
  199.  
  200.     cboCategory.setItems(qdsCategory, "CATEGORY");
  201.   }
  202.  
  203.   /**
  204.    * Class method to access the singleton instance of the class.
  205.    */
  206.   public static ProductFrame getProductFrame() {
  207.     if (myProductFrame == null)
  208.       myProductFrame = new ProductFrame();
  209.  
  210.     return myProductFrame;
  211.   }
  212.  
  213.   /**
  214.    * Initialize form controls when the form is activated.
  215.    */
  216.   void this_windowActivated(WindowEvent e) {
  217.     // determine which navigator buttons to enable
  218.  
  219.     // Note that we set the buttons here instead of in
  220.     // windowOpened because when the NavigatorControl rebuilds
  221.     // itself, the Save and Refresh buttons get re-enabled
  222.     int[] enabledFlags = {1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0};
  223.     for (int i = 0; i < enabledFlags.length; i++)
  224.       navigatorControl1.setButtonEnabled(i, enabledFlags[i] == 1);
  225.   }
  226.  
  227.   /**
  228.    * Before closing the window, check for any pending changes,
  229.    * and prompt user if there are changes pending.
  230.    * Note that we need to both check if there is anything to Post,
  231.    * and to check if there is anything to resolve.
  232.    */
  233.   void this_windowClosing(WindowEvent e) {
  234.     try {
  235.       int status = qdsCategory.getStatus();
  236.       if ( (qdsProduct.isEditing() || qdsProduct.isEditingNewRow()) ||
  237.            ((status & (RowStatus.UPDATED|RowStatus.INSERTED|RowStatus.DELETED)) != 0)
  238.          ) {
  239.         int answer = CliffhangerApplication.messageDlg(res.getString("PF_Save_Pending_Changes"),
  240.                                                        res.getString("PF_Save_pending_changes?"),
  241.                                                        Message.YES_NO_CANCEL);
  242.         // Post changes and resolve them back to DB
  243.         if (answer == Message.YES) {
  244.           qdsProduct.post();
  245.           // if there are pending changes to be resolved,
  246.           // save the changes via the Database.saveChanges()
  247.           // method. Then refresh the dataset.
  248.           status = qdsProduct.getStatus();
  249.           if ( (status & (RowStatus.UPDATED|RowStatus.INSERTED|RowStatus.DELETED)) != 0) {
  250.             qdsProduct.getDatabase().saveChanges(new DataSet[] {qdsProduct}, true);
  251.           }
  252.         }
  253.         // If user chooses no, then cancel any inserts or edits
  254.         // and refresh the dataset.
  255.         else if (answer == Message.NO) {
  256.           qdsProduct.cancel();
  257.         }
  258.         // If user cancels, just return
  259.         else //if (answer == Message.CANCEL)
  260.           return;
  261.       }
  262.  
  263.       // Refresh the dataset to ensure that we have updated data
  264.       qdsProduct.refresh();
  265.       setVisible(false);
  266.       dispose();
  267.  
  268.     }
  269.     catch (Exception ex) {
  270.       ex.printStackTrace();
  271.       new ExceptionDialog(this, res.getString("PF_Error"), ex, true).show();
  272.     }
  273.   }
  274.  
  275.   /**
  276.    * Method to insert a new product order record.
  277.    * Useful for inserting a new product from other forms in
  278.    * the application.
  279.    */
  280.   public void newProduct() {
  281.     try {
  282.       // Open the dataset if not already opened.
  283.       qdsProduct.open();
  284.       // Insert a new Product record at the end of the
  285.       // dataset
  286.       qdsProduct.insertRow(false);
  287.     }
  288.     catch (Exception e) {
  289.       e.printStackTrace();
  290.     }
  291.   }
  292.  
  293.   /**
  294.    * Method to insert a new product record
  295.    * and setting the category that is passed as
  296.    * the parameter category.
  297.    */
  298.   public void newProduct(String category) {
  299.     try {
  300.       // Open the dataset if not already opened.
  301.       qdsProduct.open();
  302.       // Insert a new Product record at the end of the
  303.       // dataset
  304.       qdsProduct.insertRow(false);
  305.       qdsProduct.setString("CATEGORY", category);
  306.     }
  307.     catch (Exception e) {
  308.       e.printStackTrace();
  309.     }
  310.   }
  311.  
  312.   /**
  313.    * When Close button is clicked, call this frame's windowClosing
  314.    * event handler to properly handle all dataset pending updates.
  315.    */
  316.   void btnClose_actionPerformed(ActionEvent e) {
  317.     // close this Window
  318.     this.this_windowClosing(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
  319.   }
  320.  
  321. }
  322.  
  323.  
  324. class ProductFrame_this_windowAdapter extends java.awt.event.WindowAdapter {
  325.   ProductFrame adaptee;
  326.  
  327.   ProductFrame_this_windowAdapter(ProductFrame adaptee) {
  328.     this.adaptee = adaptee;
  329.   }
  330.  
  331.   public void windowClosing(WindowEvent e) {
  332.     adaptee.this_windowClosing(e);
  333.   }
  334.  
  335.   public void windowActivated(WindowEvent e) {
  336.     adaptee.this_windowActivated(e);
  337.   }
  338. }
  339.  
  340. class ProductFrame_btnClose_actionAdapter implements java.awt.event.ActionListener {
  341.   ProductFrame adaptee;
  342.  
  343.   ProductFrame_btnClose_actionAdapter(ProductFrame adaptee) {
  344.     this.adaptee = adaptee;
  345.   }
  346.  
  347.   public void actionPerformed(ActionEvent e) {
  348.     adaptee.btnClose_actionPerformed(e);
  349.   }
  350. }
  351.  
  352.