How Do I...

Example: Open and save files

Prompt for path and file names No tip for this topic No example for this topic Prompt for text input

Suppose you have an ASCII editor that looks like the following:
ASCII editor

It uses a File Selection Prompter part to open and save text files.

Adding the using interface

Begin by creating an application, adding a visual part to it, and opening a Composition Editor on the visual part. Next, add the following parts to the Window part shown in the Composition Editor:

Parts to add
Where they are in the Parts palette

1 Multi-line Edit
Text category icon category

Multi-line Edit icon part

3 Push Buttons
Buttons category icon category

Push Button icon part

After you add the parts, change their labels and arrange them so the user interface looks like the following:
ASCII editor

You may also want to change the settings of the Multi-line Edit part so the wordWrap property is True.

Adding the File Selection Prompter part

Now, select Prompter category icon (Prompter category) and File Selection Prompter icon (File Selection Prompter part). Then, click on the free-form surface.

Adding scripts to open and save files

To open a text file, add a script that uses the method open: of the class CfsReadFileStream:

  1. Select the Script Editor icon in the lower-right corner of the Composition Editor.
  2. In the Script Editor that opens, select New Method Template from the Methods menu.
  3. Replace the template in the Script pane with following code:
    showFile: file
      | fileStream text |
      (file = nil or: [file isEmpty])
        ifTrue: [^self]
        ifFalse:
          [fileStream := CfsReadFileStream open: file.
          text := (fileStream upToEnd) trimSeparators.
          fileStream close.].
      (self subpartNamed: 'Multi-line Edit1') string: text.
     
    

    If you've changed the name of the Multi-line Edit part, change the last line of code so it uses your name for the part instead of Multi-line Edit1. You can click on Subpart Features Syntax to open the Subpart Features Syntax tool and see what you named the part in the Subparts pane.

  4. Save the script. Click mouse button 2 on the lower pane, and select Save from the pop-up menu. (On UNIX, use mouse button 3.)

Next, repeat steps 3 and 4, except add the code below so your application can save files to disk. The code uses the method openEmpty: of the class CfsReadWriteFileStream.

saveFile: file
  | content fileStream |
  content := (self subpartNamed: 'Multi-line Edit1') object.
  (file = nil)
    ifTrue: [^self]
    ifFalse:
      [fileStream := CfsReadWriteFileStream openEmpty: file].
  (fileStream size = 0)
    ifFalse: [^self]
    ifTrue: [fileStream nextPutAll: content].
  fileStream close.               
 

Again, if you've changed the name of the Multi-line Edit part, change Multi-line Edit1 so it uses your name for the part.

Making connections

After you add the scripts, select make the following connections:

After you make the connections, the parts and connections resemble the following:
ASCII editor

Fine-tuning: Attaching parts

You can now run the application. To ensure that the Multi-line Edit part resizes itself when you change the window's size, change the framingSpec settings of the Multi-line Edit part to make attachments such as the following:

Make similar attachments in the push button settings. Remember, attach two widgets only once. That is, don't attach the bottom of the Multi-line Edit to the top edge of the Open push button in the settings for both parts. Otherwise, you get an error.

You can also change the wordWrap property of the Multi-line Edit part to true.


[ Top of Page | Previous Page | Next Page | Table of Contents ]