View Javadoc
1 package com.bonevich.erj.ui; 2 3 import com.bonevich.java.util.ImmutableIterator; 4 5 import java.util.*; 6 7 /*** 8 * A class that manages the history of command execution 9 */ 10 public final class CommandManager 11 { 12 ////////////////////////////////////////////////////////// 13 // Attributes 14 private int _maxHistory = 100; 15 private LinkedList _history = new LinkedList(); 16 private LinkedList _redoList = new LinkedList(); 17 private LinkedList _registry = new LinkedList(); 18 19 ////////////////////////////////////////////////////////// 20 // Operations 21 public void register(Command cmd) 22 { 23 _registry.add(cmd); 24 } 25 public Iterator getRegistryIterator() 26 { 27 return _registry.iterator(); 28 } 29 protected void updateAllRegisteredCommands() 30 { 31 Iterator itr = getRegistryIterator(); 32 while (itr.hasNext()) 33 { 34 Command cmd = (Command) itr.next(); 35 cmd.updateEnabled(); 36 } 37 } 38 39 public void invoke(Command cmd) 40 { 41 if (cmd instanceof CommandUndo) 42 { 43 undo(); 44 return; 45 } 46 47 if (cmd instanceof CommandRedo) 48 { 49 redo(); 50 return; 51 } 52 53 if (cmd.doIt()) 54 { 55 addToHistory(cmd); 56 } 57 else 58 { 59 clearHistory(); 60 } 61 _redoList.clear(); 62 updateAllRegisteredCommands(); 63 return; 64 } 65 66 private void undo() 67 { 68 if (_history.size() > 0) 69 { 70 Command cmdToUndo = (Command) _history.removeFirst(); 71 cmdToUndo.undoIt(); 72 _redoList.addFirst(cmdToUndo); 73 } 74 } 75 76 private void redo() 77 { 78 if (_redoList.size() > 0) 79 { 80 Command cmdToRedo = (Command) _redoList.removeFirst(); 81 cmdToRedo.doIt(); 82 _history.addFirst(cmdToRedo); 83 } 84 } 85 86 private void addToHistory(Command cmd) 87 { 88 _history.addFirst(cmd); 89 if (_history.size() > _maxHistory) 90 { 91 _history.removeLast(); 92 } 93 } 94 95 private void clearHistory() 96 { 97 _history.clear(); 98 } 99 100 } /*** end class CommandManager */

This page was automatically generated by Maven