source: trunk/gli/src/org/greenstone/gatherer/gems/TransformCharacterTextField.java@ 9647

Last change on this file since 9647 was 9170, checked in by mdewsnip, 19 years ago

Removed a whole lot of dead code.

  • Property svn:keywords set to Author Date Id Revision
File size: 3.2 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.gems;
28
29import java.util.*;
30import javax.swing.*;
31import javax.swing.text.*;
32
33
34/** A JTextField that doesn't allow certain characters, either blocking or replacing them
35 * @author John Thompson, Greenstone Digital Library, University of Waikato
36 * @version 2.4
37 */
38public class TransformCharacterTextField
39 extends JTextField {
40
41 private boolean block_all_except_mappings = false;
42 static private HashMap mappings = new HashMap();
43
44 static public TransformCharacterTextField createNamespaceTextField() {
45 TransformCharacterTextField text = new TransformCharacterTextField();
46 text.setBlockAllExceptMappings(true);
47 int i = 'a';
48 int j = 'A';
49 for( ; i <= 'z'; i++, j++) {
50 text.replaceCharacter((char)i, (char)i);
51 text.replaceCharacter((char)j, (char)i);
52 }
53 return text;
54 }
55
56 public TransformCharacterTextField() {
57 super();
58 }
59
60 public TransformCharacterTextField(String text) {
61 super(text);
62 }
63
64 public void replaceCharacter(char x, char y) {
65 mappings.put(new Character(x), new Character(y));
66 }
67
68 public void setBlockAllExceptMappings(boolean block_all_except_mappings) {
69 this.block_all_except_mappings = block_all_except_mappings;
70 setText(""); // Have to reset
71 }
72
73 protected Document createDefaultModel() {
74 return new TransformCharacterTextDocument();
75 }
76
77 private class TransformCharacterTextDocument
78 extends PlainDocument {
79
80 public void insertString(int offs, String str, AttributeSet a)
81 throws BadLocationException {
82
83 if (str == null) {
84 return;
85 }
86 char[] raw = str.toCharArray();
87 StringBuffer result = new StringBuffer("");
88 for (int i = 0; i < raw.length; i++) {
89 Character key = new Character(raw[i]);
90 if(mappings.containsKey(key)) {
91 Character replacement = (Character) mappings.get(key);
92 if(replacement != null) {
93 result.append(replacement.charValue());
94 replacement = null;
95 }
96 }
97 else if(!block_all_except_mappings) {
98 result.append(raw[i]);
99 }
100 key = null;
101 }
102 super.insertString(offs, result.toString(), a);
103 }
104 }
105}
Note: See TracBrowser for help on using the repository browser.