简体   繁体   中英

Undo/Redo functionality in javafx spreadsheet

I am working on javafx application in which i'm dealing with spreadsheet view. I'm importing files from local storage and show in spreadsheet view in my javafx application. I have implemented almost functionalities but Undo and Redo functionality seems very difficult for me. Even i have no where to start and what will be operation cases, No idea :( Please help me with you experience and knowledge. Thank you in Advance...!

I have resolved this issue and Logic behind this solution is :-

public class UndoRedo {

    private SpreadsheetCell cell;

    private String oldValue;

    private String newValue;

    public UndoRedo(SpreadsheetCell cell, String oldValue, String newValue) {
        this.cell = cell;
        this.oldValue = oldValue;
        this.newValue = newValue;
    }

    public SpreadsheetCell getCell() {
        return cell;
    }

    public void setCell(SpreadsheetCell cell) {
        this.cell = cell;
    }

    public String getOldValue() {
        return oldValue;
    }

    public void setOldValue(String oldValue) {
        this.oldValue = oldValue;
    }

    public String getNewValue() {
        return newValue;
    }

    public void setNewValue(String newValue) {
        this.newValue = newValue;
    }

}

add event on SpreadSheet for add object in list of any change operation in Cell.

mGridBase.addEventHandler(GridChange.GRID_CHANGE_EVENT, (GridChange e) -> {
            isCellEdited = true;
            SpreadsheetCell cell = mGridBase.getRows().get(e.getRow()).get(e.getColumn());
            String oldValue = lastValue;
            UndoRedo undoRedo = new UndoRedo(cell, oldValue, cell.getText());
            undoRedoList.add(undoRedo);
        }); 

Now add Key event on

Ctrl + Z

for UNDO last changes

if (KeyCode.Z == event.getCode() && event.isControlDown()) {
                if (!undoRedoList.isEmpty()) {
                    UndoRedo undoRedo = undoRedoList.remove(undoRedoList.size() - 1);
                    undoRedo.getCell().setItem(undoRedo.getOldValue());
                    mSpreadsheet.getSelectionModel().clearAndSelect(undoRedo.getCell().getRow(), mSpreadsheet.getColumns().get(undoRedo.getCell().getColumn()));
                }
            }

And now its working perfectly and reliable then

Grid grid = ...;  Stack<GridChange> undoStack = ...;  grid.addEventHandler(GridChange.GRID_CHANGE_EVENT, new EventHandler<GridChange>() {

         public void handle(GridChange change) {
                 undoStack.push(change);
             }
         });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM