Java Swing TextArea: How to make JTextArea DocumentListener DocumentEvent Trigger Two Or More Combined Codes/Keys?

 ̄綄美尐妖づ 提交于 2020-01-16 11:30:45

问题


My text editor contains two textAreas (TA). TA1 is for input Devanagari Unicode texts and TA2 is for output in ASCII as transliterated texts which get printed simultaneously as the input text got typed key by key using DocumentListener. I do this using a one to one transliteration scheme. However there are cases I need to map multiple/two Devanagari UCodes input in a single ASCII character. Eg. क्=k (क्=क+्). The problem is here that a single two UCodes containing character is generated through the single standard key input k. Eg. क् is represented actually by two UCodes. Then the document listener counts as two DocumentEvents. Thus the transliterator cannot trigger and not generate the corresponding single ASCII character. The transliterator class:

import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

public class MyWindow extends JPanel {

    private static final int ROWS = 10, COLS = 50;

    private final JTextArea outputTextArea;

    public MyWindow() {
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

        JTextArea inputTextArea = new JTextArea(ROWS, COLS);
        inputTextArea.getDocument().addDocumentListener(new TransliterateDocumentListener());
        add(putInTitledScrollPane(inputTextArea, "Input"));

        outputTextArea = new JTextArea(ROWS, COLS);
        outputTextArea.setFocusable(false);
        outputTextArea.setEditable(false);

        add(putInTitledScrollPane(outputTextArea, "Output"));
    }


    private JPanel putInTitledScrollPane(JComponent component, String title) {
        JPanel wrapperPanel = new JPanel(new BorderLayout());
        wrapperPanel.setBorder(BorderFactory.createTitledBorder(title));
        wrapperPanel.add(new JScrollPane(component));
        return wrapperPanel;
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("Document Listener Demo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(new MyWindow());
        frame.pack();
        frame.setVisible(true);
    }


    private void insert(String text, int from) {
        text = Transliterate(text);
        try {
            outputTextArea.getDocument().insertString(from, text, null);
        } catch (BadLocationException ex) {
            ex.printStackTrace();
        }
    }

    /*================================================================
     *================================================================
     */
    private void remove(int from, int length) {
        try {
            outputTextArea.getDocument().remove(from, length);
        } catch (BadLocationException ex) {
            ex.printStackTrace();
        }
    }

    //the Transliterator method
    private String Transliterate(String DevanagariText) {
        String asciiText = "";
        //devanagari text is assigned to ascciiText which must be returned
        asciiText = DevanagariText;

        if (asciiText.length() >= 0) {

            for (int i = 0; i <= asciiText.length() - 1; i++) {
                //if input key is Devanagari अ, then it must be replaced by ascii lower a
                if(asciiText.charAt(i)=='अ'){
                    asciiText=asciiText.replace(asciiText.charAt(i),'a');
                }
                //if input key is Devanagari आ or its diacritics ा, then it must be replaced by ascii capital A
                else if(asciiText.charAt(i)=='आ'||asciiText.charAt(i)=='ा'){
                    asciiText=asciiText.replace(asciiText.charAt(i),'A');
                }
                //if input key is Devanagari इ or its diacritics ि, then it must be replaced by ascii lower i
                else if(asciiText.charAt(i)=='इ'||asciiText.charAt(i)=='ि'){
                    asciiText=asciiText.replace(asciiText.charAt(i),'i');
                }
                //All of these above ⇪ codes are producing the target characters
                //===============================================================================
                //if input key is Devanagari क्, then it must be replaced by ascii lower k
                else if(asciiText.equals("क्")){
                    asciiText.replace("क्", "k");
                    // ↓ this is not producing the target/desired character
                    //Because, No. 1. क् has two unicodes क + ्. The ् (halanta) is a diacritic added to 25 such Devanagari...
                    //...consonant letters to represent as a consonant phone. They are ख् ग् घ् ङ् च् छ् ज् झ् etc.
                    //No. 2. क् (क  ्) is produced by only one single standard keyboard key 'k'
                    //however the document listerner counts two keys as it contains two UCodes. And it cannot capture
                    //the moment of the each code since both of the क  ् codes entered through a single key typed.
                }
            }
        }
        return asciiText;
    }
//-----------------------------------document listener
//-------------------------------------------------------

    class TransliterateDocumentListener implements DocumentListener {

        @Override
        public void insertUpdate(DocumentEvent e) {
            Document doc = e.getDocument();
            int from = e.getOffset(), length = e.getLength();
            try {
                insert(doc.getText(from, length), from);
            } catch (BadLocationException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            remove(e.getOffset(), e.getLength());
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            //Plain text components don't fire these events.
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

来源:https://stackoverflow.com/questions/59488546/java-swing-textarea-how-to-make-jtextarea-documentlistener-documentevent-trigge

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!