简体   繁体   English

制表符,换行符,DocumentFilters和蚀文本

[英]Tabs, newlines, DocumentFilters and eclipse text

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. 但是,作为带有一堆标签和换行符的最接近的文本,我尝试将代码本身的一部分粘贴到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. 尽管我的用户可能不会将eclipse代码粘贴到我的程序中,但是我不确定eclipse代码是唯一的例外。 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. 我认为tab和newline是唯一的,但是如果还有更多,请告诉我。

Anyway, what do I have to change to make it work? 无论如何,要使其正常运行,我必须进行哪些更改?

Here's the fixed SSCCE: 这是固定的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. 它是Java 1.7。 Create a new project, package core, file Main. 创建一个新项目,包核心,文件Main。

Document filter is the first class and it's applied to the JTextArea you'll see. 文档过滤器是第一类,它应用于您将看到的JTextArea。 Everything you need is within that class. 您需要的一切都在该课程中。

EDIT: I fixed the SSCCE. 编辑:我修复了SSCCE。 Also, the problem only occurs when you try to paste more character that can fit the JTextArea (I set limit to 2000). 此外,仅当您尝试粘贴更多适合JTextArea的字符时才出现此问题(我将限制设置为2000)。 Then tas and newlines wont get filtered out. 然后,tas和换行符将不会被过滤掉。

replace方法的方法的else部分中,仅替换“ \\ n”,而不替换“ \\ t”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM