editor
Interface Document

All Known Implementing Classes:
AbstractDocument, EditorDocument

public abstract interface Document

Document interface must be implemented by all Editor implementations wishing to share a common clipboard.

Author:
Valter Crescenzi, Alessandro Masci, Gianni Mecca
See Also:
EditorDocument

Field Summary
static Document clipboard
          A common clipboard for classes implementing this interface.
 
Method Summary
 void clear()
          Clears document content.
 void copy()
          Overwrites the currently selected region to the clipboard.
 void cut()
          Removes the currently selected region and overwrites it to the clipboard.
 boolean getCaseSensitive()
          Returns search and match sensitivity to characters case.
 int getPosition()
          Get current position.
 int getStartPoint()
          Get current start position.
 boolean isEmpty()
          Returns true if document is empty.
 boolean loopSearch(String p)
          A version of search to be used in loop conditions.
 boolean match(String pattern)
          Matches a pattern starting from current position.
 void open(Reader reader)
          Reads document content from a characters-stream Reader.
 void paste()
          Pastes the clipboard content to document, immediately following the current position.
 void put(String s)
          Inserts a string after current position.
 void remove()
          Removes current selection.
 boolean replace(String pattern, String s)
          Replaces the first occurrence of a pattern with a string.
 void reset()
          Moves current selection to (0,0).
 void save(Writer writer)
          Saves document content into a characters-stream Writer.
 boolean search(String pattern)
          Searches for the next occurrence of pattern starting.
 void setCaseSensitive(boolean cs)
          Set search and match sensitivity to characters case.
 void setPosition(int index)
          Set current position (right delimeter of current selection).
 void setStartPoint(int index)
          Set current start position (left delimeter of current selection).
 int size()
          Return the number of characters composing this document.
 String toString()
          Returns a String object representing document's content.
 

Field Detail

clipboard

public static final Document clipboard
A common clipboard for classes implementing this interface.
Method Detail

cut

public void cut()
Removes the currently selected region and overwrites it to the clipboard.
See Also:
copy(), paste()

copy

public void copy()
Overwrites the currently selected region to the clipboard.
See Also:
cut(), paste()

paste

public void paste()
Pastes the clipboard content to document, immediately following the current position. The region just pasted become the new currently selected region.
See Also:
copy(), cut()

replace

public boolean replace(String pattern,
                       String s)
Replaces the first occurrence of a pattern with a string.
Parameters:
pattern - pattern to search for and replace
s - string to replace the first occurrence of pattern with
Returns:
true if the pattern is found and replaced; false otherwise.

put

public void put(String s)
Inserts a string after current position. The string just inserted becomes the new current selection.
Parameters:
s - string to insert

remove

public void remove()
Removes current selection. Clipboard is unaffected by this method and current selection becomes empty.

reset

public void reset()
Moves current selection to (0,0).

clear

public void clear()
Clears document content. Current selection becomes (0,0)

loopSearch

public boolean loopSearch(String p)
A version of search to be used in loop conditions. The search method cannot be used to iterate a searching over document because of potentially indefinite loops. For example (the semantic of pattern "b" is obvious):
   doc.clear();
   doc.put("aaaab");
   while (doc.search("b")) {
     System.out.println("found: "+doc.clipboard);
   }
 
will never end because after a successful searching, current position is at the end of document, so that next searching start again from position 0.

This method slightly modify search semantic to solve this problem.

Parameters:
p - pattern to search for
Returns:
true it the pattern occurrence has been found and selected as current region; false otherwise.
See Also:
search(java.lang.String)

search

public boolean search(String pattern)
Searches for the next occurrence of pattern starting. The search is started from current position or from 0 if current position is n (n = size of document) so that a document is searched in a circular way. If the pattern occurrence is found, then it becomes the new selected region and true is returned; otherwise false is returned and the final empty region (n,n) is selected.

Parameters:
pattern - the pattern to search for
Returns:
true it the pattern occurrence has been found and selected as current region; false if it has not been found and the final empty region is selected.
See Also:
loopSearch(java.lang.String)

match

public boolean match(String pattern)
Matches a pattern starting from current position. Returns true when an occurrence of a pattern is found starting from current position. If the match succeeded, the current position is moved just after the match: if the match failed, no side-effects are produced.

This method is useful for incrementally delimit a region starting from a given position.

Parameters:
pattern - the pattern to match
Returns:
true if the match succeeded.

setPosition

public void setPosition(int index)
Set current position (right delimeter of current selection).
Parameters:
index - new current position to set

getPosition

public int getPosition()
Get current position.
Returns:
the current position

setStartPoint

public void setStartPoint(int index)
Set current start position (left delimeter of current selection).
Parameters:
index - new start position

getStartPoint

public int getStartPoint()
Get current start position.
Returns:
the current start position

size

public int size()
Return the number of characters composing this document.
Returns:
document's size

isEmpty

public boolean isEmpty()
Returns true if document is empty.
Returns:
true if document is empty; false otherwise.

open

public void open(Reader reader)
          throws IOException
Reads document content from a characters-stream Reader. Old document content is discarded and new document's content is read from a characters stream Reader.
Parameters:
reader - Reader from which new document's content is read
Throws:
IOException - if an I/O error occurred
See Also:
save(java.io.Writer)

save

public void save(Writer writer)
          throws IOException
Saves document content into a characters-stream Writer.
Parameters:
writer - Weader into which document's content is written
Throws:
IOException - if an I/O error occurred
See Also:
open(java.io.Reader)

setCaseSensitive

public void setCaseSensitive(boolean cs)
Set search and match sensitivity to characters case.
See Also:
match(java.lang.String), search(java.lang.String)

getCaseSensitive

public boolean getCaseSensitive()
Returns search and match sensitivity to characters case.
Returns:
true if searching is case-sensitive; false otherwise
See Also:
match(java.lang.String), search(java.lang.String)

toString

public String toString()
Returns a String object representing document's content.
Returns:
a string representation of document's content.
Overrides:
toString in class Object