This has been programmed before, but here it is for you to see (and use):
IPATextField, a simple descendant of JTextField that will only accept phonetic input (either SAMPA or IPA if you know your uni codes by heart) and show IPA symbols.
You can try it out directly, as a main routine is included. It's even useful as your tiny copy-and-paste-IPA-editor.
/** Copyright (C) 2008 Timo Baumann
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
**/
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
public class IPATextField extends JTextField {
public IPATextField(int cols) {
super(cols);
}
protected Document createDefaultModel() {
return new IPADocument();
}
static class IPADocument extends PlainDocument {
String search = "Q?ɡTDSZCχʁNR=" + "IEA{OUY29@6:_";
char[] replace = "ʔʔgθðʃʒçxrŋʀu0329ɪɛɑæɔʊʏøœəɐːu032F".toCharArray();
String pass = search + replace + "pbtdkgfvszwjxhmnlrieaouy .";
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException
{
if (str == null) {
return;
}
StringBuffer sb = new StringBuffer();
CharacterIterator ci = new StringCharacterIterator(str);
while (ci.getIndex() < ci.getEndIndex()) {
char[] c = new char[1];
c[0] = ci.current();
String s = new String(c);
if (pass.contains(s)) {
int replaceIndex = search.indexOf(s);
if (replaceIndex == -1) {
sb.append(s);
} else {
sb.append(replace[replaceIndex]);
}
}
ci.next();
}
super.insertString(offs, sb.toString(), a);
}
}
/**
* pretty much stolen from the Swing-Tutorial...
* @param args does not take any arguments
*/
public static void main(String[] args) {
//Create the top-level container and add contents to it.
JFrame frame = new JFrame("VoxforgeDE Lexicon Tool");
final IPATextField tf = new IPATextField(10);
tf.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
System.out.println(tf.getText());
}
});
frame.add(tf);
//Finish setting up the frame, and show it.
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
}
