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         * @og.rev 5.9.27.1 (2017/12/08) 呼び方変更
067         *
068         */
069        public Renderer_NUMBER() {
070//              defValue     = "";                      // 4.0.0.0 (2007/11/29)
071//              minFraction  = 0;
072//              noDisplayVal = null;            // 5.5.1.0 (2012/04/03)
073                this( "",0,null );                      // 5.9.27.1 (2017/12/08)
074        }
075
076        /**
077         * デフォルトコンストラクター。
078         *
079         * @og.rev 3.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
080         * @og.rev 3.3.0.0 (2003/06/23) 初期値設定追加。NumberFormatクラスは、廃止します。
081         * @og.rev 5.6.2.3 (2013/03/22) noDisplayVal 変数追加
082         * @og.rev 5.9.27.1 (2017/12/08) 呼び方変更
083         *
084         * @param       defval  初期値
085         * @param       size    小数点
086         */
087        private Renderer_NUMBER( final String defval,final int size ) {
088//              defValue     = defval;
089//              minFraction  = size;
090//              noDisplayVal = null;            // 5.5.1.0 (2012/04/03)
091                this( defval,size,null );       // 5.9.27.1 (2017/12/08)
092        }
093        
094        /**
095         * コンストラクター。
096         *
097         * @og.rev 5.9.27.1 (2017/12/08) noDisplayVal 対応漏れのため、追加
098         *
099         * @param       defval  初期値
100         * @param       size    小数点
101         * @param       noDispVal       非表示文字の設定
102         */
103        private Renderer_NUMBER( final String defval,final int size , final String noDispVal ) {
104                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
105                defValue     = defval;
106                minFraction  = size;
107                noDisplayVal = noDispVal;               // 5.5.1.0 (2012/04/03)
108        }
109
110        /**
111         * 各オブジェクトから自分のインスタンスを返します。
112         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
113         * まかされます。
114         *
115         * @og.rev 3.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
116         * @og.rev 3.1.2.1 (2003/04/10) synchronized を、削除します。
117         * @og.rev 5.2.2.0 (2010/11/01) defval,size の初期値設定の変更
118         * @og.rev 5.3.5.0 (2011/05/01) ↑の判定ロジックのバグ修正
119         * @og.rev 5.9.27.1 (2017/12/08) noDispVal対応
120         *
121         * @param       clm     DBColumnオブジェクト
122         *
123         * @return      CellRendererオブジェクト
124         */
125        public CellRenderer newInstance( final DBColumn clm ) {
126                String defval = clm.getDefault();
127                int    size   = clm.getSizeY();
128                final String noDispVal = clm.getNoDisplayVal(); // 5.9.27.1 (2017/12/08)
129//              if( ( defval == null || defval.length() == 0 || defval.equals( "0" ) )
130//                      && size < dbCell.length ) {
131//                              return dbCell[size];
132//              }
133
134                // 5.2.2.0 (2010/11/01) defval,size の初期値設定の変更
135                // 5.3.5.0 (2011/05/01) ↑の判定ロジックのバグ修正
136//              if( size < dbCell.length ) {
137                if( size < 3 ) {
138                        if( defval == null || defval.length() == 0 ) {
139                                return dbCell[size];                            // 0,1,2
140                        }
141//                      else if( defval.equals( "0" ) ) {
142                        else if( "0".equals( defval ) ) {                                               // 5.3.0.0 (2010/12/01) リテラルで比較する。
143                                return dbCell[size+3];                          // 3,4,5
144                        }
145                }
146
147//              return new Renderer_NUMBER( defval,size );
148                return new Renderer_NUMBER( defval,size,noDispVal );    // 5.9.27.1 (2017/12/08)
149        }
150
151        /**
152         * データの表示用文字列を返します。
153         *
154         * @og.rev 3.1.0.0 (2003/03/20) 内部に、DBColumn オブジェクトをキープしないように変更
155         * @og.rev 3.3.0.0 (2003/06/23) NumberFormatクラスは、廃止します。
156         * @og.rev 5.6.2.3 (2013/03/22) noDisplayVal 変数追加
157         *
158         * @param       value 入力値
159         *
160         * @return      データの表示用文字列
161         */
162        @Override
163        public String getValue( final String value ) {
164                // 5.6.2.3 (2013/03/22) noDisplayVal 変数追加
165                if( noDisplayVal != null && noDisplayVal.equalsIgnoreCase( value ) ) { return "" ; }
166
167                String rtn ;
168                if( value == null || (value.trim()).length() == 0 ) {
169                        rtn = defValue;
170                }
171                else {
172                        rtn = value;
173                }
174
175                rtn = StringUtil.numberFormat( rtn,minFraction );
176                if( rtn != null && (rtn.trim()).length() > 0 && rtn.charAt( 0 ) == '-' ) {
177                        rtn = "<span class=\"minus\">" + rtn + "</span>";
178                }
179                return rtn;
180        }
181}