Package net.helipilot50.stocktrade.displayproject

Source Code of net.helipilot50.stocktrade.displayproject.MappedDocument

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package net.helipilot50.stocktrade.displayproject;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Hashtable;

import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

import net.helipilot50.stocktrade.displayproject.events.ChildEventHelper;
import net.helipilot50.stocktrade.displayproject.events.ClientEventManager;
import net.helipilot50.stocktrade.framework.DataFormat;
import net.helipilot50.stocktrade.framework.EventManager;
import net.helipilot50.stocktrade.framework.ForteKeyboardFocusManager;
import net.helipilot50.stocktrade.framework.ParameterHolder;

import org.apache.log4j.Logger;


@SuppressWarnings("serial")
public abstract class MappedDocument extends PlainDocument implements PropertyChangeListener, DocumentListener, FocusListener {
    protected boolean internalChange = false;
    protected boolean HasDataChanged = false;

    protected DataFormat Format = null;

    protected JTextArea ta = null;
    protected String possibleResult = "";

    public MappedDocument(JTextArea ta){
        super();
        this.ta = ta;
        this.ta.addFocusListener(this);
        this.ta.addKeyListener(new KeyAdapter(){
            protected boolean firstKey = true;
            public void keyTyped(KeyEvent ev){
                if (this.firstKey){
                    this.firstKey = false;
                    ClientEventManager.postEvent(ev.getComponent(), "AfterFirstKeystroke", null);
                    // TF:27/9/07:Changed this to use the new Event structure.
                    ChildEventHelper.postEventToAllParents(ev.getComponent(), "ChildAfterFirstKeystroke");
                }
            }
        });
        this.addDocumentListener(this);

    }

    public abstract String fromData();

    public void toData(){
    }

    /**
     *
     * @uml.property name="hasDataChanged"
     */
    public boolean getHasDataChanged() {
        return this.HasDataChanged;
    }

    public DataFormat getFormat() {
        return null;
    }

    public boolean hasFormat() {
        return false;
    }
    public void insertString(int offs, String str, AttributeSet attr) {
        try {
            String current = getText(0, getLength());
            String before = current.substring(0, offs);
            String after = current.substring(offs, current.length());
            possibleResult = before + str + after;
            super.insertString(offs, str, attr);
            this.HasDataChanged = true;
        }
        catch (BadLocationException e) {
            Logger.getLogger("task.part.logmgr").error("Bad location exception", e);
        }
    }
    public void remove(int offs, int len) {
        try {
            String current = getText(0, getLength());
            String before = current.substring(0, offs);
            String after = current.substring(len + offs, current.length());
            possibleResult = before + after;
            super.remove(offs, len);
            this.HasDataChanged = true;
        }
        catch (BadLocationException e) {
            Logger.getLogger("task.part.logmgr").error("Bad Location exception", e);
        }
    }

    public void propertyChange(PropertyChangeEvent arg0) {
        fireChange();

    }

    // ==== ValueChangeListener ====
    public void fireChange() {
//        final String s = fromData();
//        if (internalChange) { return; }
//        UIutils.invokeOnGuiThread(new Runnable() {
//                public void run(){
//                    remove(0, getLength());
//                    insertString(0, s, new SimpleAttributeSet());
//                }
//            });
    }

    // ==== DocumentListener ====
    public void changedUpdate(DocumentEvent e) {
        this.HasDataChanged = true;

    }
    public void insertUpdate(DocumentEvent e) {
        this.HasDataChanged = true;

    }
    public void removeUpdate(DocumentEvent e) {
        this.HasDataChanged = true;

    }

    // ==== FocusListener ====
    public void focusGained(FocusEvent e) {
        EventManager.startEventChain();
        int reason = ForteKeyboardFocusManager.getTraversalReason();
        if (reason != Constants.FC_SUPRESS) {
            Hashtable<String, Object> params = new Hashtable<String, Object>();
            params.put("reason", new ParameterHolder(reason));
            ClientEventManager.postEvent( ta, "AfterFocusGain", params );
        }
        EventManager.endEventChain();
        this.HasDataChanged = false;
    }
    public void focusLost(FocusEvent e) {
        EventManager.startEventChain();
        int reason = ForteKeyboardFocusManager.getTraversalReason();
        if (reason != Constants.FC_SUPRESS) {
            Hashtable<String, Object> params = new Hashtable<String, Object>();
            params.put("reason", new ParameterHolder(reason));
            ClientEventManager.postEvent( ta, "BeforeFocusLoss", params );
        }
//      The following is replaced by component.setVerifyInputWhenFocusTarget
//        if (e.getOppositeComponent() instanceof JComponent){
//            JComponent comp = (JComponent)e.getOppositeComponent();
//            Boolean isFinalized = (Boolean)comp.getClientProperty("isInputFinalized");
//            if ((isFinalized != null) &&
//                (!isFinalized.booleanValue())) return;
//        }
        if (this.HasDataChanged){
            internalChange = true;
            toData();
            internalChange = false;
            ClientEventManager.postEvent( ta, "AfterValueChange" );
            UIutils.setDataChangedFlag(ta);
            // TF:27/9/07:Changed to use new event poster
            ChildEventHelper.postEventToAllParents(ta, "ChildAfterValueChange");
        }
        EventManager.endEventChain();
    }
}
TOP

Related Classes of net.helipilot50.stocktrade.displayproject.MappedDocument

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.