alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.openide.text;

import org.openide.util.WeakListeners;
import javax.swing.text.*;

/** Listener to changes in the document.
*
* @author Jaroslav Tulach
*/
final class LineListener extends Object
    implements javax.swing.event.DocumentListener {
    /** original count of lines */
    private int orig;
    /** document to work with */
    public final StyledDocument doc;
    /** root element of all lines */
    private Element root;
    /** last tested amount of lines */
    private int lines;
    /** operations on lines */
    private LineStruct struct;

    
    /** Support necessary for getting Set of lines*/
    CloneableEditorSupport support;
    
    /** Creates new LineListener */
    public LineListener (StyledDocument doc, CloneableEditorSupport support) {
        this.doc = doc;
        this.struct = new LineStruct ();
        root = NbDocument.findLineRootElement (doc);
        orig = lines = root.getElementCount ();
        this.support = support;
        
        doc.addDocumentListener(org.openide.util.WeakListeners.document (this, doc));
    }

    /** Getter for amount of lines */
    public int getOriginalLineCount () {
        return orig;
    }

    /** Convertor between old and new line sets */
    public int getLine (int i) {
        return struct.convert (i, true/*originalToCurrent*/);
    }
    /** Convertor between old and new line sets */
    public int getOld (int i) {
        return struct.convert (i, false/*currentToOriginal*/);
    }

    public void removeUpdate(javax.swing.event.DocumentEvent p0) {
        int elem = root.getElementCount ();
        int delta = lines - elem;
        lines = elem;

        int lineNumber = NbDocument.findLineNumber (doc, p0.getOffset ());
        
        if (delta > 0) {
            struct.deleteLines (
                lineNumber,
                delta
            );
        }        
        if ( support == null)
            return;
        Line.Set set = support.getLineSet ();
        if (!(set instanceof DocumentLine.Set))
            return;

        // Notify lineSet there was changed range of lines.
        ((DocumentLine.Set)set).linesChanged(lineNumber, lineNumber+delta, p0);
        
        if (delta > 0) {
            // Notify Line.Set there was moved range of lines.
            ((DocumentLine.Set)set).linesMoved(lineNumber, elem);
        }
    }

    public void changedUpdate(javax.swing.event.DocumentEvent p0) {
    }

    public void insertUpdate(javax.swing.event.DocumentEvent p0) {
        int elem = root.getElementCount ();
        
        int delta = elem - lines;
        lines = elem;

        int lineNumber = NbDocument.findLineNumber (doc, p0.getOffset ());
        
        if (delta > 0) {
            struct.insertLines (
                lineNumber,
                delta
            );
        }
        
        if ( support == null)
            return;
        Line.Set set = support.getLineSet ();
        if (!(set instanceof DocumentLine.Set))
            return;
        
        // Nptify Line.Set there was changed range of lines.
        ((DocumentLine.Set)set).linesChanged(lineNumber, lineNumber, p0);
        
        if (delta > 0) {
            // Notify Line.Set there was moved range of lines.
            ((DocumentLine.Set)set).linesMoved(lineNumber, elem);
        }
    }
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.