Coverage Report - org.argouml.uml.diagram.DiagramUndoManager
 
Classes in this File Line Coverage Branch Coverage Complexity
DiagramUndoManager
78%
15/19
50%
6/12
1.625
DiagramUndoManager$DiagramCommand
37%
3/8
N/A
1.625
 
 1  
 /* $Id: DiagramUndoManager.java 17850 2010-01-12 19:53:35Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    mvw
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 2007-2008 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies. This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason. IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.uml.diagram;
 40  
 
 41  
 import java.beans.PropertyChangeListener;
 42  
 
 43  
 import org.apache.log4j.Logger;
 44  
 import org.argouml.kernel.Project;
 45  
 import org.argouml.kernel.ProjectManager;
 46  
 import org.tigris.gef.undo.Memento;
 47  
 import org.tigris.gef.undo.UndoManager;
 48  
 
 49  
 /**
 50  
  * This class is a temporary wrapper around the GEF UndoManager.
 51  
  * This will be changed when GEF is modified to create commands and
 52  
  * provide an observer interface for ArgoUML to receive them.
 53  
  * <p>
 54  
  * TODO: How does this relate to {@link org.argouml.kernel.DefaultUndoManager}?
 55  
  * 
 56  
  * @author Bob Tarling
 57  
  */
 58  900
 public class DiagramUndoManager extends UndoManager {
 59  
     
 60  900
     private static final Logger LOG = Logger.getLogger(UndoManager.class);
 61  
     
 62  
     private boolean startChain;
 63  
     
 64  
     /**
 65  
      * Called when a new user interaction starts
 66  
      * @see org.tigris.gef.undo.UndoManager#startChain()
 67  
      */
 68  
     @Override
 69  
     public void startChain() {
 70  64
         startChain = true;
 71  64
     }
 72  
     
 73  
 
 74  
     @Override
 75  
     public boolean isGenerateMementos() {
 76  
         // TODO: This shouldn't depend on the current project, but for now
 77  
         // just make sure it's defined and that we have an undo manager
 78  37
         Project p = ProjectManager.getManager().getCurrentProject();
 79  37
         return super.isGenerateMementos() && p != null 
 80  
                 && p.getUndoManager() != null;
 81  
     }
 82  
     
 83  
     /**
 84  
      * @param memento the GEF memento
 85  
      * @see org.tigris.gef.undo.UndoManager#addMemento(org.tigris.gef.undo.Memento)
 86  
      */
 87  
     @Override
 88  
     public void addMemento(final Memento memento) {
 89  
         // TODO: This shouldn't be referencing the current project.  Instead
 90  
         // the appropriate UndoManager should have already been retrieved from
 91  
         // the correct project.
 92  37
         Project p = ProjectManager.getManager().getCurrentProject();
 93  37
         if (p != null) {
 94  37
             org.argouml.kernel.UndoManager undo = p.getUndoManager();
 95  37
             if (undo != null) {
 96  37
                 if (startChain) {
 97  
                     //TODO i18n: GEF needs to pass us back the description
 98  
                     // of what is being done.
 99  0
                     undo.startInteraction("Diagram Interaction");
 100  
                 }
 101  
                 // TODO: I presume this would fix issue 5250 - but
 102  
                 // GEF would need to be adapted:
 103  
 //                if (!(memento instanceof SelectionMemento))  
 104  37
                 undo.addCommand(new DiagramCommand(memento));
 105  
 
 106  37
                 startChain = false;
 107  
             }
 108  
         }
 109  37
     }
 110  
     
 111  
     public void addPropertyChangeListener(PropertyChangeListener listener) {
 112  0
         LOG.info("Adding property listener " + listener);
 113  0
         super.addPropertyChangeListener(listener);
 114  0
     }
 115  
     
 116  
     
 117  900
     private class DiagramCommand
 118  
             extends org.argouml.kernel.AbstractCommand {
 119  
         
 120  
         private final Memento memento;
 121  
         
 122  37
         DiagramCommand(final Memento theMemento) {
 123  37
             this.memento = theMemento;
 124  37
         }
 125  
 
 126  
         @Override
 127  
         public Object execute() {
 128  0
             memento.redo();
 129  0
             return null;
 130  
         }
 131  
 
 132  
         @Override
 133  
         public void undo() {
 134  0
             memento.undo();
 135  0
         }
 136  
         
 137  
         @Override
 138  
         public String toString() {
 139  0
             return memento.toString();
 140  
         }
 141  
     }
 142  
 }