View Javadoc

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          this( aParentSource, new NSArray( (Object) anObject ) );
40  	}
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  	public ChildDataSource( 
53          EODataSource aParentSource,
54          Collection anObjectList )
55  	{
56          EOEditingContext parentContext = 
57              aParentSource.editingContext();
58  
59          parent = aParentSource;
60          context = new EOEditingContext( parentContext );
61  //!new net.wotonomy.ui.swing.util.ObjectInspector( context );        
62          objects = new NSMutableArray();
63          classDescription = null;
64  
65          Object o;        
66          Object copy;
67          boolean allSameClass = true;
68          Iterator it = anObjectList.iterator();
69          while ( it.hasNext() )
70          {
71              o = it.next();
72              
73              // determine class
74              if ( allSameClass == true )
75              {
76                  Class c = o.getClass();
77                  if ( classDescription == null ) 
78                  {
79                      classDescription = 
80                          EOClassDescription.classDescriptionForClass( c );   
81                  }
82                  else
83                  {
84                      if ( c != classDescription.getDescribedClass() )
85                      {
86                          allSameClass = false;  
87                          classDescription = null;
88                      }
89                  }
90              }
91              
92              // copy and add to list
93              objects.addObject( parentContext.faultForGlobalID(
94                  parentContext.globalIDForObject( o ), context ) );
95          }
96  	}
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         return context;
107     }
108 	
109     /***
110     * This implementation does nothing.
111     */
112     public void insertObject ( Object anObject )
113 	{
114 
115 	}
116 
117     /***
118     * This implementation does nothing.
119     */
120     public void deleteObject ( Object anObject )
121 	{
122 
123 	}
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         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         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 		parent.qualifyWithRelationshipKey( aKey, anObject );	
171 	}
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         return classDescription;
185     }
186 
187 }