Example usage for javax.swing.text DefaultStyledDocument getCharacterElement

List of usage examples for javax.swing.text DefaultStyledDocument getCharacterElement

Introduction

In this page you can find the example usage for javax.swing.text DefaultStyledDocument getCharacterElement.

Prototype

public Element getCharacterElement(int pos) 

Source Link

Document

Gets a character element based on a position.

Usage

From source file:edu.ku.brc.af.ui.forms.formatters.DataObjFieldFormatSinglePanel.java

/**
 * @param formatter//from   w w  w .  j av a  2s.  c  om
 */
protected void setFormatterFromTextPane(final DataObjSwitchFormatter formatter) {
    // visit every character in the document text looking for fields
    // store characters not associated with components (jbutton) to make up the separator text
    DefaultStyledDocument doc = (DefaultStyledDocument) formatEditor.getStyledDocument();
    String text = formatEditor.getText();
    int docLen = doc.getLength();
    int lastFieldPos = 0;

    Vector<DataObjDataField> fields = new Vector<DataObjDataField>();
    //int cnt = 0;
    for (int i = 0; i < docLen; ++i) {
        Element element = doc.getCharacterElement(i);
        AttributeSet attrs = element.getAttributes();
        Object obj = attrs.getAttribute(StyleConstants.ComponentAttribute);
        //System.out.print(String.format("i: %d, lastFieldPos: %d cnt: %d, F: %s", i, lastFieldPos, cnt, (obj instanceof FieldDefinitionComp ? "Y" : "N")));
        if (obj instanceof FieldDefinitionComp) {
            //System.out.println(cnt+"  "+(obj instanceof FieldDefinitionComp));
            // found button at the current position
            // create corresponding field
            String sepStr = (lastFieldPos <= i - 1) ? text.substring(lastFieldPos, i) : "";

            FieldDefinitionComp fieldDefBtn = (FieldDefinitionComp) obj;
            DataObjDataField fmtField = fieldDefBtn.getValue();
            fmtField.setSep(sepStr);
            fields.add(fmtField);

            //System.out.print(" Sep: ["+sepStr+"]");

            lastFieldPos = i + 1;
            //cnt++;
        }
    }

    // XXX: what do we do with the remaining of the text? right now we ignore it
    // That's because we can't create an empty formatter field just to use the separator... 

    DataObjDataField[] fieldsArray = new DataObjDataField[fields.size()];
    for (int i = 0; i < fields.size(); ++i) {
        fieldsArray[i] = fields.elementAt(i);
    }

    DataObjDataFieldFormat singleFormatter = fieldsArray.length == 0 ? null
            : new DataObjDataFieldFormat("", tableInfo.getClassObj(), false, "", "", fieldsArray);
    formatter.setSingle(singleFormatter);
}