The Query
object provides the following mechanism for performing undo and redo operations:
Every time that a change alters the state of a Query
object an UndoAvailableEvent
is fired. This event provides listeners with an undoable edit object.
The getEdit
method of the UndoAvailableEvent
returns the undoable edit object, which is of type QueryEdit
.
The QueryEdit
object contains a reference to the Query
object whose state was changed. The undo
and redo
methods of the QueryEdit
object perform the actual undo and redo operations on the appropriate Query
object.
This example assumes that the variables in the following table exist.
Variable |
Description |
---|---|
|
Represents the Query object. |
|
Represents a list of measure names. |
|
Represents an array of dimension layout arrays. |
In preparation for performing an undo or a redo, the following code creates an UndoRedoListener
that extends the standard QueryListener
class.
private class UndoRedoListener extends QueryListener { public UndoRedoListener(Query m_query) { super(m_query); //Called when a new undoable edit is available from the Query object. public void undoAvailable(UndoAvailableEvent e) { //Retrieve the undoable edit object. m_edit = e.getEdit(); } public void redo() { if (m_edit != null){ m_edit.redo(); } } public void undo(){ if (m_edit != null){ m_edit.undo(); } } QueryEdit m_edit = null; }
To demonstrate both an undo and a redo operation, the following code initializes a Query object m_query
, swaps edges, and then performs an undo operation followed by a redo operation.
//Creates a listener for changes to the Query object m_ql = new UndoRedoListener(m_query); //Adds the listener to the Query object m_query.addQueryListener(m_ql); m_query.initCubeQuery(measureList, dimensions); m_query.swapEdges(DataDirector.PAGE_EDGE, DataDirector.COLUMN_EDGE); //Perform an undo operation (back to the state before //the initCubeQuery call). m_ql.undo(); //Perform a redo operation (to the state after the swapEdges call). m_ql.redo();