Reusable Code


Project: Cliffhanger Adventure Gear
Author: Application Methods, Inc.

Description:
This document will highlight the areas of the Cliffhanger project that might be useful for your own JBuilder projects:

messageDlg method:

The messageDlg method wraps the code to show a message dialog modally and returns which button the user clicked. This method makes it easy to show a message dialog modally and get the modal result, all in one line of code. It is declared as a static method in the CliffhangerApplication class. Here is the code fragment of the messageDlg method taken from the CliffhangerApplication class:

  public int messageDlg(String title, String message, int buttonSet) {
    Frame frame = new Frame(); // dummy frame for message dialog
    Message msgDlg = new Message( frame, title, message );
    msgDlg.setButtonSet(buttonSet);
    msgDlg.show();
    return msgDlg.getResult();
  }

The following code sample is taken from the DataModule1 class. It is an example of using the messageDlg method to prompt the user for confirmation before deleting a record. The method confirmDelete() throws an exception if the user clicks on the No button, which aborts the delete command in the DataSet.deleting event handler:

  private void confirmDelete(String message) throws Exception {
    if (CliffhangerApplication.messageDlg("Confirm", message, Message.YES_NO) == Message.NO) {
      throw new Exception("Delete canceled");
    }
  }

  void productDataSet_deleting(DataSet dataSet) throws Exception{
    // Prompt the user to confirm delete.
    confirmDelete("Delete this product?");
  }

showCenteredFrame method:

The showCenteredFrame method wraps the code to show a frame in the center of the screen. The method makes sure that the frame dimensions do not size beyond the dimensions of the screen. The method is declared as a static method in the CliffhangerApplication class. Here is the code snippet of the showCentered method taken from the CliffhangerApplication class:

  public void showCenteredFrame(Frame frame, boolean usePreferredSize) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize;
    if (usePreferredSize)
      frameSize = frame.getPreferredSize();
    else
      frameSize = frame.getSize();

    if (frameSize.height > screenSize.height)
      frameSize.height = screenSize.height;
    if (frameSize.width > screenSize.width)
      frameSize.width = screenSize.width;

    frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    frame.setVisible(true);
  }

The following code sample is taken from the MainFrame1 class. In the ActionPerformed event of the Category button, the singleton instance of the CategoryFrame is retrieved and displayed centered on the screen:

  void btnCategories_actionPerformed(java.awt.event.ActionEvent e) {
    DecoratedFrame frame = CategoryFrame.getCategoryFrame();
    //Center the window
    CliffhangerApplication.showCenteredFrame(frame, false);
  }

FindFrame class:

The FindFrame class is used in the Cliffhanger application as a base class from which the Find dialogs for the various records are subclassed; e.g. OrderFindFrame, CustomerFindFrame and ProductFindFrame. The FindFrame subclasses need to add the following functionality: