001    /* HierarchyEvent.java -- generated for a change in hierarchy
002       Copyright (C) 2000, 2002 Free Software Foundation
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package java.awt.event;
040    
041    import java.awt.AWTEvent;
042    import java.awt.Component;
043    import java.awt.Container;
044    
045    /**
046     * This class represents an event generated for an ancestor component which
047     * may affect this component. These events normally do not need to be handled
048     * by the application, since the AWT system automatically takes care of them.
049     *
050     * <p>There are two types of hierarchy events. The first type is handled by
051     * HierarchyListener, and includes addition or removal of an ancestor, or
052     * an ancestor changing its on-screen status (visible and/or displayble). The
053     * second type is handled by HierarchyBoundsListener, and includes resizing
054     * or moving of an ancestor.
055     *
056     * @author Bryce McKinlay
057     * @see HierarchyListener
058     * @see HierarchyBoundsAdapter
059     * @see HierarchyBoundsListener
060     * @since 1.3
061     * @status updated to 1.4
062     */
063    public class HierarchyEvent extends AWTEvent
064    {
065      /**
066       * Compatible with JDK 1.3+.
067       */
068      private static final long serialVersionUID = -5337576970038043990L;
069    
070      /** This is the first id in the range of ids used by this class. */
071      public static final int HIERARCHY_FIRST = 1400;
072    
073      /** This id indicates that the hierarchy tree changed. */
074      public static final int HIERARCHY_CHANGED = 1400;
075    
076      /** This id indicates that an ancestor was moved. */
077      public static final int ANCESTOR_MOVED = 1401;
078    
079      /** This id indicates that an ancestor was resized. */
080      public static final int ANCESTOR_RESIZED = 1402;
081    
082      /** This is the last id in the range of ids used by this class. */
083      public static final int HIERARCHY_LAST = 1402;
084    
085      /** This indicates that the HIERARCHY_CHANGED is a changed parent. */
086      public static final int PARENT_CHANGED = 1;
087    
088      /**
089       * This indicates that the HIERARCHY_CHANGED is caused by a change in
090       * displayability.
091       *
092       * @see Component#isDisplayable()
093       * @see Component#addNotify()
094       * @see Component#removeNotify()
095       */
096      public static final int DISPLAYABILITY_CHANGED = 2;
097    
098      /**
099       * This indicates that the HIERARCHY_CHANGED is a changed visibility.
100       *
101       * @see Component#isShowing()
102       * @see Component#addNotify()
103       * @see Component#removeNotify()
104       * @see Component#show()
105       * @see Component#hide()
106       */
107      public static final int SHOWING_CHANGED = 4;
108    
109      /**
110       * The component at the top of the changed hierarchy.
111       *
112       * @serial the top component changed
113       */
114      private final Component changed;
115    
116      /**
117       * The parent of this component, either before or after the change depending
118       * on the type of change.
119       *
120       * @serial the parent component changed
121       */
122      private final Container changedParent;
123    
124      /**
125       * The bitmask of HIERARCHY_CHANGED event types.
126       *
127       * @serial the change flags
128       */
129      private final long changeFlags;
130    
131      /**
132       * Initializes a new instance of <code>HierarchyEvent</code> with the
133       * specified parameters. Note that an invalid id leads to unspecified
134       * results.
135       *
136       * @param source the component whose hierarchy changed
137       * @param id the event id
138       * @param changed the top component in the tree of changed hierarchy
139       * @param changedParent the updated parent of this object
140       * @throws IllegalArgumentException if source is null
141       */
142      public HierarchyEvent(Component source, int id, Component changed,
143                            Container changedParent)
144      {
145        this(source, id, changed, changedParent, 0);
146      }
147    
148      /**
149       * Initializes a new instance of <code>HierarchyEvent</code> with the
150       * specified parameters. Note that an invalid id leads to unspecified
151       * results.
152       *
153       * @param source the component whose hierarchy changed
154       * @param id the event id
155       * @param changed the top component in the tree of changed hierarchy
156       * @param changedParent the updated parent of this object
157       * @param changeFlags the bitmask of specific HIERARCHY_CHANGED events
158       * @throws IllegalArgumentException if source is null
159       */
160      public HierarchyEvent(Component source, int id, Component changed,
161                            Container changedParent, long changeFlags)
162      {
163        super(source, id);
164        this.changed = changed;
165        this.changedParent = changedParent;
166        this.changeFlags = changeFlags;
167      }
168    
169      /**
170       * This method returns the event source as a <code>Component</code>. If the
171       * source has subsequently been modified to a non-Component, this returns
172       * null.
173       *
174       * @return the event source as a <code>Component</code>, or null
175       */
176      public Component getComponent()
177      {
178        return source instanceof Component ? (Component) source : null;
179      }
180    
181      /**
182       * Returns the component at the top of the hierarchy which changed.
183       *
184       * @return the top changed component
185       */
186      public Component getChanged()
187      {
188        return changed;
189      }
190    
191      /**
192       * Returns the parent of the component listed in <code>getChanged()</code>.
193       * If the cause of this event was <code>Container.add</code>, this is the
194       * new parent; if the cause was <code>Container.remove</code>, this is the
195       * old parent; otherwise it is the unchanged parent.
196       *
197       * @return the parent container of the changed component
198       */
199      public Container getChangedParent()
200      {
201        return changedParent;
202      }
203    
204      /**
205       * If this is a HIERARCHY_CHANGED event, this returns a bitmask of the
206       * types of changes that took place.
207       *
208       * @return the bitwise or of hierarchy change types, or 0
209       * @see #PARENT_CHANGED
210       * @see #DISPLAYABILITY_CHANGED
211       * @see #SHOWING_CHANGED
212       */
213      public long getChangeFlags()
214      {
215        return changeFlags;
216      }
217    
218      /**
219       * This method returns a string identifying this event. This is the field
220       * name of the id type, followed by a parenthesized listing of the changed
221       * component and its parent container. In addition, if the type is
222       * HIERARCHY_CHANGED, the flags preceed the changed component, in the
223       * order PARENT_CHANGED, DISPLAYABILITY_CHANGED, and SHOWING_CHANGED.
224       *
225       * @return a string identifying this event
226       */
227      public String paramString()
228      {
229        StringBuffer r = new StringBuffer();
230        switch (id)
231          {
232          case HIERARCHY_CHANGED:
233            r.append("HIERARCHY_CHANGED (");
234            if ((changeFlags & PARENT_CHANGED) != 0)
235              r.append("PARENT_CHANGED,");
236            if ((changeFlags & DISPLAYABILITY_CHANGED) != 0)
237              r.append("DISPLAYABILITY_CHANGED,");
238            if ((changeFlags & SHOWING_CHANGED) != 0)
239              r.append("SHOWING_CHANGED,");
240            break;
241          case ANCESTOR_MOVED:
242            r.append("ANCESTOR_MOVED (");
243            break;
244          case ANCESTOR_RESIZED:
245            r.append("ANCESTOR_RESIZED (");
246            break;
247          default:
248            return "unknown type";
249          }
250        r.append(changed).append(',').append(changedParent).append(')');
251        return r.toString();
252      }
253    } // class HierarchyEvent