在相应的字符串中将插入位置与文本同步

时间:2013-11-19 20:59:17

标签: java html swing jtextpane caret

我正在向JTextPane添加一些基于html的格式化功能。想法是用户将在JTextPane中选择文本,然后单击按钮(粗体,斜体等)以在适当的位置插入html标记。我可以毫不费力地使用JTextPane.getSelectionStart()和.getSelectionEnd()方法。

我的问题是我还想扫描JTextPane中的每个字符以索引所有html标记位置 - 这样软件就可以检测到JTextPane插入符与html标记相关的位置。如果用户想要删除格式化标签,则使用此信息。 我在将此字符索引与JTextPane中的插入符位置同步时遇到困难。这是我一直在使用的代码:

    public void scanHTML(){
    try {
        boolean blnDocStartFlag = false;
        alTagRecords = new ArrayList(25);
        alTextOnlyIndex = new ArrayList();
        String strTagBuild = "";

        int intTagIndex = 0; // The index for a tag pair record in alTagRecords.
        int intTextOnlyCount = 0; // Counts each text character, ignoring all html tags.

        // Loop through HTMLDoc character array:
        for (int i = 0; i <= strHTMLDoc.length() -1; i ++){
            // Look for the "<" angle bracket enclosing the tag keyword ...
            if (strHTMLDoc.charAt(i) == '<'){// It is a html tag ...
                int intTagStartLocation = i; // this value will go into alTagFields(?,0) later ...
                while (strHTMLDoc.charAt(i) != '>'){
                    strTagBuild += strHTMLDoc.charAt(i);
                    i ++; // continue incrementing the iterator whilst in this sub loop ...
                } 

                strTagBuild += '>'; // makes sure the closing tag is not missed from the string

                if (!strTagBuild.startsWith("</")){
                    // Create new tag record:
                    ArrayList<Integer> alTagFields = new ArrayList(3);
                    alTagFields.add(0, intTagStartLocation); // Tag start location index ...
                    alTagFields.add(1, -1); // Tag end not known at this stage ...
                    alTagFields.add(2, getTagType(strTagBuild));
                    alTagRecords.add(intTagIndex, alTagFields); // Tag Type
                    System.out.println("Tag: " + strTagBuild);
                    intTagIndex ++; // Increment the tag records index ...
                } else { // find corresponding start tag and store its location in the appropriate field of alTagFields:
                    int intManipulatedTagIndex = getMyOpeningTag(getTagType(strTagBuild));
                    ArrayList<Integer> alManipulateTagFields = alTagRecords.get(intManipulatedTagIndex);
                    alManipulateTagFields.set(1, (intTagStartLocation + strTagBuild.length() -1) ); // store the position of the end angled bracket of the closing tag ...
                    alTagRecords.set(intManipulatedTagIndex, alManipulateTagFields);
                    System.out.println("Tag: " + strTagBuild);

                }
                strTagBuild = "";
            } else { 
                // Create the text index:
                if (blnDocStartFlag == false){ 
                    int intAscii = (int) strHTMLDoc.charAt(i);
                    if (intAscii >= 33){ // Ascii character 33 is an exclamation mark(!).  It is the first character after a space.
                        blnDocStartFlag = true;
                    }     
                }

                // Has the first non space text character has been reached? ...
                if (blnDocStartFlag == true){ // Index the character if it has ...
                    alTextOnlyIndex.add(i);
                    intTextOnlyCount ++;
                }

            }

        }

    } catch (Exception ex){
        System.err.println("Error at HTMLTagIndexer.scanHTML: " + ex);
    }

} 

上面代码的问题是字符串变量 strHTMLDoc 是使用 JTextPane.getText 获得的,这似乎在字符串中插入了一些额外的空格字符。因此,这使其与文本窗格中的相应插入符号位置不同步。

有人可以提出另一种方法来实现我想要实现的目标吗?

非常感谢

0 个答案:

没有答案