Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/progeeks/meta/DefaultListMutator.java


1   /*
2    * $Id: DefaultListMutator.java,v 1.2 2003/08/28 15:53:19 pspeed Exp $
3    *
4    * Copyright (c) 2001-2002, Paul Speed
5    * All rights reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions
9    * are met:
10   *
11   * 1) Redistributions of source code must retain the above copyright notice,
12   *    this list of conditions and the following disclaimer.
13   * 2) Redistributions in binary form must reproduce the above copyright
14   *    notice, this list of conditions and the following disclaimer in the
15   *    documentation and/or other materials provided with the distribution.
16   * 3) Neither the names "Progeeks", "Meta-JB", nor the names of its contributors
17   *    may be used to endorse or promote products derived from this software
18   *    without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   */
32  
33  package org.progeeks.meta;
34  
35  import java.beans.*;
36  import java.util.*;
37  
38  /**
39   *  A default implementation of ListMutator that will work
40   *  with any MetaObject, given certain restrictions.  It can be used
41   *  as an implementation on its own or subclassed to provide performance
42   *  improvements in metakit implementation specific cases.
43   *
44   *  Subclasses must implement the firePropertyChangeEvent() event
45   *  method to support delivery of list change events.
46   *
47   *  @version   $Revision: 1.2 $
48   *  @author    Paul Speed
49   */
50  public abstract class DefaultListMutator extends AbstractListMutator
51                                           implements ListMutator
52  {
53      // Kind of weird to have two abstract mutator classes.  Probably
54      // only need one.  FIXME  Combine this with AbstractListMutator.
55      private String propertyName;
56      private MetaObject metaObject;
57  
58      public DefaultListMutator( String propertyName, MetaObject metaObject )
59      {
60          if( propertyName == null )
61              throw new IllegalArgumentException( "Property name cannot be null." );
62          if( metaObject == null )
63              throw new IllegalArgumentException( "Meta-object cannot be null." );
64  
65          // Verify the value type
66          PropertyType type = metaObject.getMetaClass().getPropertyType( propertyName );
67          if( !(type instanceof ListPropertyType) )
68              throw new IllegalArgumentException( "Property type must be a form of ListPropertyType." );
69  
70          this.propertyName = propertyName;
71          this.metaObject = metaObject;
72      }
73  
74      /**
75       *  Returns the name of this property.
76       */
77      public String getPropertyName()
78      {
79          return( propertyName );
80      }
81  
82      /**
83       *  Returns the object that contains this property.
84       */
85      public MetaObject getParentObject()
86      {
87          return( metaObject );
88      }
89  
90      /**
91       *  Returns the info associated with this property.
92       */
93      public PropertyInfo getPropertyInfo()
94      {
95          return( metaObject.getMetaClass().getPropertyInfo( propertyName ) );
96      }
97  
98      /**
99       *  Returns the value of this property.
100      */
101     public Object getValue()
102     {
103         return( metaObject.getProperty( propertyName ) );
104     }
105 
106     /**
107      *  Resets the value of this property.
108      */
109     public void setValue( Object value )
110     {
111         metaObject.setProperty( propertyName, value );
112     }
113 
114     /**
115      *  Adds the specified PropertyChangeListener to this
116      *  mutator.
117      */
118     public void addPropertyChangeListener( PropertyChangeListener l )
119     {
120         metaObject.addPropertyChangeListener( propertyName, l );
121     }
122 
123     /**
124      *  Removes the specified PropertyChangeListener from this
125      *  mutator.
126      */
127     public void removePropertyChangeListener( PropertyChangeListener l )
128     {
129         metaObject.removePropertyChangeListener( propertyName, l );
130     }
131 
132     protected List getListValue()
133     {
134         return( (List)getValue() );
135     }
136 
137     public int size()
138     {
139         List list = getListValue();
140         if( list == null )
141             return( 0 );
142         return( list.size() );
143     }
144 
145     public void add( int index, Object object )
146     {
147         // Note: the meta-object implementation should already
148         //       automatically wrap and unwrap meta-objects for us.
149         getListValue().add( index, object );
150 
151         fireElementsInserted( index, index );
152     }
153 
154     public Object set( int index, Object object )
155     {
156         Object ret = getListValue().set( index, object );
157 
158         fireElementsModified( index, index );
159 
160         return( ret );
161     }
162 
163     public Object remove( int index )
164     {
165         Object ret = getListValue().remove( index );
166 
167         fireElementsRemoved( index, index );
168 
169         return( ret );
170     }
171 
172     public Object get( int index )
173     {
174         return( getListValue().get( index ) );
175     }
176 
177     protected abstract void firePropertyChangeEvent( PropertyChangeEvent event );
178 }
179