Analysis
Overview
The user should be able to insert Plain text and RTF as resources in a book. For this revision only plain text is needed.
- When inserted, the resource should create a frame on the existing page with it's content.
Task requirements
- Provide Text document in the Insert menu
- Provide a file dialog for text files (*.txt)
- Provide RTF document in the Insert menu
- Provide a file dialog for RTF files (*.rtf)
- Make sure the book with inserted plain and rtf resources is saveable.
Task result
Code
Implementation idea
Related
How to demo
- Insert a plain text
- Insert a RTF file
- Save and reload the book
Design
Two new modules are created: one for importing plain text documents (org.sophie2.extra.func.plain) and the other - for importing rtf documents (org.sophie2.extra.func.rtf). The html module is extended with functionality for importing html documents. A new abstract class HotTextImportUtil is created in org.sophie2.main.func.text with the following methods:
- handleImport - helper fo the handle function of each newly created modules.
- insertFrame - inserts a new Frame in the current book
- createHotTextBookResource - creates HotTextBookResource for insert operation.
- abstract method createRes - fills the HotTextBookResource with appropriate content for each import module.
Each module has RtfImportManager/HtmlImportManager/PlainTextImportManager which extends this class and cretes appropriate content for the newly added HotTextBookResource. The module for importing rtf documents and the module for importing html documents have the following class:
- ApplyRtfStyles/ApplyHtmlStyles (in org.sophie2.extra.func.rtfText.util/org.sophie2.extra.func.html.util package) -
use AttributedPair class (in org.sophie2.main.app.commons.frame package) to get the corresponding CommonAttr for each pair tag/attribute.Each one has the following methods:
- applyStyles - apply styles recurselively to its HotText parameter according to the html/rtf tags.
Each of the modules for importing rtf/html uses RTFEditorKit/HTMLEditorKit to get the attributes from the html/rtf document.
- getCorrespondingVal - returns the corresponding CommonAttr with value for each tag in html/rtf document.
ApplyRtfStyles uses the root element of the created StyledDocument and javax.swing.text.rtf.RTFEditorKit to obtain the attributes of the StyledDocument. ApplyHtmlStyles uses the root view of the created HTMLDocument and javax.swing.text.html.HTMLEditorKit to obtain the attributes of the HTMLDocument. Used library from javax.swing.text:
- javax.swing.text.StyledDocument - used in RtfImportUtil to convert the rtf document to attributed document.
- javax.swing.text.rtf.RTFEditorKit - used in RtfImportUtil to obtain the attributes of the StyledDocument.
- javax.swing.text.View - used in HtmlImportUtil to obtain the attributes with their values hrom the HTMLDocument.
- javax.swing.text.html.HTMLDocument - used in HtmlImportUtil to convert the html document to attributed html document.
- javax.swing.text.html.HTMLEditorKit - used in HtmlImportUtil to obtain the attributes of the StyledDocument. Create a HotTextContentProvider in org.sophie2.main.func.text.content.view package that creates HotTextFrameContent for given HotTextBookResource. Create a new method addToEnd in org.sophie2.main.app.commons.util.TextManipulationUtility class to add a String at the end of the current HotText. Create a new package org.sophie2.main.app.menus.logic and a class InsertTextItemLogic in it to provide the logic for inserting a text resource in sophie2 project. Refactor the existing class InsertTextItem to keep consistency with the rest of the project.
Tests can be found here: [4050][4051][4053][4055][4056][4057][4120][4121][4123][4131][4137]
Implementation
Done according to the design. [4399]
Also added utility class for inserting frames - FrameUtility#insertFrame (pageArea : PageWorkArea, frameSize : ImmSize, res : Resource). This is done because this method is commonly used and therefore pasted in every logic enumeration (implementation of OperationDef).
Merged to the trunk in [4424].
Testing
(Place the testing results here.)
Comments
In the next revision the function getCorrespondingVal should be removed. Some kind of AttributedIterator may be used to obtain the values directly.