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 */
016package org.opengion.plugin.column;
017
018import org.opengion.fukurou.util.StringUtil;
019import org.opengion.hayabusa.db.AbstractEditor;
020import org.opengion.hayabusa.db.CellEditor;
021import org.opengion.hayabusa.db.DBColumn;
022import org.opengion.fukurou.util.XHTMLTag;
023
024import java.text.DecimalFormat;
025
026/**
027 * DECIMAL エディターは、カラムのデータをDecimal(10進数、少数)表示する場合に
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 オブジェクト毎に1つ作成されます。
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 */
049public 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                minFraction = 0;
065                format = null;
066                defValue   = null;
067        }
068
069        /**
070         * コンストラクター。
071         *
072         * @og.rev 5.3.1.0 (2009/12/01) 初期値がnullの場合は、defValueはnullとする。
073         * @og.rev 5.4.0.0 (2011/10/01) RendererParamが読み込まれているバグを修正
074         *
075         * @param       clm     DBColumnオブジェクト
076         */
077        private Editor_DECIMAL( final DBColumn clm ) {
078                super( clm );
079                minFraction = clm.getSizeY();
080
081                String fm = clm.getEditorParam();
082                if( fm == null || fm.length() == 0 || fm.equals( "_" ) ) {
083                        fm = "#,##0.#";
084                }
085                format = new DecimalFormat( fm );
086
087                // 5.3.1.0 (2009/12/01)
088                String defv = clm.getDefault();
089                if( defv == null || defv.length() == 0 ) {
090                        defValue = "";
091                }
092                else {
093                        double dd = 0.0;
094                        if( defv != null && defv.length() != 0 && !defv.equals( "_" ) ) {
095                                dd  = StringUtil.parseDouble( defv );
096                        }
097                        defValue = format.format( dd );
098                }
099
100                // -XXX,XXX,XXX.YYY 形式では、カンマ、小数点、マイナスなどの項目が入ります。
101                int maxlength = (int)(clm.getSizeX() * 4.0/3.0) + minFraction + 2 ;     // +2 は、マイナスと少数点
102                attributes.set( "maxlength"   ,String.valueOf( maxlength ) );
103                tagBuffer.add( XHTMLTag.inputAttri( attributes ) );
104        }
105
106        /**
107         * 各オブジェクトから自分のインスタンスを返します。
108         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
109         * まかされます。
110         *
111         * @param       clm     DBColumnオブジェクト
112         *
113         * @return      CellEditorオブジェクト
114         */
115        public CellEditor newInstance( final DBColumn clm ) {
116                return new Editor_DECIMAL( clm );
117        }
118
119        /**
120         * データの編集用文字列を返します。
121         *
122         * @param   value 入力値
123         *
124         * @return  データの編集用文字列
125         */
126        @Override
127        public String getValue( final String value ) {
128                return super.getValue( formatValue( value ) );
129        }
130
131        /**
132         * name属性を変えた、データ表示/編集用のHTML文字列を作成します。
133         * テーブル上の name に 行番号を付加して、名前_行番号 で登録するキーを作成し,
134         * リクエスト情報を1つ毎のフィールドで処理できます。
135         *
136         * @param       row   行番号
137         * @param       value 入力値
138         *
139         * @return      データ表示/編集用の文字列
140         */
141        @Override
142        public String getValue( final int row,final String value ) {
143                return super.getValue( row,formatValue( value ) );
144        }
145
146        /**
147         * 内部的なフォーマット変換後の文字列を返します。
148         *
149         * @og.rev 5.3.1.0 (2009/12/01) 値が0の場合は、初期値を適用する。
150         *
151         * @param       value 入力値
152         *
153         * @return      データの表示用文字列
154         */
155        private String formatValue( final String value ) {
156                if( value == null || (value.trim()).length() == 0 || "0".equals( value ) ) {
157                        return defValue;
158                }
159
160                double dd = StringUtil.parseDouble( value );
161
162                final String rtn ;
163                synchronized( format ) {
164                        rtn = format.format( dd );
165                }
166
167                return rtn;
168        }
169}