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.fukurou.util.TagBuffer;
020import org.opengion.hayabusa.common.HybsSystem;
021import org.opengion.hayabusa.db.AbstractRenderer;
022import org.opengion.hayabusa.db.CellRenderer;
023import org.opengion.hayabusa.db.DBColumn;
024
025/**
026 * TEXTAREA レンデラは、カラムのデータをテキストエリアで表示する場合に
027 * 使用するクラスです。
028 * readonlyのテキストエリアでclass=renderer-textareaとして出力し、
029 * name属性は付けません。(データは送信されません)
030 * エリアの縦、横サイズはエディタのテキストエリアと同様にして算出されます。
031 *
032 * カラムの表示に必要な属性は, DBColumn オブジェクト より取り出します。
033 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。
034 *
035 * @og.rev 4.3.5.7 (2009/03/22) 新規作成
036 * @og.group データ編集
037 *
038 * @version  4.0
039 * @author   Takahashi Masakazu
040 * @since    JDK5.0,
041 */
042public class Renderer_TEXTAREA extends AbstractRenderer {
043        //* このプログラムのVERSION文字列を設定します。   {@value} */
044        private static final String VERSION = "4.3.5.7 (2009/03/22)" ;
045
046        private final int COLUMNS_MAXSIZE = HybsSystem.sysInt( "HTML_COLUMNS_MAXSIZE" ) ;       // 表示フィールドの大きさ
047        // viewタグで表示する場合のカラムの大きさ
048        private final int VIEW_COLUMNS_MAXSIZE = HybsSystem.sysInt( "HTML_VIEW_COLUMNS_MAXSIZE" ) ;
049
050        private final TagBuffer tagBuffer = new TagBuffer();
051
052        private  String         rows1;
053        private  String         rows2;
054        private  String         size1;
055        private  String         size2;
056
057        /**
058         * デフォルトコンストラクター。
059         * このコンストラクターで、基本オブジェクトを作成します。
060         *
061         */
062        public Renderer_TEXTAREA() {
063                // 何もありません。(super を呼び出しません)
064        }
065
066        /**
067         * コンストラクター
068         * textareaのサイズを決めるため、sizeとrowを決定する
069         * editorの計算を移植。
070         *
071         * @param       clm     DBColumnオブジェクト
072         */
073        private Renderer_TEXTAREA( final DBColumn clm ) {
074                String size = clm.getViewLength();
075                int maxlength = clm.getTotalSize();
076
077                if( size != null ) {
078                        if( size.indexOf( ',' ) >= 0 ) {
079                                size = size.substring( 0, size.indexOf( ',' ) );
080                        }
081                        size1 = size;
082                        size2 = size;
083                }
084                else {
085                        size1 = String.valueOf( clm.getFieldSize( maxlength, COLUMNS_MAXSIZE ) );
086                        size2 = String.valueOf( clm.getFieldSize( maxlength, VIEW_COLUMNS_MAXSIZE ) );
087                }
088
089                int r1 = ( clm.getTotalSize() / Integer.parseInt( size1 ) ) + 1;
090                if( r1 > 5 ) {
091                        rows1 = "5";
092                }
093                else {
094                        rows1 = String.valueOf( r1 );
095                }
096
097                int r2 = ( clm.getTotalSize() / Integer.parseInt( size2 ) ) + 1;
098                if( r2 > 5 ) {
099                        rows2 = "5";
100                }
101                else {
102                        rows2 = String.valueOf( r2 );
103                }
104
105                String param = StringUtil.nval( clm.getRendererParam(), clm.getViewLength() );
106                if( param != null && param.length() != 0 ) {
107                        int st = param.indexOf( ',' );
108                        if( st > 0 ) {
109                                rows1 = param.substring( 0, st );
110                                rows2 = rows1;
111                                size1 = param.substring( st + 1 );
112                                size2 = size1;
113                        }
114                }
115        }
116
117        /**
118         * 各オブジェクトから自分のインスタンスを返します。
119         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
120         * まかされます。
121         *
122         * @param       clm     DBColumnオブジェクト
123         *
124         * @return      CellEditorオブジェクト
125         */
126        public CellRenderer newInstance( final DBColumn clm ) {
127                return new Renderer_TEXTAREA( clm );
128        }
129
130        /**
131         * データの表示用文字列を返します。
132         *
133         * @param   value 入力値
134         *
135         * @return  データの表示用文字列
136         */
137        @Override
138        public String getValue( final String value ) {
139
140                TagBuffer tag = new TagBuffer( "textarea" );
141                tag.add( "cols"    , size1 );
142                tag.add( "rows"    , rows1 );
143                tag.add( "readonly", "readonly" );
144                tag.add( "class"   , "renderer-textarea" );
145                tag.add( tagBuffer.makeTag() );
146                tag.setBody( value );
147
148                return tag.makeTag();
149        }
150
151        /**
152         * データの表示用文字列を返します。
153         *
154         * @param   row   行番号
155         * @param   value 入力値
156         *
157         * @return  データ表示用の文字列
158         */
159        @Override
160        public String getValue( final int row,final String value ) {
161
162                TagBuffer tag = new TagBuffer( "textarea" );
163                tag.add( "cols"    , size2 );
164                tag.add( "rows"    , rows2 );
165                tag.add( "readonly", "readonly" );
166                tag.add( "class"   , "renderer-textarea" );
167                tag.add( tagBuffer.makeTag() );
168                tag.setBody( value );
169
170                return tag.makeTag( row,value );
171        }
172}