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 * NUMBER レンデラーは、カラムのデータを数字表示する場合に使用するクラスです。
025 * x,yの形式で表示パラメータを指定可能です。
026 * 負数の場合はspanタグclass="minus"を付けて出力します。
027 *
028 * フォーマットには、java.text.NumberFormat を使用せずに、独自クラスを使用しており
029 * double 以上の精度をもつ値でも正確に変換できます。
030 *  カラムの表示に必要な属性は, DBColumn オブジェクト より取り出します。
031 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。
032 *
033 * @og.group データ表示
034 * @og.rev 5.4.3.6 (2012/01/19) コメント修正
035 *
036 * @version  4.0
037 * @author       Kazuhiko Hasegawa
038 * @since    JDK5.0,
039 */
040public class Renderer_NUMBER extends AbstractRenderer {
041        //* このプログラムのVERSION文字列を設定します。   {@value} */
042        private static final String VERSION = "5.6.2.3 (2013/03/22)" ;
043
044        private final String    defValue ;
045        private final String    noDisplayVal ;          // 5.6.2.3 (2013/03/22)
046        private final int               minFraction;
047
048        // 5.2.2.0 (2010/11/01) defval,size の初期値設定の変更
049        private static final CellRenderer[] dbCell = {
050                                                        new Renderer_NUMBER(),                          // 0
051                                                        new Renderer_NUMBER("",1),                      // 1
052                                                        new Renderer_NUMBER("",2),                      // 2
053                                                        new Renderer_NUMBER("0",0),                     // 3
054                                                        new Renderer_NUMBER("0",1),                     // 4
055                                                        new Renderer_NUMBER("0",2)                      // 5
056        };
057
058        /**
059         * デフォルトコンストラクター。
060         * このコンストラクターで、基本オブジェクトを作成します。
061         *
062         * @og.rev 3.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
063         * @og.rev 3.3.0.0 (2003/06/23) 初期値設定追加。
064         * @og.rev 4.0.0.0 (2007/11/29) 初期値を""に変更。
065         * @og.rev 5.6.2.3 (2013/03/22) noDisplayVal 変数初期化
066         *
067         */
068        public Renderer_NUMBER() {
069                defValue     = "";                      // 4.0.0.0 (2007/11/29)
070                minFraction  = 0;
071                noDisplayVal = null;            // 5.5.1.0 (2012/04/03)
072        }
073
074        /**
075         * デフォルトコンストラクター。
076         *
077         * @og.rev 3.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
078         * @og.rev 3.3.0.0 (2003/06/23) 初期値設定追加。NumberFormatクラスは、廃止します。
079         * @og.rev 5.6.2.3 (2013/03/22) noDisplayVal 変数追加
080         *
081         * @param       defval  初期値
082         * @param       size    小数点
083         */
084        private Renderer_NUMBER( final String defval,final int size ) {
085                defValue     = defval;
086                minFraction  = size;
087                noDisplayVal = null;            // 5.5.1.0 (2012/04/03)
088        }
089
090        /**
091         * 各オブジェクトから自分のインスタンスを返します。
092         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
093         * まかされます。
094         *
095         * @og.rev 3.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
096         * @og.rev 3.1.2.1 (2003/04/10) synchronized を、削除します。
097         * @og.rev 5.2.2.0 (2010/11/01) defval,size の初期値設定の変更
098         * @og.rev 5.3.5.0 (2011/05/01) ↑の判定ロジックのバグ修正
099         *
100         * @param       clm     DBColumnオブジェクト
101         *
102         * @return      CellRendererオブジェクト
103         */
104        public CellRenderer newInstance( final DBColumn clm ) {
105                String defval = clm.getDefault();
106                int    size   = clm.getSizeY();
107//              if( ( defval == null || defval.length() == 0 || defval.equals( "0" ) )
108//                      && size < dbCell.length ) {
109//                              return dbCell[size];
110//              }
111
112                // 5.2.2.0 (2010/11/01) defval,size の初期値設定の変更
113                // 5.3.5.0 (2011/05/01) ↑の判定ロジックのバグ修正
114//              if( size < dbCell.length ) {
115                if( size < 3 ) {
116                        if( defval == null || defval.length() == 0 ) {
117                                return dbCell[size];                            // 0,1,2
118                        }
119//                      else if( defval.equals( "0" ) ) {
120                        else if( "0".equals( defval ) ) {                                               // 5.3.0.0 (2010/12/01) リテラルで比較する。
121                                return dbCell[size+3];                          // 3,4,5
122                        }
123                }
124
125                return new Renderer_NUMBER( defval,size );
126        }
127
128        /**
129         * データの表示用文字列を返します。
130         *
131         * @og.rev 3.1.0.0 (2003/03/20) 内部に、DBColumn オブジェクトをキープしないように変更
132         * @og.rev 3.3.0.0 (2003/06/23) NumberFormatクラスは、廃止します。
133         * @og.rev 5.6.2.3 (2013/03/22) noDisplayVal 変数追加
134         *
135         * @param       value 入力値
136         *
137         * @return      データの表示用文字列
138         */
139        @Override
140        public String getValue( final String value ) {
141                // 5.6.2.3 (2013/03/22) noDisplayVal 変数追加
142                if( noDisplayVal != null && noDisplayVal.equalsIgnoreCase( value ) ) { return "" ; }
143
144                String rtn ;
145                if( value == null || (value.trim()).length() == 0 ) {
146                        rtn = defValue;
147                }
148                else {
149                        rtn = value;
150                }
151
152                rtn = StringUtil.numberFormat( rtn,minFraction );
153                if( rtn != null && (rtn.trim()).length() > 0 && rtn.charAt( 0 ) == '-' ) {
154                        rtn = "<span class=\"minus\">" + rtn + "</span>";
155                }
156                return rtn;
157        }
158}