com.clustercontrol.composite.action.RealNumberVerifyListener.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.composite.action.RealNumberVerifyListener.java

Source

/*
    
Copyright (C) 2006 NTT DATA Corporation
    
This program is free software; you can redistribute it and/or
Modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, version 2.
    
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 General Public License for more details.
    
 */

package com.clustercontrol.composite.action;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.widgets.Text;

import com.clustercontrol.util.Messages;
import com.clustercontrol.util.WidgetTestUtil;

/**
 * VerifyListener<BR>
 *
 * @version 2.2.0
 * @since 2.0.0
 */
public class RealNumberVerifyListener implements VerifyListener {
    public static final String MINUS_CHAR = "-";
    public static final String DOT_CHAR = ".";
    public static final String EXP_CHAR_U = "E";
    public static final String EXP_CHAR_L = "e";

    private Double low;
    private Double high;

    /**
     * 
     */
    public RealNumberVerifyListener() {
        this.low = null;
        this.high = null;
    }

    /**
     * 
     *
     * @param low ?
     * @param high ?
     */
    public RealNumberVerifyListener(double low, double high) {
        this.low = Double.valueOf(low);
        this.high = Double.valueOf(high);
    }

    /**
     * ?????
     *
     * @return ?
     */
    public Double getHigh() {
        return high;
    }

    /**
     * ????
     *
     * @param high ?
     */
    public void setHigh(Double high) {
        this.high = high;
    }

    /**
     * ?????
     *
     * @return ?
     */
    public Double getLow() {
        return low;
    }

    /**
     * ????
     *
     * @param low ?
     */
    public void setLow(Double low) {
        this.low = low;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.swt.events.VerifyListener#verifyText(org.eclipse.swt.events.VerifyEvent)
     */
    @Override
    public void verifyText(VerifyEvent e) {

        //????
        Text text = (Text) e.getSource();
        WidgetTestUtil.setTestId(this, null, text);
        StringBuilder input = new StringBuilder(text.getText());

        //???
        if (e.keyCode == 0) {
            //??????

            //
            input.replace(e.start, e.end, e.text);
        } else {
            //BackspaceDelete??????????
            if (e.character == SWT.BS || e.character == SWT.DEL) {
                input.delete(e.start, e.end);
            } else {
                //
                input.replace(e.start, e.end, e.text);
            }
        }

        //'.'???????????? ??? ???'.'???????
        if (e.text.length() == 1 && DOT_CHAR.indexOf(e.text) == 0) {
            if (!input.toString().matches("-?\\d+\\.?\\d*(E?|E-?)\\d*")) {
                e.doit = false;
            }
            return;
        }
        //'-'???????????????'E'????
        else if (e.text.length() == 1 && MINUS_CHAR.indexOf(e.text) == 0) {
            if (!input.toString().matches("-?\\d*\\.?\\d*(E?|E-?)\\d*")) {
                e.doit = false;
            }
            return;
        }
        //'E'???????????? ??? ???'E'???????
        else if (e.text.length() == 1 && EXP_CHAR_U.indexOf(e.text) == 0) {
            if (!input.toString().matches("-?\\d*\\.?\\d+(E?|E-?)\\d*")) {
                e.doit = false;
            }
            return;
        } else {
            //'-'????
            if (input.toString().matches("-")) {
                return;
            } else if (!input.toString().matches("-?(\\d*|\\d+\\.\\d+)(\\d*|E\\d+|E-\\d+)")) {
                e.doit = false;
                return;
            }
        }

        //?
        checkRange(e, input.toString());
    }

    /**
     * ?
     *
     * @param e 
     * @param inputText 
     */
    protected void checkRange(VerifyEvent e, String inputText) {
        try {
            //?????
            if (inputText.length() == 0) {
                return;
            }

            //???????????
            if (this.low == null || this.high == null) {
                return;
            }

            //??
            Double input = Double.valueOf(inputText.toString());

            //?
            if (input.compareTo(low) < 0 || input.compareTo(high) > 0) {
                //?
                e.doit = false;

                String[] args = { this.low.toString(), this.high.toString() };

                //
                MessageDialog.openWarning(null, Messages.getString("message.hinemos.1"),
                        Messages.getString("message.hinemos.8", args));
            }
        } catch (NumberFormatException ex) {
            //????
            e.doit = false;
        }
    }
}