source: trunk/gli/src/org/greenstone/gatherer/gui/TransformCharacterTextField.java@ 8003

Last change on this file since 8003 was 8003, checked in by mdewsnip, 20 years ago

Tightened up some public functions to be private.

  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 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.gui;
28
29/**************************************************************************************
30 * Written: 04/07/03
31 * Revised:
32 **************************************************************************************/
33import java.util.*;
34import javax.swing.*;
35import javax.swing.text.*;
36/** A JTextField that doesn't allow certain characters, either blocking or replacing them
37 * @author John Thompson, Greenstone Digital Library, University of Waikato
38 * @version 2.4
39 */
40public class TransformCharacterTextField
41 extends JTextField {
42
43 private boolean block_all_except_mappings = false;
44 static private HashMap mappings = new HashMap();
45
46 static public TransformCharacterTextField createNamespaceTextField() {
47 TransformCharacterTextField text = new TransformCharacterTextField();
48 text.setBlockAllExceptMappings(true);
49 int i = 'a';
50 int j = 'A';
51 for( ; i <= 'z'; i++, j++) {
52 text.replaceCharacter((char)i, (char)i);
53 text.replaceCharacter((char)j, (char)i);
54 }
55 return text;
56 }
57
58 public TransformCharacterTextField() {
59 super();
60 }
61
62 public TransformCharacterTextField(String text) {
63 super(text);
64 }
65
66// public void blockCharacter(char x) {
67// mappings.put(new Character(x), null);
68// }
69
70 public void replaceCharacter(char x, char y) {
71 mappings.put(new Character(x), new Character(y));
72 }
73
74// public void restoreCharacter(char x) {
75// mappings.remove(new Character(x));
76// }
77
78 public void setBlockAllExceptMappings(boolean block_all_except_mappings) {
79 this.block_all_except_mappings = block_all_except_mappings;
80 setText(""); // Have to reset
81 }
82
83 protected Document createDefaultModel() {
84 return new TransformCharacterTextDocument();
85 }
86
87 private class TransformCharacterTextDocument
88 extends PlainDocument {
89
90 public void insertString(int offs, String str, AttributeSet a)
91 throws BadLocationException {
92
93 if (str == null) {
94 return;
95 }
96 char[] raw = str.toCharArray();
97 StringBuffer result = new StringBuffer("");
98 for (int i = 0; i < raw.length; i++) {
99 Character key = new Character(raw[i]);
100 if(mappings.containsKey(key)) {
101 Character replacement = (Character) mappings.get(key);
102 if(replacement != null) {
103 result.append(replacement.charValue());
104 replacement = null;
105 }
106 }
107 else if(!block_all_except_mappings) {
108 result.append(raw[i]);
109 }
110 key = null;
111 }
112 super.insertString(offs, result.toString(), a);
113 }
114 }
115}
Note: See TracBrowser for help on using the repository browser.