Implementing image storage
To enable image reference manipulation functionality as described in the next chapter, maintenance of an image repository is required. The following parts have been created to support image repository maintenance:
Class, Method, Field |
Description |
FrmMain.appTempDir |
new field referencing the temporary directory of application SimplyHTML |
FrmMain.initAppTempDir() |
method to initialize the temporary directory of application SimplyHTML |
FrmMain.getAppTempDir() |
method to get the directory for temporary storage of application SimplyHTML |
DocumentPane.getImageDir() |
method to get the image directory for a particular document open in SimplyHTML |
DocumentPane.saveImages() |
method to save images at a new location |
Util.copyFile() |
method to copy files |
Generally speaking, an image repository always is kept with the document it belongs to. Whenever a document is saved, the image directory of the document is saved at the same location, necessarily copying image files as described below.
Methods initAppTempDir and getAppTempDir
With method initAppTempDir of class FrmMain new field appTempDir is initialized. The method creates a file object referencing a directory named [user home]/SimplyHTML/temp. If the directory does not exist it is created by method initAppTempDir.
Method getAppTempDir publicly makes available field appTempDir for read only access.
Method saveImages
In class DocumentPane documents are being saved with method saveDocument. With the new image support of stage 7 of SImplyHTML document storage has to be extended by a method to save any image files referenced in a particular document.
New method saveImages of class DocumentPane is called by method saveDocument for this task. It uses new method getImageDir (see below) to find out the source location of any image files. It then copies all image files to the location, the document is being saved to using method copyFile of class Util (see below).
Method getImageDir
Method getImageDir finds out the source location of image files for a document to be saved. The method checks whether images are currently stored in a temporary directory for a given document. If the document was not newly created, getImageDir finds out if the document is about to be saved at a new location (save as) or if it is being saved at the location it was loaded from (save) in which cases the source locations are to be taken from different locations in class DocumentPane .
The source image directory is returned to the calling method.
Method copyFile
Method copyFile in class Util is a simple way to copy a file from one location to another. It accepts two file objects as parameters for the source and destination file to be copied. It opens RandomAccessFile objects for the two files and creates the destination file if necessary. It then reads blocks of content bytes from the source file and writes them to the destination file.
If the destination file already exists, copyFile does nothing.