2016-08-17 50 views
2

早上好親愛的所有人, 我有一個內容類型爲'text/html'的JTextPane。在這個JTextPane中,我加載了聖經,其中聖經的標籤被分隔開。我想要做的是在用戶點擊文檔的一部分時確定選定的詩集。 從現在起,我可以通過方法getSelectedText()獲取選定的文本。但結果不包含原始文檔的標籤,它可以幫助我檢測所選文本所包含的詩集的開始或結尾。獲取JTextPane中選定文本的標籤

我無法進行簡單的搜索,因爲方法getSelectedText()返回一個沒有標記的文本。例如

Abraham eut pour descendant <g v=\"Isaac\">Isaac</g>. 

將會是「Abraham eut pour descendant Isaac」。 所以,如果我做簡單的搜索,我什麼也找不到。 總之,我想要的是知道如何獲取選定文本的標籤,以便我可以確定所選文本的版本。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.text.*; 
import javax.swing.text.html.HTMLDocument; 
import javax.swing.text.html.HTMLEditorKit; 

public class VersetBibliques { 

    Rectangle[] rects = new Rectangle[2]; 
    HTMLEditorKit kit; 
    HTMLDocument htmlDoc;  

    private JPanel getContent() { 
     rects[0] = new Rectangle(); 
     rects[1] = new Rectangle(); 
     JPanel panel = new JPanel(new GridLayout(1, 0, 10, 0)); 
     panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 
     panel.add(new JScrollPane(getTextPane())); 
     return panel; 
    } 

    private JTextPane getTextPane() { 
     JTextPane textPane = new JTextPane(); 
     kit = new HTMLEditorKit(); 
     htmlDoc = new HTMLDocument(); 
     textPane.setEditable(false); 

     textPane.setEditorKit(kit); 
     textPane.setDocument(htmlDoc); 
     textPane.setContentType("text/html"); 
     textPane.setBackground(Color.WHITE); 
     textPane.setFont(new Font("Arial Bold", Font.ITALIC, 38)); 
     try { 
      kit.insertHTML(htmlDoc, 0, text, 0, 0, null);     
     } catch (BadLocationException e) { 
      System.out.println("bad location: " + e.getMessage()); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     textPane.addMouseListener(selector); 
     Style s = htmlDoc.addStyle("logical", null); 
     StyleConstants.setFontFamily(s, "georgia"); 
     StyleConstants.setFontSize(s, 58); 
     s = htmlDoc.addStyle("lineSpace", null); 
     StyleConstants.setLineSpacing(s, 0.25f); 
     htmlDoc.setLogicalStyle(0, htmlDoc.getStyle("logical")); 
     return textPane; 
    } 

    public static void main(String[] args) { 
     VersetBibliques test = new VersetBibliques(); 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.add(test.getContent()); 
     f.setSize(600, 400); 
     f.setLocation(200, 200); 
     f.setVisible(true); 
    } 

    private MouseListener selector = new MouseAdapter() { 
     public void mousePressed(MouseEvent e) { 
      try { 
       JTextPane textPane = (JTextPane) e.getComponent(); 
       int start = textPane.getSelectionStart() - 10; 
       int end = textPane.getSelectionEnd() + 10; 

       textPane.setSelectionEnd(end); 
       textPane.setSelectionStart(start); 

       String selectedText = textPane.getSelectedText(); 
       System.out.println("text content : " + selectedText);//Il faudra faire le test avec Jean 11:35 
       System.out.println("start: "+start+"- end:"+end); 


      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } 
     } 
    }; 

    String text = "<mt>Évangile selon Matthieu</mt>\n" 
      + "<c><cn>1</cn>\n" 
      + "<mt>Naissance et enfance de Jésus</mt><s>La généalogie de Jésus</s><r>(<ref v=\"LUK3.23-38\">Lc 3.23-38</ref>)</r><v><vn>1</vn>Voici la généalogie de Jésus-Christ, de la descendance de <g v=\"David\">David</g> et d'<g v=\"Abraham\">Abraham</g>. </v>" 
      + "<p/><v><q><vn>2 </vn>Abraham eut pour descendant <g v=\"Isaac\">Isaac</g>.<br/>Isaac eut pour descendant <g v=\"Jacob\">Jacob</g>.<br/>Jacob eut pour descendant <g v=\"Juda\">Juda</g> et ses frères.</q></v>" 
      + "<v><q><vn>3 </vn>De Thamar, Juda eut pour descendant Péretz et Zérah.<br/>Péretz eut pour descendant Hetsrom.<br/>Hetsrom eut pour descendant Aram.</q></v>" 
      + "<v><q><vn>4 </vn>Aram eut pour descendant Aminadab.<br/>Aminadab eut pour descendant Nahchôn,<br/>Nahchôn eut pour descendant Salma.</q></v>" 
      + "<v><q><vn>5 </vn>De Rahab, Salma eut pour descendant Booz.<br/>De Ruth, Booz eut pour descendant Obed.</q></v></c>"; 

} 

在此先感謝

回答

0

爲了從文件中讀取HTML,你需要使用HTMLEditorKit。特別是write方法。

得到選擇區偏移,你需要使用getSelectionStartgetSelectionEnd,或者只是getCaretPosition,從JTextPane

要解決這些偏移的標記,你可以使用getCharacterElement().getStartOffset()getCharacterElement().getEndOffset()HTMLDocument

最後,這裏是一個示例向您展示如何使用這些:

package test; 

import java.awt.BorderLayout; 
import java.io.IOException; 
import java.io.StringReader; 
import java.io.StringWriter; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.JFrame; 
import javax.swing.JTextPane; 
import javax.swing.SwingUtilities; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.html.HTMLDocument; 
import javax.swing.text.html.HTMLEditorKit; 

public class Test { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 

       String text = "<p><mt>Évangile selon Matthieu</mt>\n" 
         + "<c><cn>1</cn>\n" 
         + "<mt>Naissance et enfance de Jésus</mt><s>La généalogie de Jésus</s><r>(<ref v=\"LUK3.23-38\">Lc 3.23-38</ref>)</r><v><vn>1</vn>Voici la généalogie de Jésus-Christ, de la descendance de <g v=\"David\">David</g> et d'<g v=\"Abraham\">Abraham</g>. </v>" 
         + "<p/><v><q><vn>2 </vn>Abraham eut pour descendant <g v=\"Isaac\">Isaac</g>.<br/>Isaac eut pour descendant <g v=\"Jacob\">Jacob</g>.<br/>Jacob eut pour descendant <g v=\"Juda\">Juda</g> et ses frères.</q></v>" 
         + "<v><q><vn>3 </vn>De Thamar, Juda eut pour descendant Péretz et Zérah.<br/>Péretz eut pour descendant Hetsrom.<br/>Hetsrom eut pour descendant Aram.</q></v>" 
         + "<v><q><vn>4 </vn>Aram eut pour descendant Aminadab.<br/>Aminadab eut pour descendant Nahchôn,<br/>Nahchôn eut pour descendant Salma.</q></v>" 
         + "<v><q><vn>5 </vn>De Rahab, Salma eut pour descendant Booz.<br/>De Ruth, Booz eut pour descendant Obed.</q></v></c></p>"; 

       final JFrame mainFrame = new JFrame("test"); 
       mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       final JTextPane field = new JTextPane(); 
       field.setContentType("text/html"); 

       HTMLEditorKit kit = (HTMLEditorKit) field.getEditorKit(); 
       HTMLDocument doc = (HTMLDocument) field.getDocument(); 

       try (StringReader r = new StringReader(text)) { 
        kit.read(r, field.getDocument(), 0); 
       } catch (IOException | BadLocationException ex) { 
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
       } 

       field.setCaretPosition(10); 
       field.setSelectionStart(doc.getCharacterElement(field.getCaretPosition()).getParentElement().getStartOffset()); 
       field.setSelectionEnd(doc.getCharacterElement(field.getCaretPosition()).getParentElement().getEndOffset()); 

       try (StringWriter w = new StringWriter()) { 
        kit.write(w, field.getDocument(), field.getSelectionStart(), field.getSelectionEnd()-field.getSelectionStart()); 
        System.out.println(w.toString()); 
       } catch (IOException | BadLocationException ex) { 
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
       } 

       mainFrame.getContentPane().setLayout(new BorderLayout()); 
       mainFrame.getContentPane().add(field,BorderLayout.CENTER); 
       mainFrame.setSize(500,500); 
       mainFrame.setVisible(true); 
      } 
     }); 
    } 
} 

然後,你仍然有一些問題需要解決,喜歡的事實,非HTML標記不會正確地代表JTextPane,因此您必須修復ViewFactory以創建自己的XMLViewFactory,但這是另一個主題。

+0

非常感謝。這正是我想要的。關於我自己的標記的使用,你能給我一個關於ViewFactory和XMLVievFactory管理的好鏈接。 – Doukya

+1

http://java-sl.com/xml_editor_kit.html – StanislavL

+0

一旦你的問題解決了,請選擇一個答案。這對於這個地方的工作很重要。 ;) – Sharcoux