KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > tree > model > TreeModelEvent


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.custom.tree.model;
17
18
19 /**
20  * @author <a HREF="mailto:oliver@rossmueller.com">Oliver Rossmueller</a>
21  * @version $Revision: 1.3 $ $Date: 2004/10/13 11:50:58 $
22  * $Log: TreeModelEvent.java,v $
23  * Revision 1.3 2004/10/13 11:50:58 matze
24  * renamed packages to org.apache
25  *
26  * Revision 1.2 2004/07/01 21:53:04 mwessendorf
27  * ASF switch
28  *
29  * Revision 1.1 2004/04/22 10:20:24 manolito
30  * tree component
31  *
32  */

33 public class TreeModelEvent
34 {
35
36     private Object JavaDoc source;
37     private TreePath path;
38     private int[] childIndices;
39     private Object JavaDoc[] children;
40
41
42     /**
43      * Used to create an event when nodes have been changed, inserted, or
44      * removed, identifying the path to the parent of the modified items as
45      * an array of Objects. All of the modified objects are siblings which are
46      * direct descendents (not grandchildren) of the specified parent.
47      * The positions at which the inserts, deletes, or changes occurred are
48      * specified by an array of <code>int</code>. The indexes in that array
49      * must be in order, from lowest to highest.
50      *
51      * @param source the Object responsible for generating the event
52      * @param path an array of Object identifying the path to the parent of the modified item(s)
53      * @param childIndices array that specifies the
54      * index values of the removed items. The indices must be in sorted order, from lowest to highest
55      * @param children an array containing the inserted, removed, or changed objects
56      */

57     public TreeModelEvent(Object JavaDoc source, Object JavaDoc[] path, int[] childIndices, Object JavaDoc[] children)
58     {
59         this(source, new TreePath(path), childIndices, children);
60     }
61
62
63     /**
64      * Used to create an event when nodes have been changed, inserted, or
65      * removed, identifying the path to the parent of the modified items as
66      * a TreePath object.
67      *
68      * @param source the Object responsible for generating the event
69      * @param path a TreePath object that identifies the path to the parent of the modified item(s)
70      * @param childIndices array that specifies the index values of the modified items
71      * @param children an array containing the inserted, removed, or changed objects
72      * @see #TreeModelEvent(Object,Object[],int[],Object[])
73      */

74     public TreeModelEvent(Object JavaDoc source, TreePath path, int[] childIndices, Object JavaDoc[] children)
75     {
76         this.source = source;
77         this.path = path;
78         this.childIndices = childIndices;
79         this.children = children;
80     }
81
82
83     /**
84      * Used to create an event when nodes have been changed, inserted, or
85      * removed, identifying the path to the parent of the modified items as
86      * a TreePath object.
87      *
88      * @param source the Object responsible for generating the event
89      * @param path an array of Object identifying the path to the parent of the modified item(s)
90      */

91     public TreeModelEvent(Object JavaDoc source, Object JavaDoc[] path)
92     {
93         this(source, new TreePath(path));
94     }
95
96
97     /**
98      * Used to create an event when nodes have been changed, inserted, or
99      * removed, identifying the path to the parent of the modified items as
100      * a TreePath object.
101      *
102      * @param source the Object responsible for generating the event
103      * @param path a TreePath object that identifies the path to the parent of the modified item(s)
104      */

105     public TreeModelEvent(Object JavaDoc source, TreePath path)
106     {
107         this.source = source;
108         this.path = path;
109         this.childIndices = new int[0];
110     }
111
112
113     /**
114      * Answer the source of this event
115      *
116      * @return the source of this event
117      */

118     public Object JavaDoc getSource()
119     {
120         return source;
121     }
122
123
124     /**
125      * For all events, except treeStructureChanged,
126      * returns the parent of the changed nodes.
127      * For treeStructureChanged events, returns the ancestor of the
128      * structure that has changed. This and
129      * <code>getChildIndices</code> are used to get a list of the effected
130      * nodes.
131      * <p/>
132      * The one exception to this is a treeNodesChanged event that is to
133      * identify the root, in which case this will return the root
134      * and <code>getChildIndices</code> will return null.
135      *
136      * @return the TreePath used in identifying the changed nodes.
137      */

138     public TreePath getTreePath()
139     {
140         return path;
141     }
142
143
144     /**
145      * Return the objects that are children of the node identified by
146      * the path of this event at the locations specified by
147      * <code>getChildIndices</code>. If this is a removal event the
148      * returned objects are no longer children of the parent node.
149      *
150      * @return an array of Object containing the children specified by
151      * the event
152      */

153     public Object JavaDoc[] getChildren()
154     {
155         if (children != null)
156         {
157             Object JavaDoc[] answer = new Object JavaDoc[children.length];
158
159             System.arraycopy(children, 0, answer, 0, children.length);
160             return answer;
161         }
162         return null;
163     }
164
165
166     /**
167      * Returns the values of the child indexes. If this is a removal event
168      * the indexes point to locations in the initial list where items
169      * were removed. If it is an insert, the indices point to locations
170      * in the final list where the items were added. For node changes,
171      * the indices point to the locations of the modified nodes.
172      *
173      * @return an array containing index locations for the children specified by the event
174      */

175     public int[] getChildIndices()
176     {
177         if (childIndices != null)
178         {
179             int[] answer = new int[childIndices.length];
180
181             System.arraycopy(childIndices, 0, answer, 0, childIndices.length);
182             return answer;
183         }
184         return null;
185     }
186
187
188     /**
189      * Returns a string that displays and identifies this object's
190      * properties.
191      *
192      * @return a String representation of this object
193      */

194     public String JavaDoc toString()
195     {
196         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
197
198         buffer.append(super.toString());
199         if (path != null)
200         {
201             buffer.append(" path " + path);
202         }
203         if (childIndices != null)
204         {
205             buffer.append(" indices [ ");
206             for (int i = 0; i < childIndices.length; i++)
207             {
208                 buffer.append(Integer.toString(childIndices[i]) + " ");
209             }
210             buffer.append("]");
211         }
212         if (children != null)
213         {
214             buffer.append(" children [ ");
215             for (int i = 0; i < children.length; i++)
216             {
217                 buffer.append(children[i] + " ");
218             }
219             buffer.append("]");
220         }
221         return buffer.toString();
222     }
223
224 }
225
Popular Tags