1 /* HierarchyEvent.java -- generated for a change in hierarchy
2    Copyright (C) 2000, 2002 Free Software Foundation
3 
4 This file is part of GNU Classpath.
5 
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37 
38 
39 package java.awt.event;
40 
41 import gnu.java.lang.CPStringBuilder;
42 
43 import java.awt.AWTEvent;
44 import java.awt.Component;
45 import java.awt.Container;
46 
47 /**
48  * This class represents an event generated for an ancestor component which
49  * may affect this component. These events normally do not need to be handled
50  * by the application, since the AWT system automatically takes care of them.
51  *
52  * <p>There are two types of hierarchy events. The first type is handled by
53  * HierarchyListener, and includes addition or removal of an ancestor, or
54  * an ancestor changing its on-screen status (visible and/or displayble). The
55  * second type is handled by HierarchyBoundsListener, and includes resizing
56  * or moving of an ancestor.
57  *
58  * @author Bryce McKinlay
59  * @see HierarchyListener
60  * @see HierarchyBoundsAdapter
61  * @see HierarchyBoundsListener
62  * @since 1.3
63  * @status updated to 1.4
64  */
65 public class HierarchyEvent extends AWTEvent
66 {
67   /**
68    * Compatible with JDK 1.3+.
69    */
70   private static final long serialVersionUID = -5337576970038043990L;
71 
72   /** This is the first id in the range of ids used by this class. */
73   public static final int HIERARCHY_FIRST = 1400;
74 
75   /** This id indicates that the hierarchy tree changed. */
76   public static final int HIERARCHY_CHANGED = 1400;
77 
78   /** This id indicates that an ancestor was moved. */
79   public static final int ANCESTOR_MOVED = 1401;
80 
81   /** This id indicates that an ancestor was resized. */
82   public static final int ANCESTOR_RESIZED = 1402;
83 
84   /** This is the last id in the range of ids used by this class. */
85   public static final int HIERARCHY_LAST = 1402;
86 
87   /** This indicates that the HIERARCHY_CHANGED is a changed parent. */
88   public static final int PARENT_CHANGED = 1;
89 
90   /**
91    * This indicates that the HIERARCHY_CHANGED is caused by a change in
92    * displayability.
93    *
94    * @see Component#isDisplayable()
95    * @see Component#addNotify()
96    * @see Component#removeNotify()
97    */
98   public static final int DISPLAYABILITY_CHANGED = 2;
99 
100   /**
101    * This indicates that the HIERARCHY_CHANGED is a changed visibility.
102    *
103    * @see Component#isShowing()
104    * @see Component#addNotify()
105    * @see Component#removeNotify()
106    * @see Component#show()
107    * @see Component#hide()
108    */
109   public static final int SHOWING_CHANGED = 4;
110 
111   /**
112    * The component at the top of the changed hierarchy.
113    *
114    * @serial the top component changed
115    */
116   private final Component changed;
117 
118   /**
119    * The parent of this component, either before or after the change depending
120    * on the type of change.
121    *
122    * @serial the parent component changed
123    */
124   private final Container changedParent;
125 
126   /**
127    * The bitmask of HIERARCHY_CHANGED event types.
128    *
129    * @serial the change flags
130    */
131   private final long changeFlags;
132 
133   /**
134    * Initializes a new instance of <code>HierarchyEvent</code> with the
135    * specified parameters. Note that an invalid id leads to unspecified
136    * results.
137    *
138    * @param source the component whose hierarchy changed
139    * @param id the event id
140    * @param changed the top component in the tree of changed hierarchy
141    * @param changedParent the updated parent of this object
142    * @throws IllegalArgumentException if source is null
143    */
HierarchyEvent(Component source, int id, Component changed, Container changedParent)144   public HierarchyEvent(Component source, int id, Component changed,
145                         Container changedParent)
146   {
147     this(source, id, changed, changedParent, 0);
148   }
149 
150   /**
151    * Initializes a new instance of <code>HierarchyEvent</code> with the
152    * specified parameters. Note that an invalid id leads to unspecified
153    * results.
154    *
155    * @param source the component whose hierarchy changed
156    * @param id the event id
157    * @param changed the top component in the tree of changed hierarchy
158    * @param changedParent the updated parent of this object
159    * @param changeFlags the bitmask of specific HIERARCHY_CHANGED events
160    * @throws IllegalArgumentException if source is null
161    */
HierarchyEvent(Component source, int id, Component changed, Container changedParent, long changeFlags)162   public HierarchyEvent(Component source, int id, Component changed,
163                         Container changedParent, long changeFlags)
164   {
165     super(source, id);
166     this.changed = changed;
167     this.changedParent = changedParent;
168     this.changeFlags = changeFlags;
169   }
170 
171   /**
172    * This method returns the event source as a <code>Component</code>. If the
173    * source has subsequently been modified to a non-Component, this returns
174    * null.
175    *
176    * @return the event source as a <code>Component</code>, or null
177    */
getComponent()178   public Component getComponent()
179   {
180     return source instanceof Component ? (Component) source : null;
181   }
182 
183   /**
184    * Returns the component at the top of the hierarchy which changed.
185    *
186    * @return the top changed component
187    */
getChanged()188   public Component getChanged()
189   {
190     return changed;
191   }
192 
193   /**
194    * Returns the parent of the component listed in <code>getChanged()</code>.
195    * If the cause of this event was <code>Container.add</code>, this is the
196    * new parent; if the cause was <code>Container.remove</code>, this is the
197    * old parent; otherwise it is the unchanged parent.
198    *
199    * @return the parent container of the changed component
200    */
getChangedParent()201   public Container getChangedParent()
202   {
203     return changedParent;
204   }
205 
206   /**
207    * If this is a HIERARCHY_CHANGED event, this returns a bitmask of the
208    * types of changes that took place.
209    *
210    * @return the bitwise or of hierarchy change types, or 0
211    * @see #PARENT_CHANGED
212    * @see #DISPLAYABILITY_CHANGED
213    * @see #SHOWING_CHANGED
214    */
getChangeFlags()215   public long getChangeFlags()
216   {
217     return changeFlags;
218   }
219 
220   /**
221    * This method returns a string identifying this event. This is the field
222    * name of the id type, followed by a parenthesized listing of the changed
223    * component and its parent container. In addition, if the type is
224    * HIERARCHY_CHANGED, the flags preceed the changed component, in the
225    * order PARENT_CHANGED, DISPLAYABILITY_CHANGED, and SHOWING_CHANGED.
226    *
227    * @return a string identifying this event
228    */
paramString()229   public String paramString()
230   {
231     CPStringBuilder r = new CPStringBuilder();
232     switch (id)
233       {
234       case HIERARCHY_CHANGED:
235         r.append("HIERARCHY_CHANGED (");
236         if ((changeFlags & PARENT_CHANGED) != 0)
237           r.append("PARENT_CHANGED,");
238         if ((changeFlags & DISPLAYABILITY_CHANGED) != 0)
239           r.append("DISPLAYABILITY_CHANGED,");
240         if ((changeFlags & SHOWING_CHANGED) != 0)
241           r.append("SHOWING_CHANGED,");
242         break;
243       case ANCESTOR_MOVED:
244         r.append("ANCESTOR_MOVED (");
245         break;
246       case ANCESTOR_RESIZED:
247         r.append("ANCESTOR_RESIZED (");
248         break;
249       default:
250         return "unknown type";
251       }
252     r.append(changed).append(',').append(changedParent).append(')');
253     return r.toString();
254   }
255 } // class HierarchyEvent
256