0

我有一个奇怪的问题(至少看起来是这样),当在 textPane 中搜索字符串时,我会为使用 StyledDoc 时搜索并返回的每个行号获得一个额外的索引,而不是从 textPane 中获取文本。我从同一个窗格中得到相同的文本,只是一个来自纯文本,另一个来自样式化的文档。我在这里错过了什么吗?我将尝试列出我正在使用的两个版本之间的尽可能多的更改。

纯文本版本:

public int displayXMLFile(String path, int target){
    InputStreamReader inputStream;
    FileInputStream fileStream;
    BufferedReader buffReader;

    if(target == 1){

        try{                
            File file = new File(path);
            fileStream = new FileInputStream(file);
            inputStream = new InputStreamReader(fileStream,"UTF-8");
            buffReader = new BufferedReader(inputStream);
            StringBuffer content = new StringBuffer("");
            String line = "";
            while((line = buffReader.readLine())!=null){
                content.append(line+"\n");
            }
            buffReader.close();
            xhw.txtDisplay_1.setText(content.toString());
        }
        catch(Exception e){
            e.printStackTrace();
            return -1;
        }
    }
}

与 Styled Doc 对比(未应用样式)

    protected void openFile(String path, StyledDocument sDoc, int target) 
                throws BadLocationException {

    FileInputStream fileStream;
    String file;
    if(target == 1){
        file = "Openning First File";
    } else {
        file = "Openning Second File";
    }


    try {
        fileStream = new FileInputStream(path);

        // Get the object of DataInputStream
        //DataInputStream in = new DataInputStream(fileStream);

        ProgressMonitorInputStream in = new ProgressMonitorInputStream(
                xw.getContentPane(), file, fileStream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;

        //Read File Line By Line
        while ((strLine = br.readLine()) != null)   {                   
            sDoc.insertString(sDoc.getLength(), strLine + "\n", sDoc.getStyle("regular"));
        xw.updateProgress(target);
        } 

        //Close the input stream
        in.close();
    } catch (Exception e){//Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }

这就是我搜索的方式:

    public int searchText(int sPos, int target) throws BadLocationException{

    String search = xhw.textSearch.getText();
    String contents;
    JTextPane searchPane;

    if(target == 1){
        searchPane = xhw.txtDisplay_1;              
    } else {
        searchPane = xhw.txtDisplay_2;
    }

    if(xhw.textSearch.getText().isEmpty()){
        xhw.displayDialog("Nothing to search for");
        highlight(searchPane, null, 0,0);
    } else {


        contents = searchPane.getText();

        // Search for the desired string starting at cursor position
        int newPos = contents.indexOf( search, sPos );

        // cycle cursor to beginning of doc window
        if (newPos == -1 && sPos > 0){
            sPos = 0;
            newPos = contents.indexOf( search, sPos );
        } 

        if ( newPos >= 0 ) {
            // Select occurrence if found

            highlight(searchPane, contents, newPos, target);

            sPos = newPos + search.length()+1;
        } else {
            xhw.displayDialog("\"" + search + "\"" + " was not found in File " + target);
        }
    } 
    return sPos;
}

示例文件:

<?xml version="1.0" encoding="UTF-8"?>
<AlternateDepartureRoutes>
  <AlternateDepartureRoute>
    <AdrName>BOIRR</AdrName>
    <AdrRouteAlpha>..BROPH..</AdrRouteAlpha>
    <TransitionFix>
      <FixName>BROPH</FixName>
    </TransitionFix>
  </AlternateDepartureRoute>
  <AlternateDepartureRoute>
</AlternateDepartureRoutes>

还有我的荧光笔:

    public void highlight(JTextPane tPane, String text, int position, int target) throws BadLocationException {
    Highlighter highlighter =  new DefaultHighlighter();
    Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.LIGHT_GRAY);
    tPane.setHighlighter(highlighter);

    String searchText = xhw.textSearch.getText();
    String document = tPane.getText();
    int startOfSString = document.indexOf(searchText,position);

    if(startOfSString >= 0){
        int endOfSString = startOfSString + searchText.length();
        highlighter.addHighlight(startOfSString, endOfSString, painter);
        tPane.setCaretPosition(endOfSString);
        int caretPos = tPane.getCaretPosition();
        javax.swing.text.Element root = tPane.getDocument().getDefaultRootElement();
        int lineNum = root.getElementIndex(caretPos) +1;
        if (target == 1){
            xhw.txtLineNum1.setText(Integer.toString(lineNum));
        } else if (target == 2){
            xhw.txtLineNum2.setText(Integer.toString(lineNum));
        } else {
            xhw.txtLineNum1.setText(null);
            xhw.txtLineNum2.setText(null);
        }

    } else {
        highlighter.removeAllHighlights();
    }

}

当我使用 indexof() 搜索 Alt 时,我得到 40 的纯文本(这是它应该返回的)和 41 搜索样式文档时。对于 Alt 出现的每一行,我都会得到额外的索引(以便 indexof() 调用返回 2 比第 3 行中需要的多)。这发生在它找到的每一行。我错过了一些明显的东西吗?(如果我需要将其推送到较小的单个班级以便更容易检查,我可以稍后在我有更多时间时执行此操作)。

提前致谢...

4

2 回答 2

1

如果您使用的是 Windows,则 TextComponent 文本 (searchPane.getText()) 可以包含回车+换行符 (\r\n),但 TextComponent 的样式化文档 (sSearchPane.getText(0, sSearchPane.getLength()) ) 仅包含换行符 (\n)。这就是为什么你的 newPos 总是大于 newPosS 的换行数。要解决此问题,您可以在搜索功能中更改:

contents = searchPane.getText();

至:

contents = searchPane.getText().replaceAll("\r\n","\n");

这样,搜索就会使用样式化文档使用的相同索引进行。

于 2012-08-21T19:40:22.963 回答
0

好的,我找到了解决方案(基本上)。我从以两种不同方式从同一文本组件获取文本的方面来处理这个问题......

    String search = xw.textSearch.getText();
    String contents;
    String contentsS;
    JTextPane searchPane;
    StyledDocument sSearchPane;

    searchPane = xw.txtDisplay_left;
    sSearchPane = xw.txtDisplay_left.getStyledDocument();


    contents = searchPane.getText();
    contentsS = sSearchPane.getText(0, sSearchPane.getLength());

    // Search for the desired string starting at cursor position
    int newPos = contents.indexOf( search, sPos );
    int newPosS = contentsS.indexOf(search, sPos);

因此,当比较“newPos”和“newPosS”这两个变量时,newPos 为找到搜索字符串的每一行返回 1 比 newPosS 多。因此,当查看示例文件并搜索“Alt”时,第一个实例位于第 2 行。“newPos”返回 41,“newPosS”返回 40(然后突出显示正确的文本)。下一次出现(在第 2 行中找到) 3)“newPos”返回 71,“newPosS”返回 69。如您所见,每一个新行都会通过出现开始的行号来增加计数。我怀疑每个新行都会添加一个额外的字符来自 StyledDoc 中不存在的 textPane。

我确信有一个合理的解释,但我现在没有。

于 2012-07-15T16:47:48.510 回答