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 gnu.java.lang.CPStringBuilder;
042    
043    import java.awt.AWTEvent;
044    import java.awt.Component;
045    import java.awt.Container;
046    
047    /**
048     * This class represents an event generated for an ancestor component which
049     * may affect this component. These events normally do not need to be handled
050     * by the application, since the AWT system automatically takes care of them.
051     *
052     * <p>There are two types of hierarchy events. The first type is handled by
053     * HierarchyListener, and includes addition or removal of an ancestor, or
054     * an ancestor changing its on-screen status (visible and/or displayble). The
055     * second type is handled by HierarchyBoundsListener, and includes resizing
056     * or moving of an ancestor.
057     *
058     * @author Bryce McKinlay
059     * @see HierarchyListener
060     * @see HierarchyBoundsAdapter
061     * @see HierarchyBoundsListener
062     * @since 1.3
063     * @status updated to 1.4
064     */
065    public class HierarchyEvent extends AWTEvent
066    {
067      /**
068       * Compatible with JDK 1.3+.
069       */
070      private static final long serialVersionUID = -5337576970038043990L;
071    
072      /** This is the first id in the range of ids used by this class. */
073      public static final int HIERARCHY_FIRST = 1400;
074    
075      /** This id indicates that the hierarchy tree changed. */
076      public static final int HIERARCHY_CHANGED = 1400;
077    
078      /** This id indicates that an ancestor was moved. */
079      public static final int ANCESTOR_MOVED = 1401;
080    
081      /** This id indicates that an ancestor was resized. */
082      public static final int ANCESTOR_RESIZED = 1402;
083    
084      /** This is the last id in the range of ids used by this class. */
085      public static final int HIERARCHY_LAST = 1402;
086    
087      /** This indicates that the HIERARCHY_CHANGED is a changed parent. */
088      public static final int PARENT_CHANGED = 1;
089    
090      /**
091       * This indicates that the HIERARCHY_CHANGED is caused by a change in
092       * displayability.
093       *
094       * @see Component#isDisplayable()
095       * @see Component#addNotify()
096       * @see Component#removeNotify()
097       */
098      public static final int DISPLAYABILITY_CHANGED = 2;
099    
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       */
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       */
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       */
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       */
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       */
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       */
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       */
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