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.AbstractEditor; 020 import org.opengion.hayabusa.db.CellEditor; 021 import org.opengion.hayabusa.db.DBColumn; 022 import org.opengion.fukurou.util.XHTMLTag; 023 024 import java.text.DecimalFormat; 025 026 /** 027 * DECIMAL エãƒ?‚£ã‚¿ãƒ¼ã¯ã€ã‚«ãƒ©ãƒ??ãƒ??ã‚¿ã‚’Decimal(?‘ï¼é?æ•°ã€å°‘æ•°)表示ã™ã‚‹å ´åˆã« 028 * 使用ã™ã‚‹ã‚¯ãƒ©ã‚¹ã§ã™ã? 029 * 030 * ç·¨é›?ƒ‘ラメータã«ä¸Žãˆã‚‰ã‚ŒãŸæ–‡å—å?ã¯ã€java.text.DecimalFormat を使用ã—㦠031 * フォーマットã•れã¾ã™ã? 032 * フォーマット変æ›å‰ã«ã€ã‚«ãƒ³ãƒžãªã©ã®æ•°å€¤å¤‰æ›æ™‚ã«ã‚¨ãƒ©ãƒ¼ã«ãªã‚‹æƒ…å ±ã‚’å‰Šé™¤ã—ã¦ãŠãã¾ã™ã? 033 * 標準ã?フォーマットã?"#,##0.#"ã§ã™ã? 034 * default値ãŒè¨å®šã•れã¦ã?ªã??åˆã?åˆæœŸå€¤ã¯0.0ã«ãƒ•ォーマットå?ç?‚’ã—ãŸã‚‚ã?ã§ã™ã? 035 * 036 * フォーマットルールã¯ã€{@link java.text.DecimalFormat} ã‚’å‚照願ã„ã¾ã™ã? 037 * 038 * カラãƒ??表示ã«å¿?¦ãªå±žæ?ã¯, DBColumn オブジェクãƒ?よりå–り出ã—ã¾ã™ã? 039 * ã“ã?クラスã¯ã€DBColumn オブジェクト毎ã«?‘ã¤ä½œæ?ã•れã¾ã™ã? 040 * 041 * @og.group ãƒ??ã‚¿ç·¨é›? 042 * @og.rev 3.8.8.5 (2007/03/09) æ–°è¦ä½œæ? 043 * @og.rev 5.4.3.6 (2012/01/19) ã‚³ãƒ¡ãƒ³ãƒˆä¿®æ£ 044 * 045 * @version 4.0 046 * @author Kazuhiko Hasegawa 047 * @since JDK5.0, 048 */ 049 public class Editor_DECIMAL extends AbstractEditor { 050 //* ã“ã?プãƒã‚°ãƒ©ãƒ??VERSIONæ–?—å?ã‚’è¨å®šã—ã¾ã™ã? {@value} */ 051 private static final String VERSION = "5.4.3.6 (2012/01/19)" ; 052 053 private final int minFraction; 054 private final DecimalFormat format ; 055 private final String defValue ; 056 057 /** 058 * ãƒ?ƒ•ォルトコンストラクターã€? 059 * ã“ã?コンストラクターã§ã€åŸºæœ¬ã‚ªãƒ–ジェクトを作æ?ã—ã¾ã™ã? 060 * 061 */ 062 public Editor_DECIMAL() { 063 // 4.3.4.4 (2009/01/01) 064 // super(); 065 minFraction = 0; 066 format = null; 067 defValue = null; 068 } 069 070 /** 071 * コンストラクターã€? 072 * 073 * @og.rev 5.3.1.0 (2009/12/01) åˆæœŸå€¤ãŒnullã®å ´åˆã?ã€defValueã¯nullã¨ã™ã‚‹ã€? 074 * @og.rev 5.4.0.0 (2011/10/01) RendererParamãŒèªã¿è¾¼ã¾ã‚Œã¦ã?‚‹ãƒã‚°ã‚’ä¿®æ£ 075 * 076 * @param clm DBColumnオブジェクãƒ? 077 */ 078 private Editor_DECIMAL( final DBColumn clm ) { 079 super( clm ); 080 minFraction = clm.getSizeY(); 081 082 // String fm = clm.getRendererParam(); 083 String fm = clm.getEditorParam(); 084 if( fm == null || fm.length() == 0 || fm.equals( "_" ) ) { 085 fm = "#,##0.#"; 086 } 087 format = new DecimalFormat( fm ); 088 089 // 5.3.1.0 (2009/12/01) 090 String defv = clm.getDefault(); 091 if( defv == null || defv.length() == 0 ) { 092 defValue = ""; 093 } 094 else { 095 double dd = 0.0; 096 if( defv != null && defv.length() != 0 && !defv.equals( "_" ) ) { 097 dd = StringUtil.parseDouble( defv ); 098 } 099 defValue = format.format( dd ); 100 } 101 102 // -XXX,XXX,XXX.YYY å½¢å¼ã§ã¯ã€ã‚«ãƒ³ãƒžã?å°æ•°ç‚¹ã€ã?イナスãªã©ã®é ?›®ãŒå?りã¾ã™ã? 103 int maxlength = (int)(clm.getSizeX() * 4.0/3.0) + minFraction + 2 ; // +2 ã¯ã€ã?イナスã¨å°‘数点 104 attributes.set( "maxlength" ,String.valueOf( maxlength ) ); 105 tagBuffer.add( XHTMLTag.inputAttri( attributes ) ); 106 } 107 108 /** 109 * å?‚ªãƒ–ジェクトã‹ã‚‰è?åˆ??インスタンスを返ã—ã¾ã™ã? 110 * 自åˆ??身をã‚ャãƒ?‚·ãƒ¥ã™ã‚‹ã®ã‹ã?æ–°ãŸã«ä½œæ?ã™ã‚‹ã®ã‹ã?ã€å„サブクラスã®å®Ÿè£?« 111 * ã¾ã‹ã•れã¾ã™ã? 112 * 113 * @param clm DBColumnオブジェクãƒ? 114 * 115 * @return CellEditorオブジェクãƒ? 116 */ 117 public CellEditor newInstance( final DBColumn clm ) { 118 return new Editor_DECIMAL( clm ); 119 } 120 121 /** 122 * ãƒ??ã‚¿ã®ç·¨é›?”¨æ–?—å?ã‚’è¿”ã—ã¾ã™ã? 123 * 124 * @param value 入力å? 125 * 126 * @return ãƒ??ã‚¿ã®ç·¨é›?”¨æ–?—å? 127 */ 128 @Override 129 public String getValue( final String value ) { 130 return super.getValue( formatValue( value ) ); 131 } 132 133 /** 134 * name属æ?を変ãˆãŸã?ãƒ??タ表示/ç·¨é›?”¨ã®HTMLæ–?—å?を作æ?ã—ã¾ã™ã? 135 * ãƒ??ブル上ã? name 㫠行番å·ã‚’ä»˜åŠ ã—ã¦ã€åå‰_è¡Œç•ªå· ã§ç™»éŒ²ã™ã‚‹ã‚ーを作æ?ã? 136 * ãƒªã‚¯ã‚¨ã‚¹ãƒˆæƒ…å ±ã‚’ï¼‘ã¤æ¯Žã?フィールドã§å‡¦ç?§ãã¾ã™ã? 137 * 138 * @param row è¡Œç•ªå· 139 * @param value 入力å? 140 * 141 * @return ãƒ??タ表示/ç·¨é›?”¨ã®æ–?—å? 142 */ 143 @Override 144 public String getValue( final int row,final String value ) { 145 return super.getValue( row,formatValue( value ) ); 146 } 147 148 /** 149 * å†?ƒ¨çš?ªãƒ•ォーマット変æ›å¾Œã?æ–?—å?ã‚’è¿”ã—ã¾ã™ã? 150 * 151 * @og.rev 5.3.1.0 (2009/12/01) 値ã?ã®å ´åˆã?ã€å?期å?ã‚’é©ç”¨ã™ã‚‹ã€? 152 * 153 * @param value 入力å? 154 * 155 * @return ãƒ??ã‚¿ã®è¡¨ç¤ºç”¨æ–?—å? 156 */ 157 private String formatValue( final String value ) { 158 // if( value == null || (value.trim()).length() == 0 ) { 159 if( value == null || (value.trim()).length() == 0 || "0".equals( value ) ) { 160 return defValue; 161 } 162 163 double dd = StringUtil.parseDouble( value ); 164 165 final String rtn ; 166 synchronized( format ) { 167 rtn = format.format( dd ); 168 } 169 170 return rtn; 171 } 172 }