View Javadoc

1   /* (c) Copyright 2003 Caleigo AB, All rights reserved. 
2    * 
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2.1 of the License, or (at your option) any later version.
7    * 
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   * Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   *  
17   */
18  
19  package org.caleigo.core;
20  
21  import org.caleigo.core.event.*;
22  
23  /*** EntitySelection is an entity collection class that can store zero or more 
24   * IEntity objects. The entities stored by this interface are type specified 
25   * and must be defined by a single IEntityDescriptor. 
26   *
27   * The EntitySelection differs from EntityCollection by using a contained
28   * Qualifier to define the scope of the contained entity objects. This means
29   * that the collection does not accept objects that are not qualified by the
30   * the defined Qualifier. This means that if data is change in a contained
31   * etity that "unqualifies" it it will automatically be removed from the 
32   * collection. Also a call to RefreshAll uses the qualifier to search for all 
33   * entities defined that it qualifies which may change the number of contained
34   * entities according to external changes in the persistent storage. 
35   *
36   * IEntitySelection is ordered and contained entities can be accesed by index.
37   *
38   * @author  Dennis Zikovic
39   * @version 1.00
40   * 
41   *//* 
42   *
43   * WHEN        WHO               WHY & WHAT
44   * ------------------------------------------------------------------------------
45   * 2001-10-10  Dennis Zikovic    Creation
46   */
47  public class QualifiedSelection extends Selection implements IQualifiedSelection
48  {
49      // Data members ------------------------------------------------------------
50      private Qualifier mQualifier;
51      
52      // Constructors ------------------------------------------------------------
53      
54      /*** This costructor will create a new selection object for the provided
55       * IEntityDescriptor. The selection will be loaded with entities from the  
56       * default persistent storage using the provided qualifier that will define
57       * the scope of the selector.
58       */ 
59      public QualifiedSelection(IEntityDescriptor entityDescriptor, Qualifier qualifier)
60      {
61          super(entityDescriptor);
62          mQualifier = qualifier;
63          
64          // Register listerner to perform automatic removals.
65          this.addEntityChangeListener(new EntityRemovalListener());
66          
67          // Load selection with qualified entities.
68          this.refreshAll();
69      }
70      
71      /*** This is a copy constructor that makes a shallow copy of the contained 
72       * entities in the provided selection.
73       */
74      public QualifiedSelection(IQualifiedSelection selection)
75      {
76          super(selection);
77          mQualifier = selection.getQualifier();
78          
79          // Register listerner to perform automatic removals.
80          this.addEntityChangeListener(new EntityRemovalListener());
81      }
82      
83      // Superclass overrides ----------------------------------------------------
84      
85      public void refreshAll()
86      {
87          // TODO!!!
88      }
89      
90      public boolean doesAccept(IEntity entity)
91      {
92          return super.doesAccept(entity) && (mQualifier==null || mQualifier.doesQualify(entity));
93      }
94          
95      // IEntitySelection implementation -----------------------------------------
96      
97      public Qualifier getQualifier()
98      {
99          return mQualifier;
100     }
101     
102     public void setQualifier(Qualifier qualifier)
103     {
104         mQualifier = qualifier;
105     }    
106     
107     // Nested classes ----------------------------------------------------------
108     
109     private class EntityRemovalListener implements IEntityChangeListener 
110     {
111         public void dataChanged(EntityChangeEvent event)
112         {
113             if(mQualifier!=null && !mQualifier.doesQualify(event.getSourceEntity()))
114                 removeEntity(event.getSourceEntity());
115         }
116         
117         public void statusChanged(EntityChangeEvent event)
118         {
119         }
120     }
121 }