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

Java tutorial

Introduction

Here is the source code for com.clustercontrol.composite.action.NumberVerifyListener.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 1.0.0
 */
public class NumberVerifyListener implements VerifyListener {
    private Integer low;
    private Integer high;

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

    /**
     * 
     *
     * @param low ?
     * @param high ?
     */
    public NumberVerifyListener(int low, int high) {
        this.low = Integer.valueOf(low);
        this.high = Integer.valueOf(high);
    }

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

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

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

    /**
     * ????
     *
     * @param low ?
     */
    public void setLow(Integer 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 (input.toString().matches("-")) {
            return;
        }
        //'-'??????
        else if (!input.toString().matches("-?\\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;
            }

            //??
            Integer input = Integer.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;
        }
    }
}