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.hayabusa.db.AbstractRenderer;
019    import org.opengion.hayabusa.db.CellRenderer;
020    import org.opengion.hayabusa.db.DBColumn;
021    import org.opengion.fukurou.util.StringUtil;
022    
023    import java.text.DecimalFormat;
024    
025    /**
026     * DECIMAL レンãƒ?ƒ©ãƒ¼ã¯ã€ã‚«ãƒ©ãƒ??ãƒ??ã‚¿ã‚’Decimal(?‘ï¼é?æ•°ã€å°‘æ•°)表示ã™ã‚‹å ´åˆã«
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 オブジェクト毎ã«?‘ã¤ä½œæ?ã•れã¾ã™ã?
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     */
048    public 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   = null;
065                    defValue   = ""; // 5.1.1.0 (2009/12/01)
066                    noDisplayVal = null;            // 5.5.1.0 (2012/04/03)
067            }
068    
069            /**
070             * 引数ã¤ãコンストラクターã€?
071             *
072             * @param       clm     DBColumnオブジェク�
073             *
074             * @og.rev 5.1.1.0 (2009/12/01) åˆæœŸå€¤ãŒnullã®å ´åˆã?ã€defValueã¯nullã¨ã™ã‚‹ã€?
075             * @og.rev 5.6.2.3 (2013/03/22) noDisplayVal 変数追�
076             */
077            private Renderer_DECIMAL( final DBColumn clm ) {
078    
079                    String fm = clm.getRendererParam();
080                    if( fm == null || fm.length() == 0 || fm.equals( "_" ) ) {
081                            fm = "#,##0.#";
082                    }
083                    format = new DecimalFormat( fm );
084    
085                    // 5.1.1.0 (2009/12/01)
086                    String defv = clm.getDefault();
087                    if( defv == null || defv.length() == 0 ) {
088                            defValue = "";
089                    }
090                    else {
091                            double dd = StringUtil.parseDouble( defv );
092                            defValue = format.format( dd );
093                    }
094                    noDisplayVal = clm.getNoDisplayVal();   // 5.6.2.3 (2013/03/22)
095            }
096    
097            /**
098             * å?‚ªãƒ–ジェクトã‹ã‚‰è?åˆ??インスタンスを返ã—ã¾ã™ã?
099             * 自åˆ??身をキャãƒ?‚·ãƒ¥ã™ã‚‹ã®ã‹ã?æ–°ãŸã«ä½œæ?ã™ã‚‹ã®ã‹ã?ã€å„サブクラスã®å®Ÿè£?«
100             * ã¾ã‹ã•れã¾ã™ã?
101             *
102             * @param       clm     DBColumnオブジェク�
103             *
104             * @return      CellRendererオブジェク�
105             */
106            public CellRenderer newInstance( final DBColumn clm ) {
107                    return new Renderer_DECIMAL( clm );
108            }
109    
110            /**
111             * ãƒ??ã‚¿ã®è¡¨ç¤ºç”¨æ–?­—å?ã‚’è¿”ã—ã¾ã™ã?
112             *
113             * ã“ã“ã§ã¯ã€å?ã?0 ã®å ´åˆã«ã€å?期å?を使ã?ƒ­ã‚¸ãƒ?‚¯ãŒçµ?¿è¾¼ã¾ã‚Œã¦ãŠã‚Šã€?
114             * 従æ¥ã¯ã“れを利用ã—ã¦ã€å?期å?ã«ã‚¼ãƒ­æ–?­—å?を設定ã™ã‚‹ã“ã¨ã§ã€?0" ã‚?
115             * éžè¡¨ç¤ºæ–?­—ã¨ã—ã¦æ‰±ã£ã¦ã?¾ã—ãŸã€?
116             * äº’æ›æ€§ã®å•題ãŒã‚ã‚‹ã®ã§ã€æ—¢å­˜ã?機è?ã¯æ®‹ã—ã¾ã™ãŒã€ã“ã®å‡¦ç??ã€noDisplayVal ã‚?
117             * 利用ã—ãŸå‡¦ç?«ç½®ãæ›ãˆã¦ãã ã•ã„ã€?
118             * ä»–ã?処ç?¼?UMBER,MONEY,DBMENUãªã©?‰ã?ã€noDisplayVal を使ã?¾ã™ã?
119             *
120             * @og.rev 3.8.5.2 (2006/05/31) カンマ編é›?•ã‚ŒãŸæ•°å­—ã?対å¿?
121             * @og.rev 5.3.1.0 (2009/12/01) 値ã?ã®å ´åˆã?ã€å?期å?ã‚’é©ç”¨ã™ã‚‹ã€?
122             * @og.rev 5.6.2.3 (2013/03/22) noDisplayVal 変数追�
123             *
124             * @param       value 入力å?
125             *
126             * @return      ãƒ??ã‚¿ã®è¡¨ç¤ºç”¨æ–?­—å?
127             */
128            @Override
129            public String getValue( final String value ) {
130                    // 5.6.2.3 (2013/03/22) noDisplayVal 変数追�
131                    if( noDisplayVal != null && noDisplayVal.equalsIgnoreCase( value ) ) { return "" ; }
132    
133    //              if( value == null || (value.trim()).length() == 0 ) {
134                    if( value == null || (value.trim()).length() == 0 || "0".equals( value ) ) {
135                            return defValue;
136                    }
137    
138                    double dd = StringUtil.parseDouble( value );
139    
140                    String rtn ;
141                    synchronized( format ) {
142                            rtn = format.format( dd );
143                    }
144    
145                    if( dd < 0.0 ) {
146                            rtn = "<span class=\"minus\">" + rtn + "</span>";
147                    }
148    
149                    return rtn;
150            }
151    }