Coverage Report - org.argouml.model.euml.RootContainerAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
RootContainerAdapter
0%
0/45
0%
0/18
2.111
 
 1  
 // $Id: RootContainerAdapter.java 18220 2010-04-08 20:37:15Z tfmorris $
 2  
 /*******************************************************************************
 3  
  * Copyright (c) 2007,2010 Bogdan Pistol and other contributors
 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  
  *    Bogdan Pistol - initial API and implementation
 11  
  *******************************************************************************/
 12  
 package org.argouml.model.euml;
 13  
 
 14  
 import java.util.ArrayList;
 15  
 import java.util.List;
 16  
 
 17  
 import org.eclipse.emf.common.notify.Notification;
 18  
 import org.eclipse.emf.common.notify.Notifier;
 19  
 import org.eclipse.emf.ecore.util.EContentAdapter;
 20  
 
 21  
 /**
 22  
  * Maintains a list of notifiers descendants of the root container that are
 23  
  * firing events to this adapter.
 24  
  * 
 25  
  * @author Bogdan Pistol
 26  
  */
 27  
 public class RootContainerAdapter extends EContentAdapter {
 28  
 
 29  0
     private List<Notifier> notifiers = new ArrayList<Notifier>();
 30  
     
 31  0
     private List<Notification> events = new ArrayList<Notification>();
 32  
 
 33  
     private Notifier rootContainer;
 34  
     
 35  
     private ModelEventPumpEUMLImpl pump;
 36  
     
 37  0
     private boolean deliverEvents = true;
 38  
     
 39  0
     private boolean holdEvents = false;
 40  
     
 41  
     /**
 42  
      * Constructor
 43  
      * @param pump The ModelEventPump instance
 44  
      */
 45  
     public RootContainerAdapter(ModelEventPumpEUMLImpl pump) {
 46  0
         super();
 47  0
         this.pump = pump;
 48  0
     }
 49  
     
 50  
     /**
 51  
      * Stop or start the firing of events.
 52  
      * @param value True for delivering events, false otherwise
 53  
      */
 54  
     public void setDeliverEvents(boolean value) {
 55  0
         deliverEvents = value;
 56  0
     }
 57  
     
 58  
     /**
 59  
      * Setter for the root container
 60  
      * @param n the new root container
 61  
      */
 62  
     public void setRootContainer(Notifier n) {
 63  0
         if (n == rootContainer) {
 64  0
             return;
 65  
         }
 66  
 
 67  0
         removeAllAdapters();
 68  0
         if (n != null) {
 69  0
             rootContainer = n;
 70  0
             rootContainer.eAdapters().add(this);
 71  
         }
 72  0
     }
 73  
 
 74  
     @Override
 75  
     protected void addAdapter(Notifier notifier) {
 76  0
         notifiers.add(notifier);
 77  0
         super.addAdapter(notifier);
 78  0
     }
 79  
 
 80  
     @Override
 81  
     protected void removeAdapter(Notifier notifier) {
 82  0
         notifiers.remove(notifier);
 83  0
         super.removeAdapter(notifier);
 84  0
     }
 85  
 
 86  
     /**
 87  
      * Removes this listener from all the notifiers' eAdapters list.
 88  
      */
 89  
     public void removeAllAdapters() {
 90  0
         List<Notifier> notifiersToRemove = new ArrayList<Notifier>(notifiers);
 91  0
         for (Notifier n : notifiersToRemove) {
 92  0
             super.removeAdapter(n);
 93  
         }
 94  0
         if (rootContainer != null) {
 95  0
             super.removeAdapter(rootContainer);
 96  0
             rootContainer = null;
 97  
         }
 98  0
         notifiers.clear();
 99  0
     }
 100  
 
 101  
     @Override
 102  
     public void notifyChanged(Notification notification) {
 103  0
         super.notifyChanged(notification);
 104  0
         if (deliverEvents) {
 105  0
             if (holdEvents) {
 106  0
                 events.add(notification);                
 107  
             } else {
 108  0
                 pump.notifyChanged(notification);
 109  
             }
 110  
         }
 111  0
     }
 112  
     
 113  
     /**
 114  
      * Clears all the events held until now
 115  
      */
 116  
     public void clearHeldEvents() {
 117  0
         events.clear();
 118  0
     }
 119  
     
 120  
     /**
 121  
      * Determine if the events should be delivered when they arrive or to wait until holdEvents is false
 122  
      * @param value the holdEvents value
 123  
      */
 124  
     public void setHoldEvents(boolean value) {
 125  0
         if (value == false) {
 126  0
             if (deliverEvents) {
 127  0
                 for (Notification n : events) {
 128  0
                     pump.notifyChanged(n);
 129  
                 }
 130  
             }
 131  0
             events.clear();
 132  
         }
 133  0
         holdEvents = value;
 134  0
     }
 135  
 
 136  
 }