Package jsynoptic.plugins.java3d.panels

Source Code of jsynoptic.plugins.java3d.panels.TextField$FloatValue

/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info:  http://jsynoptic.sourceforge.net/index.html
*
* This program 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 2.1 of the License, or (at your option) any later version.
*
* This program 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 this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2007, by :
*     Corporate:
*         EADS Astrium
*     Individual:
*         Claude Cazenave
*
* $Id: TextField.java,v 1.1 2008/02/11 14:09:28 cazenave Exp $
*
* Changes
* -------
* 24 janv. 08  : Initial public release
*
*/
package jsynoptic.plugins.java3d.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.media.j3d.SceneGraphObject;
import javax.swing.JTextField;

import jsynoptic.plugins.java3d.edit.PropertyEdit;

/**
*/
public abstract class TextField <T extends SceneGraphObject, E> extends JTextField
                implements PropertyEdit.UndoRedoListener,
                PropertiesPanel.Editor<T, E> {
    static final String DEFAULT_TEXT = "";

    String _text;
   
    PropertyEdit<T, E> _editor;
   
    public TextField(){
        super(10);
        _text=DEFAULT_TEXT;
       
        addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                readText();
            }});
        addFocusListener(new FocusListener(){
            @Override
            public void focusLost(FocusEvent e) {
                readText();
            }
            @Override
            public void focusGained(FocusEvent e) {
            }});
    }
   
    protected void readText(){
        boolean changed=true;
        if(!getText().equals(DEFAULT_TEXT)){
            if(!getText().equals(_text)){
                _text=getText();
            }
            else{
                changed=false;
            }
        }
        else if(_text==null){
            // no change
            changed=false;
        }
        else{
            _text=null;
        }
        if(changed && _editor!=null){
            setEditorValue(_text);
        }
    }
   
    protected abstract void setEditorValue(String t);
   
    protected abstract String getEditorValue();
   
    @Override
    public void edit(PropertyEdit<T, E> editor){
        if(_editor!=null){
            _editor.removeListener(this);
        }
        _editor=editor;
        if(_editor!=null){
            _editor.addListener(this);
            _text = getEditorValue();
            setText(_text==null ? DEFAULT_TEXT : _text);
        }
    }

    @Override
    public void undoRedoPerfomed(boolean isUndo) {
        _text = getEditorValue();
        setText(_text==null ? DEFAULT_TEXT : _text);
    }

    public static class StringValue <T extends SceneGraphObject> extends TextField<T,String>{
        protected void setEditorValue(String t){
            _editor.setNewValue(t);
        }
       
        protected String getEditorValue(){
            return _editor.getPropertyValue();
        }
    }
   
    public static abstract class NumberValue <T extends SceneGraphObject,N extends Number> extends TextField<T,Number>{
        protected void setEditorValue(String t){
            boolean failed=false;
            if(t==null){
                failed=true;
            }
            else{
                try{
                    N v=parse(t);
                    _editor.setNewValue(v);
                }
                catch(NumberFormatException e){
                    failed=true;
                }
            }
            if(failed){
                _text = getEditorValue();
                setText(_text==null ? DEFAULT_TEXT : _text);
            }
        }
       
        protected String getEditorValue(){
            return _editor.getPropertyValue()==null ? "" : _editor.getPropertyValue().toString();
        }
       
        protected abstract N parse(String text) throws NumberFormatException;
    }

    public static class IntegerValue <T extends SceneGraphObject> extends NumberValue<T,Integer>{
        protected Integer parse(String text) throws NumberFormatException{
            return Integer.parseInt(text);
        }
    }

    public static class DoubleValue <T extends SceneGraphObject> extends NumberValue<T,Double>{
        protected Double parse(String text) throws NumberFormatException{
            return Double.parseDouble(text);
        }
    }

    public static class FloatValue <T extends SceneGraphObject> extends NumberValue<T,Float>{
        protected Float parse(String text) throws NumberFormatException{
            return Float.parseFloat(text);
        }
    }

}
TOP

Related Classes of jsynoptic.plugins.java3d.panels.TextField$FloatValue

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.