home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / afc11 / JNotepad / src / JNoteDialog.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  4.7 KB  |  190 lines

  1. //
  2. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3. //
  4. import com.ms.ui.*;
  5. import com.ms.ui.resource.*;
  6. import java.awt.*;
  7.  
  8. /**
  9. *    An extension of UIDialog which emulates, to a certain extent, a Windows
  10. *    dialog box. Several helper functions have been added which are called at
  11. *    certain stages in the dialog's creation and display. Inherit from this
  12. *    class and override the helper functions.
  13. *    
  14. *    @version    1.0, 7/20/97
  15. */
  16.  
  17. public class JNoteDialog extends UIDialog implements IConstants
  18. {
  19.     /**
  20.     *    Parent frame
  21.     */
  22.     protected UIFrame parentFrame;
  23.     
  24.     /**
  25.     *    Creates a new JNoteDialog
  26.     *
  27.     *    @param    parentframe    Parent frame 
  28.     *    @param    title    Title of the dialog
  29.     *    @param    isModal    true if the dialog is to be run as a modal dialog box, false if it is to be modeless
  30.     */
  31.     public JNoteDialog(UIFrame parentframe, String title, boolean isModal)
  32.     {
  33.         super(parentframe, title, isModal);
  34.         init(parentframe);
  35.     }
  36.     
  37.     /**
  38.     *    Private init function. Sets the parent frame variable.
  39.     */
  40.     private void init(UIFrame parentframe)
  41.     {
  42.         parentFrame = parentframe;            
  43.     }
  44.     
  45.     /**
  46.     *    Loads the dialog from a resource file. Fills the current dialog
  47.     *    box with the controls read in from a res file. Uses Win32ResourceDecoder.
  48.     *
  49.     *    @param    resFile    Name of resource file to load from
  50.     *    @param    dialogID    integer identifier of the dialog resource to load
  51.     *    @returns    true if the loading succeeded, false if it did not.
  52.     */
  53.     public boolean loadDialog(String resFile, int dialogID)
  54.     {
  55.         Win32ResourceDecoder resdec;
  56.         
  57.         try
  58.         {
  59.             resdec = new Win32ResourceDecoder(resFile);
  60.         }
  61.         catch (Exception e) // !!!!!! Bad!
  62.         {
  63.             // dialog could not be loaded
  64.             UIMessageBox box = new UIMessageBox(new UIFrame(), 
  65.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  66.                 JNotePad.loadString(ResourceIDs.IDS_MSGNODIALOG),
  67.                 box.STOP, UIButtonBar.OK);
  68.             box.doModal();
  69.             
  70.             return false;
  71.         }
  72.         return loadDialog(resdec, dialogID);
  73.     }
  74.         
  75.     public boolean loadDialog(Win32ResourceDecoder resFile, int dialogID)
  76.     {
  77.         try 
  78.         {
  79.             resFile.populateDialog(this, dialogID);
  80.         }
  81.         catch (Exception e)
  82.         {
  83.             // couldn't load dialog
  84.             UIMessageBox box = new UIMessageBox(new UIFrame(), 
  85.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  86.                 JNotePad.loadString(ResourceIDs.IDS_MSGNODIALOG),
  87.                 box.STOP, UIButtonBar.OK);
  88.             box.doModal();
  89.             
  90.             return false;
  91.         }        
  92.         
  93.         return true;
  94.     }
  95.     
  96.     /**
  97.     *    Displays the dialog. Centers the dialog in the middle of the screen,
  98.     *    calls initDialog(), and then shows it. Use this method, instead of show(),
  99.     *    to display your dialog.
  100.     */
  101.     public void display()
  102.     {
  103.         position(false);
  104.         initDialog();
  105.         show();
  106.     }
  107.     
  108.     /**
  109.     *    Called right before the dialog is to be displayed. Override this to
  110.     *    perform pre-display initialization of the dialog.
  111.     */
  112.     protected void initDialog() { 
  113.         // set focus to the OK button
  114.         getComponentFromID(IDR_FILEOPTIONS_OK).requestFocus();
  115.     }
  116.     
  117.     /**
  118.     *    Called when the dialog is closed. Hides the dialog. Override this
  119.     *    to add any cleanup that is necessary, but be sure to call the superclass's
  120.     *    method too.
  121.     */
  122.     protected void closeDialog()
  123.     {
  124.         setVisible(false);
  125.     }
  126.     
  127.     /**
  128.     *    Called when the OK button is clicked. Default action is to close the dialog
  129.     *    by calling closeDialog(). Override this if custom functionality is needed.
  130.     */
  131.     protected void onOK()
  132.     {
  133.         closeDialog();
  134.     }
  135.     
  136.     /**
  137.     *    Called when the Cancel button is clicked. Default action is to close the dialog
  138.     *    by calling closeDialog(). Override this if custom functionality is needed.
  139.     */     
  140.     protected void onCancel()
  141.     {
  142.         closeDialog();
  143.     }
  144.     
  145.     /**
  146.     *    Called when a button is clicked. Default action is to do nothing. Override
  147.     *    this to add custom functionality.
  148.     *
  149.     *    @param    buttonID    Resource ID of the button that was clicked.
  150.     *    @param    buttonName    Name of button that was clicked.
  151.     */
  152.     protected void onButton(int buttonID, String buttonName)
  153.     {
  154.     }
  155.     
  156.     /**
  157.     *    Handles events that occur in the dialog box. Calls onOK() if OK is clicked,
  158.     *    onCancel() if cancel is clicked, and onButton() if any other button is clicked.
  159.     *
  160.     *    @param    evt    Event which is being handled
  161.     *    @returns    true if the event was handled, false if not
  162.     */
  163.     public boolean handleEvent(Event evt)
  164.     {            
  165.         if ((evt.target instanceof UIButton) &&
  166.             (evt.id == Event.ACTION_EVENT))
  167.         {
  168.             int buttonID = ((UIButton)evt.target).getID();
  169.             if (buttonID == IDR_FILEOPTIONS_OK)
  170.             {
  171.                 onOK();
  172.             }
  173.             else if (buttonID == IDR_FILEOPTIONS_CANCEL)
  174.             {
  175.                 onCancel();
  176.             }                        
  177.             else
  178.             {
  179.                 onButton(buttonID, ((UIButton)evt.target).getName());
  180.             }
  181.             
  182.             return true;
  183.         }
  184.         
  185.         return super.handleEvent(evt);
  186.     }
  187.     
  188. }
  189.  
  190.