home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 7.3 KB | 231 lines |
- /*
- * @(#)TileChangeMulticaster.java 1.3 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.awt.image;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.Vector;
- import java.awt.Point;
-
- /**
- * A convenience class that takes care of the details of implementing
- * the TileChangeListener interface. A BufferedImage or other
- * implementation of the WritableRenderedImage interface will use
- * this class by constructing a single instance and forwarding calls
- * to the methods addTileChangeListener, removeTileChangeListener,
- * getTileChangeListeners, getWritableTile, releaseWritableTile,
- * getWritableTiles, and hasTileWriters. Except for
- * getWritableTile and releaseWritableTile, there will rarely be
- * any reason for the WritableRenderedImage to do any additional
- * work.
- *
- */
- public class TileChangeMulticaster {
-
-
- /**
- * List of TileListeners.
- */
- private Vector listeners = new Vector();
-
- /**
- * A Hashtable containg key/value pairs of the form
- * TileIndex/Integer. The TileIndex key indicates the (x, y)
- * position of the tile and the Integer encodes the number of
- * current writers. When the number of writers drops to zero,
- * the entry is removed from the Hashtable.
- */
- private Hashtable tileWriterCount = new Hashtable();
-
- public TileChangeMulticaster () { }
-
- /** Inform the multicaster of a new listener. */
- public void addTileChangeListener (TileChangeListener tcl) {
- listeners.addElement(tcl);
- }
-
- /** Inform the multicaster that a listener has dropped out. */
- public void removeTileChangeListener (TileChangeListener tcl) {
- listeners.removeElement(tcl);
- }
-
- /** Allow the multicaster to return its list of listeners. */
- public TileChangeListener[] getTileChangeListeners () {
- int length = listeners.size();
- TileChangeListener larray[] = new TileChangeListener[length];
- Object objArray[] = listeners.toArray();
- for (int i = 0; i < length; i++) {
- larray[i] = (TileChangeListener)objArray[i];
- }
- return larray;
- }
-
- /**
- * Multicast the tileGrabbed message.
- *
- * @param source the image that owns the tile being grabbed.
- * @param tileX the X index of the tile.
- * @param tileY the Y index of the tile.
- */
- public void tileGrabbed (WritableRenderedImage source,
- int tileX, int tileY) {
- Enumeration enum = listeners.elements();
- while (enum.hasMoreElements()) {
- TileChangeListener tcl = (TileChangeListener) enum.nextElement();
- tcl.tileGrabbed(source, tileX, tileY);
- }
- }
-
- /**
- * Multicast the tileReleased message.
- *
- * @param source the image that owns the tile being released.
- * @param tileX the X index of the tile.
- * @param tileY the Y index of the tile.
- */
- public void tileReleased (WritableRenderedImage source,
- int tileX, int tileY) {
- Enumeration enum = listeners.elements();
- while (enum.hasMoreElements()) {
- TileChangeListener tcl = (TileChangeListener) enum.nextElement();
- tcl.tileReleased(source, tileX, tileY);
- }
- }
-
-
- /**
- * Record a new getWritableTile request to a particular tile
- * of a source image.
- *
- * @param source the image that owns the tile being grabbed.
- * @param tileX the X index of the tile.
- * @param tileY the Y index of the tile.
- */
- public void addTileWriter (WritableRenderedImage source,
- int tileX, int tileY) {
- TileIndex index = new TileIndex(tileX, tileY);
- Object count = tileWriterCount.get(index);
-
- if (count != null) { // The tile is in the Hashtable
- int icount = ((Integer) count).intValue(); // Increment the count
- tileWriterCount.put(index, new Integer(icount + 1));
- } else {
- tileWriterCount.put(index, new Integer(1)); // Insert with count = 1
- tileGrabbed(source, tileX, tileY); // Multicast the notification
- }
- }
-
- /**
- * Record a new releaseWritableTile request to a particular tile
- * of a source image.
- *
- * @param source the image that owns the tile being released.
- * @param tileX the X index of the tile.
- * @param tileY the Y index of the tile.
- */
- public void removeTileWriter (WritableRenderedImage source,
- int tileX, int tileY) {
- TileIndex index = new TileIndex(tileX, tileY);
- Object count = tileWriterCount.get(index);
-
- if (count != null) { // The tile is in the Hashtable
- int icount = ((Integer) count).intValue(); // Get the count
- if (icount == 1) { // Only one writer, remove it from the Hashtable
- tileWriterCount.remove(index);
- tileReleased(source, tileX, tileY);
- } else { // Multiple writers exist
- // Decrement the count
- tileWriterCount.put(index, new Integer(icount - 1));
- }
- } else {
- // The tile had no writers, this should never happen.
- }
- }
-
- /**
- * Returns whether a particular tile has a current writer.
- * @param tileX the X index of the tile.
- * @param tileY the Y index of the tile.
- * @returns true if a writer currently has access to the tile.
- */
- public boolean isTileWritable (int tileX, int tileY) {
- TileIndex index = new TileIndex(tileX, tileY);
- Object count = tileWriterCount.get(index);
- return (count != null); // index has a match in the Hashtable
- }
-
- /**
- * Returns a Vector listing the tiles that are currently writable.
- * @returns a Vector of TileIndex objects indicating which tiles
- * are held by writers.
- */
- public Point[] getWritableTiles () {
- Point tiles[] = new Point[tileWriterCount.size()];
-
- int count = 0;
- Enumeration enum = tileWriterCount.keys();
- while (enum.hasMoreElements()) {
- TileIndex index = (TileIndex) enum.nextElement();
- tiles[count++] = new Point(index.tileX,index.tileY);
- }
-
- return tiles;
- }
-
- /**
- * Return whether any tile is currently checked out for writing.
- * @returns true if any tile has a current writer.
- */
- public boolean hasTileWriters () {
- return (tileWriterCount.size() != 0); // Hashtable non-empty
- }
- }
-
-
- /**
- * A pair of integers with hash and equality functions, used by the
- * WritableRenderedImage and TileChangeFilter classes to represent
- * tile indices. Equality is based on equality of the tileX and
- * tileY values, not object equality.
- *
- * @see WritableRenderedImage
- * @see TileChangeFilter
- */
- class TileIndex extends Object {
- int tileX;
- int tileY;
-
- /** Construct a TileIndex from a pair of integers. */
- TileIndex (int tileX, int tileY) {
- this.tileX = tileX;
- this.tileY = tileY;
- }
-
- /** Create a hash code by concatenating the X and Y indices. */
- public int hashCode () {
- return (tileX << 16) | tileY;
- }
-
- /** Compare for equality based on equality of the integer values. */
- public boolean equals (Object obj) {
- if (obj instanceof TileIndex) {
- TileIndex index = (TileIndex) obj;
- return ((tileX == index.tileX) && (tileY == index.tileY));
- } else {
- return false;
- }
- }
- }
-
-