Back to index

wims  3.65+svn20090927
Classes | Public Member Functions | Static Public Attributes | Package Functions | Private Attributes
SharpTools.History Class Reference

This is the class to support nearly-arbitrary undo/redo operations. More...

Collaboration diagram for SharpTools.History:
Collaboration graph
[legend]

List of all members.

Classes

class  ListNode
 This is the doubly linked list class that undo/redo is implemented with. More...

Public Member Functions

void add (SharpTableModel model, CellRange range)
 This adds the range of cells to the history.
void add (SharpTableModel model, SharpClipboard clip)
 This adds a clipboard object to the history list.
void add (SharpTableModel model, CellRange range, int type)
void undo (SharpTableModel model)
 This is the undo method.
void redo (SharpTableModel model)
 This is the redo method.
boolean isUndoable ()
 This method check if undo can be done.
boolean isRedoable ()
 This method check if redo can be done.

Static Public Attributes

final static int UNCHANGED = 0
final static int INSERTROW = 1
final static int INSERTCOLUMN = 2
final static int REMOVEROW = 3
final static int REMOVECOLUMN = 4

Package Functions

 History (SharpTools sharp)
 Constructor:

Private Attributes

SharpTools sharp
ListNode current
 Holds the objects that are cut or copied.

Detailed Description

This is the class to support nearly-arbitrary undo/redo operations.

It's based on clipboard. Each time a range of cells being changed, they are saved in a clipboard and the clibboard is added to a linked list. A current pointer is maintained.

  1. When a new clipboard is added, the objects after current are deleted and the new clipboard is added to the list and current is updated to point to the this new clipboard.
  2. To undo, take out the object that current points to, and paste to the table. But before the paste the table data is saved to replace the current clipboard. Then move current one step backward
  3. To redo, move current a step forward, take out the object current points to, and paste to the table. But before the paste the table data is saved to replace the current clipboard.
  4. Row/Column insertion/deletion are specially treated.
Author:
Hua Zhong huaz@.nosp@m.cs.c.nosp@m.olumb.nosp@m.ia.e.nosp@m.du
Version:
Revision:
1.26

Definition at line 33 of file History.java.


Constructor & Destructor Documentation

SharpTools.History.History ( SharpTools  sharp) [inline, package]

Constructor:

Parameters:
sharpthe SharpTools object

Definition at line 78 of file History.java.

                              {
       this.sharp = sharp;
       current = new ListNode(null);      
    }

Member Function Documentation

void SharpTools.History.add ( SharpTableModel  model,
CellRange  range 
) [inline]

This adds the range of cells to the history.

Parameters:
modelthe SharpTableModel we operate on
rangethe cell range the operation will affect

Definition at line 89 of file History.java.

                                                            {
       // construct the clipboard to be saved
       SharpClipboard clip = new SharpClipboard(model, range, false);
        add(model, clip);
    }

Here is the caller graph for this function:

void SharpTools.History.add ( SharpTableModel  model,
SharpClipboard  clip 
) [inline]

This adds a clipboard object to the history list.

Parameters:
modelthe SharpTableModel we operate on
rangethe cell range the operation will affect

Definition at line 101 of file History.java.

                                                                {
       ListNode node = new ListNode(clip);
       if (Debug.isDebug())
           Debug.println("Add history for range "+clip.getSource());

       // add to the linked list
       current.setNext(node);
       node.setPrev(current);

       // move current forward
       current = node;

       // modified!
       // all operations should call add instead of setModified themselves
       model.setModified(true);
       sharp.checkUndoRedoState();
    }

Here is the call graph for this function:

void SharpTools.History.add ( SharpTableModel  model,
CellRange  range,
int  type 
) [inline]

Definition at line 127 of file History.java.

                                                                      {
       SharpClipboard clip;
       ListNode node;       

       if (type == UNCHANGED) {
           add(model, range);
           return;
       }
           
       if (type == REMOVEROW || type == REMOVECOLUMN) {
           // save the current range
           clip = new SharpClipboard(model, range, false);
           node = new ListNode(clip);
       }
       else {
           // for insertion, no data need to be saved
           // just save the range value
           node = new ListNode(range);
       }
       
       node.setType(type);

       // add to the end of history list
       current.setNext(node);
       node.setPrev(current);

       // move current forward
       current = node;
       model.setModified(true);
       sharp.checkUndoRedoState();
    }

Here is the call graph for this function:

This method check if redo can be done.

Returns:
true if redo is possible

Definition at line 288 of file History.java.

                                {
       return current.getNext() != null;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

This method check if undo can be done.

Returns:
true if undo is possible

Definition at line 279 of file History.java.

                                {
       return current.getPrev() != null;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

void SharpTools.History.redo ( SharpTableModel  model) [inline]

This is the redo method.

Parameters:
modelthe SharpTableModel we operate on

Definition at line 227 of file History.java.

                                            {
       if (!isRedoable())
           return;
       
       current = current.getNext();
       int type = current.getType();
       CellRange range;
       
       if (type == UNCHANGED) {

           // get the saved clipboard
           SharpClipboard oldClip = (SharpClipboard)current.getObject();
           range = oldClip.getSource();
       
           // replace the current object with the current model data
           SharpClipboard newClip = new SharpClipboard(model, range, false);
           current.setObject(newClip);

           // restore data and selection
           oldClip.paste(model, range.getminCorner());
           model.setSelection(range);
       }
       else if (type == REMOVEROW ||
               type == REMOVECOLUMN) {
           // redo a removal
           SharpClipboard clip = (SharpClipboard)current.getObject();
           range = clip.getSource();
           // insert lines first
           if (type == REMOVEROW)
              model.removeRow(range);
           else
              model.removeColumn(range);
       }
       else {
           // redo an insertion
           range = (CellRange)current.getObject();
           
           if (type == INSERTROW)
              model.insertRow(range);
           else
              model.insertColumn(range);      
       }

       model.setModified(true);
       sharp.checkUndoRedoState();
    }

Here is the call graph for this function:

Here is the caller graph for this function:

void SharpTools.History.undo ( SharpTableModel  model) [inline]

This is the undo method.

Parameters:
modelthe SharpTableModel we operate on

Definition at line 164 of file History.java.

                                            {
       if (!isUndoable())
           return;

       int type = current.getType();
       CellRange range;
       
       if (type == UNCHANGED) {
           // get the saved clipboard and its range
           SharpClipboard oldClip = (SharpClipboard)current.getObject();
           range = oldClip.getSource();
       
           // replace the current object with the current table data
           SharpClipboard newClip = new SharpClipboard(model, range, false);
           current.setObject(newClip);
       
           // recover the data (undo)
           oldClip.paste(model, range.getminCorner());
       }
       else if (type == REMOVEROW ||
               type == REMOVECOLUMN) {
           // undo a removal is just do an insertion and paste
           // saved data to it
           SharpClipboard clip = (SharpClipboard)current.getObject();
           range = clip.getSource();
           // insert lines first
           if (type == REMOVEROW)
              model.insertRow(range);
           else
              model.insertColumn(range);

           // then paste stuff
           clip.paste(model, range.getminCorner());
       }
       else {
           // undo an insertion is just a removal
           range = (CellRange)current.getObject();
           //     System.out.println("Undo: remove "+range);
           if (type == INSERTROW)
              model.removeRow(range);
           else
              model.removeColumn(range);
       }

       current = current.getPrev();

       // recover the selection
       model.setSelection(range);

       /*
       if (!isUndoable()) { // has come back to the beginning
           model.setModified(false);
       }
       */
       model.setModified(true);
       sharp.checkUndoRedoState();
    }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Holds the objects that are cut or copied.

Definition at line 71 of file History.java.

final static int SharpTools.History.INSERTCOLUMN = 2 [static]

Definition at line 66 of file History.java.

final static int SharpTools.History.INSERTROW = 1 [static]

Definition at line 65 of file History.java.

final static int SharpTools.History.REMOVECOLUMN = 4 [static]

Definition at line 68 of file History.java.

final static int SharpTools.History.REMOVEROW = 3 [static]

Definition at line 67 of file History.java.

Definition at line 63 of file History.java.

final static int SharpTools.History.UNCHANGED = 0 [static]

Definition at line 64 of file History.java.


The documentation for this class was generated from the following file: