001: /*
002: * CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF
003: * NETSCAPE COMMUNICATIONS CORPORATION
004: *
005: * Copyright (c) 1996 Netscape Communications Corporation.
006: * All Rights Reserved.
007: * Use of this Source Code is subject to the terms of the applicable
008: * license agreement from Netscape Communications Corporation.
009: */
010:
011: package graphical;
012:
013: import java.util.Hashtable;
014: import netscape.application.KeyEvent;
015: import netscape.application.TextFilter;
016: import netscape.util.Vector;
017: import util.Key;
018:
019: /**
020: * Encapsulate up the TextFilter for simple rejection/acceptance
021: * handling based on a list.
022: * Can be set to be either inclusive, accepting only characters
023: * on the list, or exclusive, rejecting only characters on
024: * the list.
025: */
026: public class HyperTextFilter implements TextFilter {
027: /**
028: * Hashtable of values to be handled by the filter.
029: * The key is assumed to be an Integer representing
030: * the keyboard value,
031: * the value is assumed to be a Character representing
032: * the keyboard value.
033: * @see #java.lang.Character
034: * @see #java.lang.Integer
035: */
036: private Hashtable hashTable;
037:
038: /**
039: * If inclusive is true and the KeyEvent character is
040: * on the list it is accepted; if inclusive is false
041: * and the KeyEvent character is not on the list,
042: * it is accepted; otherwise, it is rejected.
043: */
044: public boolean inclusive;
045:
046: /**
047: * If debug is true, print characters and accept or
048: * reject status.
049: */
050: public boolean debug;
051:
052: /**
053: * Constructor.
054: */
055: public HyperTextFilter() {
056: hashTable = new Hashtable();
057: inclusive = true;
058: debug = false;
059: }
060:
061: /**
062: * Constructor.
063: * @param n number of anticipated characters in the filter
064: * @param b set value of the inclusive attribute
065: */
066: public HyperTextFilter(int n, boolean b) {
067: hashTable = new Hashtable(n);
068: inclusive = b;
069: }
070:
071: /**
072: * Create a default inclusive alphanumeric filter.
073: */
074: public static HyperTextFilter createAlphaNumericFilter() {
075: return createAlphaNumericFilter(Key.ALPHANUMERIC_COUNT, true);
076: }
077:
078: /**
079: * Create an alphanumeric filter.
080: * @param n number of slots in the filter
081: * @param b inclusiveness
082: */
083: public static HyperTextFilter createAlphaNumericFilter(int n,
084: boolean b) {
085: HyperTextFilter htf = new HyperTextFilter();
086: htf.hashTable = Key.alphanumerics(n);
087: htf.inclusive = b;
088: return htf;
089: }
090:
091: /**
092: * Create a default inclusive i18n alphanumeric filter.
093: */
094: public static HyperTextFilter createI18NAlphaNumericFilter() {
095: return createI18NAlphaNumericFilter(Key.I18NALPHANUMERIC_COUNT,
096: true);
097: }
098:
099: /**
100: * Create an i18n alphanumeric filter.
101: * @param n number of slots in the filter
102: * @param b inclusiveness
103: */
104: public static HyperTextFilter createI18NAlphaNumericFilter(int n,
105: boolean b) {
106: HyperTextFilter htf = new HyperTextFilter();
107: htf.hashTable = Key.i18nalphanumerics(n);
108: htf.inclusive = b;
109: return htf;
110: }
111:
112: /**
113: * Determine whether to accept keystroke event.
114: */
115: public boolean acceptsEvent(Object textObject, KeyEvent event,
116: Vector events) {
117: boolean accept = hashTable.containsKey(new Integer(event.key)) == inclusive;
118: if (debug)
119: System.out.println("key: " + event.key
120: + (accept ? " accept" : " reject"));
121: return accept;
122: }
123:
124: public void put(Integer i, Character c) {
125: hashTable.put(i, c);
126: }
127:
128: }
|