Coverage Report - org.jtheque.core.managers.undo.UndoRedoManager
 
Classes in this File Line Coverage Branch Coverage Complexity
UndoRedoManager
0 %
0/33
N/A
1.182
 
 1  
 package org.jtheque.core.managers.undo;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.core.managers.ManagerException;
 20  
 import org.jtheque.core.managers.Managers;
 21  
 import org.jtheque.core.managers.log.ILoggingManager;
 22  
 
 23  
 import javax.swing.Action;
 24  
 import javax.swing.undo.CannotUndoException;
 25  
 import javax.swing.undo.UndoManager;
 26  
 import javax.swing.undo.UndoableEdit;
 27  
 
 28  
 /**
 29  
  * An undo-redo manager implementation.
 30  
  *
 31  
  * @author Baptiste Wicht
 32  
  */
 33  0
 public final class UndoRedoManager extends UndoManager implements IUndoRedoManager {
 34  
     private Action undoAction;
 35  
     private Action redoAction;
 36  
 
 37  0
     private boolean enabled = true;
 38  
 
 39  
     @Override
 40  
     public void preInit() {
 41  
         //Nothing to do
 42  0
     }
 43  
 
 44  
     @Override
 45  
     public void close(){
 46  
         //Nothing to do
 47  0
     }
 48  
 
 49  
     @Override
 50  
     public void init() throws ManagerException {
 51  0
         stateChanged();
 52  0
     }
 53  
 
 54  
     @Override
 55  
     public boolean isEnabled() {
 56  0
         return enabled;
 57  
     }
 58  
 
 59  
     @Override
 60  
     public void setEnabled(boolean enabled) {
 61  0
         this.enabled = enabled;
 62  0
     }
 63  
 
 64  
     @Override
 65  
     public synchronized boolean addEdit(UndoableEdit arg0) {
 66  0
         boolean add = super.addEdit(arg0);
 67  
 
 68  0
         stateChanged();
 69  
 
 70  0
         return add;
 71  
     }
 72  
 
 73  
     @Override
 74  
     public void setUndoAction(Action undoAction){
 75  0
         this.undoAction = undoAction;
 76  0
     }
 77  
 
 78  
     @Override
 79  
     public void setRedoAction(Action redoAction){
 80  0
         this.redoAction = redoAction;
 81  0
     }
 82  
 
 83  
     @Override
 84  
     public synchronized void undo() {
 85  
         try {
 86  0
             super.undo();
 87  0
         } catch (CannotUndoException e) {
 88  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 89  
         } finally {
 90  0
             stateChanged();
 91  0
         }
 92  0
     }
 93  
 
 94  
     @Override
 95  
     public synchronized void redo() {
 96  
         try {
 97  0
             super.redo();
 98  0
         } catch (CannotUndoException e) {
 99  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 100  
         } finally {
 101  0
             stateChanged();
 102  0
         }
 103  0
     }
 104  
 
 105  
     /**
 106  
      * Update the state of the undo/redo action.
 107  
      */
 108  
     private void stateChanged() {
 109  0
         undoAction.putValue(Action.NAME, getUndoPresentationName());
 110  0
         redoAction.putValue(Action.NAME, getRedoPresentationName());
 111  0
         undoAction.setEnabled(canUndo());
 112  0
         redoAction.setEnabled(canRedo());
 113  0
     }
 114  
 }