MyNotepad.java

/*******************************************************************
MyNotepad.java
This sample is provided as a companion to the Introduction to WFC 
Programming topic in the Visual J++ documentation. Read the section  
titled MyNotepad Sample Walkthrough in conjunction with this sample.

********************************************************************/ 

import com.ms.wfc.app.*;
import com.ms.wfc.core.*;
import com.ms.wfc.ui.*;
import com.ms.wfc.io.*;

public class MyNotepad extends Form
{

    private File currentDoc; // the I/O file stream  
    private String fileName; // the most recently-used file name
    private boolean fileOpen = false; // set true after file opened

    public MyNotepad()
    {
        // Required for Visual J++ Forms Designer support
        initForm();     
        this.setBounds(100, 100, 300, 300);
        this.setText("Untitled - MyNotepad");
    }
    
    private void HelpMenuAbout_click(Object sender, Event e)
    {
        MessageBox.show("Version: Visual J++ 6.0", "MyNotepad");
    }

    private void FileMenuNew_click(Object sender, Event e)
    {
        // If edit control contains text, check if it should be saved
        if (editbox.getText().length() != 0) {       
            // Open NewDialog class as a modal dialog
            int result = new NewDialog().showDialog(this);    
            // Retrieve result 
            // If Yes button was clicked open Save As dialog box
            if (result == DialogResult.YES)
                this.FileMenuSaveAs_click(sender, e);
            // If No button was clicked clear edit control and set title
            else if (result == DialogResult.NO) {
                editbox.setText("");
                this.setText("Untitled - MyNotepad");
            }
        }   
    }

    private void FileMenuOpen_click(Object sender, Event e)
    {
        // Create an Open File dialog box   
        OpenFileDialog ofd = new OpenFileDialog();
        // Set up filters and options
        ofd.setFilter("Text Docs (*.txt)|*.txt|All Files (*.*)|*.*");
        ofd.setDefaultExt("txt");
        // Run the Open File dialog box
        int OK = ofd.showDialog();
        // Check result of dialog box after it closes
        if (OK == DialogResult.OK) {
            // Retrieve the filename entered 
            fileName = ofd.getFileName();
            // Open a File stream on that filename 
            currentDoc = File.open(fileName); 
            // Retrieve length of file
            int ilength = (int)currentDoc.getLength();
            // Read in ANSI characters to edit buffer
            editbox.setText(currentDoc.readStringCharsAnsi(ilength));   
            // Close the file handle
            currentDoc.close();
            fileOpen=true;
            // Set the application's caption  
            this.setText(File.getName(fileName) + " - MyNotepad");
        }
    }

    private void FileMenuSave_click(Object sender, Event e)
    {
        // If there has been a file opened or saved
        if (fileOpen){
            // Open the current file again
            currentDoc = File.open(fileName); 
            // Write edit control contents to file
            currentDoc.writeStringCharsAnsi(editbox.getText());
            // Close file handle
            currentDoc.close();
        }
        else
            this.FileMenuSaveAs_click(sender, e);
    }

    private void FileMenuSaveAs_click(Object sender, Event e)
    {
        SaveFileDialog sfd = new SaveFileDialog();
        // Set the options  
        sfd.setFileName (fileName);
        sfd.setTitle("Save Text File");
        sfd.setFilter("Text Docs (*.txt)|*.txt|All Files (*.*)|*.*");
        sfd.setDefaultExt("txt");
        // Run the dialog box     
        int result = sfd.showDialog();
        if (result == DialogResult.OK ) {
            // Retrieve the filename entered in the dialog box
            fileName = sfd.getFileName();
            // Open a File stream with ability to create a file if needed
            currentDoc = new File(fileName, FileMode.OPEN_OR_CREATE);
            // Write the contents of the edit control to the file
            currentDoc.writeStringCharsAnsi(editbox.getText());
            // Close the file handle
            currentDoc.close();         
            fileOpen = true;
            // Set the app's caption using the filename minus its path 
            this.setText(File.getName(fileName) + " - MyNotepad");      
        }
    }

    private void FileMenuExit_click(Object sender, Event e)
    {
        // Call the new file handler to invoke NewDialog
        // to ask if user wants to save current data 
        this.FileMenuNew_click(sender, e);  
        Application.exit();
    }

    /**
     * NOTE:  The following code is required by the Visual J++ Forms
     * Designer. It can be modified using the Form editor. Do not
     * modify it using the Text editor.
     */

    Container components = new Container();
    MainMenu Menu = new MainMenu();
    MenuItem FileMenu = new MenuItem();
    MenuItem FileMenuNew = new MenuItem();
    MenuItem FileMenuOpen = new MenuItem();
    MenuItem FileMenuSave = new MenuItem();
    MenuItem FileMenuSaveAs = new MenuItem();
    MenuItem FileMenuExit = new MenuItem();
    MenuItem HelpMenu = new MenuItem();
    MenuItem HelpMenuAbout = new MenuItem();
    Edit editbox = new Edit();

    private void initForm()
    {
      FileMenuNew.setText("&New");
      FileMenuNew.addOnClick(new EventHandler(this.FileMenuNew_click));

      FileMenuOpen.setText("&Open");
      FileMenuOpen.addOnClick(new EventHandler(this.FileMenuOpen_click));

      FileMenuSave.setText("&Save");
      FileMenuSave.addOnClick(new EventHandler(this.FileMenuSave_click));

      FileMenuSaveAs.setText("Save &As");
      FileMenuSaveAs.addOnClick(new   
        EventHandler(this.FileMenuSaveAs_click));

      FileMenuExit.setText("E&xit");
      FileMenuExit.addOnClick(new EventHandler(this.FileMenuExit_click));

      FileMenu.setMenuItems(new MenuItem[] {
                            FileMenuNew, 
                            FileMenuOpen, 
                            FileMenuSave, 
                            FileMenuSaveAs, 
                            FileMenuExit});
      FileMenu.setText("&File");

      HelpMenuAbout.setText("&About MyNotepad...");
      HelpMenuAbout.addOnClick(new 
        EventHandler(this.HelpMenuAbout_click));

      HelpMenu.setMenuItems(new MenuItem[] {
                            HelpMenuAbout});
      HelpMenu.setText("&Help");

      Menu.setMenuItems(new MenuItem[] {
                        FileMenu, 
                        HelpMenu});

      this.setText("MyNotepad");
      this.setVisible(false);
      this.setAutoScaleBaseSize(13);
      this.setClientSize(new Point(302, 314));
      this.setMenu(Menu);

      editbox.setDock(ControlDock.FILL);
      editbox.setFont(new Font("Fixedsys", 8.0f,  
        FontSize.POINTS, FontWeight.NORMAL, false, false, false,
        CharacterSet.DEFAULT, 0));
      editbox.setSize(new Point(302, 314));
      editbox.setTabIndex(1);
      editbox.setText("");
      editbox.setMultiline(true);
      editbox.setScrollBars(ScrollBars.VERTICAL);

      this.setNewControls(new Control[] {
                          editbox});
    }

    /**
     * The main entry point for the application. 
     *
     * @param args Array of parameters passed to the application
     * via the command line.
     */
    public static void main(String args[])
    {
        Application.run(new MyNotepad());
    }
}