Source for java.awt.event.WindowEvent

   1: /* WindowEvent.java -- window change event
   2:    Copyright (C) 1999, 2002, 2005  Free Software Foundation, Inc.
   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.Frame;
  44: import java.awt.Window;
  45: 
  46: /**
  47:  * This event is generated when there is a change in a window. This includes
  48:  * creation, closing, iconification, activation, and focus changes. There
  49:  * are three listeners, for three types of events: WindowListeners deal with
  50:  * the lifecycle of a window, WindowStateListeners deal with window state
  51:  * like maximization, and WindowFocusListeners deal with focus switching to
  52:  * or from a window.
  53:  *
  54:  * @author Aaron M. Renn (arenn@urbanophile.com)
  55:  * @see WindowAdapter
  56:  * @see WindowListener
  57:  * @see WindowFocusListener
  58:  * @see WindowStateListener
  59:  * @since 1.1
  60:  * @status updated to 1.4
  61:  */
  62: public class WindowEvent extends ComponentEvent
  63: {
  64:   /**
  65:    * Compatible with JDK 1.1+.
  66:    */
  67:   private static final long serialVersionUID = -1567959133147912127L;
  68: 
  69:   /** This is the first id in the range of event ids used by this class. */
  70:   public static final int WINDOW_FIRST = 200;
  71: 
  72:   /** This is the id for a window that is opened. */
  73:   public static final int WINDOW_OPENED = 200;
  74: 
  75:   /** This is the id for a window that is about to close. */
  76:   public static final int WINDOW_CLOSING = 201;
  77: 
  78:   /** This is the id for a window that finished closing. */
  79:   public static final int WINDOW_CLOSED = 202;
  80: 
  81:   /** This is the id for a window that is iconified. */
  82:   public static final int WINDOW_ICONIFIED = 203;
  83: 
  84:   /** This is the id for a window that is de-iconified. */
  85:   public static final int WINDOW_DEICONIFIED = 204;
  86: 
  87:   /** This is the id for a window that is activated. */
  88:   public static final int WINDOW_ACTIVATED = 205;
  89: 
  90:   /** This is the id for a window that is de-activated. */
  91:   public static final int WINDOW_DEACTIVATED = 206;
  92: 
  93:   /**
  94:    * This is the id for a window becoming the focused window.
  95:    *
  96:    * @since 1.4
  97:    */
  98:   public static final int WINDOW_GAINED_FOCUS = 207;
  99: 
 100:   /**
 101:    * This is the id for a window losing all focus.
 102:    *
 103:    * @since 1.4
 104:    */
 105:   public static final int WINDOW_LOST_FOCUS = 208;
 106: 
 107:   /**
 108:    * This is the id for a window state change, such as maximization.
 109:    *
 110:    * @since 1.4
 111:    */
 112:   public static final int WINDOW_STATE_CHANGED = 209;
 113: 
 114:   /** This is the last id in the range of event ids used by this class. */
 115:   public static final int WINDOW_LAST = 209;
 116: 
 117:   /**
 118:    * The other Window involved in a focus or activation change. For
 119:    * WINDOW_ACTIVATED and WINDOW_GAINED_FOCUS events, this is the window that
 120:    * lost focus; for WINDOW_DEACTIVATED and WINDOW_LOST_FOCUS, this is the
 121:    * window that stole focus; and for other events (or when native
 122:    * implementation does not have the data available), this is null.
 123:    *
 124:    * @see #getOppositeWindow()
 125:    * @serial the opposite window, or null
 126:    * @since 1.4
 127:    */
 128:   private final Window opposite;
 129: 
 130:   /**
 131:    * The former state of the window.
 132:    *
 133:    * @serial bitmask of the old window state
 134:    * @since 1.4
 135:    */
 136:   private final int oldState;
 137: 
 138:   /**
 139:    * The present state of the window.
 140:    *
 141:    * @serial bitmask of the new window state
 142:    * @since 1.4
 143:    */
 144:   private final int newState;
 145: 
 146:   /**
 147:    * Initializes a new instance of <code>WindowEvent</code> with the specified
 148:    * parameters. Note that an invalid id leads to unspecified results.
 149:    *
 150:    * @param source the window that generated this event
 151:    * @param id the event id
 152:    * @param opposite the window that received the opposite event, or null
 153:    * @param oldState the previous state of this window
 154:    * @param newState the new state of this window
 155:    * @throws IllegalArgumentException if source is null
 156:    * @since 1.4
 157:    */
 158:   public WindowEvent(Window source, int id, Window opposite,
 159:                      int oldState, int newState)
 160:   {
 161:     super(source, id);
 162:     this.opposite = opposite;
 163:     this.oldState = oldState;
 164:     this.newState = newState;
 165:   }
 166: 
 167:   /**
 168:    * Initializes a new instance of <code>WindowEvent</code> with the specified
 169:    * parameters. Note that an invalid id leads to unspecified results.
 170:    *
 171:    * @param source the window that generated this event
 172:    * @param id the event id
 173:    * @param opposite the window that received the opposite event, or null
 174:    * @throws IllegalArgumentException if source is null
 175:    * @since 1.4
 176:    */
 177:   public WindowEvent(Window source, int id, Window opposite)
 178:   {
 179:     this(source, id, opposite, 0, 0);
 180:   }
 181: 
 182:   /**
 183:    * Initializes a new instance of <code>WindowEvent</code> with the specified
 184:    * parameters. Note that an invalid id leads to unspecified results.
 185:    *
 186:    * @param source the window that generated this event
 187:    * @param id the event id
 188:    * @param oldState the previous state of this window
 189:    * @param newState the new state of this window
 190:    * @throws IllegalArgumentException if source is null
 191:    * @since 1.4
 192:    */
 193:   public WindowEvent(Window source, int id, int oldState, int newState)
 194:   {
 195:     this(source, id, null, oldState, newState);
 196:   }
 197: 
 198:   /**
 199:    * Initializes a new instance of <code>WindowEvent</code> with the specified
 200:    * parameters. Note that an invalid id leads to unspecified results.
 201:    *
 202:    * @param source the window that generated this event
 203:    * @param id the event id
 204:    * @throws IllegalArgumentException if source is null
 205:    */
 206:   public WindowEvent(Window source, int id)
 207:   {
 208:     this(source, id, null, 0, 0);
 209:   }
 210: 
 211:   /**
 212:    * Returns the event source as a <code>Window</code>. If the source has
 213:    * subsequently been modified to a non-Window, this returns null.
 214:   *
 215:   * @return the event source as a <code>Window</code>
 216:   */
 217:   public Window getWindow()
 218:   {
 219:     return source instanceof Window ? (Window) source : null;
 220:   }
 221: 
 222:   /**
 223:    * Returns the opposite window if this window was involved in an activation
 224:    * or focus change. For WINDOW_ACTIVATED and WINDOW_GAINED_FOCUS events,
 225:    * this is the window that lost focus; for WINDOW_DEACTIVATED and
 226:    * WINDOW_LOST_FOCUS, this is the window that stole focus; and for other
 227:    * events (or when native implementation does not have the data available),
 228:    * this is null.
 229:    *
 230:    * @return the opposite window, or null
 231:    * @since 1.4
 232:    */
 233:   public Window getOppositeWindow()
 234:   {
 235:     return opposite;
 236:   }
 237: 
 238:   /**
 239:    * Returns the state of this window before the event. This is the bitwise
 240:    * or of fields in Frame: NORMAL, ICONIFIED, MAXIMIZED_HORIZ, MAXIMIZED_VERT,
 241:    * and MAXIMIZED_BOTH.
 242:    *
 243:    * @return the former state
 244:    * @see Frame#getExtendedState()
 245:    * @since 1.4
 246:    */
 247:   public int getOldState()
 248:   {
 249:     return oldState;
 250:   }
 251: 
 252:   /**
 253:    * Returns the state of this window after the event. This is the bitwise
 254:    * or of fields in Frame: NORMAL, ICONIFIED, MAXIMIZED_HORIZ, MAXIMIZED_VERT,
 255:    * and MAXIMIZED_BOTH.
 256:    *
 257:    * @return the updated state
 258:    * @see Frame#getExtendedState()
 259:    * @since 1.4
 260:    */
 261:   public int getNewState()
 262:   {
 263:     return newState;
 264:   }
 265: 
 266:   /**
 267:    * Returns a string that identifies this event. This is formatted as the
 268:    * field name of the id, followed by the opposite window, old state, and
 269:    * new state.
 270:    *
 271:    * @return a string that identifies this event
 272:    */
 273:   public String paramString()
 274:   {
 275:     CPStringBuilder s = new CPStringBuilder();
 276:     switch (id)
 277:       {
 278:       case WINDOW_OPENED:
 279:         s.append("WINDOW_OPENED,opposite=");
 280:         break;
 281:       case WINDOW_CLOSING:
 282:         s.append("WINDOW_CLOSING,opposite=");
 283:         break;
 284:       case WINDOW_CLOSED:
 285:         s.append("WINDOW_CLOSED,opposite=");
 286:         break;
 287:       case WINDOW_ICONIFIED:
 288:         s.append("WINDOW_ICONIFIED,opposite=");
 289:         break;
 290:       case WINDOW_DEICONIFIED:
 291:         s.append("WINDOW_DEICONIFIED,opposite=");
 292:         break;
 293:       case WINDOW_ACTIVATED:
 294:         s.append("WINDOW_ACTIVATED,opposite=");
 295:         break;
 296:       case WINDOW_DEACTIVATED:
 297:         s.append("WINDOW_DEACTIVATED,opposite=");
 298:         break;
 299:       case WINDOW_GAINED_FOCUS:
 300:         s.append("WINDOW_GAINED_FOCUS,opposite=");
 301:         break;
 302:       case WINDOW_LOST_FOCUS:
 303:         s.append("WINDOW_LOST_FOCUS,opposite=");
 304:         break;
 305:       case WINDOW_STATE_CHANGED:
 306:         s.append("WINDOW_STATE_CHANGED,opposite=");
 307:         break;
 308:       default:
 309:         s.append("unknown type,opposite=");
 310:       }
 311:     return s.append(opposite).append(",oldState=").append(oldState)
 312:       .append(",newState=").append(newState).toString();
 313:   }
 314: } // class WindowEvent