Coverage Report - de.glossmaker.gloss.listener.UndoListener
 
Classes in this File Line Coverage Branch Coverage Complexity
UndoListener
9%
11/118
1%
1/72
2,8
 
 1  
 package de.glossmaker.gloss.listener;
 2  
 
 3  
 import java.beans.PropertyChangeEvent;
 4  
 import java.beans.PropertyChangeListener;
 5  
 import java.util.ArrayList;
 6  
 
 7  
 import org.apache.log4j.Logger;
 8  
 
 9  
 import de.glossmaker.gloss.datastructure.GlossItem;
 10  
 import de.glossmaker.gloss.datastructure.GlossItems;
 11  
 import de.glossmaker.gloss.observer.GlossItemsChangePublisher;
 12  
 import de.glossmaker.gloss.undo.UndoGlossStack;
 13  
 import de.glossmaker.undo.UndoableItem;
 14  
 
 15  
 
 16  
 /**
 17  
  * 
 18  
  * @author Markus Flingelli
 19  
  *
 20  
  */
 21  
 public class UndoListener implements PropertyChangeListener {
 22  1
         private static final Logger mLogger = Logger.getLogger(UndoListener.class);
 23  
         
 24  1
         private static UndoListener mUndoListener = null;
 25  1
         private UndoGlossStack mUndoStack = null;
 26  1
         private GlossItemsChangePublisher mGlossItemsChangePublisher = null;
 27  
         
 28  1
         private UndoListener(GlossItems items) {
 29  1
                 mUndoStack = UndoGlossStack.getInstance();
 30  1
                 mGlossItemsChangePublisher = GlossItemsChangePublisher.getInstance(items);
 31  1
         }
 32  
         
 33  
         public static UndoListener getInstance(GlossItems items) {
 34  1
                 if (mUndoListener == null) {
 35  1
                         mUndoListener = new UndoListener(items);
 36  
                 }
 37  1
                 return mUndoListener;
 38  
         }
 39  
         
 40  
         public int undoSize() {
 41  0
                 return mUndoStack.undoSize();
 42  
         }
 43  
         
 44  
         public int redoSize() {
 45  0
                 return mUndoStack.redoSize();
 46  
         }
 47  
         
 48  
         private void undoHeadingChanged(UndoableItem item, boolean isRedo) {
 49  
                 @SuppressWarnings("unchecked")
 50  0
                 ArrayList<Object> oldValues = (ArrayList<Object>) item.getOldValue();
 51  0
                 if (isRedo == true) {
 52  0
                         mGlossItemsChangePublisher.setHeading((GlossItem)oldValues.get(1), (String)item.getNewValue());
 53  
                 } else {
 54  0
                         mGlossItemsChangePublisher.setHeading((GlossItem)oldValues.get(1), (String)oldValues.get(0));
 55  
                 }
 56  0
         }
 57  
         
 58  
         private void undoKeyChanged(UndoableItem item, boolean isRedo) {
 59  
                 @SuppressWarnings("unchecked")
 60  0
                 ArrayList<Object> oldValues = (ArrayList<Object>) item.getOldValue();
 61  0
                 if (isRedo == true) {
 62  0
                         mGlossItemsChangePublisher.setKey((GlossItem)oldValues.get(1), (String)item.getNewValue());
 63  
                 } else {
 64  0
                         mGlossItemsChangePublisher.setKey((GlossItem)oldValues.get(1), (String)oldValues.get(0));
 65  
                 }
 66  0
         }
 67  
         
 68  
         @SuppressWarnings("unchecked")
 69  
         private void undoWordChanged(UndoableItem item, boolean isRedo) {
 70  0
                 ArrayList<Object> oldValues = (ArrayList<Object>) item.getOldValue();
 71  0
                 if (isRedo == true) {
 72  0
                         mGlossItemsChangePublisher.setWord((GlossItem)oldValues.get(1), (String)item.getNewValue(), false);
 73  
                 } else {
 74  0
                         mGlossItemsChangePublisher.setWord((GlossItem)oldValues.get(1), (String)oldValues.get(0), false);
 75  
                 }
 76  0
         }
 77  
         
 78  
         //TODO Implement Undo/Redo
 79  
         @SuppressWarnings("unchecked")
 80  
         private void undoPerAutoUpdateChanged(UndoableItem item, boolean isRedo) {
 81  0
                 ArrayList<Object> oldValues = (ArrayList<Object>) item.getOldValue();
 82  0
                 boolean isAutoUpdate = (Boolean)oldValues.get(2);
 83  0
                 if (isRedo == true) {
 84  0
                         GlossItem newItem = (GlossItem) oldValues.get(4);
 85  0
                         mGlossItemsChangePublisher.setWord((GlossItem)oldValues.get(1), newItem.getWord(), isAutoUpdate);
 86  0
                 } else {
 87  0
                         GlossItem oldItem = (GlossItem) oldValues.get(3);
 88  0
                         mGlossItemsChangePublisher.setWord((GlossItem)oldValues.get(1), oldItem.getWord(), isAutoUpdate);
 89  
                 }
 90  0
         }
 91  
         
 92  
         private void undoDefinitionChanged(UndoableItem item, boolean isRedo) {
 93  
                 @SuppressWarnings("unchecked")
 94  0
                 ArrayList<Object> oldValues = (ArrayList<Object>) item.getOldValue();
 95  0
                 if (isRedo == true) {
 96  0
                         mGlossItemsChangePublisher.setDefinition((GlossItem)oldValues.get(1), (String)item.getNewValue());
 97  
                 } else {
 98  0
                         mGlossItemsChangePublisher.setDefinition((GlossItem)oldValues.get(1), (String)oldValues.get(0));
 99  
                 }
 100  0
         }
 101  
 
 102  
         private void undoAddItem(UndoableItem item, boolean isRedo) {
 103  0
                 if (isRedo == true) {
 104  0
                         mGlossItemsChangePublisher.addGlossItem((GlossItem) item.getNewValue());
 105  
                 } else {
 106  0
                         mGlossItemsChangePublisher.removeGlossItem((GlossItem) item.getNewValue());
 107  
                 }
 108  0
         }
 109  
         
 110  
         private void undoRemoveItem(UndoableItem item, boolean isRedo) {
 111  0
                 if (isRedo == true) {                        
 112  0
                         mGlossItemsChangePublisher.removeGlossItem((GlossItem) item.getNewValue());
 113  
                 } else {
 114  0
                         mGlossItemsChangePublisher.addGlossItem((GlossItem) item.getNewValue());
 115  
                 }
 116  0
         }
 117  
         
 118  
         private void undoImportFile(UndoableItem item, boolean isRedo) {
 119  0
                 mGlossItemsChangePublisher.clear();
 120  0
                 if (isRedo == true) {
 121  0
                         mGlossItemsChangePublisher.importGlossItems((GlossItems) item.getNewValue());
 122  
                 } else {                        
 123  0
                         mGlossItemsChangePublisher.importGlossItems((GlossItems) item.getOldValue());
 124  
                 }
 125  0
         }
 126  
         
 127  
         private void undoRemoveHeading(UndoableItem item, boolean isRedo) {
 128  0
                 if (isRedo == true) {
 129  0
                         GlossItems items = (GlossItems) item.getOldValue();
 130  0
                         GlossItem glossItem = items.get(0);
 131  0
                         mGlossItemsChangePublisher.removeGlossItemHeading(glossItem.getHeading());
 132  0
                 } else {                        
 133  0
                         mGlossItemsChangePublisher.importGlossItems((GlossItems) item.getOldValue());
 134  
                 }
 135  0
         }
 136  
         
 137  
         private void undoAction(UndoableItem item, boolean isRedo) {                
 138  0
                 mGlossItemsChangePublisher.removeUndoListener();
 139  0
                 if (item.getPropertyName().equals(EActionCommands.GLOSS_ITEM_ADDED.toString())) {
 140  0
                         undoAddItem(item, isRedo);
 141  0
                 } else if (item.getPropertyName().equals(EActionCommands.GLOSS_ITEM_REMOVED.toString())) {
 142  0
                         undoRemoveItem(item, isRedo);
 143  0
                 } else if(item.getPropertyName().equals(EActionCommands.GLOSS_ITEM_HEADING_REMOVED.toString())) {
 144  0
                         undoRemoveHeading(item, isRedo);
 145  0
                 } else if (item.getPropertyName().equals(EActionCommands.GLOSS_ITEM_DEFINITION_CHANGED.toString())) {
 146  0
                         undoDefinitionChanged(item, isRedo);
 147  0
                 } else if (item.getPropertyName().equals(EActionCommands.GLOSS_ITEM_HEADING_CHANGED.toString())) {
 148  0
                         undoHeadingChanged(item, isRedo);
 149  0
                 } else if (item.getPropertyName().equals(EActionCommands.GLOSS_ITEM_KEY_CHANGED.toString())) {
 150  0
                         undoKeyChanged(item, isRedo);
 151  0
                 } else if (item.getPropertyName().equals(EActionCommands.GLOSS_ITEM_WORD_CHANGED.toString())) {
 152  0
                         undoWordChanged(item, isRedo);
 153  0
                 } else if (item.getPropertyName().equals(EActionCommands.FILE_IMPORTED.toString())) {
 154  0
                         undoImportFile(item, isRedo);
 155  0
                 } else if (item.getPropertyName().equals(EActionCommands.GLOSS_ITEM_PER_AUTO_UPDATE_CHANGED.toString())) {
 156  0
                         undoPerAutoUpdateChanged(item, isRedo);
 157  
                 }
 158  
                 
 159  0
                 mGlossItemsChangePublisher.addUndoListener();
 160  0
         }
 161  
         
 162  
         
 163  
 
 164  
         public void undo() {
 165  0
                 if (mUndoStack.undoSize() > 0) {                        
 166  0
                         UndoableItem item = mUndoStack.popUndo();
 167  0
                         undoAction(item, false);
 168  0
                         mUndoStack.pushRedo(item);
 169  0
                         mGlossItemsChangePublisher.fireUndoOrRedo(item.getPropertyName());
 170  
                 }
 171  0
         }
 172  
         
 173  
         public void redo() {
 174  0
                 if (mUndoStack.redoSize() > 0) {
 175  0
                         UndoableItem item = mUndoStack.popRedo();
 176  0
                         undoAction(item, true);
 177  0
                         mUndoStack.pushUndo(item);
 178  0
                         mGlossItemsChangePublisher.fireUndoOrRedo(item.getPropertyName());
 179  
                 }
 180  0
         }
 181  
         
 182  
         private boolean isEventEquals(PropertyChangeEvent event, EActionCommands command) {
 183  0
                 return event.getPropertyName().equals(command.toString());
 184  
         }
 185  
         
 186  
         private void undoProductSave(PropertyChangeEvent event) {
 187  0
                 if (isEventEquals(event, EActionCommands.GLOSS_ITEM_ADDED) ||
 188  
                         isEventEquals(event, EActionCommands.GLOSS_ITEM_REMOVED) ||
 189  
                         isEventEquals(event, EActionCommands.GLOSS_ITEM_HEADING_REMOVED) ||
 190  
                         isEventEquals(event, EActionCommands.GLOSS_ITEM_DEFINITION_CHANGED) ||
 191  
                         isEventEquals(event, EActionCommands.GLOSS_ITEM_HEADING_CHANGED) ||
 192  
                         isEventEquals(event, EActionCommands.GLOSS_ITEM_KEY_CHANGED) ||
 193  
                         isEventEquals(event, EActionCommands.GLOSS_ITEM_WORD_CHANGED) ||
 194  
                         isEventEquals(event, EActionCommands.FILE_IMPORTED) ||
 195  
                         isEventEquals(event, EActionCommands.GLOSS_ITEM_PER_AUTO_UPDATE_CHANGED)) {
 196  0
                         UndoableItem item = new UndoableItem();
 197  0
                         item.setOldValue(event.getOldValue());
 198  0
                         item.setNewValue(event.getNewValue());
 199  0
                         item.setPropertyName(event.getPropertyName());
 200  0
                         mUndoStack.pushUndo(item);
 201  0
                         String oldValue = null;
 202  0
                         String newValue = null;
 203  0
                         if (event.getOldValue() != null) {
 204  0
                                 oldValue = event.getOldValue().toString();
 205  
                         }
 206  0
                         if (event.getNewValue() != null) {
 207  0
                                 newValue = event.getNewValue().toString();
 208  
                         }
 209  
                         
 210  0
                         mLogger.debug("undoProductSave(): Add object to undo stack. " +
 211  
                                         "Event=" + event.getPropertyName() +
 212  
                                         ", old=" + oldValue + 
 213  
                                     ", new=" + newValue);
 214  
                 }
 215  
                 
 216  0
                 if (event.getPropertyName().equals("file_opened") ||
 217  
                         event.getPropertyName().equals("file_saved") ||
 218  
                         event.getPropertyName().equals("new_product_created")) {
 219  0
                         mUndoStack.reset();
 220  
                 }
 221  0
         }
 222  
         
 223  
         public boolean isContentChanged() {
 224  0
                 boolean result = true;
 225  0
                 if (mUndoStack.undoSize() == 0)        {
 226  0
                         result = false;
 227  
                 }
 228  
                 
 229  0
                 return result;
 230  
         }
 231  
         
 232  
         
 233  
         @Override
 234  
         public void propertyChange(PropertyChangeEvent event) {
 235  0
                 undoProductSave(event);
 236  0
         }
 237  
 
 238  
 }