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//              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) 値が0の場合は、初期値を適用する。
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}