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     */
016    package org.opengion.plugin.column;
017    
018    import org.opengion.fukurou.util.StringUtil;
019    import org.opengion.hayabusa.db.AbstractRenderer;
020    import org.opengion.hayabusa.db.CellRenderer;
021    import 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 オブジェクト毎に?‘つ作æ?されますã?
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     */
040    public 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    }