制表符,换行符,DocumentFilters和蚀文本 [英] Tabs, newlines, DocumentFilters and eclipse text

查看:94
本文介绍了制表符,换行符,DocumentFilters和蚀文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我还有一个关于这个问题的问题. >

一旦应用了此修复程序,请转到记事本,使用一些随机字符键入一堆选项卡和换行符,然后将它们粘贴到我的程序中,一切都将变得柔和.

但是,由于是带有一堆标签和换行符的最接近的文本,我尝试将代码本身的一部分粘贴到JTextArea.所有选项卡和换行符都停留在那里,没有被过滤掉.

尽管我的用户可能不会将eclipse代码粘贴到我的程序中,但是我不能确定eclipse代码是唯一的例外.所以我想知道为什么会这样.

此外,我想为我的代码过滤掉空格字符以外的空白字符,并将其转换为空格字符.我认为tab和newline是唯一的,但是如果还有更多,请告诉我.

无论如何,要使其正常运行,我必须进行哪些更改?

这是固定的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();
    } }

它是Java 1.7.创建一个新项目,包核心,文件Main.

文档过滤器是第一类,它应用于您将看到的JTextArea.您需要的一切都在该课程之内.

我修复了SSCCE.此外,仅当您尝试粘贴更多适合JTextArea的字符时才出现此问题(我将限制设置为2000).然后,tas和换行符将不会被过滤掉.

解决方案

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

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.

解决方案

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

这篇关于制表符,换行符,DocumentFilters和蚀文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆