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 java.util.StringTokenizer;
019    
020    import org.opengion.fukurou.util.StringUtil;
021    import org.opengion.fukurou.util.TagBuffer;
022    import org.opengion.hayabusa.common.HybsSystem;
023    import org.opengion.hayabusa.db.AbstractRenderer;
024    import org.opengion.hayabusa.db.CellRenderer;
025    import org.opengion.hayabusa.db.DBColumn;
026    
027    /**
028     * AUTOAREA レンãƒ?ƒ©ã¯ã€ã‚«ãƒ©ãƒ??ãƒ??タをテキストエリアã§è¡¨ç¤ºã™ã‚‹å ´åˆã«
029     * 使用ã™ã‚‹ã‚¯ãƒ©ã‚¹ã§ã™ã?
030     * readonlyã®ãƒ?‚­ã‚¹ãƒˆã‚¨ãƒªã‚¢ã§class=renderer-textareaã¨ã—ã¦å‡ºåŠ›ã—ã€?
031     * name属æ?ã¯ä»˜ã‘ã¾ã›ã‚“ã€?ãƒ??ã‚¿ã¯é€ä¿¡ã•れã¾ã›ã‚“)
032     * エリアã®ç¸¦ã€æ¨ªã‚µã‚¤ã‚ºã¯ã‚¨ãƒ?‚£ã‚¿ã®è‡ªå‹•æ‹¡å¼µãƒ?‚­ã‚¹ãƒˆã‚¨ãƒªã‚¢(AUTOAREA)ã¨åŒæ§˜ã«ã—ã¦ç®—å?ã•れã¾ã™ã?
033     *
034     * カラãƒ??表示ã«å¿?¦ãªå±žæ?ã¯, DBColumn オブジェクãƒ?よりå–り出ã—ã¾ã™ã?
035     * ã“ã?クラスã¯ã€DBColumn オブジェクト毎ã«?‘ã¤ä½œæ?ã•れã¾ã™ã?
036     *
037     * @og.rev 5.3.4.0 (2011/04/01) æ–°è¦ä½œæ?
038     * @og.group �?タ編�
039     *
040     * @version  4.0
041     * @author   Hiroki Nakamura
042     * @since    JDK5.0,
043     */
044    public class Renderer_AUTOAREA extends AbstractRenderer {
045            //* ã“ã?プログラãƒ??VERSIONæ–?­—å?を設定ã—ã¾ã™ã?       {@value} */
046            private static final String VERSION = "5.3.4.0 (2011/04/01)" ;
047    
048            private final int COLUMNS_MAXSIZE = HybsSystem.sysInt( "HTML_COLUMNS_MAXSIZE" );
049            // viewã‚¿ã‚°ã§è¡¨ç¤ºã™ã‚‹å ´åˆã?カラãƒ??大ãã•
050            private final int VIEW_COLUMNS_MAXSIZE = HybsSystem.sysInt( "HTML_VIEW_COLUMNS_MAXSIZE" );
051    
052            private  String         size1;
053            private  String         size2;
054    
055            private static final int COL = 0 ;
056            private static final int ROW = 1 ;
057            private static final String CODE = "Windows-31J";
058    
059            private int     cols1   = 0 ;
060            private int     cols2   = 0 ;
061            private int     rows1   = 0 ;
062            private int     rows2   = 0 ;
063            private int maxColSize = HybsSystem.sysInt( "HTML_AUTOAREA_MAX_COL_SIZE" );
064            private int maxRowSize = HybsSystem.sysInt( "HTML_AUTOAREA_MAX_ROW_SIZE" );
065    
066            private final TagBuffer tagBuffer = new TagBuffer();
067    
068            /**
069             * ãƒ?ƒ•ォルトコンストラクターã€?
070             * ã“ã?コンストラクターã§ã€åŸºæœ¬ã‚ªãƒ–ジェクトを作æ?ã—ã¾ã™ã?
071             *
072             */
073            public Renderer_AUTOAREA() {
074                    // 何もã‚りã¾ã›ã‚“ã€?super を呼ã³å‡ºã—ã¾ã›ã‚“)
075            }
076    
077            /**
078             * コンストラクター
079             * textareaã®ã‚µã‚¤ã‚ºã‚’決ã‚ã‚‹ãŸã‚ã€sizeã¨rowを決定ã™ã‚?
080             * editorã®è¨ˆç®—ã‚’ç§»æ¤ã?
081             *
082             * @param       clm     DBColumnオブジェク�
083             */
084            private Renderer_AUTOAREA( final DBColumn clm ) {
085                    String size = clm.getViewLength();
086                    int maxlength = clm.getTotalSize();
087    
088                    if( size != null ) {
089                            if( size.indexOf( ',' ) >= 0 ) {
090                                    size = size.substring( 0, size.indexOf( ',' ) );
091                            }
092                            size1 = size;
093                            size2 = size;
094                    }
095                    else {
096                            size1 = String.valueOf( clm.getFieldSize( maxlength, COLUMNS_MAXSIZE ) );
097                            size2 = String.valueOf( clm.getFieldSize( maxlength, VIEW_COLUMNS_MAXSIZE ) );
098                    }
099    
100                    int r1 = (clm.getTotalSize()/Integer.parseInt(size1)) + 1;      // 4.0.0 (2005/01/31) メソãƒ?ƒ‰å変更
101                    if( r1 > 5 ) { rows1 = 5; }
102                    else { rows1 = r1; }
103    
104                    int r2 = (clm.getTotalSize()/Integer.parseInt(size2)) + 1;      // 4.0.0 (2005/01/31) メソãƒ?ƒ‰å変更
105                    if( r2 > 5 ) { rows2 = 5; }
106                    else { rows2 = r2; }
107    
108                    // 3.8.0.2 (2005/07/11) size ã«ã€?rows-maxRow,cols-maxCols" を指å®?
109                    String param = StringUtil.nval( clm.getRendererParam(),clm.getViewLength() );
110                    if( param != null && param.length() != 0 ) {
111                            int prmAdrs = param.indexOf( ',' );
112                            if( prmAdrs > 0 ) {
113                                    String rowStr = param.substring( 0,prmAdrs );
114                                    String colStr = param.substring( prmAdrs+1 );
115    
116                                    int rowAdrs = rowStr.indexOf( '-' );    // rows-maxRow 設定時 '-' ãŒãªã‘れã°ã€ãŸã??rows
117                                    if( rowAdrs > 0 ) {
118                                            rows1 = Integer.parseInt( rowStr.substring( 0,rowAdrs ) );
119                                            maxRowSize = Integer.parseInt( rowStr.substring( rowAdrs+1 ) );
120                                    }
121                                    else {
122                                            rows1 = Integer.parseInt( rowStr );
123                                    }
124                                    rows2 = rows1 ;
125    
126                                    int colAdrs = colStr.indexOf( '-' );    // cols-maxCols 設定時 '-' ãŒãªã‘れã°ã€ãŸã??cols
127                                    if( colAdrs > 0 ) {
128                                            cols1 = Integer.parseInt( colStr.substring( 0,colAdrs ) );
129                                            maxColSize = Integer.parseInt( colStr.substring( colAdrs+1 ) );
130                                    }
131                                    else {
132                                            cols1 = Integer.parseInt( colStr );
133                                    }
134                                    cols2 = cols1;
135                            }
136                    }
137            }
138    
139            /**
140             * å?‚ªãƒ–ジェクトã‹ã‚‰è?åˆ??インスタンスを返ã—ã¾ã™ã?
141             * 自åˆ??身をキャãƒ?‚·ãƒ¥ã™ã‚‹ã®ã‹ã?æ–°ãŸã«ä½œæ?ã™ã‚‹ã®ã‹ã?ã€å„サブクラスã®å®Ÿè£?«
142             * ã¾ã‹ã•れã¾ã™ã?
143             *
144             * @param       clm     DBColumnオブジェク�
145             *
146             * @return      CellRendererオブジェク�
147             */
148            public CellRenderer newInstance( final DBColumn clm ) {
149                    return new Renderer_AUTOAREA( clm );
150            }
151    
152            /**
153             * ãƒ??ã‚¿ã®è¡¨ç¤ºç”¨æ–?­—å?ã‚’è¿”ã—ã¾ã™ã?
154             *
155             * @param   value 入力å?
156             *
157             * @return  ãƒ??ã‚¿ã®è¡¨ç¤ºç”¨æ–?­—å?
158             */
159            @Override
160            public String getValue( final String value ) {
161    
162                    int[] rowcol = getRowsCols( value,cols1,rows1 );
163    
164                    TagBuffer tag = new TagBuffer( "textarea" );
165                    tag.add( "cols"    , String.valueOf( rowcol[COL] ) );
166                    tag.add( "rows"    , String.valueOf( rowcol[ROW] ) );
167                    tag.add( "readonly", "readonly" );
168                    tag.add( "class"   , "renderer-textarea" );
169                    tag.add( tagBuffer.makeTag() );
170                    tag.setBody( value );
171    
172                    return tag.makeTag();
173            }
174    
175            /**
176             * ãƒ??ã‚¿ã®è¡¨ç¤ºç”¨æ–?­—å?ã‚’è¿”ã—ã¾ã™ã?
177             *
178             * @param   row   行番å·
179             * @param   value 入力å?
180             *
181             * @return  ãƒ??ã‚¿è¡¨ç¤ºç”¨ã®æ–?­—å?
182             */
183            @Override
184            public String getValue( final int row,final String value ) {
185    
186                    int[] rowcol = getRowsCols( value,cols2,rows2 );
187    
188                    TagBuffer tag = new TagBuffer( "textarea" );
189                    tag.add( "cols"    , String.valueOf( rowcol[COL] ) );
190                    tag.add( "rows"    , String.valueOf( rowcol[ROW] ) );
191                    tag.add( "readonly", "readonly" );
192                    tag.add( "class"   , "renderer-textarea" );
193                    tag.add( tagBuffer.makeTag() );
194                    tag.setBody( value );
195    
196                    return tag.makeTag( row,value );
197            }
198    
199            /**
200             * 自動表示ã™ã‚‹è¡Œå?ã®æ•°ã‚’求ã‚ã¾ã™ã?
201             * 行数ã¯ã€å¼•æ•°ã®æ–?­—å?中ã«å«ã¾ã‚Œã‚‹ 改行コードã?個数を求ã‚ã¾ã™ã?
202             * 列数ã¯ã€å„行数ã®ãªã‹ã?æœ?¤§æ¡æ•°ã‚ˆã‚Šæ±‚ã‚ã¾ã™ã?ã“れã«ã¯åŠè§’ã?全角ãŒå«ã¾ã‚Œã‚‹ç‚ºã€?
203             * åŠè§’æ›ç®—ã§ã®æ–?­—æ•°ã¨ã?†ã“ã¨ã«ãªã‚Šã¾ã™ã?
204             * 行数ã¨åˆ—æ•°ãŒã?åˆæœŸè¨­å®šã?行数ã¨åˆ—数よりå°ã•ã??åˆã?ã€å?期設定å?ãŒä½¿ç”¨ã•れã¾ã™ã?
205             *
206             * @param   value 入力å? 表示æ–?­—å?
207             * @param       cols    æœ?°ã‚«ãƒ©ãƒ?•°
208             * @param       rows    æœ?°è¡Œæ•°
209             *
210             * @return      自動計算ã—ãŸè¡Œå?ã®é…å?
211             */
212            private int[] getRowsCols( final String value,final int cols, final int rows ) {
213                    if( value == null ) {
214                            return new int[] { rows,cols };
215                    }
216    
217                    StringTokenizer token = new StringTokenizer( value, "\n", true );
218    
219                    int cntRow = 1;
220                    int maxCol = 0;
221                    while ( token.hasMoreTokens() ) {
222                            String val = token.nextToken();
223                            if( "\n".equals( val ) ) { cntRow++; }
224                            else {
225                                    byte[] byteValue = StringUtil.makeByte( val,CODE );     // 3.5.5.3 (2004/04/09)
226                                    int byteSize = byteValue.length;
227                                    if( maxColSize > 0 && byteSize > maxColSize ) {           // æœ?¤§åˆ—æ•°
228                                            cntRow += (byteSize / maxColSize);
229                                            maxCol = maxColSize ;
230                                    }
231                                    else if( byteSize > maxCol ) { maxCol = byteSize; }
232                            }
233                            if( maxRowSize > 0 && cntRow >= maxRowSize ) {            // æœ?¤§è¡Œæ•°
234                                    cntRow = maxRowSize;
235                                    break;
236                            }
237                    }
238    
239                    maxCol += 2;    // マã?ジン。フォントや画é¢ã‚µã‚¤ã‚ºã«å½±éŸ¿ã™ã‚‹ç‚ºã€æ¯”率ã®ã»ã?Œã‚ˆã„??
240    
241                    int[] rtn = new int[2];
242                    rtn[ROW] = (rows<cntRow) ? cntRow : rows ;
243                    rtn[COL] = (cols<maxCol) ? maxCol : cols ;
244    
245                    return rtn ;
246            }
247    }