001    /*
002     * Copyright (c) 2009 The openGion Project.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013     * either express or implied. See the License for the specific language
014     * governing permissions and limitations under the License.
015     */
016    package org.opengion.plugin.column;
017    
018    import org.opengion.fukurou.util.StringUtil;
019    import org.opengion.hayabusa.db.AbstractEditor;
020    import org.opengion.hayabusa.db.CellEditor;
021    import org.opengion.hayabusa.db.DBColumn;
022    import org.opengion.fukurou.util.XHTMLTag;
023    
024    import java.text.DecimalFormat;
025    
026    /**
027     * DECIMAL エãƒ?‚£ターは、カラãƒ??ãƒ??タをDecimal(?‘0é?数、少数)表示する場合に
028     * 使用するクラスですã?
029     *
030     * 編é›?ƒ‘ラメータに与えられた文字å?は、java.text.DecimalFormat を使用して
031     * フォーマットされますã?
032     * フォーマット変換前に、カンマなどの数値変換時にエラーになる情報を削除しておきますã?
033     * 標準ã?フォーマットã?"#,##0.#"ですã?
034     * default値が設定されてã�?�ªã�??合ã?初期値は0.0にフォーマットå?ç�?‚’したもã?ですã?
035     *
036     * フォーマットルールは、{@link java.text.DecimalFormat} を参照願いますã?
037     *
038     *  カラãƒ??表示にå¿?¦�な属æ?は, DBColumn オブジェクãƒ?より取り出しますã?
039     * こã?クラスは、DBColumn オブジェクト毎に?‘つ作æ?されますã?
040     *
041     * @og.group ãƒ??タ編é›?
042     * @og.rev 3.8.8.5 (2007/03/09) 新規作æ?
043     * @og.rev 5.4.3.6 (2012/01/19) コメント修正
044     *
045     * @version  4.0
046     * @author       Kazuhiko Hasegawa
047     * @since    JDK5.0,
048     */
049    public class Editor_DECIMAL extends AbstractEditor {
050            //* こã?プログラãƒ??VERSIONæ–?­—å?を設定しますã?       {@value} */
051            private static final String VERSION = "5.4.3.6 (2012/01/19)" ;
052    
053            private final int       minFraction;
054            private final DecimalFormat format ;
055            private final String    defValue ;
056    
057            /**
058             * ãƒ?ƒ•ォルトコンストラクターã€?
059             * こã?コンストラクターで、基本オブジェクトを作æ?しますã?
060             *
061             */
062            public Editor_DECIMAL() {
063                    // 4.3.4.4 (2009/01/01)
064    //              super();
065                    minFraction = 0;
066                    format = null;
067                    defValue   = null;
068            }
069    
070            /**
071             * コンストラクターã€?
072             *
073             * @og.rev 5.3.1.0 (2009/12/01) 初期値がnullの場合ã?、defValueはnullとするã€?
074             * @og.rev 5.4.0.0 (2011/10/01) RendererParamが読み込まれてã�?‚‹バグを修正
075             *
076             * @param       clm     DBColumnオブジェクãƒ?
077             */
078            private Editor_DECIMAL( final DBColumn clm ) {
079                    super( clm );
080                    minFraction = clm.getSizeY();
081    
082    //              String fm = clm.getRendererParam();
083                    String fm = clm.getEditorParam();
084                    if( fm == null || fm.length() == 0 || fm.equals( "_" ) ) {
085                            fm = "#,##0.#";
086                    }
087                    format = new DecimalFormat( fm );
088    
089                    // 5.3.1.0 (2009/12/01)
090                    String defv = clm.getDefault();
091                    if( defv == null || defv.length() == 0 ) {
092                            defValue = "";
093                    }
094                    else {
095                            double dd = 0.0;
096                            if( defv != null && defv.length() != 0 && !defv.equals( "_" ) ) {
097                                    dd  = StringUtil.parseDouble( defv );
098                            }
099                            defValue = format.format( dd );
100                    }
101    
102                    // -XXX,XXX,XXX.YYY 形式では、カンマã?小数点、ã?イナスなどのé ?›®がå?りますã?
103                    int maxlength = (int)(clm.getSizeX() * 4.0/3.0) + minFraction + 2 ;     // +2 は、ã?イナスと少数点
104                    attributes.set( "maxlength"   ,String.valueOf( maxlength ) );
105                    tagBuffer.add( XHTMLTag.inputAttri( attributes ) );
106            }
107    
108            /**
109             * å�?‚ªブジェクトからè?åˆ??インスタンスを返しますã?
110             * 自åˆ??身をキャãƒ?‚·ュするのかã?新たに作æ?するのかã?、各サブクラスの実è£?�«
111             * まかされますã?
112             *
113             * @param       clm     DBColumnオブジェクãƒ?
114             *
115             * @return      CellEditorオブジェクãƒ?
116             */
117            public CellEditor newInstance( final DBColumn clm ) {
118                    return new Editor_DECIMAL( clm );
119            }
120    
121            /**
122             * ãƒ??タの編é›?”¨æ–?­—å?を返しますã?
123             *
124             * @param   value 入力å?
125             *
126             * @return  ãƒ??タの編é›?”¨æ–?­—å?
127             */
128            @Override
129            public String getValue( final String value ) {
130                    return super.getValue( formatValue( value ) );
131            }
132    
133            /**
134             * name属æ?を変えたã?ãƒ??タ表示/編é›?”¨のHTMLæ–?­—å?を作æ?しますã?
135             * ãƒ??ブル上ã? name に 行番号を付加して、名前_行番号 で登録するキーを作æ?ã�?
136             * リクエスト情報を1つ毎ã?フィールドで処ç�?�§きますã?
137             *
138             * @param       row   行番号
139             * @param       value 入力å?
140             *
141             * @return      ãƒ??タ表示/編é›?”¨のæ–?­—å?
142             */
143            @Override
144            public String getValue( final int row,final String value ) {
145                    return super.getValue( row,formatValue( value ) );
146            }
147    
148            /**
149             * å†?ƒ¨çš?�ªフォーマット変換後ã?æ–?­—å?を返しますã?
150             *
151             * @og.rev 5.3.1.0 (2009/12/01) 値ã�?の場合ã?、å?期å?を適用するã€?
152             *
153             * @param       value 入力å?
154             *
155             * @return      ãƒ??タの表示用æ–?­—å?
156             */
157            private String formatValue( final String value ) {
158    //              if( value == null || (value.trim()).length() == 0 ) {
159                    if( value == null || (value.trim()).length() == 0 || "0".equals( value ) ) {
160                            return defValue;
161                    }
162    
163                    double dd = StringUtil.parseDouble( value );
164    
165                    final String rtn ;
166                    synchronized( format ) {
167                            rtn = format.format( dd );
168                    }
169    
170                    return rtn;
171            }
172    }