minutespropertyeditor.java

来自「《java敏捷开发--使用spring、hibernate和eclipse》源码」· Java 代码 · 共 46 行

JAVA
46
字号
package com.visualpatterns.timex.controller;

import java.beans.PropertyEditorSupport;
import java.text.DecimalFormat;

/**
 * Property editor for the Enter Hours screen; registered in 
 * the EnterHoursController class.
 * @author anil
 * @see com.visualpatterns.timex.controller.EnterHoursController
 */
public class MinutesPropertyEditor extends PropertyEditorSupport
{
    private final double ALTER_BY = 60.0; // 60 seconds
    DecimalFormat decimalFormat = new DecimalFormat("#.00");

    /**
     * Divides value by 60 and returns result
     */
    public String getAsText()
    {
        Integer value = (Integer) getValue();
        if (value == null) return "";

        float newValue = (float) value.intValue() / (float) ALTER_BY;
        return decimalFormat.format(newValue);
    }

    /**
     * Multiplies value by 60 and returns result
     */
    public void setAsText(String text) throws IllegalArgumentException
    {
        try
        {
            Float newValue = new Float(Float.valueOf(text).floatValue()
                    * ALTER_BY);
            setValue(new Integer(newValue.intValue()));
        }
        catch (NumberFormatException ex)
        {
            throw new IllegalArgumentException("Invalid number: " + text);
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?