chemaxon.struc.graphics
Class MTextDocument

java.lang.Object
  extended by chemaxon.struc.graphics.MTextDocument
All Implemented Interfaces:
java.io.Externalizable, java.io.Serializable

public class MTextDocument
extends java.lang.Object
implements java.io.Externalizable

Text document consisting of sections with different attributes.

Since:
Marvin 4.1, 09/29/2005
Version:
5.0, 02/14/2007
Author:
Peter Csizmadia
See Also:
Serialized Form

Nested Class Summary
 class MTextDocument.Portion
          Selected text.
static class MTextDocument.Section
          Plain text section with uniform attributes.
 
Constructor Summary
MTextDocument()
          Constructs an empty document.
MTextDocument(MTextDocument d)
          Copy constructor.
MTextDocument(java.lang.String s)
          Constructs a document from the specified string.
 
Method Summary
 void append(java.lang.String s, MTextAttributes attr)
          Append text to end of document.
 char charAt(int i)
          Gets a character of the document.
 boolean endsWith(char c)
          Tests whether the document ends with the specified character.
 int findScriptLastSectionIndex(int k, int subl)
          Finds the end of a subscript or superscript.
 MFont getAttrFont(MTextAttributes attr)
          Gets the font for the specified attribute object.
 MTextAttributes getAttributes(int start, int len, int set0)
          Gets the common attributes of the selected text.
 MFont getDefaultFont()
          Gets the default font.
 java.lang.String getPlainText()
          Gets the document as plain text.
 MTextDocument.Portion getPortion(int start, int end)
          Gets a portion of the document.
 int getScriptCount(int i1, int subl)
          Gets the number of subscripts or superscripts on the last normal character.
 MTextDocument.Section getSection(int k)
          Gets a section of the document.
 int getSectionCount()
          Gets the number of sections in the document.
 int getSectionIndexOfCharAt(int i)
          Gets the section index of a character.
 double getSectionShiftY(int k)
          Gets the y shift of a section.
 void insert(int i, java.lang.String s, MTextAttributes attr)
          Inserts a string.
 boolean isFontDefaultInRange(int start, int len)
          Tests whether the default font is used in the specified range.
 boolean isFontRegularInRange(int start, int len)
          Tests whether the default font is used in the specified range.
 int length()
          Gets the length of the document.
 void readExternal(java.io.ObjectInput in)
          Restores the document.
 void readFromString(java.lang.String str)
          Reads the document from a string.
 void remove(int start, int len)
          Deletes a substring in the document.
 void replace(int start, int len, java.lang.String s)
          Replaces a substring in the document.
 MFont reuseFont(java.lang.String name, int style, double size)
           
 void setAttributes(int start, int len, MTextAttributes attr)
          Sets attributes of selected text.
 void setDefaultFont(MFont f)
          Sets the default font.
 void setPlainText(java.lang.String s)
          Sets the document as plain text.
 java.lang.String substring(int start)
          Gets a substring of the document.
 java.lang.String substring(int start, int end)
          Gets a substring of the document.
 java.lang.String toString()
          Gets the string representation of the document.
 void updateSubLevel(double[] sublevX, double x, int k)
           
 void writeExternal(java.io.ObjectOutput out)
          Saves the document.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MTextDocument

public MTextDocument()
Constructs an empty document.


MTextDocument

public MTextDocument(java.lang.String s)
Constructs a document from the specified string.

Parameters:
s - the string

MTextDocument

public MTextDocument(MTextDocument d)
Copy constructor.

Parameters:
d - the document to copy
Method Detail

getPlainText

public final java.lang.String getPlainText()
Gets the document as plain text.

Returns:
the text

setPlainText

public void setPlainText(java.lang.String s)
Sets the document as plain text.

Parameters:
s - the text

length

public final int length()
Gets the length of the document.

Returns:
the length

charAt

public final char charAt(int i)
Gets a character of the document.

Returns:
the character

endsWith

public boolean endsWith(char c)
Tests whether the document ends with the specified character.

Parameters:
c - the character
Returns:
true if c is the last character, false otherwise

substring

public final java.lang.String substring(int start)
Gets a substring of the document.

Parameters:
start - index of first character
Returns:
the substring

substring

public final java.lang.String substring(int start,
                                        int end)
Gets a substring of the document.

Parameters:
start - index of first character
end - index of last character + 1
Returns:
the substring

remove

public final void remove(int start,
                         int len)
Deletes a substring in the document.

Parameters:
start - index of first character to delete
len - number of characters to delete

append

public final void append(java.lang.String s,
                         MTextAttributes attr)
Append text to end of document.

Parameters:
s - text to add
attr - section attributes or null

insert

public final void insert(int i,
                         java.lang.String s,
                         MTextAttributes attr)
Inserts a string.

Parameters:
i - character index
s - string to insert
attr - text attributes or null

replace

public final void replace(int start,
                          int len,
                          java.lang.String s)
Replaces a substring in the document.

Parameters:
start - index of first character to delete
len - number of characters to replace
s - substitution

getAttributes

public final MTextAttributes getAttributes(int start,
                                           int len,
                                           int set0)
Gets the common attributes of the selected text. A two-element object array is returned, the first element is the attributes object, the second element is an Integer with nonzero bits corresponding to the common attributes.

Parameters:
start - position of first character
len - number of characters
set0 - the set of attributes that are supposed to be set even if their values are "default"
Returns:
the attributes and the flags

isFontDefaultInRange

public final boolean isFontDefaultInRange(int start,
                                          int len)
Tests whether the default font is used in the specified range.

Returns:
true if only the default font is used, false otherwise

isFontRegularInRange

public final boolean isFontRegularInRange(int start,
                                          int len)
Tests whether the default font is used in the specified range.

Returns:
true if only the default font is used, false otherwise

setAttributes

public final void setAttributes(int start,
                                int len,
                                MTextAttributes attr)
Sets attributes of selected text.

Parameters:
start - position of first character
len - number of characters
attr - the attributes
See Also:
MTextAttributes.A_SUBLEVEL, MTextAttributes.A_FOREGROUND, MTextAttributes.A_FONT, MTextAttributes.A_FONTFAMILY, MTextAttributes.A_FONTSIZE, MTextAttributes.A_BOLD, MTextAttributes.A_ITALIC, MTextAttributes.A_SCALE, MTextAttributes.A_DX, MTextAttributes.A_DY

getPortion

public final MTextDocument.Portion getPortion(int start,
                                              int end)
Gets a portion of the document.

Parameters:
start - position of first character
end - position of last character + 1
Returns:
the portion

getDefaultFont

public MFont getDefaultFont()
Gets the default font.

Returns:
the font

setDefaultFont

public void setDefaultFont(MFont f)
Sets the default font.

Parameters:
f - the font

getSectionCount

public final int getSectionCount()
Gets the number of sections in the document.

Returns:
the number of sections

getSectionIndexOfCharAt

public final int getSectionIndexOfCharAt(int i)
Gets the section index of a character.

Parameters:
i - character position
Returns:
the section index

getSection

public final MTextDocument.Section getSection(int k)
Gets a section of the document.

Parameters:
k - section number
Returns:
the section

getSectionShiftY

public final double getSectionShiftY(int k)
Gets the y shift of a section.

Parameters:
k - the section number
Returns:
the shift

findScriptLastSectionIndex

public final int findScriptLastSectionIndex(int k,
                                            int subl)
Finds the end of a subscript or superscript.

Parameters:
k - the first section index
subl - the script level to query, +1 (superscript) or -1 (subscript)
Returns:
the index of the last section containing this subscript or superscript
Since:
Marvin 4.1, 02/25/2006

getScriptCount

public final int getScriptCount(int i1,
                                int subl)
Gets the number of subscripts or superscripts on the last normal character.

Parameters:
i1 - the position
subl - the script level to query, +1 (superscript) or -1 (subscript)
Returns:
the number of subscripts or superscripts
Since:
Marvin 4.1, 02/25/2006

getAttrFont

public MFont getAttrFont(MTextAttributes attr)
Gets the font for the specified attribute object.

Returns:
the font

toString

public java.lang.String toString()
Gets the string representation of the document.

Overrides:
toString in class java.lang.Object
Returns:
the string

readFromString

public void readFromString(java.lang.String str)
                    throws java.lang.IllegalArgumentException
Reads the document from a string.

Parameters:
str - the string
Throws:
java.lang.IllegalArgumentException

reuseFont

public MFont reuseFont(java.lang.String name,
                       int style,
                       double size)

updateSubLevel

public void updateSubLevel(double[] sublevX,
                           double x,
                           int k)

readExternal

public void readExternal(java.io.ObjectInput in)
                  throws java.io.IOException,
                         java.lang.ClassNotFoundException
Restores the document.

Specified by:
readExternal in interface java.io.Externalizable
Parameters:
in - the stream to read data from in order to restore the object
Throws:
java.io.IOException - if I/O errors occur
java.lang.ClassNotFoundException - If the class for an object being restored cannot be found.

writeExternal

public void writeExternal(java.io.ObjectOutput out)
                   throws java.io.IOException
Saves the document.

Specified by:
writeExternal in interface java.io.Externalizable
Parameters:
out - the stream to write the object to
Throws:
java.io.IOException - Includes any I/O exceptions that may occur