Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

commandhistory.tcl

00001 # Copyright (C) 1997-2004 The CDG Team <cdg@nats.informatik.uni-hamburg.de> 00002 # 00003 # This file is free software; as a special exception the author gives 00004 # unlimited permission to copy and/or distribute it, with or without 00005 # modifications, as long as this notice is preserved. 00006 # 00007 # This program is distributed in the hope that it will be useful, but 00008 # WITHOUT ANY WARRANTY, to the extent permitted by law; without even the 00009 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00010 00011 ## ----------------------------------------------------------------------------- 00012 ## CommandHistory - implements generic undo-functionality 00013 ## (used for editing Parse-Trees, see class ParseTree and VisParses) 00014 ## 00015 ## 00016 ## \author Dietmar Dreyer 00017 ## 00018 ## $Id commandhistory.tcl $ 00019 ##------------------------------------------------------------------------------- 00020 00021 class CommandHistory { 00022 00023 # private variables 00024 00025 ## current position in command lists 00026 private variable _cursor -1 ; # \type TclNumber 00027 00028 ## list of commands used for redoing actions 00029 private variable _commandList "" ;# \type TclList 00030 00031 ## list of commands used for undoing actions 00032 private variable _inverseCommandList "" ; # \type TclList 00033 00034 ## list of object-IDs recognizing update-method 00035 private variable _listenerList "" ; # \type TclList 00036 00037 constructor {} {} 00038 00039 # public methods 00040 public method add {command commandInverse}; ## \type TclList, TclList 00041 public method undo {} 00042 public method redo {} 00043 public method canUndo {} 00044 public method canRedo {} 00045 public method addListener {listener}; ## \type TclString 00046 00047 # protected methods 00048 protected method update {} 00049 00050 } 00051 00052 ## ---------------------------------------------------------------------------- 00053 ## constructor 00054 ## ---------------------------------------------------------------------------- 00055 body CommandHistory::constructor {} { 00056 00057 } 00058 00059 00060 ##------------------------------------------------------------------------------- 00061 ## Adds command for redoing and undoing current action to the end of command-list 00062 ## @param command command for redoing 00063 ## @param inverseCommand command for undoing 00064 ##------------------------------------------------------------------------------- 00065 body CommandHistory::add {command inverseCommand} { 00066 set length [llength $_commandList]; 00067 00068 if { $length > [expr $_cursor]} { 00069 set _commandList [lrange $_commandList 0 $_cursor] 00070 set _inverseCommandList [lrange $_inverseCommandList 0 $_cursor] 00071 } 00072 00073 lappend _commandList $command 00074 lappend _inverseCommandList $inverseCommand 00075 set _cursor [expr [llength $_commandList] -1] 00076 00077 update 00078 } 00079 00080 00081 ##------------------------------------------------------------------------------- 00082 ## Internal method used to update registered listeners whenever a change to the 00083 ## list of commands occurred 00084 ##------------------------------------------------------------------------------- 00085 body CommandHistory::update {} { 00086 foreach listener $_listenerList { 00087 $listener update 00088 } 00089 00090 } 00091 00092 00093 ##------------------------------------------------------------------------------- 00094 ## Registers listener object which will be called whenever a change to the 00095 ## command list happens 00096 ## @param listener Object-ID implementing method update 00097 ##------------------------------------------------------------------------------- 00098 body CommandHistory::addListener { listener } { 00099 if { [lsearch $_listenerList $listener] == -1} { 00100 lappend _listenerList $listener 00101 } 00102 } 00103 00104 00105 00106 ##------------------------------------------------------------------------------- 00107 ## Calls current undo command and modifies internal cursor to point to previous 00108 ## command in the list (move to beginning) 00109 ##------------------------------------------------------------------------------- 00110 body CommandHistory::undo {} { 00111 00112 if { $_cursor >= 0 } { 00113 eval [lindex $_inverseCommandList $_cursor] 00114 set _cursor [expr $_cursor-1] 00115 } 00116 00117 update 00118 } 00119 00120 00121 ##------------------------------------------------------------------------------- 00122 ## Calls current redo command and let cursor point to next command in the list 00123 ## (move to end) 00124 ##------------------------------------------------------------------------------- 00125 body CommandHistory::redo {} { 00126 00127 if { $_cursor < [expr [llength $_commandList] -1]} { 00128 set _cursor [expr $_cursor + 1] 00129 eval [lindex $_commandList $_cursor] 00130 } 00131 update 00132 } 00133 00134 00135 ##------------------------------------------------------------------------------- 00136 ## Predicate to determine if calling undo will take effect 00137 ## @return 1 => undo will take effect, 0 => not possible 00138 ##------------------------------------------------------------------------------- 00139 body CommandHistory::canUndo {} { 00140 00141 if { $_cursor >= 0} { 00142 return 1 00143 } else { 00144 return 0 00145 } 00146 00147 } 00148 00149 00150 ##------------------------------------------------------------------------------- 00151 ## Similar to canUndo 00152 ## @return 1 => redo possible, 0 => no redo possible 00153 ##------------------------------------------------------------------------------- 00154 body CommandHistory::canRedo {} { 00155 00156 if { $_cursor < [expr [llength $_commandList] -1]} { 00157 return 1 00158 } else { 00159 return 0 00160 } 00161 }

XCDG 0.95 (20 Oct 2004)