Tabs, newlines, DocumentFilters and eclipse text

女生的网名这么多〃 提交于 2019-12-13 03:39:05

问题


So, I have an additional issue regarding THIS question.

Once I apply the fix, go to notepad, typeout bunch of tabs and newlines with some random characters and then paste them into my program, it all works peachy.

However, being the closest text with bunch of tabs and newlines, I tried pasting a part the code itself to the JTextArea. All tabs and newlines stuck there and weren't filtered out.

Although my users probably won't paste eclipse code into my program , I can't be sure that eclipse code is the only exception. So I'd like to know why is this happening.

Also, I'd like for my code to filter out the blank characters except for space caracter and turn them into space character. I think tab and newline are the only ones, but if there are any more, please let me know.

Anyway, what do I have to change to make it work?

Here's the fixed SSCCE:

package core;

import java.awt.BorderLayout; import java.awt.Dimension; import java.io.FileNotFoundException; import java.io.IOException;

import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.text.AbstractDocument; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.DocumentFilter;

class DefaultDocFilter extends DocumentFilter {
    public void insertString(FilterBypass fb, int offs,String str, AttributeSet a) throws BadLocationException 
    {
        if ((fb.getDocument().getLength() + str.length()) <= 2000)
        {
            str = str.replaceAll("\n", " ");
            str = str.replaceAll("\t", " ");
            fb.insertString(offs, str, a);
        }
        else
        {
            int spaceLeft = 2000 - fb.getDocument().getLength();
            if (spaceLeft <= 0)
                return;

            str = str.substring(0, spaceLeft);
            str = str.replaceAll("\n", " ");
            str = str.replaceAll("\t", " ");

            fb.insertString(offs, str, a);
        }
    }

    public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a) throws BadLocationException 
    {
        if (str.equals("\n") || str.equals("\t"))
        { 
            str = "";
        }
        if ((fb.getDocument().getLength() + str.length() - length) <= 2000)
        {
            str = str.replaceAll("\n", " ");
            str = str.replaceAll("\t", " ");
            fb.replace(offs, length, str, a);
        }
        else
        {
            int spaceLeft = 2000 - fb.getDocument().getLength() + length;
            if (spaceLeft <= 0)
                return;

            fb.replace(offs, length, str.substring(0,spaceLeft).replaceAll("\n", " "), a);
        }
    } }


public class Main {
    public static JFrame mWindow;

    public static void main(String[] args) throws FileNotFoundException, IOException
    {   
        //create main window
        mWindow = new JFrame("title");
        mWindow.setSize(1000, 800);
        mWindow.setMinimumSize(new Dimension(1000, 800));
        mWindow.setLocationRelativeTo(null);
        mWindow.setLayout(new BorderLayout());
        mWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextArea a = new JTextArea();
        AbstractDocument doc = (AbstractDocument) a.getDocument();
        doc.setDocumentFilter(new DefaultDocFilter());
        a.setLineWrap(true);
        a.setWrapStyleWord(true);

        mWindow.add(a);
        mWindow.pack();

        mWindow.setVisible(true);

        mWindow.repaint();
        mWindow.validate();
    } }

It's Java 1.7. Create a new project, package core, file Main.

Document filter is the first class and it's applied to the JTextArea you'll see. Everything you need is within that class.

EDIT: I fixed the SSCCE. Also, the problem only occurs when you try to paste more character that can fit the JTextArea (I set limit to 2000). Then tas and newlines wont get filtered out.


回答1:


In the replace method, in the else part of the method, you only replace "\n" but not "\t"



来源:https://stackoverflow.com/questions/14526591/tabs-newlines-documentfilters-and-eclipse-text

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