Visual Tagging Tool

DefaultStyledDocument

Java Swing class JTextPane is used for the core operation of VTT.

  • Scrollable:
    JTextPane is added to JScollPane for scrollability
    The hierarchy of GUI objects is:
    • MainFrame (JFrame)
      • MainPanel (JPanel)
        • scrollPane (JScrollPane)
          • textPane_ (JTextPane)
            • setEditable(false)
            • setCaretPosition(int)

            • setSelectionStart(int)
            • setSelectionEnd(int)
            • setSelectedTextColor(color)
            • setSelectionColor(color)

            • print()

            • (DefaultStyledDocument) doc (StyledDocument)
              • set style change in doc
  • Controller (listeners):
    MouseListener and CaretListener are added in JTextPane for mouse control and caret control.
  • StyledDocument
    JTextPane.getStyledDocument( ) is used for core operations of changing color, style, size, font, etc.. The sample code is implemented in VttDocument.java as follows:

    // get doc
    DefaultStyledDocument doc
    	= (DefaultStyledDocument) JTextPane.getStyledDocument( );
    
    // set SimpleAttibuteSet by StyleConstants
    SimpleAttributeSet sas = new SimpleAttributeSet();
    StyleConstants.setBold(sas, tag.IsBold());
    StyleConstants.setItalic(sas, tag.IsItalic());
    StyleConstants.setUnderline(sas, tag.IsUnderline());
    
    StyleConstants.setForeground(sas, TEXT_COLOR);
    StyleConstants.setBackground(sas, TEXT_BG_COLOR);
    
    StyleConstants.setFontFamily(sas, tag.GetFontFamily());
    StyleConstants.setFontSize(sas, tag.GetFontSize());
    
    // init/load doc
    doc.remove(0, doc.getLength());
    doc.insertString(doc.getLength(), tagDocument.GetText(), sas);
    
    // set style to doc
    doc.setCharacterAttributes(offset, length, sas, false);