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                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
064                // 4.3.4.4 (2009/01/01)
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.getEditorParam();
083                if( fm == null || fm.isEmpty() || fm.equals( "_" ) ) {
084                        fm = "#,##0.#";
085                }
086                format = new DecimalFormat( fm );
087
088                // 5.3.1.0 (2009/12/01)
089                final String defv = clm.getDefault();
090                if( defv == null || defv.isEmpty() ) {
091                        defValue = "";
092                }
093                else {
094                        double dd = 0.0;
095                        // 6.0.2.5 (2014/10/31) null でないことがわかっている値の冗長な null チェックがあります。
096                        if( !"_".equals( defv ) ) {
097                                dd  = StringUtil.parseDouble( defv );
098                        }
099                        defValue = format.format( dd );
100                }
101
102                // -XXX,XXX,XXX.YYY 形式では、カンマ、小数点、マイナスなどの項目が入ります。
103                final 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         * @og.rtnNotNull
117         */
118        public CellEditor newInstance( final DBColumn clm ) {
119                return new Editor_DECIMAL( clm );
120        }
121
122        /**
123         * データの編集用文字列を返します。
124         *
125         * @param   value 入力値
126         *
127         * @return  データの編集用文字列
128         */
129        @Override
130        public String getValue( final String value ) {
131                return super.getValue( formatValue( value ) );
132        }
133
134        /**
135         * name属性を変えた、データ表示/編集用のHTML文字列を作成します。
136         * テーブル上の name に 行番号を付加して、名前_行番号 で登録するキーを作成し,
137         * リクエスト情報を1つ毎のフィールドで処理できます。
138         *
139         * @param       row   行番号
140         * @param       value 入力値
141         *
142         * @return      データ表示/編集用の文字列
143         */
144        @Override
145        public String getValue( final int row,final String value ) {
146                return super.getValue( row,formatValue( value ) );
147        }
148
149        /**
150         * 内部的なフォーマット変換後の文字列を返します。
151         *
152         * @og.rev 5.3.1.0 (2009/12/01) 値が0の場合は、初期値を適用する。
153         *
154         * @param       value 入力値
155         *
156         * @return      データの表示用文字列
157         */
158        private String formatValue( final String value ) {
159                if( value == null || value.trim().isEmpty() || "0".equals( value ) ) {
160                        return defValue;
161                }
162
163                final 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}