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   = ""; // 5.1.1.0 (2009/12/01)
065                noDisplayVal = null;            // 5.5.1.0 (2012/04/03)
066        }
067
068        /**
069         * 引数つきコンストラクター。
070         *
071         * @param       clm     DBColumnオブジェクト
072         *
073         * @og.rev 5.1.1.0 (2009/12/01) 初期値がnullの場合は、defValueはnullとする。
074         * @og.rev 5.6.2.3 (2013/03/22) noDisplayVal 変数追加
075         */
076        private Renderer_DECIMAL( final DBColumn clm ) {
077
078                String fm = clm.getRendererParam();
079                if( fm == null || fm.length() == 0 || fm.equals( "_" ) ) {
080                        fm = "#,##0.#";
081                }
082                format = new DecimalFormat( fm );
083
084                // 5.1.1.0 (2009/12/01)
085                String defv = clm.getDefault();
086                if( defv == null || defv.length() == 0 ) {
087                        defValue = "";
088                }
089                else {
090                        double dd = StringUtil.parseDouble( defv );
091                        defValue = format.format( dd );
092                }
093                noDisplayVal = clm.getNoDisplayVal();   // 5.6.2.3 (2013/03/22)
094        }
095
096        /**
097         * 各オブジェクトから自分のインスタンスを返します。
098         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
099         * まかされます。
100         *
101         * @param       clm     DBColumnオブジェクト
102         *
103         * @return      CellRendererオブジェクト
104         */
105        public CellRenderer newInstance( final DBColumn clm ) {
106                return new Renderer_DECIMAL( clm );
107        }
108
109        /**
110         * データの表示用文字列を返します。
111         *
112         * ここでは、値が 0 の場合に、初期値を使うロジックが組み込まれており、
113         * 従来はこれを利用して、初期値にゼロ文字列を設定することで、"0" を
114         * 非表示文字として扱っていました。
115         * 互換性の問題があるので、既存の機能は残しますが、この処理は、noDisplayVal を
116         * 利用した処理に置き換えてください。
117         * 他の処理(NUMBER,MONEY,DBMENUなど)は、noDisplayVal を使います。
118         *
119         * @og.rev 3.8.5.2 (2006/05/31) カンマ編集された数字の対応
120         * @og.rev 5.3.1.0 (2009/12/01) 値が0の場合は、初期値を適用する。
121         * @og.rev 5.6.2.3 (2013/03/22) noDisplayVal 変数追加
122         *
123         * @param       value 入力値
124         *
125         * @return      データの表示用文字列
126         */
127        @Override
128        public String getValue( final String value ) {
129                // 5.6.2.3 (2013/03/22) noDisplayVal 変数追加
130                if( noDisplayVal != null && noDisplayVal.equalsIgnoreCase( value ) ) { return "" ; }
131
132                if( value == null || (value.trim()).length() == 0 || "0".equals( value ) ) {
133                        return defValue;
134                }
135
136                double dd = StringUtil.parseDouble( value );
137
138                String rtn ;
139                synchronized( format ) {
140                        rtn = format.format( dd );
141                }
142
143                if( dd < 0.0 ) {
144                        rtn = "<span class=\"minus\">" + rtn + "</span>";
145                }
146
147                return rtn;
148        }
149}