Changeset 744


Ignore:
Timestamp:
01/24/14 10:10:59 (10 years ago)
Author:
jts21
Message:
  • Implement redo buffer (every time you undo the changes get pushed to the redo buffer)
  • Modify undo/redo so each individual undo/redo is either a deletion or a modification, this fixes the problem where undoing a modification to an item whose id had changed would clone the item (now instead it will just not undo that modification, which should be fine since the only way to change an item's ID now is to move it to another frame and back again)
  • Fix issue with copying/cutting when there were items on FreeItems that were connected to items on the Frame (i.e. when you were moving the corner of a shape)
Location:
trunk/src/org/expeditee
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/agents/Format.java

    r740 r744  
    163163                 */
    164164                start.setChanged(true);
    165                 start.addToUndo(changedItems);
     165                start.addToUndoMove(changedItems);
    166166                FrameGraphics.requestRefresh(true);
    167167                return null;
  • trunk/src/org/expeditee/agents/HFormat.java

    r742 r744  
    4848               
    4949                System.out.println(changedItems);
    50                 start.addToUndo(changedItems);
     50                start.addToUndoMove(changedItems);
    5151                return null;
    5252        }
  • trunk/src/org/expeditee/gui/Frame.java

    r740 r744  
    9494        private boolean _saved = false;
    9595
     96        private static final class History {
     97                public enum Type {
     98                        deletion,
     99                        movement
     100                }
     101                public final List<Item> items;
     102                public final Type type;
     103               
     104                public History(Collection<Item> items, Type type) {
     105                        this.items = new LinkedList<Item>(items);
     106                        this.type = type;
     107                }
     108               
     109                public String toString() {
     110                        return this.type.toString() + ":\n" + this.items.toString();
     111                }
     112        }
     113       
    96114        // list of deleted items that can be restored
    97         private Stack<List<Item>> _undo = new Stack<List<Item>>();
     115        private Stack<History> _undo = new Stack<History>();
     116        private Stack<History> _redo = new Stack<History>();
    98117
    99118        // basically just a list of smaller objects?
     
    892911
    893912        /**
    894          * Adds the given list of Items to the undo stack. This is the same as
    895          * calling addToUndo() for each Item in the list.
    896          *
    897          * @param items
    898          *            The List of Items to add to the undo stack.
    899          */
    900         public void addToUndo(Collection<Item> items) {
     913         * Adds the given History event to the stack.
     914         *
     915         * @param stack The stack to add to
     916         * @param items The items to put in the event
     917         * @param type  The type of event that occurred
     918         */
     919        private void addToUndo(Collection<Item> items, History.Type type) {
    901920                if (items.size() < 1)
    902921                        return;
    903922               
    904 //              System.out.println("Added: " + items);
    905 
    906                 _undo.push(new LinkedList<Item>(items));
     923                System.out.println("Added: " + items);
     924
     925                _undo.push(new History(items, type));
     926        }
     927       
     928        public void addToUndoDelete(Collection<Item> items) {
     929                addToUndo(items, History.Type.deletion);
     930        }
     931        public void addToUndoMove(Collection<Item> items) {
     932                addToUndo(items, History.Type.movement);
    907933        }
    908934
    909935        public void undo() {
    910                 List<Item> undo = null;
    911936                boolean bReparse = false;
    912937                boolean bRecalculate = false;
     
    915940                        return;
    916941
    917                 undo = _undo.pop();
     942                History undo = _undo.pop();
    918943               
    919 //              System.out.println("Undoing: " + undo);
    920 
    921                 for(Item i : undo) {
    922                         // if the change was to characteristics
    923                         if(i.isVisible() && _body.contains(i)) {
    924                                 _body.set(_body.indexOf(i), i);
    925                         // the item was deleted
    926                         } else {
    927                                 _body.add(i);
     944                System.out.println("Undoing: " + undo);
     945
     946                switch(undo.type) {
     947                case deletion:
     948                        _redo.push(undo);
     949                for(Item i : undo.items) {
     950                        _body.add(i);
    928951                                bReparse |= i.hasOverlay();
    929952                                bRecalculate |= i.recalculateWhenChanged();
     
    935958                                        i.setOffset(0, 0);
    936959                                }
    937                         }
     960                }
     961                break;
     962                case movement:
     963                        List<Item> changed = new LinkedList<Item>(_body);
     964                        changed.retainAll(undo.items);
     965                        _redo.push(new History(changed, History.Type.movement));
     966                        for(Item i : undo.items) {
     967                                int index;
     968                                if(i.isVisible() && (index = _body.indexOf(i)) != -1) {
     969                                        _body.set(index, i);
     970                                }
     971                }
     972                        break;
     973                }
     974                change();
     975                FrameMouseActions.getInstance().refreshHighlights();
     976                if (bReparse) {
     977                        FrameUtils.Parse(this, false, false);
     978                } else {
     979                        notifyObservers(bRecalculate);
     980                }
     981                // always request a refresh otherwise filled shapes
     982                // that were broken by a deletion and then reconnected by the undo
     983                // don't get filled until the user otherwise causes them to redraw
     984                FrameGraphics.requestRefresh(false);
     985                FrameGraphics.Repaint();
     986                ItemUtils.EnclosedCheck(_body);
     987        }
     988       
     989        public void redo() {
     990                boolean bReparse = false;
     991                boolean bRecalculate = false;
     992
     993                if (_redo.size() <= 0)
     994                        return;
     995
     996                History redo = _redo.pop();
     997               
     998                System.out.println("Redoing: " + redo);
     999
     1000                switch(redo.type) {
     1001                case deletion:
     1002                        _undo.push(redo);
     1003                for(Item i : redo.items) {
     1004                        _body.remove(i);
     1005                                bReparse |= i.hasOverlay();
     1006                                bRecalculate |= i.recalculateWhenChanged();
     1007                                if (i instanceof Line) {
     1008                                        Line line = (Line) i;
     1009                                        line.getStartItem().removeLine(line);
     1010                                        line.getEndItem().removeLine(line);
     1011                                } else {
     1012                                        i.setOffset(0, 0);
     1013                                }
     1014                }
     1015                break;
     1016                case movement:
     1017                        List<Item> changed = new LinkedList<Item>(_body);
     1018                        changed.retainAll(redo.items);
     1019                        _undo.push(new History(changed, History.Type.movement));
     1020                        for(Item i : redo.items) {
     1021                                int index;
     1022                                if(i.isVisible() && (index = _body.indexOf(i)) != -1) {
     1023                                        _body.set(index, i);
     1024                                }
     1025                }
     1026                        break;
    9381027                }
    9391028                change();
     
    14771566                }
    14781567                _body.removeAll(newBody);
    1479                 addToUndo(_body);
     1568                addToUndoDelete(_body);
    14801569                _body = newBody;
    14811570                change();
  • trunk/src/org/expeditee/gui/FrameKeyboardActions.java

    r740 r744  
    817817                        break;
    818818                case KeyEvent.VK_Z:
    819                         DisplayIO.getCurrentFrame().undo();
     819                        if(isShiftDown) {
     820                                DisplayIO.getCurrentFrame().redo();
     821                        } else {
     822                                DisplayIO.getCurrentFrame().undo();
     823                        }
    820824                        return;
    821825                case KeyEvent.VK_D:
  • trunk/src/org/expeditee/gui/FrameMouseActions.java

    r740 r744  
    16751675                inWindow = false;
    16761676                // System.out.println("Left window");
    1677                 if(FreeItems.itemsAttachedToCursor())
    1678                         ItemSelection.cut();
     1677                if(FreeItems.itemsAttachedToCursor()) {
     1678                        boolean cut = true;
     1679                        for(Item i : FreeItems.getInstance()) {
     1680                                for(Item j : i.getAllConnected()) {
     1681                                        if(!FreeItems.getInstance().contains(j)) {
     1682                                                cut = false;
     1683                                                break;
     1684                                        }
     1685                                }
     1686                        }
     1687                        if(cut) {
     1688                                ItemSelection.cut();
     1689                        }
     1690                }
    16791691        }
    16801692       
     
    25992611                                // otherwise this is an undo command
    26002612                        } else {
    2601                                 DisplayIO.getCurrentFrame().undo();
     2613                                if(isControlDown()) {
     2614                                        DisplayIO.getCurrentFrame().redo();
     2615                                } else {
     2616                                        DisplayIO.getCurrentFrame().undo();
     2617                                }
    26022618                        }
    26032619                        return;
     
    26692685                        SessionStats.DeletedItems(toUndo);
    26702686                        if (parent != null) {
    2671                                 parent.addToUndo(toUndo);
     2687                                parent.addToUndoDelete(toUndo);
    26722688                                parent.removeAllItems(toUndo); // toDelete.getConnected()
    26732689                        }
     
    27552771                // the current frame as well as the overlay frame
    27562772                Frame currentFrame = DisplayIO.getCurrentFrame();
    2757                 currentFrame.addToUndo(itemList);
     2773                currentFrame.addToUndoDelete(itemList);
    27582774                itemList.clear();
    27592775                if (bReparse) {
  • trunk/src/org/expeditee/gui/Help.java

    r717 r744  
    129129                        break;
    130130                case background:
    131                         if((other & cursor) == 0) return "Undo last delete";
     131                        if((other & cursor) == 0) {
     132                                if((mod & control) != 0) return "Redo";
     133                                return "Undo";
     134                        }
    132135                        break;
    133136                }
Note: See TracChangeset for help on using the changeset viewer.