aboutsummaryrefslogtreecommitdiffstats
path: root/Java/com/phidgets/DictionaryKeyListener.java
blob: db09a904d6fbf0b06e471df0f805f0bb8dcd4d0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright 2006 Phidgets Inc.  All rights reserved.
 */

package com.phidgets;

import com.phidgets.event.*;

import com.phidgets.Dictionary;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * This class represents a key listener.
 * <p>
 * This key listener is used, along with the Dictionary object, to set up listener for specific keys, or groups of keys.
 * Events are available for key add or change, and for key removal.
 */
public class DictionaryKeyListener
{
	private String pattern;
	Dictionary dict;

	static
	{
		try
		{
			System.loadLibrary("phidget21");
		}
		catch(UnsatisfiedLinkError ex)
		{
			String os = System.getProperty("os.name");
			if(os.startsWith("Linux"))
			{
				throw new ExceptionInInitializerError(ex.getMessage()
				+"\nCould not locate the Phidget C library."
				+"\nMake sure to compile with 'make jni' and install with 'make install'.");
			}
			else if(os.startsWith("Windows"))
			{
				throw new ExceptionInInitializerError(ex.getMessage()
				+"\nCould not locate the Phidget C library."
				+"\nThe Windows Phidget21 MSI must be installed.");
			}
			else if(os.startsWith("Mac"))
			{
				throw new ExceptionInInitializerError(ex.getMessage()
				+"\nCould not locate the Phidget C library."
				+"\nThe Mac Phidget21 DMG must be installed.");
			}
			else
			{
				throw new ExceptionInInitializerError(ex.getMessage()
				+"\nCould not locate the Phidget C library.");
			}
		}
	}

	private long handle = 0;
	private long nativeHandler = 0;
	public long listenerhandle = 0;

	/**
	 * Start this key listener. This method should not be called until the coresponding dictionary is connected.
	 */
	public void start() throws PhidgetException
	{
		this.handle = dict.handle;
		if (handle == 0)
			throw (new PhidgetException(5, "Dictionary is not attached - call open first"));
		listenerhandle = enableDictionaryKeyEvents((keyChangeListeners.size() > 0) || (keyRemovalListeners.size() > 0), pattern);
	}

	/**
	 * Stop this key listener.
	 */
	public void stop()
	{
		listenerhandle = enableDictionaryKeyEvents(false, pattern);
	}

	/**
	 * Returns the Dictionary object that this listener is listening on.
	 */
	public Dictionary getDictionary()
	{
		return dict;
	}

	/**
	 * Creates a new key listener, for a specific pattern, on a specific dictionary object.
	 * The pattern is a regular expression.
	 */
	public DictionaryKeyListener(Dictionary dict, String pattern)
	{
		this.pattern = pattern;
		this.dict = dict;
		this.handle = dict.handle;
	}

	private LinkedList keyChangeListeners = new LinkedList();
	private long nativeKeyChangeHandler = 0;

	/**
	 * Add a new listener for key change events. This also applies for key add events..
	 */
	public final void addKeyChangeListener(KeyChangeListener l)
	{
		synchronized (keyChangeListeners)
		{
			keyChangeListeners.add(l);
			//enableDictionaryKeyEvents(true, pattern);
		}
	}
	/**
	 * Removes a key change listener.
	 */
	public final void removeKeyChangeListener(KeyChangeListener l)
	{
		synchronized (keyChangeListeners)
		{
			keyChangeListeners.remove(l);
			//enableDictionaryKeyEvents(keyChangeListeners.size() > 0, pattern);
		}
	}
	private void fireKeyChange(KeyChangeEvent e)
	{
		synchronized (keyChangeListeners)
		{
			for (Iterator it = keyChangeListeners.iterator();
			  it.hasNext(); )
				((KeyChangeListener)it.next()).keyChanged(e);
		}
	}
	private native long enableDictionaryKeyEvents(boolean b, String pattern);

	private LinkedList keyRemovalListeners = new LinkedList();
	private long nativeKeyRemovalHandler = 0;

	/**
	 * Add a new listener for key removal events.
	 */
	public final void addKeyRemovalListener(KeyRemovalListener l)
	{
		synchronized (keyRemovalListeners)
		{
			keyRemovalListeners.add(l);
			//enableDictionaryKeyEvents(true, pattern);
		}
	}
	/**
	 * removes a key removal listener.
	 */
	public final void removeKeyRemovalListener(KeyRemovalListener l)
	{
		synchronized (keyRemovalListeners)
		{
			keyRemovalListeners.remove(l);
			//enableDictionaryKeyEvents(keyRemovalListeners.size() > 0, pattern);
		}
	}
	private void fireKeyRemoval(KeyRemovalEvent e)
	{
		synchronized (keyRemovalListeners)
		{
			for (Iterator it = keyRemovalListeners.iterator();
			  it.hasNext(); )
				((KeyRemovalListener)it.next()).keyRemoved(e);
		}
	}

	/**
	 * Return a Sring describing this dictionary key listener.
	 */
	public String toString()
	{
		return dict.toString() + "Dictionary Key Listener ("+pattern+"): ";
	}

	protected void finalize()
	{
		listenerhandle = enableDictionaryKeyEvents(false, pattern);
	}
}