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.hayabusa.db.AbstractRenderer;
019import org.opengion.hayabusa.db.CellRenderer;
020import org.opengion.hayabusa.db.DBColumn;
021import org.opengion.fukurou.util.StringUtil;
022
023import java.text.DecimalFormat;
024
025/**
026 * DECIMAL レンデラーは、カラムのデータをDecimal(10進数、少数)表示する場合に
027 * 使用するクラスです。
028 * 負数の場合はspanタグclass="minus"を付けて出力します。
029 *
030 * 表示パラメータに与えられた文字列は、java.text.DecimalFormat を
031 * 使用してフォーマットされます。
032 * フォーマット変換前に、カンマなどの数値変換時にエラーになる情報を削除しておきます。
033 *
034 * フォーマットルールは、{@link java.text.DecimalFormat} を参照願います。
035 * 標準のフォーマットは"#,##0.#"です。
036 *
037 *  カラムの表示に必要な属性は, DBColumn オブジェクト より取り出します。
038 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。
039 *
040 * @og.rev 3.5.0.0 (2003/09/17) 新規作成
041 * @og.rev 5.4.3.6 (2012/01/19) コメント修正
042 * @og.group データ表示
043 *
044 * @version  4.0
045 * @author       Kazuhiko Hasegawa
046 * @since    JDK5.0,
047 */
048public class Renderer_DECIMAL extends AbstractRenderer {
049        //* このプログラムのVERSION文字列を設定します。   {@value} */
050        private static final String VERSION = "5.6.2.3 (2013/03/22)" ;
051
052        private final DecimalFormat format ;
053        private final String defValue ;
054        private final String noDisplayVal ;             // 5.6.2.3 (2013/03/22)
055
056        /**
057         * デフォルトコンストラクター。
058         * このコンストラクターで、基本オブジェクトを作成します。
059         *
060         * @og.rev 5.6.2.3 (2013/03/22) noDisplayVal 変数初期化
061         */
062        public Renderer_DECIMAL() {
063                format = null;
064//              defValue   = null;
065                defValue   = ""; // 5.1.1.0 (2009/12/01)
066                noDisplayVal = null;            // 5.5.1.0 (2012/04/03)
067        }
068
069        /**
070         * 引数つきコンストラクター。
071         *
072         * @param       clm     DBColumnオブジェクト
073         *
074         * @og.rev 5.1.1.0 (2009/12/01) 初期値がnullの場合は、defValueはnullとする。
075         * @og.rev 5.6.2.3 (2013/03/22) noDisplayVal 変数追加
076         */
077        private Renderer_DECIMAL( final DBColumn clm ) {
078
079                String fm = clm.getRendererParam();
080                if( fm == null || fm.length() == 0 || fm.equals( "_" ) ) {
081                        fm = "#,##0.#";
082                }
083                format = new DecimalFormat( fm );
084
085                // 5.1.1.0 (2009/12/01)
086                String defv = clm.getDefault();
087                if( defv == null || defv.length() == 0 ) {
088                        defValue = "";
089                }
090                else {
091                        double dd = StringUtil.parseDouble( defv );
092                        defValue = format.format( dd );
093                }
094                noDisplayVal = clm.getNoDisplayVal();   // 5.6.2.3 (2013/03/22)
095        }
096
097        /**
098         * 各オブジェクトから自分のインスタンスを返します。
099         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
100         * まかされます。
101         *
102         * @param       clm     DBColumnオブジェクト
103         *
104         * @return      CellRendererオブジェクト
105         */
106        public CellRenderer newInstance( final DBColumn clm ) {
107                return new Renderer_DECIMAL( clm );
108        }
109
110        /**
111         * データの表示用文字列を返します。
112         *
113         * ここでは、値が 0 の場合に、初期値を使うロジックが組み込まれており、
114         * 従来はこれを利用して、初期値にゼロ文字列を設定することで、"0" を
115         * 非表示文字として扱っていました。
116         * 互換性の問題があるので、既存の機能は残しますが、この処理は、noDisplayVal を
117         * 利用した処理に置き換えてください。
118         * 他の処理(NUMBER,MONEY,DBMENUなど)は、noDisplayVal を使います。
119         *
120         * @og.rev 3.8.5.2 (2006/05/31) カンマ編集された数字の対応
121         * @og.rev 5.3.1.0 (2009/12/01) 値が0の場合は、初期値を適用する。
122         * @og.rev 5.6.2.3 (2013/03/22) noDisplayVal 変数追加
123         *
124         * @param       value 入力値
125         *
126         * @return      データの表示用文字列
127         */
128        @Override
129        public String getValue( final String value ) {
130                // 5.6.2.3 (2013/03/22) noDisplayVal 変数追加
131                if( noDisplayVal != null && noDisplayVal.equalsIgnoreCase( value ) ) { return "" ; }
132
133//              if( value == null || (value.trim()).length() == 0 ) {
134                if( value == null || (value.trim()).length() == 0 || "0".equals( value ) ) {
135                        return defValue;
136                }
137
138                double dd = StringUtil.parseDouble( value );
139
140                String rtn ;
141                synchronized( format ) {
142                        rtn = format.format( dd );
143                }
144
145                if( dd < 0.0 ) {
146                        rtn = "<span class=\"minus\">" + rtn + "</span>";
147                }
148
149                return rtn;
150        }
151}