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 = "7.4.2.0 (2021/04/30)" ;
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();    // 7.4.2.0 (2021/04/30)
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() { super(); }         // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
063
064        /**
065         * DBColumnオブジェクトを指定したprivateコンストラクター。
066         *
067         * textareaのサイズを決めるため、sizeとrowを決定する
068         * editorの計算を移植。
069         *
070         * @og.rev 6.2.0.0 (2015/02/27) フィールドサイズ 追加(VIEW_LENGTHと分離して、役割を明確にする)
071         *
072         * @param       clm     DBColumnオブジェクト
073         */
074        private Renderer_TEXTAREA( final DBColumn clm ) {
075                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
076                super();
077
078                // 6.2.0.0 (2015/02/27) フィールドサイズ 追加(VIEW_LENGTHと分離して、役割を明確にする)
079                size1 = String.valueOf( clm.getFieldSize( COLUMNS_MAXSIZE      ) );
080                size2 = String.valueOf( clm.getFieldSize( VIEW_COLUMNS_MAXSIZE ) );
081
082                final int r1 = clm.getTotalSize() / Integer.parseInt( size1 ) + 1;
083                if( r1 > 5 ) {
084                        rows1 = "5";
085                }
086                else {
087                        rows1 = String.valueOf( r1 );
088                }
089
090                final int r2 = clm.getTotalSize() / Integer.parseInt( size2 ) + 1;
091                if( r2 > 5 ) {
092                        rows2 = "5";
093                }
094                else {
095                        rows2 = String.valueOf( r2 );
096                }
097
098                final String param = StringUtil.nval( clm.getRendererParam(), clm.getViewLength() );
099                if( param != null && param.length() != 0 ) {
100                        final int st = param.indexOf( ',' );
101                        if( st > 0 ) {
102                                rows1 = param.substring( 0, st );
103                                rows2 = rows1;
104                                size1 = param.substring( st + 1 );
105                                size2 = size1;
106                        }
107                }
108        }
109
110        /**
111         * 各オブジェクトから自分のインスタンスを返します。
112         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
113         * まかされます。
114         *
115         * @param       clm     DBColumnオブジェクト
116         *
117         * @return      CellEditorオブジェクト
118         * @og.rtnNotNull
119         */
120        public CellRenderer newInstance( final DBColumn clm ) {
121                return new Renderer_TEXTAREA( clm );
122        }
123
124        /**
125         * データの表示用文字列を返します。
126         *
127         * @og.rev 6.0.4.0 (2014/11/28) ロジックの共通化
128         *
129         * @param   value 入力値
130         *
131         * @return  データの表示用文字列
132         * @og.rtnNotNull
133         */
134        @Override
135        public String getValue( final String value ) {
136                return getRowsColsValue( value==null ? "" : value,size1,rows1 );
137        }
138
139        /**
140         * データの表示用文字列を返します。
141         *
142         * @og.rev 6.0.4.0 (2014/11/28) ロジックの共通化
143         *
144         * @param   row   行番号
145         * @param   value 入力値
146         *
147         * @return  データ表示用の文字列
148         * @og.rtnNotNull
149         */
150        @Override
151        public String getValue( final int row,final String value ) {
152                return getRowsColsValue( value==null ? "" : value,size2,rows2 );
153        }
154
155        /**
156         * データ出力用の文字列を作成します。
157         * ファイル等に出力する形式を想定しますので、HTMLタグを含まない
158         * データを返します。
159         * 基本は、#getValue( String ) をそのまま返します。
160         *
161         * @og.rev 6.0.4.0 (2014/11/28) データ出力用のレンデラー
162         *
163         * @param   value 入力値
164         *
165         * @return  データ出力用の文字列
166         * @og.rtnNotNull
167         * @see         #getValue( String )
168         */
169        @Override
170        public String getWriteValue( final String value ) {
171                return value == null ? "" : value;
172        }
173
174        /**
175         * データの表示用文字列を返します。
176         *
177         * @og.rev 6.0.4.0 (2014/11/28) ロジックの共通化
178         * @og.rev 7.4.2.0 (2021/04/30) tagBuffer は使っていないので削除
179         *
180         * @param   value 入力値 表示文字列
181         * @param       cols    最小カラム数
182         * @param       rows    最小行数
183         *
184         * @return  データ表示用の文字列
185         * @og.rtnNotNull
186         */
187        private String getRowsColsValue( final String value,final String cols, final String rows ) {
188                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
189                return new TagBuffer( "textarea" )
190                                        .add( "cols"    , cols )
191                                        .add( "rows"    , rows )
192                                        .add( "readonly", "readonly" )
193                                        .add( "class"   , "renderer-textarea" )
194//                                      .add( tagBuffer.makeTag() )                                     7.4.2.0 (2021/04/30)
195                                        .addBody( value )
196                                        .makeTag();
197        }
198}