Package ca.cgjennings.ui.textedit
Class AbbreviationTable
- java.lang.Object
-
- ca.cgjennings.ui.textedit.AbbreviationTable
-
- Direct Known Subclasses:
AbbreviationTableManager.LanguageAwareAbbreviationTable
public class AbbreviationTable extends java.lang.Object
An abbreviation table stores a set of abbreviations. Abbreviations can be matched against a context string, which typically consists of the last several characters that were typed.- Since:
- 3.0
- Author:
- Chris Jennings
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AbbreviationTable.Expansion
Encapsulates an expansion result fromexpandAbbreviation(java.lang.String)
.
-
Constructor Summary
Constructors Constructor Description AbbreviationTable()
Creates a new, empty abbreviation table.AbbreviationTable(AbbreviationTable toCopy)
Creates a new abbreviation table whose entries match the specified source table.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Removes all abbreviations from the table.boolean
expandAbbreviation(CodeEditorBase ed)
Expands an abbreviation inline in the specified editor.AbbreviationTable.Expansion
expandAbbreviation(java.lang.String context)
Returns the expansion for the longest abbreviation that is a suffix of the context string.boolean
expandAbbreviation(javax.swing.text.JTextComponent ed, char suffixToIgnore)
Expands an abbreviation inline in a Swing text component.java.lang.String
get(java.lang.String abbrev)
Returns the expansion of the abbreviation.int
getMaximumAbbreviationLength()
Returns the length of the longest abbreviation in the table, or 0 if the table is empty.AbbreviationTable
getParent()
Returns the parent table of this table, ornull
if this table has no parent defined.java.util.Set<java.lang.String>
keySet()
Returns a set of all of the currently defined abbreviations.void
load(java.io.File f)
Loads an abbreviation table from a file.void
load(java.io.InputStream in)
Loads an abbreviation table from an input stream.void
load(java.io.Reader in)
Loads an abbreviation table from a reader.void
load(java.net.URL url)
Loads an abbreviation table from a URL.void
put(java.lang.String abbrev, java.lang.String expansion)
Adds a new abbreviation to the table.void
remove(java.lang.String abbrev)
Removes an abbreviation from the table.void
setParent(AbbreviationTable parent)
Sets the parent table for this table.int
size()
Returns the number of abbreviations in the table.void
store(java.io.File out)
Writes an abbreviation table to a file.void
store(java.io.OutputStream out)
Writes an abbreviation table to an output stream.void
store(java.io.Writer out)
Writes an abbreviation table to a stream writer.
-
-
-
Constructor Detail
-
AbbreviationTable
public AbbreviationTable()
Creates a new, empty abbreviation table.
-
AbbreviationTable
public AbbreviationTable(AbbreviationTable toCopy)
Creates a new abbreviation table whose entries match the specified source table.- Parameters:
toCopy
- the table to copy entries from
-
-
Method Detail
-
put
public void put(java.lang.String abbrev, java.lang.String expansion)
Adds a new abbreviation to the table.- Parameters:
abbrev
- the abbreviationexpansion
- the text that the abbreviation expands to (is short for)- Throws:
java.lang.NullPointerException
- if either parameter isnull
-
get
public java.lang.String get(java.lang.String abbrev)
Returns the expansion of the abbreviation. If an abbreviation is not defined by this table, thennull
is returned unless this table has a parent. If it has a parent, then the parent's expansion for the abbreviation is returned as if by callinggetParent().get( abbrev )
.- Parameters:
abbrev
- the abbreviation to expand- Returns:
- the expansion, or
null
if none is defined - See Also:
put(java.lang.String, java.lang.String)
,setParent(ca.cgjennings.ui.textedit.AbbreviationTable)
-
setParent
public final void setParent(AbbreviationTable parent)
Sets the parent table for this table. If there is no expansion defined for an abbreviation in this table, and the parent table is non-null
, then the expansion from the parent table (if any) will be returned instead. The only methods whose return value is affected by the parent areget(java.lang.String)
andgetMaximumAbbreviationLength()
. (For example,keySet()
returns only the keys immediately defined in this table. It does not include the keys defined in the parent table.)- Parameters:
parent
- the new parent table to set for this table- See Also:
getParent()
-
getParent
public final AbbreviationTable getParent()
Returns the parent table of this table, ornull
if this table has no parent defined.- Returns:
- the parent table that is consulted to expand abbreviations when this table does not define an expansion for an abbreviation
- See Also:
setParent(ca.cgjennings.ui.textedit.AbbreviationTable)
-
keySet
public java.util.Set<java.lang.String> keySet()
Returns a set of all of the currently defined abbreviations.- Returns:
- a set of the defined abbreviations
-
remove
public void remove(java.lang.String abbrev)
Removes an abbreviation from the table.- Parameters:
abbrev
- the abbreviation to remove- Throws:
java.lang.NullPointerException
- if the abbreviation isnull
-
clear
public void clear()
Removes all abbreviations from the table.
-
size
public int size()
Returns the number of abbreviations in the table.- Returns:
- the number of abbreviations
-
getMaximumAbbreviationLength
public int getMaximumAbbreviationLength()
Returns the length of the longest abbreviation in the table, or 0 if the table is empty. If the table has a parent, then the larger of this table's maximum abbreviation and the parent table's is returned.- Returns:
- the longest abbreviation
-
expandAbbreviation
public AbbreviationTable.Expansion expandAbbreviation(java.lang.String context)
Returns the expansion for the longest abbreviation that is a suffix of the context string. Ideally, the context string has lengthgetMaximumAbbreviationLength()
, but any length string can be provided.- Parameters:
context
- the context for expanding the abbreviation- Returns:
- the expansion of the longest abbreviation
a
for whichcontext.endsWith( a )
is true, ornull
- Throws:
java.lang.NullPointerException
- if the context string isnull
-
expandAbbreviation
public boolean expandAbbreviation(CodeEditorBase ed)
Expands an abbreviation inline in the specified editor. The context for the expansion will be taken from the text immediately prior to the current caret position. If the context matches an abbreviation, then the abbreviation will be replaced by the expansion and the method will returntrue
. Otherwise, the method returnsfalse
. If the editor has a non-empty selection, this method will always return false.- Parameters:
ed
- the editor to expand an abbreviation in- Returns:
true
if an abbreviation was expanded
-
expandAbbreviation
public boolean expandAbbreviation(javax.swing.text.JTextComponent ed, char suffixToIgnore)
Expands an abbreviation inline in a Swing text component. The context for the expansion will be taken from the text immediately prior to the current caret position. If the context matches an abbreviation, then the abbreviation will be replaced by the expansion and the method will returntrue
. Otherwise, the method returnsfalse
. If the editor has a non-empty selection, this method will have no effect.- Parameters:
ed
- the editor to expand an abbreviation insuffixToIgnore
- if this is not equal to '\0' and it is the last character before the caret, it is ignored (it will not form part of the expansion context)- Returns:
true
if an abbreviation was expanded
-
load
public void load(java.io.InputStream in) throws java.io.IOException
Loads an abbreviation table from an input stream.- Parameters:
in
- the stream to read from- Throws:
java.io.IOException
- if an I/O occurs
-
load
public void load(java.io.Reader in) throws java.io.IOException
Loads an abbreviation table from a reader.- Parameters:
in
- the reader to read from- Throws:
java.io.IOException
- if an I/O occurs
-
load
public void load(java.net.URL url) throws java.io.IOException
Loads an abbreviation table from a URL.- Parameters:
url
- the URL to read from- Throws:
java.io.IOException
- if an I/O occurs
-
load
public void load(java.io.File f) throws java.io.IOException
Loads an abbreviation table from a file.- Parameters:
f
- the file to read from- Throws:
java.io.IOException
- if an I/O occurs
-
store
public void store(java.io.OutputStream out) throws java.io.IOException
Writes an abbreviation table to an output stream.- Parameters:
out
- the stream to write to- Throws:
java.io.IOException
- if an I/O occurs
-
store
public void store(java.io.Writer out) throws java.io.IOException
Writes an abbreviation table to a stream writer.- Parameters:
out
- the writer to write to- Throws:
java.io.IOException
- if an I/O error occurs
-
store
public void store(java.io.File out) throws java.io.IOException
Writes an abbreviation table to a file.- Parameters:
out
- the file to write to- Throws:
java.io.IOException
- if an I/O error occurs
-
-