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:
private void initData() {
// Instantiate a local instance of DataModule1 to
// get it's CustomerDataSet.
DataModule1 dm = DataModule1.getDataModule();
// Instantiate a local instance of the DataModule's
// customerDataSet, and set the DataSet properties of
// the db controls to this QueryDataSet.
qdsCustomer = dm.getCustomerDataSet();
locatorControl1.setDataSet(qdsCustomer);
choiceControl1.addItem("LASTNAME");
choiceControl1.addItem("FIRSTNAME");
choiceControl1.addItem("PHONE");
choiceControl1.addItem("ID");
gridControl1.setDataSet(qdsCustomer);
}
void btnGoto_actionPerformed(ActionEvent e) {
// Show the Customer form
DecoratedFrame frame = CustomerFrame.getCustomerFrame();
CliffhangerApplication.showCenteredFrame(frame, false);
// Hide this frame ...
this.setVisible(false);
this.dispose();
}