Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#5 Auto Indent

open
nobody
None
5
2003-10-01
2003-10-01
Anonymous
No

Auto indent the next line based on the current level of
indent.

markolive@mohc.co.uk

Discussion

  • Logged In: NO

    Add this to methods to the SyntaxDocument and it will work.
    Maybe not the best way to do, but it works:

    public void insertString(int offset, String str,
    AttributeSet a)
    throws BadLocationException {
    if(str.equals("\n")) {
    str = this.addWhiteSpace(offset);
    } else if(str.equals("\t")) {
    str = " ";
    }

    super.insertString(offset, str, a);
    }

    protected String addWhiteSpace(int offset)
    throws BadLocationException {
    Element root = this.getDefaultRootElement();
    StringBuffer whiteSpace = new StringBuffer();
    int line = root.getElementIndex(offset);
    int i = root.getElement(line).getStartOffset();
    while(true) {
    String temp = this.getText(i, 1);
    if(temp.equals(" ") || temp.equals("\t")) {
    whiteSpace.append(temp);
    i++;
    } else {
    break;
    }
    }
    return "\n"+whiteSpace.toString();
    }