2012-07-15 66 views
0

我有一個奇怪的問題(至少它看起來這樣),當在textPane中搜索一個字符串,我得到一個額外的索引每個行號搜索和當使用StyledDoc經文只是從textPane獲取文本時返回。我從同一個窗格中獲取相同的文本,只是一個來自純文本,另一個來自樣式文檔。我在這裏錯過了什麼。我將盡力列出我正在使用的兩個版本之間的許多變化。StyledDocument增加額外的計數indexof文件的每一行

的純文本版本:

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; 
     } 
    } 
} 

詩句風格化文件(沒有應用的樣式)

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(); 
    } 

} 

當我做Alt鍵搜索用的indexOf()我得到40的純文本(這是它應該返回什麼)和41與風格的文檔搜索時。對於Alt出現在我得到的每一個額外的行以及額外的索引(以便indexof()調用在第3行中返回2以上)。這發生在找到的每一條附加線上。我錯過了明顯的東西嗎? (如果我需要將它推送到一個較小的單個類,以便於檢查,那麼稍後我可以做更多的時間)。

在此先感謝...

回答

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 retruned 1更多的則newPosS用於搜索字符串的每一行。因此,當查看示例文件並搜索「Alt」時,第一個實例在第2行中找到。「newPos」返回41,「newPosS返回40(然後突出顯示正確的文本)。下一個發生(在行中找到3)「newPos」返回71,「newPosS」返回69.正如你所看到的,每一條新線都會增加計數的起始位置。我會懷疑每個新行都會增加一個額外的字符從

是不存在的StyledDoc的textPane。我敢肯定有一個合理的解釋,但我沒有它在這個時候。

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"); 

搜索與該樣式化文檔使用相同的指標出現這樣。