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.AbstractRenderer;
020import org.opengion.hayabusa.db.CellRenderer;
021import org.opengion.hayabusa.db.DBColumn;
022
023/**
024 * MONEY レンデラーは、カラムのデータを金額表示する場合に使用するクラスです。
025 *
026 * マイナス時の表示は、id="minus" をキーに CSSファイルで指定しています。
027 * 通貨は、標準では、¥ですが、値:記号 という形式で指定すれば、各値ごとに
028 * 通貨を指定できます。(ただし、通貨変換は、サポートしていません。)
029 * 負数の場合はspanタグclass="minus"を付けて出力します。
030 *
031 *  カラムの表示に必要な属性は, DBColumn オブジェクト より取り出します。
032 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。
033 *
034 * @og.group データ表示
035 * @og.rev 5.4.3.6 (2012/01/19) コメント修正
036 *
037 * @version  4.0
038 * @author       Kazuhiko Hasegawa
039 * @since    JDK5.0,
040 */
041public class Renderer_MONEY extends AbstractRenderer {
042        //* このプログラムのVERSION文字列を設定します。   {@value} */
043        private static final String VERSION = "5.6.2.3 (2013/03/22)" ;
044
045        private static final CellRenderer[] dbCell = {
046                                                        new Renderer_MONEY(),
047                                                        new Renderer_MONEY(1),
048                                                        new Renderer_MONEY(2)
049        };
050
051        private final int        minFraction;
052        private final String noDisplayVal ;             // 5.6.2.3 (2013/03/22)
053
054        /**
055         * デフォルトコンストラクター。
056         * このコンストラクターで、基本オブジェクトを作成します。
057         *
058         * @og.rev 3.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
059         * @og.rev 3.3.0.0 (2003/06/23) 初期値設定追加。
060         * @og.rev 5.6.2.3 (2013/03/22) noDisplayVal 変数初期化
061         *
062         */
063        public Renderer_MONEY() {
064                minFraction  = 0;
065                noDisplayVal = null;            // 5.5.1.0 (2012/04/03)
066        }
067
068        /**
069         * コンストラクター。
070         *
071         * @og.rev 3.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
072         * @og.rev 3.3.0.0 (2003/06/23) 初期値設定追加。NumberFormatクラスは、廃止します。
073         * @og.rev 5.6.2.3 (2013/03/22) noDisplayVal 変数追加
074         *
075         * @param       size    小数点
076         */
077        private Renderer_MONEY( final int size ) {
078                minFraction  = size ;
079                noDisplayVal = null;            // 5.5.1.0 (2012/04/03)
080        }
081
082        /**
083         * 各オブジェクトから自分のインスタンスを返します。
084         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
085         * まかされます。
086         *
087         * @og.rev 3.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
088         * @og.rev 3.1.2.1 (2003/04/10) synchronized を、削除します。
089         *
090         * @param       clm     DBColumnオブジェクト
091         *
092         * @return      CellRendererオブジェクト
093         */
094        public CellRenderer newInstance( final DBColumn clm ) {
095                int size = clm.getSizeY();
096                if( size < dbCell.length ) { return dbCell[size]; }
097                return new Renderer_MONEY( size );
098        }
099
100        /**
101         * データの表示用文字列を返します。
102         *
103         * 引数の値が、『数字型文字列:通貨』という値を渡すことで、通貨を
104         * 頭につけて通貨ごとに異なる値を表示させることができる。
105         *
106         * @og.rev 3.1.0.0 (2003/03/20) 内部に、DBColumn オブジェクトをキープしないように変更
107         * @og.rev 3.3.0.0 (2003/06/23) NumberFormatクラスは、廃止します。
108         * @og.rev 5.6.2.3 (2013/03/22) noDisplayVal 変数追加
109         *
110         * @param       value 入力値
111         *
112         * @return      データの表示用文字列
113         */
114        @Override
115        public String getValue( final String value ) {
116                // 5.6.2.3 (2013/03/22) noDisplayVal 変数追加
117                if( noDisplayVal != null && noDisplayVal.equalsIgnoreCase( value ) ) { return "" ; }
118
119                String rtn ;
120                if( value == null || (value.trim()).length() == 0 ) {
121                        rtn = "0" ;
122                }
123                else {
124                        rtn = value;
125                }
126
127                boolean minus = false ;
128                if( rtn.charAt( 0 ) == '-' ) {
129                        minus = true;
130                        rtn   = rtn.substring( 1 );
131                }
132
133                String tuuka = "¥";
134                int taniPos = rtn.indexOf( ':' );
135                if( taniPos >= 0 ) {
136                        tuuka = rtn.substring( taniPos+1 );
137                        rtn   = rtn.substring( 0,taniPos );
138                }
139
140                rtn = tuuka + StringUtil.numberFormat( rtn,minFraction ) + "-";
141
142                if( minus ) {
143                        rtn = "<span class=\"minus\">-" + rtn + "</span>";
144                }
145                return rtn;
146        }
147}