Creating more code
We will now create the code to load a text file into the TextArea.
- Press the Load button to show its properties in the Composer. Choose its 'Listeners' page and select 'Listen for Action events'. Select the Action page.
- Press the Code Sourcerer button from the Load button's Action page. Choose 'File operations...'. Press Next.
- Choose the second item, "Create a new File object from a FileDialog...". Press Next.
- The Code Sourcerer asks you to enter a title for the FileDialog. Enter 'Choose a file to load' for the title.
- The Code Sourcerer asks you to enter a name for the File object which will point to the file that the user chooses. Leave the default value of 'theFile' for the destination name.
- Select 'Load' mode.
- Press Done. Several lines of code should have appeared in the Action page which will launch a File Dialog and ask the user to choose a file to load. A file object has been created called theFile.
- Press the Code Sourcerer button again. Choose 'File operations...'. Press Next.
- Choose 'Read a String from a text file...'. Press Next.
- The Code Sourcerer asks for the name of a File object to read from. Leave the default value of 'theFile'. The Code Sourcerer also asks for the name of the string in which to store the data. Leave the default value of 'theText'.
- Press Done. Now you have created a File object, and read the text from the file that the user chose into the String, 'theText'.
- We will use the Code Sourcerer once more to put the text that we just read into the text area. Press the Code Sourcerer button and choose 'Change a property of an existing part...'. Press Next.
- Choose the part, 'text' to change. Press Next.
- Choose 'Change text of Text Area'. Press Next.
- Choose 'The following variable expression'. Remove any text in the text field to the right and replace it with theText. (Note: It is case sensitive!) Press Done.
You should see the following code in the Action page of the Load button.
You can now test this code by pressing the Load button. A FileDialog will appear. Choose any text file on your computer to load. The contents of that file should appear in the text area.
We will now create the code to save a text file.
- Press the Save button to show its properties in the Composer. Choose its 'Listeners' page and select 'Listen for Action events'. Select the Action page.
- Press the Code Sourcerer button from the Save button's Action page. Choose 'File operations...'. Press Next.
- Choose the second item, "Create a new File object from a FileDialog...". Press Next.
- Enter 'Choose a filename to save' for the title.
Leave the default value, 'theFile', for the destination name.
Select 'Save' mode.- Press Done. Several lines of code should have appeared in the Action page which will launch a File Dialog and ask the user to choose a filename. A file object has been created called theFile.
- Press the Code Sourcerer button again. Choose 'File operations...'. Press Next.
- Choose 'Write a String to a text file...'. Press Next.
- The Code Sourcerer asks for the name of a File object to write to. Leave the default value, 'theFile', which is the name of the File object you just created. Press Next.
- The Code Sourcerer now asks where the text to be saved should come from. Select 'A Part' and choose 'text' from the Choice box. This instructs the Code Sourcerer to save the current text from the component named 'text' (our TextArea).
- Press Done. Now you have created a File object, and saved the text to it.
You can test this code by typing a few words into the text area and pressing the Save button. A FileDialog will ask you for the filename to save it to. You can check that this worked by pressing the New button, and then using the Load button to read the text back.
Let's add one last thing to this program... A way to close it.
- Toward the top of the Composer window, find the part list Choice box and choose the first item, 'TextEditor'. This shows the settings for the window that our program is sitting in.
- Select the 'Listeners' page.
- We want to respond to the user trying to close the window. Select 'Listen for Window events'. A new page appears in the properties notebook called 'Window'. Select this new page.
- There are seven types of window events. We want to respond to a 'Window Closing' event. From the Choice box to the left of the Code Sourcerer button, select the third item, labelled 'windowClosing'. (Be sure to select windowClosing and not windowClosed, which is different.)
- Press the Code Sourcerer Button.
- Choose 'Window operations...'. Press Next.
- Select the first item, labelled 'Open/Close/etc...'for the current window. Press Next.
- Choose 'Dispose of this Frame'. Press Done.
The following code should have appeared in the Window Page for the windowClosing event.
You can test this code by closing the program window in the default manner for your operating system. (This is often done by double clicking the upper-left icon or by pressing a close button on the upper-right). You can get the window back by choosing Initialize Class from the Program menu in the Composer. This resets the program to its initial state.
You have now finished designing your text editor. Save your work by choosing Exit from the File menu in the Composer. Choose Yes when asked if you want to save your changes.
| Data Representations, Inc. http://www.datarepresentations.com support@datarepresentations.com sales@datarepresentations.com |