View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.trinidad.event;
20  
21  import javax.faces.component.UIComponent;
22  import javax.faces.event.FacesEvent;
23  
24  import org.apache.myfaces.trinidad.model.RowKeySet;
25  
26  /**
27   * Event that is generated when the contents of a RowKeySet changes.
28   */
29  public abstract class RowKeySetChangeEvent extends FacesEvent
30  {
31    /**
32     * Creates a new Event.
33     * @param addedSet This is the Set of keys that have just been added.
34     * @param removedSet This is the Set of keys that have just been removed.
35     */
36    // FIXME: What is the parametrized type inside RowKeySet?
37    public RowKeySetChangeEvent(
38        UIComponent source, 
39        RowKeySet   removedSet,
40        RowKeySet   addedSet)
41    {
42      this(source, removedSet, addedSet, false);
43    }
44  
45    /**
46     * This constructor lazily computes the difference between the 
47     * oldSet and the newSet.
48     * @param oldSet This is the Set of keys before any changes.
49     * @param newSet This is the Set of keys after any changes.
50     */
51    // FIXME: What is the parametrized type inside RowKeySet?
52    public RowKeySetChangeEvent(
53        RowKeySet   oldSet,
54        RowKeySet   newSet,
55        UIComponent source)
56    {
57      // "oldSet" is very often the actual instance-on-the-component.
58      // so make sure that we clone this object, so that subsequent mutations of
59      // the instance-on-the-component will
60      // not affect the parameters of this event: bug 4733858:
61      // we do the clone in the _diff() method:
62      this(source, oldSet, newSet, true);
63    }
64  
65    // FIXME: What is the parametrized type inside RowKeySet?
66    private RowKeySetChangeEvent(
67        UIComponent source,
68        RowKeySet   oldRemoved,
69        RowKeySet   newAdded,
70        boolean     needsDiff)
71                                 
72    {
73      super(source);
74      _newAdded = newAdded;
75      _oldRemoved = oldRemoved;
76      _needsDiff = needsDiff;
77    }
78  
79    /**
80     * Gets the Set of keys that have just been added.
81     */
82    public RowKeySet getAddedSet()
83    {
84      _diff();
85      return _newAdded;
86    }
87  
88    /**
89     * Gets the Set of keys that have just been removed.
90     */
91    public RowKeySet getRemovedSet()
92    {
93      _diff();
94      return _oldRemoved;
95    }
96    
97    private void _diff()
98    {
99      if (_needsDiff)
100     {
101       RowKeySet removed = _oldRemoved.clone();
102       removed.removeAll(_newAdded);
103       RowKeySet added = _newAdded.clone();
104       added.removeAll(_oldRemoved);
105 
106       _needsDiff = false;
107       _oldRemoved = removed;
108       _newAdded = added;
109     }
110   }
111   
112   // set1 - oldSet or removed elements
113   // set2 = newSet or added elements
114   private RowKeySet _oldRemoved;
115   private RowKeySet _newAdded;
116   private boolean _needsDiff;
117   private static final long serialVersionUID = 1L;
118 }