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

  1. //
  2. // (c) 1998 Microsoft Corporation.  All rights reserved.
  3. //
  4. /**
  5. *  JNotePadFrame
  6. *    
  7. *    Main frame class for JNotepad. 
  8. *    This class implements a frame window for JNotepad. Fills the frame 
  9. *    with all the controls needed, and handles closing the app. 
  10. *
  11. *    @version 1.0, 7/9/97
  12. */
  13.  
  14. import java.awt.*;
  15. import java.awt.event.*;
  16. import java.io.*;
  17.  
  18. import com.ms.ui.*;
  19.  
  20. class JNotePadFrame extends UIFrame implements IConstants
  21. {
  22.     protected JNoteMenubar menuBar;                    // JNotepad's menu bar
  23.     protected JNoteToolbar toolBar;                    // JNotepad's tool bar
  24.     protected JNoteTabFileViewer tabFileView;        // JNotepad's tab view - where all the files are kept
  25.     protected String loadedFileName = null;            // name of initial file to load
  26.     
  27.     /**
  28.     *    JNotePadFrame constructor.    
  29.     *
  30.     *    @param title Frame window caption
  31.     *     @param    filename    Name of initial file to load
  32.     */
  33.     
  34.     public JNotePadFrame(String title, String filename)
  35.     {
  36.         super (title);
  37.  
  38.         // create new icon for JNotepad
  39.         loadFrameIcon(ResourceIDs.IDS_JNOTEPADICON);
  40.  
  41.         // create the Settings object, which allows classes to store user
  42.         // configurable option information in. Load in its data file.
  43.         JNoteAppSettings.init("JNotepad.ini");
  44.         JNoteAppSettings.load();
  45.  
  46.         // Create a CommandFeature. This object handles all of the command
  47.         // processing in the application.
  48.         JNoteCommandFeature commandFeature = new JNoteCommandFeature();
  49.         JNoteAppCommandFeature.init(commandFeature, commandFeature);
  50.  
  51.         // create menu bar
  52.         menuBar = new JNoteMenubar(null, JNotePad.getResources(), IDR_MENUBAR);
  53.         menuBar.addWordWrapSubmenu(JNotePad.loadString(ResourceIDs.IDS_WORDWRAPMENU));
  54.  
  55.         // create a toolbar
  56.         toolBar = new JNoteToolbar(JNoteAppletObject.getApplet());
  57.         toolBar.loadButtons(JNotePad.getResources());
  58.  
  59.         // Create tabbed file viewer
  60.         // This will hold the files and edit controls        
  61.         tabFileView = new JNoteTabFileViewer(this, menuBar, commandFeature, commandFeature);
  62.         
  63.         // initialize the CommandFeature. We pass it the final destination
  64.         // of all commands.
  65.         commandFeature.init(tabFileView, tabFileView);
  66.  
  67.         // add talkers to the CommandFeature. These objects will be listened to
  68.         // by the CommandFeature for command selection events.
  69.         commandFeature.addTalker(menuBar);
  70.         commandFeature.addTalker(toolBar);
  71.         
  72.         // create a blank file or load in a current file
  73.         if (filename == null)
  74.         {
  75.             tabFileView.newFile();
  76.         }
  77.         else
  78.         {
  79.             tabFileView.addNewFileTab(null, filename);
  80.         }
  81.                 
  82.         // activate the CommandFeature.
  83.         commandFeature.register();
  84.         
  85.         // Create a bandbox and put the menu bar and the toolbar inside it.
  86.         // Set this bandbox as the header component in the frame.
  87.         UIBandBox bandBox = new UIBandBox();
  88.         bandBox.add(menuBar);
  89.         bandBox.add(toolBar);
  90.         setMenuBar(bandBox);
  91.         
  92.         // add the tabbed file viewer
  93.         add(tabFileView, "center");
  94.                 
  95.         // must show before resizing otherwise insets() doesn't
  96.         // work right
  97.         show();
  98.         hide();
  99.         
  100.         // resize
  101.         resize(getInsets().left + getInsets().right  + 600,
  102.             getInsets().top  + getInsets().bottom + 400);        
  103.         
  104.     }
  105.  
  106.     /**
  107.     *    Sets the icon for this frame window.
  108.     *
  109.     *    @param    stringID    The ID of the string which contains the name of the image to load.
  110.     */
  111.     protected void loadFrameIcon(int stringID)
  112.     {
  113.         String imgName = JNotePad.loadString(stringID);
  114.  
  115.         if (imgName != null)
  116.         {
  117.             // no need for a mediatracker since we won't be displaying it right away
  118.             Image im = Toolkit.getDefaultToolkit().getImage(imgName);
  119.  
  120.             if (im != null)
  121.             {
  122.                 getFrame().setIconImage(im);                
  123.             }
  124.         }        
  125.     }
  126.     
  127.     
  128.     /**
  129.     *    Handles window events. The only one handled is Event.WINDOW_DESTROY,
  130.     *    which signals that the window is to be closed. This is handled by a 
  131.     *    call to closeWindow(). All other events are handled by the superclass.
  132.     * 
  133.     *    @param evt Event that the window is to handle
  134.     */      
  135.     public boolean handleEvent(Event evt)
  136.     {
  137.         switch (evt.id)
  138.         {
  139.             // Application shutdown (e.g. user chooses Close from the system menu).
  140.         case Event.WINDOW_DESTROY:
  141.             
  142.             // if the tab file view says its okay to close the app
  143.             // (all files have been saved, and the user hasn't cancelled),
  144.             // then close the window.
  145.             if (tabFileView.closingApp())
  146.             {
  147.                 closeWindow();
  148.             }
  149.             
  150.             return true;
  151.             
  152.         default:
  153.             return super.handleEvent(evt);
  154.         }             
  155.     }
  156.     
  157.     /**
  158.     *    Closes the window after saving the app settings (settingsObj).
  159.     */
  160.     public void closeWindow()
  161.     {
  162.         JNoteAppSettings.save();    
  163.         
  164.         // !!! dispose();
  165.         setVisible(false);
  166.         System.exit(0);
  167.     }
  168.     
  169. }
  170.