Coverage Report - net.wotonomy.control.ChildDataSource
 
Classes in this File Line Coverage Branch Coverage Complexity
ChildDataSource
0% 
0% 
1.444
 
 1  
 package net.wotonomy.control;
 2  
 
 3  
 import java.util.Collection;
 4  
 import java.util.Iterator;
 5  
 
 6  
 import net.wotonomy.foundation.NSArray;
 7  
 import net.wotonomy.foundation.NSMutableArray;
 8  
 
 9  
 /**
 10  
 * A data source that automates the process of 
 11  
 * creating a child editing context and copying
 12  
 * objects from a parent context into it. 
 13  
 * Attach this data source to a display group
 14  
 * that represents a "detail" or "drill-down" 
 15  
 * view. <br><br>
 16  
 * 
 17  
 * Once created, editingContext() will return the 
 18  
 * child context, and fetch() will return the objects 
 19  
 * that were copied into the child context.
 20  
 */
 21  
 public class ChildDataSource extends EODataSource
 22  
 {
 23  
     private EODataSource parent;
 24  
     private EOEditingContext context;
 25  
     private EOClassDescription classDescription;
 26  
     private NSMutableArray objects;
 27  
     
 28  
     /**
 29  
     * Creates a child editing context for the
 30  
     * specified parent's context and copies the 
 31  
     * specified object into the child context.
 32  
     * The object must exist in the parent context.
 33  
     * fetch() will return the child's object.
 34  
     */
 35  
         public ChildDataSource( 
 36  
         EODataSource aParentSource,
 37  
         Object anObject )
 38  
         {
 39  0
         this( aParentSource, new NSArray( (Object) anObject ) );
 40  0
         }
 41  
     
 42  
     /**
 43  
     * Creates a child editing context for the
 44  
     * specified parent's context and copies the 
 45  
     * specified objects into the child context.
 46  
     * The objects must exist in the parent context.
 47  
     * The order of the parent's objects in the
 48  
     * collection will determine the order in 
 49  
     * which the child objects are returned from 
 50  
     * fetch().
 51  
     */
 52  0
         public ChildDataSource( 
 53  
         EODataSource aParentSource,
 54  
         Collection anObjectList )
 55  0
         {
 56  0
         EOEditingContext parentContext = 
 57  0
             aParentSource.editingContext();
 58  
 
 59  0
         parent = aParentSource;
 60  0
         context = new EOEditingContext( parentContext );
 61  
 //!new net.wotonomy.ui.swing.util.ObjectInspector( context );        
 62  0
         objects = new NSMutableArray();
 63  0
         classDescription = null;
 64  
 
 65  
         Object o;        
 66  
         Object copy;
 67  0
         boolean allSameClass = true;
 68  0
         Iterator it = anObjectList.iterator();
 69  0
         while ( it.hasNext() )
 70  
         {
 71  0
             o = it.next();
 72  
             
 73  
             // determine class
 74  0
             if ( allSameClass == true )
 75  
             {
 76  0
                 Class c = o.getClass();
 77  0
                 if ( classDescription == null ) 
 78  
                 {
 79  0
                     classDescription = 
 80  0
                         EOClassDescription.classDescriptionForClass( c );   
 81  0
                 }
 82  
                 else
 83  
                 {
 84  0
                     if ( c != classDescription.getDescribedClass() )
 85  
                     {
 86  0
                         allSameClass = false;  
 87  0
                         classDescription = null;
 88  
                     }
 89  
                 }
 90  
             }
 91  
             
 92  
             // copy and add to list
 93  0
             objects.addObject( parentContext.faultForGlobalID(
 94  0
                 parentContext.globalIDForObject( o ), context ) );
 95  0
         }
 96  0
         }
 97  
     
 98  
     /**
 99  
     * Returns the editing context for this data source,
 100  
     * which was created in the constructor and whose 
 101  
     * parent is the editing context specified in the
 102  
     * constructor.
 103  
     */
 104  
     public EOEditingContext editingContext()
 105  
     {
 106  0
         return context;
 107  
     }
 108  
         
 109  
     /**
 110  
     * This implementation does nothing.
 111  
     */
 112  
     public void insertObject ( Object anObject )
 113  
         {
 114  
 
 115  0
         }
 116  
 
 117  
     /**
 118  
     * This implementation does nothing.
 119  
     */
 120  
     public void deleteObject ( Object anObject )
 121  
         {
 122  
 
 123  0
         }
 124  
 
 125  
     /**
 126  
     * Returns a List containing the objects in this
 127  
     * data source.  This implementation returns all
 128  
         * TestObjects that have been persisted to the 
 129  
         * datastore in the data directory.
 130  
     */
 131  
     public NSArray fetchObjects ()
 132  
     {
 133  0
         return new NSArray( (Collection) objects );
 134  
     }
 135  
 
 136  
     /**
 137  
     * Returns a data source that is capable of
 138  
     * manipulating objects of the type returned by 
 139  
     * applying the specified key to objects 
 140  
     * vended by this data source.
 141  
     * This implementation forwards the call to
 142  
     * the parent data source.
 143  
     * @see #qualifyWithRelationshipKey
 144  
     */
 145  
     public EODataSource 
 146  
         dataSourceQualifiedByKey ( String aKey )
 147  
         {
 148  
         //FIXME: This is fundamentally broken.
 149  
         // Objects vended from the returned source
 150  
         // are not registered in our editing context.
 151  
         // We probably need yet another utility data
 152  
         // source class that would wrap another source
 153  
         // and convert vended objects into a different
 154  
         // context.
 155  
         
 156  0
         return parent.dataSourceQualifiedByKey( aKey );
 157  
         }
 158  
 
 159  
     /**
 160  
     * Restricts this data source to vend those 
 161  
     * objects that are associated with the specified 
 162  
     * key on the specified object.
 163  
     * This implementation forwards the call to
 164  
     * the parent data source.
 165  
     */
 166  
     public void 
 167  
         qualifyWithRelationshipKey ( 
 168  
         String aKey, Object anObject )
 169  
         {
 170  0
                 parent.qualifyWithRelationshipKey( aKey, anObject );        
 171  0
         }
 172  
 
 173  
     /**
 174  
     * Returns the description of the class of the
 175  
     * objects that is vended by this data source,
 176  
     * or null if this cannot be determined.
 177  
         * This implementation returns the class of the
 178  
     * objects passed to the constructor if they are
 179  
     * all the same class, otherwise returns null.
 180  
     */
 181  
     public EOClassDescription
 182  
         classDescriptionForObjects ()
 183  
     {
 184  0
         return classDescription;
 185  
     }
 186  
 
 187  
 }