home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / TRIAL / JBUILDER / JREFRNCE.Z / CategoryFrame.java < prev    next >
Encoding:
Java Source  |  1998-05-08  |  9.9 KB  |  268 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. package borland.reference.cliffhanger;
  28.  
  29. import java.awt.*;
  30. import java.awt.event.*;
  31. import borland.jbcl.layout.*;
  32. import borland.jbcl.control.*;
  33. import borland.sql.dataset.*;
  34. import borland.jbcl.dataset.*;
  35. import borland.jbcl.model.*;
  36. import java.util.*;
  37.  
  38. /**
  39.  * CategoryFrame implements the Category Form that is used to maintain
  40.  * Product Category records. This form allows users to view or update
  41.  * existing categories, or enter new product categories.
  42.  *
  43.  * <P>
  44.  * This class employs the Singleton pattern to ensure that the class has only
  45.  * one instance. You can access the singleton instance by calling the
  46.  * getCategoryFrame method.
  47.  */
  48. public class CategoryFrame extends DecoratedFrame {
  49.   private static CategoryFrame myCategoryFrame;
  50.  
  51.   private static DataModule1 dataModule = DataModule1.getDataModule();
  52.  
  53.   BevelPanel pnlToolbar = new BevelPanel();
  54.   BevelPanel pnlMain = new BevelPanel();
  55.   StatusBar statusBar1 = new StatusBar();
  56.   GridControl gridControl1 = new GridControl();
  57.   NavigatorControl navigatorControl1 = new NavigatorControl();
  58.   BorderLayout borderLayout1 = new BorderLayout();
  59.   BorderLayout borderLayout2 = new BorderLayout();
  60.   ButtonControl btnClose = new ButtonControl();
  61.   BevelPanel bevelPanel3 = new BevelPanel();
  62.   ButtonControl btnNewProduct = new ButtonControl();
  63.   FlowLayout flowLayout1 = new FlowLayout();
  64.   ResourceBundle res = Res.getBundle("borland.reference.cliffhanger.Res");
  65.   ImageControl imageControl1 = new ImageControl();
  66.   GridBagLayout gridBagLayout1 = new GridBagLayout();
  67.  
  68.   /**
  69.    * The constructor is protected. Use the getCategoryFrame method to get
  70.    * an instance of the class.
  71.    */
  72.   protected CategoryFrame() {
  73.     try {
  74.       jbInit();   // initialize frame's controls (JBuilder designer)
  75.     }
  76.     catch (Exception e) {
  77.       e.printStackTrace();
  78.     };
  79.   }
  80.  
  81.   /**
  82.    * Method generated and maintained by JBuilder designer to initialize
  83.    * control properties.
  84.    */
  85.   private void jbInit() throws Exception{
  86.     this.setSize(new Dimension(400, 350));
  87.     this.setTitle(res.getString("CAF_Categories"));
  88.     this.addWindowListener(new CategoryFrame_this_windowAdapter(this));
  89.     this.setExitOnClose(false);
  90.     pnlToolbar.setBevelInner(BevelPanel.FLAT);
  91.     pnlMain.setBevelInner(BevelPanel.FLAT);
  92.     statusBar1.setDataSet(dataModule.getCategoryDataSet());
  93.     statusBar1.setBevelOuter(BevelPanel.LOWERED);
  94.     statusBar1.setFont(new Font("Dialog", 0, 10));
  95.     gridControl1.setDataSet(dataModule.getCategoryDataSet());
  96.     navigatorControl1.setDataSet(dataModule.getCategoryDataSet());
  97.     pnlToolbar.setLayout(gridBagLayout1);
  98.     pnlMain.setLayout(borderLayout2);
  99.     btnClose.setLabel(res.getString("CAF_Close"));
  100.     btnClose.addActionListener(new CategoryFrame_btnClose_actionAdapter(this));
  101.     bevelPanel3.setBevelInner(BevelPanel.FLAT);
  102.     bevelPanel3.setLayout(flowLayout1);
  103.     btnNewProduct.setLabel(res.getString("CAF_New_Product"));
  104.     imageControl1.setAlignment(borland.jbcl.util.Alignment.LEFT | borland.jbcl.util.Alignment.TOP);
  105.     imageControl1.setImageName(".\\graphics\\categoryBanner.jpg");
  106.     btnNewProduct.addActionListener(new CategoryFrame_btnNewProduct_actionAdapter(this));
  107.     this.setLayout(borderLayout1);
  108.     this.add(pnlToolbar, BorderLayout.NORTH);
  109.     pnlToolbar.add(navigatorControl1, new GridBagConstraints2(0, 1, 1, 1, 1.0, 0.0
  110.             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 2, 2, 2), 0, 0));
  111.     pnlToolbar.add(btnClose, new GridBagConstraints2(1, 1, 1, 1, 0.0, 0.0
  112.             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 42, 2, 2), 0, 7));
  113.     pnlToolbar.add(imageControl1, new GridBagConstraints2(0, 0, 2, 1, 1.0, 1.0
  114.             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, -57));
  115.     this.add(pnlMain, BorderLayout.CENTER);
  116.     pnlMain.add(gridControl1, BorderLayout.CENTER);
  117.     pnlMain.add(bevelPanel3, BorderLayout.SOUTH);
  118.     bevelPanel3.add(btnNewProduct, null);
  119.     this.add(statusBar1, BorderLayout.SOUTH);
  120.   }
  121.  
  122.   /**
  123.    * Class method to access the singleton instance of the class
  124.    */
  125.   public static CategoryFrame getCategoryFrame() {
  126.     if (myCategoryFrame == null)
  127.       myCategoryFrame = new CategoryFrame();
  128.  
  129.     return myCategoryFrame;
  130.   }
  131.  
  132.   /**
  133.    * Initialize form controls when the form is activated.
  134.    */
  135.   void this_windowActivated(WindowEvent e) {
  136.     // determine which navigator buttons to enable
  137.  
  138.     // Note that we set the buttons here instead of in
  139.     // windowOpened because when the NavigatorControl rebuilds
  140.     // itself, the Save and Refresh buttons get re-enabled
  141.     int[] enabledFlags = {1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0};
  142.     for (int i = 0; i < enabledFlags.length; i++)
  143.       navigatorControl1.setButtonEnabled(i, enabledFlags[i] == 1);
  144.   }
  145.  
  146.   /**
  147.    * Before closing the window, check for any pending changes,
  148.    * and prompt user if there are changes pending.
  149.    * Note that we need to both check if there is anything to Post,
  150.    * and to check if there is anything to resolve.
  151.    */
  152.   void this_windowClosing(WindowEvent e) {
  153.     try {
  154.       QueryDataSet qdsCategory = dataModule.getCategoryDataSet();
  155.       int status = qdsCategory.getStatus();
  156.       if ( (qdsCategory.isEditing() || qdsCategory.isEditingNewRow()) ||
  157.            ((status & (RowStatus.UPDATED|RowStatus.INSERTED|RowStatus.DELETED)) != 0)
  158.          ) {
  159.         int answer = CliffhangerApplication.messageDlg(res.getString("CAF_Save_Pending_Changes"),
  160.                                                        res.getString("CAF_Save_pending_changes?"),
  161.                                                        Message.YES_NO_CANCEL);
  162.         // Post changes and resolve them back to DB
  163.         if (answer == Message.YES) {
  164.           qdsCategory.post();
  165.           // if there are pending changes to be resolved,
  166.           // save the changes via the Database.saveChanges()
  167.           // method. Then refresh the dataset.
  168.           status = qdsCategory.getStatus();
  169.           if ( (status & (RowStatus.UPDATED|RowStatus.INSERTED|RowStatus.DELETED)) != 0) {
  170.             qdsCategory.getDatabase().saveChanges(new DataSet[] {qdsCategory}, true);
  171.             qdsCategory.refresh();
  172.           }
  173.         }
  174.         // If user chooses no, then cancel any inserts or edits
  175.         // and refresh the dataset.
  176.         else if (answer == Message.NO) {
  177.           qdsCategory.cancel();
  178.           qdsCategory.refresh();
  179.         }
  180.         // If user cancels, just return
  181.         else //if (answer == Message.CANCEL)
  182.           return;
  183.       }
  184.  
  185.       setVisible(false);
  186.       dispose();
  187.  
  188.     }
  189.     catch (Exception ex) {
  190.       ex.printStackTrace();
  191.       new ExceptionDialog(this, res.getString("CAF_Error"), ex, true).show();
  192.     }
  193.  
  194.   }
  195.  
  196.   /**
  197.    * When Close button is clicked, call this frame's windowClosing
  198.    * event handler to properly handle all dataset pending updates.
  199.    */
  200.   void btnClose_actionPerformed(ActionEvent e) {
  201.     // close this Window
  202.     this.this_windowClosing(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
  203.   }
  204.  
  205.   /**
  206.    * When New Product button is clicked, show the Product form
  207.    * with a new product record inserted, default the category field to
  208.    * the current category.
  209.    */
  210.   void btnNewProduct_actionPerformed(ActionEvent e) {
  211.     try {
  212.       // Get the ProductFrame and show centered in screen
  213.       ProductFrame frame = ProductFrame.getProductFrame();
  214.       CliffhangerApplication.showCenteredFrame(frame, false);
  215.  
  216.       // create a new Product, passing the Category String
  217.       // to default the category of the new product
  218.       // to this current category.
  219.       frame.newProduct(dataModule.getCategoryDataSet().getString("Category"));
  220.     }
  221.     catch (Exception ex) {
  222.       ex.printStackTrace();
  223.     }
  224.   }
  225.  
  226. }
  227.  
  228.  
  229. class CategoryFrame_this_windowAdapter extends java.awt.event.WindowAdapter {
  230.   CategoryFrame adaptee;
  231.  
  232.   CategoryFrame_this_windowAdapter(CategoryFrame adaptee) {
  233.     this.adaptee = adaptee;
  234.   }
  235.  
  236.   public void windowActivated(WindowEvent e) {
  237.     adaptee.this_windowActivated(e);
  238.   }
  239.  
  240.   public void windowClosing(WindowEvent e) {
  241.     adaptee.this_windowClosing(e);
  242.   }
  243. }
  244.  
  245. class CategoryFrame_btnClose_actionAdapter implements java.awt.event.ActionListener {
  246.   CategoryFrame adaptee;
  247.  
  248.   CategoryFrame_btnClose_actionAdapter(CategoryFrame adaptee) {
  249.     this.adaptee = adaptee;
  250.   }
  251.  
  252.   public void actionPerformed(ActionEvent e) {
  253.     adaptee.btnClose_actionPerformed(e);
  254.   }
  255. }
  256.  
  257. class CategoryFrame_btnNewProduct_actionAdapter implements java.awt.event.ActionListener {
  258.   CategoryFrame adaptee;
  259.  
  260.   CategoryFrame_btnNewProduct_actionAdapter(CategoryFrame adaptee) {
  261.     this.adaptee = adaptee;
  262.   }
  263.  
  264.   public void actionPerformed(ActionEvent e) {
  265.     adaptee.btnNewProduct_actionPerformed(e);
  266.   }
  267. }
  268.