alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.widgets;


import org.eclipse.swt.*;
import org.eclipse.swt.internal.SWTEventListener;

/**
 * Instances of this class implement a simple
 * look up mechanism that maps an event type
 * to a listener.  Muliple listeners for the
 * same event type are supported.
 */

class EventTable {
	int [] types;
	Listener [] listeners;
	int level;
	
	static final String [] EVENT_NAMES = {
		"SWT.None",
		"SWT.KeyDown",
		"SWT.KeyUp",
		"SWT.MouseDown",
		"SWT.MouseUp",
		"SWT.MouseMove",
		"SWT.MouseEnter",
		"SWT.MouseExit",
		"SWT.MouseDoubleClick",
		"SWT.Paint",
		"SWT.Move",
		"SWT.Resize",
		"SWT.Dispose",
		"SWT.Selection",
		"SWT.DefaultSelection",
		"SWT.FocusIn",
		"SWT.FocusOut",
		"SWT.Expand",
		"SWT.Collapse",
		"SWT.Iconify",
		"SWT.Deiconify",
		"SWT.Close",
		"SWT.Show",
		"SWT.Hide",
		"SWT.Modify",
		"SWT.Verify",
		"SWT.Activate",
		"SWT.Deactivate",
		"SWT.Help",
		"SWT.DragDetect",
		"SWT.Arm",
		"SWT.Traverse",
		"SWT.MouseHover",
		"SWT.HardKeyDown",
		"SWT.HardKeyUp",
		"SWT.MenuDetect",
		"SWT.SetData",
		};
	
public void hook (int eventType, Listener listener) {
	if (types == null) types = new int [4];
	if (listeners == null) listeners = new Listener [4];
	int length = types.length, index = length - 1;
	while (index >= 0) {
		if (types [index] != 0) break;
		--index;
	}
	index++;
	if (index == length) {
		if (level == 0) {
			index = 0;
			for (int i=0; i<types.length; i++) {
				if (types [i] != 0) {
					types [index] = types [i];
					listeners [index] = listeners [i];
					index++;
				}
			}
			for (int i=index; i<types.length; i++) {
				types [i] = 0;
				listeners [i] = null;
			}
		}
		if (index == length) {
			int [] newTypes = new int [length + 4];
			System.arraycopy (types, 0, newTypes, 0, length);
			types = newTypes;
			Listener [] newListeners = new Listener [length + 4];
			System.arraycopy (listeners, 0, newListeners, 0, length);
			listeners = newListeners;
		}
	}
	types [index] = eventType;
	listeners [index] = listener;
}

public boolean hooks (int eventType) {
	if (types == null) return false;
	for (int i=0; i<types.length; i++) {
		if (types [i] == eventType) return true;
	}
	return false;
}

public void sendEvent (Event event) {
	if (types == null) return;
	level++;
	try {
		for (int i=0; i<types.length; i++) {
			if (event.type == SWT.None) return;
			if (types [i] == event.type) {
				Listener listener = listeners [i];
				if (listener != null) {
					
					//BOGUS
					long t1 = System.currentTimeMillis ();
					try {
						
					listener.handleEvent (event);

					//BOGUS
					} finally {
						long t2 = System.currentTimeMillis ();
						if (t2 - t1 > 250) {
							System.out.println ("EVENT: " + EVENT_NAMES [event.type] + " " + (t2 - t1) + "(ms) " + event.widget);
							if (listener instanceof TypedListener) {
								System.out.println (((TypedListener)listener).eventListener);
							} else {
								System.out.println (listener);
							}
							System.out.println ();
						}
					}
				}
			}
		}
	} finally {
		--level;
	}
}

public int size () {
	if (types == null) return 0;
	int count = 0;
	for (int i=0; i<types.length; i++) {
		if (types [i] != 0) count++;
	}
	return count;
}

void remove (int index) {
	if (level == 0) {
		int end = types.length - 1;
		System.arraycopy (types, index + 1, types, index, end - index);
		System.arraycopy (listeners, index + 1, listeners, index, end - index);
		index = end;
	}
	types [index] = 0;
	listeners [index] = null;
}

public void unhook (int eventType, Listener listener) {
	if (types == null) return;
	for (int i=0; i<types.length; i++) {
		if (types [i] == eventType && listeners [i] == listener) {
			remove (i);
			return;
		}
	}
}

public void unhook (int eventType, SWTEventListener listener) {
	if (types == null) return;
	for (int i=0; i<types.length; i++) {
		if (types [i] == eventType) {
			if (listeners [i] instanceof TypedListener) {
				TypedListener typedListener = (TypedListener) listeners [i];
				if (typedListener.getEventListener () == listener) {
					remove (i);
					return;
				}
			}
		}
	}
}

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.