Package DisplayProject.actions

Source Code of DisplayProject.actions.Recorder

/*
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 DisplayProject.actions;

import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JComponent;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.JTextComponent;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;

/**
* Gets and sets the undo and redo capability of text fields
*
*/
public class Recorder extends PendingAction {

    private boolean value;
    public Recorder(Component pComponent, boolean pValue) {
        super(pComponent);
        this.value = pValue;
    }
    public boolean getValue() {
        return this.value;
    }
    public void performAction() {
        if (this._component instanceof JTextComponent){
            EditListener el = new EditListener();
            ((JTextComponent)this._component).getDocument().addUndoableEditListener(el);
            ((JTextComponent)this._component).putClientProperty("qq_HasRecorder", el);
        }
    }
    public static void set(Component comp, boolean value){
        ActionMgr.addAction(new Recorder(comp, value));
    }

    public static boolean has(JComponent comp){
        EditListener value = null;
        Recorder action = ActionMgr.getAction(comp, Recorder.class);
        if (action != null) {
            return action.getValue();
        }
        if (comp instanceof JTextComponent){
            value = (EditListener)((JTextComponent)comp).getClientProperty("qq_HasRecorder");
            if (value == null)
                return false;
            else
                return true;
        }
        return false;
    }
    public static void undo(Component comp){
        if (comp instanceof JTextComponent){
            EditListener value = (EditListener)((JTextComponent)comp).getClientProperty("qq_HasRecorder");
            if (value != null)
                value.undo.undo();
        }
    }
    public static void redo(Component comp){
        if (comp instanceof JTextComponent){
            EditListener value = (EditListener)((JTextComponent)comp).getClientProperty("qq_HasRecorder");
            if (value != null)
                value.undo.redo();
        }

    }
    private class EditListener implements UndoableEditListener, KeyListener{
        protected UndoManager undo;
        public EditListener(){
            undo = new UndoManager();
        }
        public void undoableEditHappened(UndoableEditEvent e) {
            undo.addEdit(e.getEdit());
        }
        public void keyTyped(KeyEvent e) {
        }
        public void keyPressed(KeyEvent e) {
//      identify the key pressed is the combination of
            //Controlkey and Z key
            if((e.getKeyCode() == KeyEvent.VK_Z) && (e.isControlDown()))
            {                
                try
                {
                    //Undo changes
                    undo.undo();
                }
                catch(CannotUndoException cue)
                {
                    Toolkit.getDefaultToolkit().beep();
                }
            }

            //identify the key pressed is the combination of
            //Controlkey and Y key
            if((e.getKeyCode() == KeyEvent.VK_Y) && (e.isControlDown()))
            {                
                try
                {
                    //Redo changes
                    undo.redo();
                }
                catch(CannotRedoException cue)
                {
                    Toolkit.getDefaultToolkit().beep();
                }
            }

        }
        public void keyReleased(KeyEvent e) {
        }
    }
}
TOP

Related Classes of DisplayProject.actions.Recorder

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.