T
- the type of sequence elements (tokens) the implementing provider object works withpublic class SwingUndoAlignmentModel<T> extends AbstractAlignmentModel<T> implements AlignmentModel<T>, AlignmentModelView<T,T>
AlignmentModel
that creates an UndoableEdit
object for every
modification that is made to the underlying data source using any of the methods specified by
AlignmentModel
. These edit objects are created every time a modification method is called
and delegate to the modification methods of another implementation of AlignmentModel
which
has to be provided on creation. This second instance is responsible for the actual manipulation of
the underlying data source. (All non-modifying methods are directly delegated to the underlying
instance.)
Important: The modification methods of the specified underlying AlignmentModel
implementation that performs the actual data manipulation must never be called directly, because that
would change the state of the data without generating an edit object. In that case the undo
methods of the edit objects would be useless because the would work on an unexpected data state.
This class does not store an UndoManager
. It has to be provided by
the inherited class or the application using the inherited class.
By default this class generated default edit objects included in LibrAlign. If you want it to generate
custom edit objects (e.g. wrappers around the LibrAlign edit objects matching the needs of your
application) you would have to provide an SwingEditFactory
which creates custom objects.
Since UndoableEdit
and UndoManager
have not really
any Swing specific prerequisites, these classes could also be used to track the undo history in an
SWT application.
Modifier and Type | Field and Description |
---|---|
protected AlignmentModel<T> |
underlyingModel |
Constructor and Description |
---|
SwingUndoAlignmentModel(AlignmentModel<T> underlyingModel,
javax.swing.undo.UndoManager undoManager)
Creates a new instance of this class which creates default LibrAlign edit objects.
|
SwingUndoAlignmentModel(AlignmentModel<T> underlyingModel,
javax.swing.undo.UndoManager undoManager,
SwingEditFactory<T> editFactory)
Creates a new instance of this class which creates new edit objects using the provided
SwingEditFactory . |
Modifier and Type | Method and Description |
---|---|
java.lang.String |
addSequence(java.lang.String sequenceName)
Adds a new empty sequence to the underlying data source and generates an ID for it.
|
java.lang.String |
addSequence(java.lang.String sequenceName,
java.lang.String sequenceID)
Adds a new empty sequence to the underlying data source assigning the specified ID to it.
|
void |
appendToken(java.lang.String sequenceID,
T token)
Appends a token at the end of the sequence.
|
void |
appendTokens(java.lang.String sequenceID,
java.util.Collection<? extends T> tokens)
Appends a sequence of tokens starting at the end of the current sequence.
|
boolean |
containsSequence(java.lang.String sequenceID)
Checks if a sequence associated with the specified unique identifier is contained in this model.
|
SwingEditFactory<T> |
getEditFactory()
Returns the edit factory used by this instance to create new edit objects, if one was passed to
when this instance was created.
|
java.lang.String |
getLabel()
Returns a string labeling this alignment.
|
int |
getMaxSequenceLength()
Returns the length of the longest sequence in the alignment which is equivalent to the length of
the alignment.
|
int |
getSequenceCount()
Returns the number of sequences in the underlying data source.
|
int |
getSequenceLength(java.lang.String sequenceID)
Returns the length of the specified sequence.
|
T |
getTokenAt(java.lang.String sequenceID,
int index)
Returns the token at the specified position.
|
TokenSet<T> |
getTokenSet()
Returns the token set which is supported by the implementation.
|
AlignmentModel<T> |
getUnderlyingModel()
Returns the underlying sequence data provider used to perform the actual manipulation of the data.
|
javax.swing.undo.UndoManager |
getUndoManager()
Returns the undo manager used by this instance.
|
AlignmentModelWriteType |
getWriteType()
Returns a value that specifies if whole sequences or single tokens can be edited in the underlying
data source.
|
boolean |
hasEditFactory()
Checks if this instance uses a custom edit factory to create new edit objects.
|
void |
insertTokenAt(java.lang.String sequenceID,
int index,
T token)
Inserts a token at the specified position.
|
void |
insertTokensAt(java.lang.String sequenceID,
int beginIndex,
java.util.Collection<? extends T> tokens)
Inserts a sequence of tokens starting at the specified position.
|
boolean |
isSequencesReadOnly()
Checks of whole sequences can be changed in the underlying data source
|
boolean |
isTokensReadOnly()
Checks of tokens can be changed in the underlying data source
|
void |
registerDocumentChange()
This method is called every time the
redo or undo methods of any of the edit
objects generated by this class are executed. |
boolean |
removeSequence(java.lang.String sequenceID)
Removes the specified sequence from the underlying data source.
|
void |
removeTokenAt(java.lang.String sequenceID,
int index)
Removes the token at the specified position from the underlying data source.
|
void |
removeTokensAt(java.lang.String sequenceID,
int beginIndex,
int endIndex)
Removes the token inside the specified interval from the underlying data source.
|
java.lang.String |
renameSequence(java.lang.String sequenceID,
java.lang.String newSequenceName)
Renames a sequence in the underlying data source.
|
java.util.Iterator<java.lang.String> |
sequenceIDIterator()
Returns an iterator over unique IDs of all sequences contained in the underlying data source
in the order they are stored in this model.
|
java.util.Set<java.lang.String> |
sequenceIDsByName(java.lang.String sequenceName)
Returns the set of unique sequence IDs associated with the specified name.
|
java.lang.String |
sequenceNameByID(java.lang.String sequenceID)
Returns the sequence name (that would be visible to the application user) associated with the
specified unique ID.
|
void |
setLabel(java.lang.String label)
Allows to specify a new label for this alignment.
|
void |
setTokenAt(java.lang.String sequenceID,
int index,
T token)
Replaces the token at the specified position by the passed token.
|
void |
setTokensAt(java.lang.String sequenceID,
int beginIndex,
java.util.Collection<? extends T> tokens)
Replaces a sequence of tokens starting at the specified position.
|
void |
setTokenSet(TokenSet<T> set)
Replaces the current token set with the specified one.
|
fireAfterSequenceChange, fireAfterSequenceRenamed, fireAfterTokenChange, getChangeListeners, getID, setID
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getChangeListeners, getID, setID
protected AlignmentModel<T> underlyingModel
public SwingUndoAlignmentModel(AlignmentModel<T> underlyingModel, javax.swing.undo.UndoManager undoManager, SwingEditFactory<T> editFactory)
SwingEditFactory
.underlyingModel
- the alignment model used to perform the actual manipulation of the data
(It must be able to write at least one of sequences or tokens.)undoManager
- the undo manager that will store the generated edit objectseditFactory
- the custom edit factory that shall be used to create new edit objectsjava.lang.IllegalArgumentException
- if provider
does not allow to write sequences and tokens
(If only one of both is forbidden no exception will be thrown.)java.lang.NullPointerException
- if null
is specified for undoManager
public SwingUndoAlignmentModel(AlignmentModel<T> underlyingModel, javax.swing.undo.UndoManager undoManager)
underlyingModel
- the alignment model used to perform the actual manipulation of the data
(It must be able to write at least one of sequences or tokens.)undoManager
- the undo manager that will store the generated edit objectsjava.lang.IllegalArgumentException
- if provider
does not allow to write sequences and tokens
(If only one of both is forbidden no exception will be thrown.)java.lang.NullPointerException
- if null
is specified for undoManager
public AlignmentModel<T> getUnderlyingModel()
getUnderlyingModel
in interface BasicAlignmentModelView<T>
AlignmentModel
public javax.swing.undo.UndoManager getUndoManager()
UndoManager
or an inherited classpublic SwingEditFactory<T> getEditFactory()
null
if default LibrAlign edit objects are createdpublic boolean hasEditFactory()
true
if an edit factory is used, false
otherwise.public void registerDocumentChange()
redo
or undo
methods of any of the edit
objects generated by this class are executed. This default implementation is empty but inheriting
classes could overwrite it in order to react to document changes. (This would be an alternative to
registering a AlignmentModelChangeListener
.)public java.lang.String getLabel()
AlignmentModel
getLabel
in interface AlignmentModel<T>
getLabel
in class AbstractAlignmentModel<T>
public void setLabel(java.lang.String label) throws java.lang.UnsupportedOperationException
AbstractAlignmentModel
UnsupportedOperationException
in this method.setLabel
in interface AlignmentModel<T>
setLabel
in class AbstractAlignmentModel<T>
label
- the new labeljava.lang.UnsupportedOperationException
- if this implementation does not allow to change labelspublic TokenSet<T> getTokenSet()
AlignmentModel
getTokenSet
in interface AlignmentModel<T>
public void setTokenSet(TokenSet<T> set)
AlignmentModel
setTokenSet
in interface AlignmentModel<T>
set
- the new token set to be usedpublic T getTokenAt(java.lang.String sequenceID, int index)
AlignmentModel
getTokenAt
in interface AlignmentModel<T>
sequenceID
- the identifier the sequence where the token is containedindex
- the index of the element contained in the specified sequence (The first element has the index 0.)public void setTokenAt(java.lang.String sequenceID, int index, T token) throws AlignmentSourceNotWritableException
AlignmentModel
setTokenAt
in interface AlignmentModel<T>
sequenceID
- the identifier the sequence where the token is containedindex
- the index of the element to be replaced (The first element has the index 0.)token
- the new token for the specified positionAlignmentSourceNotWritableException
- if the underlying data source is not writable for tokenspublic void setTokensAt(java.lang.String sequenceID, int beginIndex, java.util.Collection<? extends T> tokens) throws AlignmentSourceNotWritableException
AlignmentModel
setTokensAt
in interface AlignmentModel<T>
sequenceID
- the identifier the sequence where the token is containedbeginIndex
- the index of the first element to be replaced
(The first element in the sequence has the index 0.)tokens
- the new tokens for the specified positionAlignmentSourceNotWritableException
- if the underlying data source is not writable for tokensAlignmentModelUtils.charSequenceToTokenList(CharSequence, TokenSet)
public void appendToken(java.lang.String sequenceID, T token) throws AlignmentSourceNotWritableException
AlignmentModel
appendToken
in interface AlignmentModel<T>
sequenceID
- the identifier the sequence in the alignmenttoken
- the token to be insertedAlignmentSourceNotWritableException
- if the underlying data source is not writable for tokenspublic void appendTokens(java.lang.String sequenceID, java.util.Collection<? extends T> tokens) throws AlignmentSourceNotWritableException
AlignmentModel
appendTokens
in interface AlignmentModel<T>
sequenceID
- the identifier the sequence where the token is containedtokens
- the new tokens for the specified positionAlignmentSourceNotWritableException
- if the underlying data source is not writable for tokensAlignmentModelUtils.charSequenceToTokenList(CharSequence, TokenSet)
public void insertTokenAt(java.lang.String sequenceID, int index, T token) throws AlignmentSourceNotWritableException
AlignmentModel
insertTokenAt
in interface AlignmentModel<T>
sequenceID
- the identifier the sequence in the alignmentindex
- the new index the inserted element will have
(0 <= elementIndex < sequenceLength
)token
- the token to be insertedAlignmentSourceNotWritableException
- if the underlying data source is not writable for tokenspublic void insertTokensAt(java.lang.String sequenceID, int beginIndex, java.util.Collection<? extends T> tokens) throws AlignmentSourceNotWritableException
AlignmentModel
tokens
.insertTokensAt
in interface AlignmentModel<T>
sequenceID
- the identifier the sequence where the token is containedbeginIndex
- the index of the first element to be replaced
(The first element in the sequence has the index 0.)tokens
- the new tokens for the specified positionAlignmentSourceNotWritableException
- if the underlying data source is not writable for tokensAlignmentModelUtils.charSequenceToTokenList(CharSequence, TokenSet)
public void removeTokenAt(java.lang.String sequenceID, int index) throws AlignmentSourceNotWritableException
AlignmentModel
removeTokenAt
in interface AlignmentModel<T>
sequenceID
- the identifier the sequence in the alignmentindex
- the index of the element to be removed (The first element has the index 0.)AlignmentSourceNotWritableException
- if the underlying data source is not writable for tokenspublic void removeTokensAt(java.lang.String sequenceID, int beginIndex, int endIndex) throws AlignmentSourceNotWritableException
AlignmentModel
beginIndex
and extends to the
token at index endIndex - 1). Thus the length of the subsequence is {@code endIndex - beginIndex}.removeTokensAt
in interface AlignmentModel<T>
sequenceID
- the identifier the sequence in the alignmentbeginIndex
- the beginning index, inclusiveendIndex
- the ending index, exclusiveAlignmentSourceNotWritableException
- if the underlying data source is not writable for tokenspublic int getSequenceLength(java.lang.String sequenceID)
AlignmentModel
getSequenceLength
in interface AlignmentModel<T>
sequenceID
- the identifier the sequence in the alignment-1
if no sequence with the specified name existspublic int getMaxSequenceLength()
AlignmentModel
Note that this value represents the number of compounds of the specified data type. If e.g. a DNA data source is viewed as amino acid data this method would still return the number of nucleotides in the longest sequence.
getMaxSequenceLength
in interface AlignmentModel<T>
0
if there are not sequences contained
in the underlying data sourcepublic AlignmentModelWriteType getWriteType()
AlignmentModel
getWriteType
in interface AlignmentModel<T>
public boolean isTokensReadOnly()
AlignmentModel
isTokensReadOnly
in interface AlignmentModel<T>
true
if token can be written or removed, false
otherwisepublic boolean isSequencesReadOnly()
AlignmentModel
isSequencesReadOnly
in interface AlignmentModel<T>
true
if sequences can be written, renamed or removed, false
otherwisepublic boolean containsSequence(java.lang.String sequenceID)
AlignmentModel
containsSequence
in interface AlignmentModel<T>
sequenceID
- the ID of the sequence to checked ontrue
if an according sequence was found, false
otherwisepublic java.util.Set<java.lang.String> sequenceIDsByName(java.lang.String sequenceName)
AlignmentModel
sequenceIDsByName
in interface AlignmentModel<T>
sequenceName
- the name of the sequence that would be visible to the application userpublic java.lang.String sequenceNameByID(java.lang.String sequenceID)
AlignmentModel
sequenceNameByID
in interface AlignmentModel<T>
sequenceID
- the unique unmodifiable ID the sequence is identified bynull
if no sequence with this ID is contained in this modelpublic java.lang.String addSequence(java.lang.String sequenceName)
AlignmentModel
addSequence
in interface AlignmentModel<T>
sequenceName
- the name of the new sequencepublic java.lang.String addSequence(java.lang.String sequenceName, java.lang.String sequenceID)
AlignmentModel
addSequence
in interface AlignmentModel<T>
sequenceName
- the name of the new sequencesequenceID
- the ID the new sequence shall havesequenceID
)public boolean removeSequence(java.lang.String sequenceID)
AlignmentModel
removeSequence
in interface AlignmentModel<T>
sequenceID
- the unique ID of the sequence to be removedtrue
if an sequence with the specified ID was removed, false
if no sequence with the specified ID was contained in this modelpublic java.lang.String renameSequence(java.lang.String sequenceID, java.lang.String newSequenceName)
AlignmentModel
renameSequence
in interface AlignmentModel<T>
sequenceID
- the ID of the sequence to be renamednewSequenceName
- the new name the sequence shall havepublic java.util.Iterator<java.lang.String> sequenceIDIterator()
AlignmentModel
sequenceIDIterator
in interface AlignmentModel<T>
public int getSequenceCount()
AlignmentModel
getSequenceCount
in interface AlignmentModel<T>