Java Drawing DrawTop

Language

JP  US  UK

 

File I/O

 H. Jyounishi, Tokyo Japan
 

Frame (Index), No frame                 version:0.3(latest)  

Summary: The FileIo and its associated classes provides the methods to handle "file new", "file open", "save", "saveAs", "print" and "exit" commands.
Class on this page: FileIo, DrawFileIo, ExtensionFileFilter, FileInformation, ConfigFile, PrintableDrawPage


1. Class FileIo
public class FileIo

Field
Description
fileChooser
JFileChooser fileChooser
The JFileChooser used in this class.
currentFilePath
private String currentFilePath
Sets the file path when the file is opened.
If the "file save" command is issued, saves the file by this file path.
fileInformation FileInformation fileInformation=new FileInformation();
The FileInformation object.

Method
Description
Constructor
public FileIo()
Creates the JFileChooser used in this class and sets the FileFilter to the JFileChooser.
fileNew
public void fileNew()
Executes the "file new" command.
∙ Calls the saveCurrentFile() and shows the message according to the return code.
∙ Create a new page by the PageManeger.init() an shows it on the window
fileOpen
public void fileOpen()
Executes the file-open command.
∙ Calls the saveCurrentFile() and shows a message according to the return code.
∙ Gets the latest directory name written in the Application.config file.

This is performed by the FileInformation.getDirectorymethod. If the latest directory isn’t null, this method sets tit to the JFileChooser by the JFileChooser.setCurrentDirectory.

∙ Shows the dialog by the fileChooser.showOpenDialog method.

Gets the file-path from the dialog and sets it to filePath.
Gets the directory-path from the file-path by the FileInformation.convertToDirectoryName and sets it to Application.config file by the FileInformation.setDirectory for future use.

∙ Gets the ObjectInputStream object using the file-path.
ObjectInputStream in=new ObjectInputStream(new FileInputStream(filePath));
∙ Reads the PageList in the draw file by the DrawFileIo.readPageList.
pageManager.readPageList(in);
∙ Shows the first page on the window by the PageManager.showPage.
=> FileChooser methods
fileSave
public void fileSave()
Executes the file-save command.
If the file being edited is new and has no name, executes the following file-saveAs command.
Returns:
0: The save was successful.
1: Returns without save because the cancel option was selected on the fileChooser.
Processing:
∙ If the currentFilePathequals null, then this method executes the showSaveDialog method of the JFileChooser.
∙ Sets the WAIT_CURSOR.
∙ Saves the curent page on the display to the PageManager by the saveCurrentPage.
∙ Saves the file being edited including the above curent page by the name of this.currentFilePath.

The saving operation to a strage is executed by the DrawFileIo.writePageList method.

∙ Saves the file directory to the Application.config file by the setDirectory method of the FileInformation.
∙ Sets the DEFAULT_CURSOR.
∙ Clear the change history of the UndoDrawManager object.
fileSaveAs
public void fileSaveAs()
Execute the file-save-as command.
Returns:
0: The save was successful.
1: Returns without save operation because the cancel option was selected on the filechooser.Processing:
∙ Sets the latest directory to the JFileChooser.

The latest directory is preserved in the FileInformation object.

∙ Opens the JFileChooser dialog by the showSaveDialog method.

If the return code of the JFileChooser is CANCEL_OPTION(1), then this method returns with 1. Otherwise, continues the following steps.

∙ Gets the file-path from the JFileChooser and sets its to this.currentFilePath.
∙ Sets the WAIT_CURSOR.
∙ Saves the curent page on the display to the PageManager by the saveCurrentPage.
∙ Saves the file being edited including the above curent page by the name of this.currentFilePath.

The saving operation to a strage is executed by the DrawFileIo.writePageList method.

∙ Saves the file directory to the Application.config file by the setDirectory method of the FileInformation.
∙ Sets the DEFAULT_CURSOR.
∙ Clear the change history of the UndoDrawManager object.
saveCurrentFile
public int saveCurrentFile()
Checks whether the draw-file being edited is changed or not. If it is changed, then shows the confirmation dialog to save it or not. If the save option is selected on the dialog, saves the draw-file by fileSave() or fileSaveAs()に.
Returns:
-1: The draw-file isn't changed, so returns without save operation.
0: "Yes" was selected on the confirmation dialog, so saves the draw-file and returns.
1: "No" was selected on the confirmation dialog, so returns without save operation.
2: "Cancel" was selected on the confirmation dialog, so returns without save operation.
Processing:
∙ Checks whether the draw-file being edited is changed or not.

If the return code of the UndoDrawManager.canUndo() is true, then the draw-file is changed, so shouws the confirmation dialog.
=>If the draw-file isn't changed, returns with -1.
: Display on the DrawTop window.
If the undo button in the Shape tab is enable, the draw-file is changed.

∙ Shows the confirmation dialog
ret=JOptionPane.showConfirmDialog(ObjectTable.getDrawMain(), message, "", option);
//ret: JOptionPane.YES_OPTION=0, NO_OPTION=1, CANCEL_OPTION=2
∙ Sets the return code.
If ret equals NO_OPTION/CANCEL_OPTION, returns with 1 or 2.
If ret equals YES_OPTION, then executes the following save operation.
∙ Save operation
If the file name of the draw-file is null, then calls fileSaveAs(), otherwise calls fileSave().
printOut
public static void printOut()
Processes the "print" command.
This method draws the drawing on a printer.
Creates the Book object by the createBook method. Sets the Book object to the PrinterJob by the setPageable method. Outputs the drawing on the printer by the print method of the PrinterJob.
=>The printer related classes
createBook
public static Book createBook()
Creates the Book object based on the PageList.
Creates a new PrintableDrawPage object and a new java.awt.print.PageFormat object for each page of the PageList, and then appends them to the Book object.
The PageFormat object is created as follow.
∙ Setting of the new PageFormat object.

(a) Sets the sheet orientation (PageFormat.PORTRAIT/ PageFormat.LANDSCAPE) to the PageFormat.
(b) Sets the java.awt.print.Paper object to the PageFormat.

∙ Creates a new java.awt.print.Paper object.

(a) Sets the sheet size to the Paper object.
(b) Sets the image area to the Paper object.

=> PrintableDrawPage
exit public void exit()
Processes the "exit" command.

: Application.config file

This file is used for saving the directory of draw files accessed previously and so on. This file is configured by using the ConfigFile class.


: FileChooser methods used in the FileIo 戻る=>page top

Method Description
setFileFilter public void setFileFilter(FileFilter filter)
Sets the current file filter.
setSelectedFile public void setSelectedFile(File file)
Sets the selected file.
showOpenDialog
public int showOpenDialog(Component parent)
Pops up an "Open File" file chooser dialog.
The return values except "JFileChooser.APPROVE_OPTION" show error or cancel.
showSaveDialog public int showSaveDialog(Component parent)
Pops up a "Save File" file chooser dialog.
The return values except "JFileChooser.APPROVE_OPTION" show error or cancel.
setCurrentDirectory public void setCurrentDirectory(File dir)
Sets the current directory.
getSelectedFile public File getSelectedFile()
Returns a list of selected files if the file chooser is set to allow multiple selection.


2. Class DrawFileIo return=>page top
public class DrawFileIo

(a) ContainerList and a group ShapeContainer object


(b) A group SerializableElement object converted in the SerializableElementUtil class



(c) SerializableElementList and a group SerializableElement object
All the group links in the Group(No-6) are disconnected
and the serializableElement objects in the Group(No-6) are stored separately to the SerializableElementList.

Figure 2.1 Handling ShapeContainer and SerializableElement objects
Converts (a) to (b) by SerializableElementUtil class and moreover converts (b) to (c) by this class , then writes them to a file.



Field Description
versions public final String[] versions={"ver0.0", "ver1.0", Æ"ver1_0"};

Method Description
writePageList public void writePageList(ObjectOutputStream out) throws Exception
This method is called from the fileSave and fileSaveAs methods to writes the PageManager.PageList to the file.
Processing:
∙ Writes a version and the number of the pages (the size of PageList).
∙ Writes a ViewData of each page by the ViewData.writeViewData method.
∙ Writes a ContainerList of each page by the writeContainerList method.
=>Figure 2.2, 2.3
writeContainerList public void writeContainerList(ObjectOutputStream out, String version, ArrayList ContainerList)
Parameters:
out - The ObjectOutputStream object.
version - The String representing the version.
ContainerList - The ArrayList which stores all the ShapeContainer objects for one page of the screen.
Processing
∙Converts the ContainerList to the SerializableElementList by the convertContainerList method and writes the elements(SerializableElement) in the SerializableElementList to the file by the writeSerializableElement method.
convertContainerList
private void convertContainerList()
Processing:
Converts ShapeContainer objects in the ContainerList to SerializableElement objects and add them to the SerializableElementList(ArrayList).
The conversion is done by the convertShapeContainer method.
: If a ShapeContainer is a group object, the ShapeContainer.getGroupedContainers() returns the ShapeContainer itself and all of its child elements objects. In the case of the Figure 2.1 (a), Group0, Group1, Single Shape0, Single Shape1 and Single Shape2 are returned. Therefore, it is sufficient to perform the iteration.
convertShapeContainer
public SerializableElement[] convertShapeContainer(ShapeContainer container)
Converts a ShapeContainer object to SerializableElement objects(Figure 2.1(a) -> (c)).
If the ShapeContainer object represents a group shape object, then also converts all its child ShapeContainer objects.
ment convertShapeContainer(ShapeContainer container)
Parameters:
container - The ShapeContainer object.
Returns:
The SerializableElement object.
Processing:
∙ If the container is a single shape ShapeContainer, then;

Converts the container to the SerializableElement object by the convertSingleContainer method of the SerializableElementUtil.

∙ If he container is a group ShapeContainer, then:

Converts the container to SerializableElement objects by calling the next convertGroupContainer method recursively(Figure 2.1(a) -> (c)).

convertGroupContainer public SerializableElement[] convertGroupContainer(ShapeContainer container)
Converts a group ShapeContainer object including its child ShapeContainer objects to SerializableElement objects and returns them (Figure 2.1 (a)->(c)).
Parameters:
container - ShapeContainer object representing a group shape.
Returns:
An array of the SerializableElement objects. Processing:
The getGroupedContainers method of the ShapeContainer returns the container itself and all of its child ShapeContainer objects.
Converts the these returned objects to the SerializableElement objects by calling the convertSingleContainer method of the SerializableElementUtil (Figure 2.1 (a)->(b)).
If the converted SerializableElement object is a group SerializableElement object, then replaces the objects in the childrenList to its shapeId. This procedure is the conversion shown in the Figure 2.1 (b)->(c).
writeSerializableElement private static void writeSerializableElement(ObjectOutputStream out, SerializableElement sElement) throws Exception
Parameters:
out - The ObjectOutputStream object.
sElement - The SerializableElement object.
Processing:
Writes the sElement to the file.
∙ Writes one of the versions to the file.
∙ Writes each field of the SerializableElement object to the file.

Writes the SerializableCurve2D object by the writeSerializableCurve2D method and the SerializableAttributedString object by the writeSerializableAttributedString method to the file.

readPageList
public void readPageList(ObjectInputStream in) throws Exception
This method is called from the fileOpen method to reads the PageManager.PageList from the file.
Processing:
∙ Reads a version and the number of the pages (the size of PageList).
∙ Reads a ViewData of each page by the ViewData.readViewData method.
∙ Reads a ContainerList of each page by the readContainerList method.
∙ Sets 1 to the PageManager.CurrentPage and calls the PageManager.showPage method.
=> Figure 2.2, Figure 2.3
readContainerList public ArrayList readContainerList(int page, ObjectInputStream in)
This method is called from the ComponentManager which is related to the component library in addition to the methods of this class.
Parameters:
page - Page number. The start page is 1 in accordance with the PageManager.CurrentPage.

This parameter is not related to processing. Used in Print.

in - The ObjectInputStream object.
Returns:
Returns the ContainerList.
Processing:
Reads SerializableElement objects from the file by the readSerializableElement method and stores them to the SerializableElementList and reconstructs the ContainerList from the SerializableElementList by the following invertContainerList method.
=> Figure 2.2, 2.3
readSerializableElement public SerializableElement readSerializableElement(ObjectInputStream in) throws Exception
Returns:
Returns the SerializableElement object.
Processing:
∙ Creates a SerializableElement object and sets the data read from the file to the fields of the SerializableElement.
invertContainerList private void invertContainerList(ArrayList containerList, ArrayList serializableElementList)
Parameters:
containerList - the ContainerList( ArrayList) which is managed by the ContainerManager,
serializableElementList - An ArrayList which stores SerializableElement objects.
Processing:
∙ Creates a SerializableElement object and sets the data read from the file to the fields of the SerializableElement.
invertShapeContainer
public ShapeContainer invertShapeContainer(SerializableElement sElement, ArrayList SerializableElementList)
Inverts a SerializableElement object stored in the SerializableElementList(ArrayList to a ShapeContainerobject.
Parameters:
sElement - SerializableElement object.
serializableElementList - The ArrayList which is stores the SerializableElement objects.
Returns:
ShapeContainer - ShapeContainer object.
Processing:
∙ If the sElement is a single shape object

Calls the invertSingleContainer methos of the SerializableElementUtil .

∙ If the sElement is a group shape object

Calls the invertGroupContainer method of the SerializableElementUtil .
The invertGroupContainer method stores the shapeId to the childrenList of the ShapeContainer and returns the ShapeContainer.

: Figure 2.1 (b) ->(a)
Retrieves the child SerializableElement of the sElement from the serializableElementList and calls this method recursively to create the child ShapeContainer object and set its to the GroupElement to the ShapeContainer object by the groupElement.addChildrenmethod.

getSerializableElement private static SerializableElement getSerializableElement(String shapeId, ArrayList sElementList)
Parameters:
shapeId - The shapeId.
sElementList - The ArrayList storing SerializableElement objects.
Returns:
The SerializableElement object whose shapeId matches to the shapeIdp arameter.
isDuplicatedNumber public boolean isDuplicatedNumber(ArrayList serializableElementList)
Processing:
printContainerList private void printContainerList(String title, ArrayList ContainerList)
Prints out the elements of the ContainerList for debug.
printSerializable
ElementList

private void printSerializableElementList(String title, ArrayList sElementList)
Prints out the elements of the sElementList for debug.


Figure2.2Hierarchy of file write methods

The DrawFileIo.readPageList - the top method of the hierarchy - is called by the fileOpen method to read the PageList from the file.
Figure2.3 Hierarchy of file read methods


3. Class ExtensionFileFilter return=>page top
public class ExtensionFileFilter extends FileFilter

Field Description
description private String description
The description of the filter.
extensions private ArrayList arrayList
The list to registers the file name extension.

Method Description
addExtension public void addExtension(String extension)
Adds the the file name extension to the extensions.
setDescription public void setDescription(String description)
Sets the description of the filter.
getDescription public String getDescription()
Returns the description of the filter.
accept public boolean accept(File file)
Returns true if the file name extension of the file exists in the extensions.


4. Class FileInformation return=>page top
public class FileInformation
This class provides the methods of creating the Application.config file, saving the directory name to the Application.config file and so on.

Field Description
configFile configFile configFile=new ConfigFile();
The ConfigFile object.

Method Description
getDirectory public String getDirectory()
Returns:
The name of the directory which was opened previously.
Processing:
∙ If the Application.config file doesn't exist, then creates it.

file.createNewFile();

∙ Loads the Application.config file by the load method of the configFile.
∙ Gets the directory name by the getValue method of the configFile.

fileChooserDirectory=this.configFile.getValue("FileChooser.Directory");

setDirectory public void setDirectory(String fileChooserDirectory)
Parameters:
fileChooserDirectory - The directory name.
Processing:
∙ Loads the Application.config file by the load method of the configFile.
∙ Puts the fileChooserDirectory to the Application.config file by the putValue method of the configFile.

configFile.putValue("FileChooser.Directory", fileChooserDirectory);

∙ Saves the Application.config file.
configFile.save();
convertToDirectoryName
public String convertToDirectoryName (String filePath)
Paremeters:
filePath - The file path name.
Returns:
The directory path name.
Processing:
Gets the directory path name from the filePath.
Example:
If the filePath equals ”C:\DrawP\DrawFile\file.draw", the directory path name is "C:\Draw\DrawFile\”.
getSimpleFileName public static String getSimpleFileName(String filePath)
Paremeters:
filePath - The file path name.
Returns:
The simple file name.
Processing:
Gets the simple file name from the filePath.
Example:
If the filePath equals ”C:\DrawP\DrawFile\file.draw", the simple file name is "file.draw".


5. Class ConfigFile return=>page top
public class ConfigFile

Field Description
properties private Properties properties
The Properties object.
fileName private String fileName
The file name which is set by the setFileName method.

Method Description
Constructor public ConfigFile()
Creates the new Properties object.
setFileName public void setFileName(String fileName)
Sets the fileName to the fileName field.
load public void load() throws IOException
Loads the file whose name is the fileName by the properties.load method.
save public void save() throws IOException
Saves the file by the properties.store method.
getValue public String getValue(String sKey)
Gets the value by the properties. getProperty(sKey).
putValue public void putValue(String sKey, String sValue)
Puts the string (sValue) by the properties.put(sKey, sValue).
getKeys
public Enumeration getKeys()
Returns the Enumeration list of all the keys by the properties.propertyNames().


6. Class PrintableDrawPage return=>page top
public class PrintableDrawPage implements Printable

Field Description
containerManager ContainerManager containerManager
The ContainerManager object which is created in this class.

Method Description
Constructor public PrintableDrawPage(int page)
Parameters:
page - The number of the page to be printed.
Processing:

Gets the ContainerList by the getContainerList method of the PageManager and sets it to the ContainerManager.
ArrayList list=pageManager.getContainerList(page);
this.containerManager.setContainerList(list);

print
public int print(Graphics g, PageFormat pf, int pageIndex)
This is the method defined by Printable interface.
Parameters:
g - The context into which the page is drawn
pf - The size and orientation of the page being drawn.
pageIndex - The zero based index of the page to be drawn (this parameter isn't referred).
Processing:

The role of this method is similar to the paint method of the DrawPanel.
Calls the drawShape method of the ShapeContainer to draw the shapes on the printer.

: The DrawMode

Sets the DrawOnPrinter to the DrawMode field of the DrawParameters.
DrawParameters.DrawMode=DrawParameters.DrawPrinter.
By this setting, the selection box and resize handles (Figure 1.1 in MousePositionLS) aren't drawn.



: The printer related classes
Class
Description
java.awt.print.
PrinterJob

Gets a PrinterJob object which is initially associated with the default printer
If the returned array of the printerJob.lookupPrintServices is empty, then outputs the error message.
The two methods of the PrinterJob are used in the printOut method of the FileIo.
∙ Sets a Book object - printerJob.setPageable(book);
∙ Prints a set of pages - printerJob.print();
java.awt.print.
Book

The Book object is created by the createBook method of the FileIo.
- book.append(new PrintableDrawPage(i+1), pageFormat);
java.awt.print.
Printable

The PrintableDrawPage class implements the java.awt.print.Printable interface.
Draws the pages by the print method defined in the Printable.
java.awt.print.PageFormat
Sets the page orientation and Paper object to the pageFormat.object as follow.
∙ pageFormat.setOrientation (PageFormat.PORTRAIT)
∙ pageFormat.setPaper(paper)


Copyright (c) 2009-2013
All other trademarks are property of their respective owners.