1 17 package org.eclipse.emf.common.command; 18 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.EventObject ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.ListIterator ; 26 27 import org.eclipse.emf.common.CommonPlugin; 28 import org.eclipse.emf.common.util.WrappedException; 29 30 31 35 public class BasicCommandStack implements CommandStack 36 { 37 40 protected List commandList; 41 42 45 protected int top; 46 47 50 protected Command mostRecentCommand; 51 52 55 protected Collection listeners; 56 57 60 protected int saveIndex = -1; 61 62 65 public BasicCommandStack() 66 { 67 commandList = new ArrayList (); 68 top = -1; 69 listeners = new ArrayList (); 70 } 71 72 75 public void execute(Command command) 76 { 77 if (command != null && command.canExecute()) 80 { 81 for (ListIterator commands = commandList.listIterator(top + 1); commands.hasNext(); commands.remove()) 84 { 85 Command otherCommand = (Command)commands.next(); 86 otherCommand.dispose(); 87 } 88 89 try 90 { 91 command.execute(); 92 mostRecentCommand = command; 93 commandList.add(command); 94 ++top; 95 } 96 catch (RuntimeException exception) 97 { 98 handleError(exception); 99 100 mostRecentCommand = null; 101 command.dispose(); 102 } 103 104 if (saveIndex >= top) 109 { 110 saveIndex = -2; 113 } 114 115 notifyListeners(); 116 } 117 else 118 { 119 command.dispose(); 120 } 121 } 122 123 126 public boolean canUndo() 127 { 128 return top != -1 && ((Command)commandList.get(top)).canUndo(); 129 } 130 131 134 public void undo() 135 { 136 if (canUndo()) 137 { 138 Command command = (Command)commandList.get(top--); 139 try 140 { 141 command.undo(); 142 mostRecentCommand = command; 143 } 144 catch (RuntimeException exception) 145 { 146 handleError(exception); 147 148 mostRecentCommand = null; 149 flush(); 150 } 151 152 notifyListeners(); 153 } 154 } 155 156 159 public boolean canRedo() 160 { 161 return top < commandList.size() - 1; 162 } 163 164 167 public void redo() 168 { 169 if (canRedo()) 170 { 171 Command command = (Command)commandList.get(++top); 172 try 173 { 174 command.redo(); 175 mostRecentCommand = command; 176 } 177 catch (RuntimeException exception) 178 { 179 handleError(exception); 180 181 mostRecentCommand = null; 182 183 for (ListIterator commands = commandList.listIterator(top--); commands.hasNext(); commands.remove()) 186 { 187 Command otherCommand = (Command)commands.next(); 188 otherCommand.dispose(); 189 } 190 } 191 192 notifyListeners(); 193 } 194 } 195 196 199 public void flush() 200 { 201 for (ListIterator commands = commandList.listIterator(); commands.hasNext(); commands.remove()) 204 { 205 Command command = (Command)commands.next(); 206 command.dispose(); 207 } 208 commandList.clear(); 209 top = -1; 210 saveIndex = -1; 211 notifyListeners(); 212 mostRecentCommand = null; 213 } 214 215 218 public Command getUndoCommand() 219 { 220 return 221 top == -1 || top == commandList.size() ? 222 null : 223 (Command)commandList.get(top); 224 } 225 226 229 public Command getRedoCommand() 230 { 231 return 232 top + 1 >= commandList.size() ? 233 null : 234 (Command)commandList.get(top + 1); 235 } 236 237 240 public Command getMostRecentCommand() 241 { 242 return mostRecentCommand; 243 } 244 245 248 public void addCommandStackListener(CommandStackListener listener) 249 { 250 listeners.add(listener); 251 } 252 253 256 public void removeCommandStackListener(CommandStackListener listener) 257 { 258 listeners.remove(listener); 259 } 260 261 264 protected void notifyListeners() 265 { 266 for (Iterator i = listeners.iterator(); i.hasNext(); ) 267 { 268 ((CommandStackListener)i.next()).commandStackChanged(new EventObject (this)); 269 } 270 } 271 272 275 protected void handleError(Exception exception) 276 { 277 CommonPlugin.INSTANCE.log 278 (new WrappedException 279 (CommonPlugin.INSTANCE.getString("_UI_IgnoreException_exception"), exception).fillInStackTrace()); 280 } 281 282 285 public void saveIsDone() 286 { 287 saveIndex = top; 290 } 291 292 296 public boolean isSaveNeeded() 297 { 298 302 if (saveIndex < -1) 303 { 304 return true; 305 } 306 307 if (top > saveIndex) 308 { 309 for (int i = top; i > saveIndex; --i) 310 { 311 if (!(commandList.get(i) instanceof AbstractCommand.NonDirtying)) 312 { 313 return true; 314 } 315 } 316 } 317 else 318 { 319 for (int i = saveIndex; i > top; --i) 320 { 321 if (!(commandList.get(i) instanceof AbstractCommand.NonDirtying)) 322 { 323 return true; 324 } 325 } 326 } 327 328 return false; 329 } 330 } 331 | Popular Tags |