home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / sdkjava.exe / SDKJava.cab / Samples / AFC / JNotepad / src / JNoteDialog.java < prev    next >
Encoding:
Java Source  |  1998-03-05  |  4.7 KB  |  191 lines

  1. //
  2. // (c) 1998 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.     {
  114.         // set focus to the OK button
  115.         getComponentFromID(IDR_FILEOPTIONS_OK).requestFocus();
  116.     }
  117.     
  118.     /**
  119.     *    Called when the dialog is closed. Hides the dialog. Override this
  120.     *    to add any cleanup that is necessary, but be sure to call the superclass's
  121.     *    method too.
  122.     */
  123.     protected void closeDialog()
  124.     {
  125.         setVisible(false);
  126.     }
  127.     
  128.     /**
  129.     *    Called when the OK button is clicked. Default action is to close the dialog
  130.     *    by calling closeDialog(). Override this if custom functionality is needed.
  131.     */
  132.     protected void onOK()
  133.     {
  134.         closeDialog();
  135.     }
  136.     
  137.     /**
  138.     *    Called when the Cancel button is clicked. Default action is to close the dialog
  139.     *    by calling closeDialog(). Override this if custom functionality is needed.
  140.     */     
  141.     protected void onCancel()
  142.     {
  143.         closeDialog();
  144.     }
  145.     
  146.     /**
  147.     *    Called when a button is clicked. Default action is to do nothing. Override
  148.     *    this to add custom functionality.
  149.     *
  150.     *    @param    buttonID    Resource ID of the button that was clicked.
  151.     *    @param    buttonName    Name of button that was clicked.
  152.     */
  153.     protected void onButton(int buttonID, String buttonName)
  154.     {
  155.     }
  156.     
  157.     /**
  158.     *    Handles events that occur in the dialog box. Calls onOK() if OK is clicked,
  159.     *    onCancel() if cancel is clicked, and onButton() if any other button is clicked.
  160.     *
  161.     *    @param    evt    Event which is being handled
  162.     *    @returns    true if the event was handled, false if not
  163.     */
  164.     public boolean handleEvent(Event evt)
  165.     {            
  166.         if ((evt.target instanceof UIButton) &&
  167.             (evt.id == Event.ACTION_EVENT))
  168.         {
  169.             int buttonID = ((UIButton)evt.target).getID();
  170.             if (buttonID == IDR_FILEOPTIONS_OK)
  171.             {
  172.                 onOK();
  173.             }
  174.             else if (buttonID == IDR_FILEOPTIONS_CANCEL)
  175.             {
  176.                 onCancel();
  177.             }                        
  178.             else
  179.             {
  180.                 onButton(buttonID, ((UIButton)evt.target).getName());
  181.             }
  182.             
  183.             return true;
  184.         }
  185.         
  186.         return super.handleEvent(evt);
  187.     }
  188.     
  189. }
  190.  
  191.