/*
 * DocumentEmitter.java  $Revision: 1.2 $  $Date: 2007/10/03 04:39:39 $
 *
 * Copyright (C) 2007 Meldcraft Corporation
 *
 * This file is part of Scaffold.
 * 
 * Scaffold is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Scaffold is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Scaffold.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.meldcraft.application.guis.mvc;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

import com.meldcraft.application.ApplicationContext;

/**
 * @version $Revision: 1.2 $ $Date: 2007/10/03 04:39:39 $
 * @author Rob Richards
 */
public class DocumentEmitter implements DocumentListener
{
    protected   ApplicationContext  mContext;
    protected   String              mElementId;
    protected   Document            mDocument;

    public DocumentEmitter(ApplicationContext context, String elementId, Document document)
    {
        mContext = context;
        mElementId = elementId;
        mDocument = document;

        mDocument.addDocumentListener(this);
    }

    public void insertUpdate(DocumentEvent e)
    {
        emitChange();
    }

    public void removeUpdate(DocumentEvent e)
    {
        emitChange();
    }

    public void changedUpdate(DocumentEvent e)
    {
        emitChange();
    }

    private void emitChange()
    {
        try
        {
            mContext.contextChanged(mElementId + ".documentChanged", mDocument.getText(0, mDocument.getLength()));
        }
        catch (BadLocationException e)
        {
            // ignored
        }
    }
}



