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.custom.tree.model;
20
21
22 /**
23 * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller</a>
24 * @version $Revision: 472638 $ $Date: 2006-11-08 15:54:13 -0500 (Wed, 08 Nov 2006) $
25 */
26 public class TreeModelEvent
27 {
28
29 private Object source;
30 private TreePath path;
31 private int[] childIndices;
32 private Object[] children;
33
34
35 /**
36 * Used to create an event when nodes have been changed, inserted, or
37 * removed, identifying the path to the parent of the modified items as
38 * an array of Objects. All of the modified objects are siblings which are
39 * direct descendents (not grandchildren) of the specified parent.
40 * The positions at which the inserts, deletes, or changes occurred are
41 * specified by an array of <code>int</code>. The indexes in that array
42 * must be in order, from lowest to highest.
43 *
44 * @param source the Object responsible for generating the event
45 * @param path an array of Object identifying the path to the parent of the modified item(s)
46 * @param childIndices array that specifies the
47 * index values of the removed items. The indices must be in sorted order, from lowest to highest
48 * @param children an array containing the inserted, removed, or changed objects
49 */
50 public TreeModelEvent(Object source, Object[] path, int[] childIndices, Object[] children)
51 {
52 this(source, new TreePath(path), childIndices, children);
53 }
54
55
56 /**
57 * Used to create an event when nodes have been changed, inserted, or
58 * removed, identifying the path to the parent of the modified items as
59 * a TreePath object.
60 *
61 * @param source the Object responsible for generating the event
62 * @param path a TreePath object that identifies the path to the parent of the modified item(s)
63 * @param childIndices array that specifies the index values of the modified items
64 * @param children an array containing the inserted, removed, or changed objects
65 * @see #TreeModelEvent(Object,Object[],int[],Object[])
66 */
67 public TreeModelEvent(Object source, TreePath path, int[] childIndices, Object[] children)
68 {
69 this.source = source;
70 this.path = path;
71 this.childIndices = childIndices;
72 this.children = children;
73 }
74
75
76 /**
77 * Used to create an event when nodes have been changed, inserted, or
78 * removed, identifying the path to the parent of the modified items as
79 * a TreePath object.
80 *
81 * @param source the Object responsible for generating the event
82 * @param path an array of Object identifying the path to the parent of the modified item(s)
83 */
84 public TreeModelEvent(Object source, Object[] path)
85 {
86 this(source, new TreePath(path));
87 }
88
89
90 /**
91 * Used to create an event when nodes have been changed, inserted, or
92 * removed, identifying the path to the parent of the modified items as
93 * a TreePath object.
94 *
95 * @param source the Object responsible for generating the event
96 * @param path a TreePath object that identifies the path to the parent of the modified item(s)
97 */
98 public TreeModelEvent(Object source, TreePath path)
99 {
100 this.source = source;
101 this.path = path;
102 this.childIndices = new int[0];
103 }
104
105
106 /**
107 * Answer the source of this event
108 *
109 * @return the source of this event
110 */
111 public Object getSource()
112 {
113 return source;
114 }
115
116
117 /**
118 * For all events, except treeStructureChanged,
119 * returns the parent of the changed nodes.
120 * For treeStructureChanged events, returns the ancestor of the
121 * structure that has changed. This and
122 * <code>getChildIndices</code> are used to get a list of the effected
123 * nodes.
124 * <p/>
125 * The one exception to this is a treeNodesChanged event that is to
126 * identify the root, in which case this will return the root
127 * and <code>getChildIndices</code> will return null.
128 *
129 * @return the TreePath used in identifying the changed nodes.
130 */
131 public TreePath getTreePath()
132 {
133 return path;
134 }
135
136
137 /**
138 * Return the objects that are children of the node identified by
139 * the path of this event at the locations specified by
140 * <code>getChildIndices</code>. If this is a removal event the
141 * returned objects are no longer children of the parent node.
142 *
143 * @return an array of Object containing the children specified by
144 * the event
145 */
146 public Object[] getChildren()
147 {
148 if (children != null)
149 {
150 Object[] answer = new Object[children.length];
151
152 System.arraycopy(children, 0, answer, 0, children.length);
153 return answer;
154 }
155 return null;
156 }
157
158
159 /**
160 * Returns the values of the child indexes. If this is a removal event
161 * the indexes point to locations in the initial list where items
162 * were removed. If it is an insert, the indices point to locations
163 * in the final list where the items were added. For node changes,
164 * the indices point to the locations of the modified nodes.
165 *
166 * @return an array containing index locations for the children specified by the event
167 */
168 public int[] getChildIndices()
169 {
170 if (childIndices != null)
171 {
172 int[] answer = new int[childIndices.length];
173
174 System.arraycopy(childIndices, 0, answer, 0, childIndices.length);
175 return answer;
176 }
177 return null;
178 }
179
180
181 /**
182 * Returns a string that displays and identifies this object's
183 * properties.
184 *
185 * @return a String representation of this object
186 */
187 public String toString()
188 {
189 StringBuffer buffer = new StringBuffer();
190
191 buffer.append(super.toString());
192 if (path != null)
193 {
194 buffer.append(" path " + path);
195 }
196 if (childIndices != null)
197 {
198 buffer.append(" indices [ ");
199 for (int i = 0; i < childIndices.length; i++)
200 {
201 buffer.append(Integer.toString(childIndices[i]) + " ");
202 }
203 buffer.append("]");
204 }
205 if (children != null)
206 {
207 buffer.append(" children [ ");
208 for (int i = 0; i < children.length; i++)
209 {
210 buffer.append(children[i] + " ");
211 }
212 buffer.append("]");
213 }
214 return buffer.toString();
215 }
216
217 }