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
018// import java.util.StringTokenizer ;
019
020// import org.opengion.hayabusa.db.AbstractEditor;
021import org.opengion.hayabusa.db.CellEditor;
022import org.opengion.hayabusa.db.DBColumn;
023// import org.opengion.fukurou.util.XHTMLTag;
024import org.opengion.hayabusa.common.HybsSystem;
025// import org.opengion.fukurou.util.Attributes;
026import org.opengion.fukurou.util.StringUtil;
027// import org.opengion.fukurou.util.TagBuffer;
028
029// import static org.opengion.fukurou.util.StringUtil.isNull;                           // 6.1.1.0 (2015/01/17)
030
031/**
032 * AUTOAREA エディターは、カラムのデータをテキストエリアで編集する場合に
033 * 使用するクラスです。
034 *
035 * エリアの大きさは、表示する文字列によって、自動的に変更されます。
036 * 初期値や、文字数が小さい場合のサイズは、デフォルト値を使用するか、
037 * 編集パラメータに、x,y形式で指定された値を使います。
038 * 列方向での最大桁数を、指定することが可能です。システムパラメータで
039 * HTML_AUTOAREA_MAX_COL_SIZE を指定することで、折り返し列数の調整も
040 * 同時に行われます。0 が指定された場合は、無制限になります。
041 * HTML_AUTOAREA_MAX_ROW_SIZE を指定することで、行数の最大値を
042 * 指定することが可能です。0 が指定された場合は、無制限になります。
043 *
044 * 編集パラメータは、『行,列』指定可能です。例えば、5,10 とすると、5行10列の
045 * テキストエリアを最小範囲として設定できます。
046 * 初期値は、HTML_COLUMS_MAXSIZE で指定の列数と、定義されているデータサイズ
047 * 割る HTML_COLUMS_MAXSIZE で、5を超えない値を、行数としています。
048 * 編集パラメータの、『行,列』指定で、同時に、最大行列数の指定も可能です。
049 * 5-10,15-20 とすると、最小5行-最大10行で、最小15列-最大20列の
050 * テキストエリアを指定できます。編集パラメータでの最大値指定は、
051 * システムパラメータでの最大値指定より、優先されます。
052 *
053 * このエディターでは、カラムの内容に応じて、書込み禁止属性を強制的に付与する
054 * 事も可能です。
055 * value (検索結果)の先頭1文字目が、アンダーバー(_) の場合は、
056 * 編集モードになりません。(読取専用)
057 * データベースに書き込むときには、通常のアンダーバー無しの文字列に変換して登録します。
058 *
059 *  カラムの表示に必要な属性は、DBColumn オブジェクト より取り出します。
060 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。
061 *
062 * @og.rev 3.8.0.2 (2005/06/30) 新規追加。
063 * @og.rev 8.5.6.1 (2024/03/29) Editor_TEXTAREA を継承します。
064 * @og.group データ編集
065 *
066 * @version  8.5
067 * @author   Kazuhiko Hasegawa
068 * @since    JDK21.0,
069 */
070// 8.5.5.1 (2024/02/29) spotbugs CT_CONSTRUCTOR_THROW(コンストラクタで、Excweptionを出さない) class を final にすれば、警告は消える。
071// public class Editor_AUTOAREA extends AbstractEditor {
072// public final class Editor_AUTOAREA extends AbstractEditor {
073public final class Editor_AUTOAREA extends Editor_TEXTAREA {
074        /** このプログラムのVERSION文字列を設定します。   {@value} */
075        private static final String VERSION = "8.5.6.1 (2024/03/29)" ;
076
077        private static final int COL = 0 ;
078        private static final int ROW = 1 ;
079//      private static final String CODE = "Windows-31J";
080
081        private String autoArea = "autoArea( this );" ;         // 6.1.1.0 (2015/01/17) 固定値の設定
082
083//      private int     cols1   ;               // 検索時の列数
084//      private int     rows1   ;               // 検索時の行数
085//      private int     cols2   ;               // 登録時の列数
086//      private int     rows2   ;               // 登録時の行数
087        private int maxColSize = HybsSystem.sysInt( "HTML_AUTOAREA_MAX_COL_SIZE" );
088        private int maxRowSize = HybsSystem.sysInt( "HTML_AUTOAREA_MAX_ROW_SIZE" );
089
090        /**
091         * デフォルトコンストラクター。
092         * このコンストラクターで、基本オブジェクトを作成します。
093         *
094         */
095        public Editor_AUTOAREA() { super(); }           // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
096
097        /**
098         * DBColumnオブジェクトを指定したprivateコンストラクター。
099         *
100         * @og.rev 6.0.4.0 (2014/11/28) optionAttributes は、コンストラクタで設定します。
101         * @og.rev 7.0.5.1 (2019/09/27) optionAttributes が二重に設定されていたため、削除
102         * @og.rev 8.5.5.1 (2024/02/29) spotbugs CT_CONSTRUCTOR_THROW(コンストラクタで、Excweptionを出さない)
103         * @og.rev 8.5.6.1 (2024/03/29) Editor_TEXTAREA を継承します。
104         *
105         * @param       clm     DBColumnオブジェクト
106         */
107        private Editor_AUTOAREA( final DBColumn clm ) {
108                super( clm );
109//              final String  disabled = clm.isWritable() ? null : "disabled" ;
110//
111//              // 8.5.5.1 (2024/02/29) spotbugs CT_CONSTRUCTOR_THROW(コンストラクタで、Excweptionを出さない)
112////            final int r1 = clm.getTotalSize()/Integer.parseInt(size1) + 1;  // 4.0.0 (2005/01/31) メソッド名変更
113////            if( r1 > 5 ) { rows1 = 5; }
114////            else { rows1 = r1; }
115////
116////            final int r2 = clm.getTotalSize()/Integer.parseInt(size2) + 1;  // 4.0.0 (2005/01/31) メソッド名変更
117////            if( r2 > 5 ) { rows2 = 5; }
118////            else { rows2 = r2; }
119//              final int totalSize = clm.getTotalSize();                               // 8.5.5.1 (2024/02/29)
120//              rows1 = RowSizeUtil.getRowSizeInt( totalSize,size1 );   // 8.5.5.1 (2024/02/29)
121//              rows2 = RowSizeUtil.getRowSizeInt( totalSize,size2 );   // 8.5.5.1 (2024/02/29)
122//
123//              // 3.8.0.2 (2005/07/11) size に、"rows-maxRow,cols-maxCols" を指定
124//              final String param = StringUtil.nval( clm.getEditorParam(),clm.getViewLength() );
125//              if( param != null && param.length() != 0 ) {
126//                      final int prmAdrs = param.indexOf( ',' );
127//                      if( prmAdrs > 0 ) {
128//                              final String rowStr = param.substring( 0,prmAdrs );
129//                              final String colStr = param.substring( prmAdrs+1 );
130//
131//                              final int rowAdrs = rowStr.indexOf( '-' );      // rows-maxRow 設定時 '-' がなければ、ただのrows
132//                              if( rowAdrs > 0 ) {
133//                                      rows1 = Integer.parseInt( rowStr.substring( 0,rowAdrs ) );
134//                                      maxRowSize = Integer.parseInt( rowStr.substring( rowAdrs+1 ) );
135//                              }
136//                              else {
137//                                      rows1 = Integer.parseInt( rowStr );
138//                              }
139//                              rows2 = rows1 ;
140//
141//                              final int colAdrs = colStr.indexOf( '-' );      // cols-maxCols 設定時 '-' がなければ、ただのcols
142//                              if( colAdrs > 0 ) {
143//                                      cols1 = Integer.parseInt( colStr.substring( 0,colAdrs ) );
144//                                      maxColSize = Integer.parseInt( colStr.substring( colAdrs+1 ) );
145//                              }
146//                              else {
147//                                      cols1 = Integer.parseInt( colStr );
148//                              }
149//                              cols2 = cols1;
150//                      }
151//              }
152//
153//              // 6.1.1.0 (2015/01/17) Attributesの連結記述
154//              attributes = new Attributes()
155//                                      .set( "disabled"        ,disabled )
156//                                      .set( clm.getEditorAttributes() )                               // #addAttributes( Attributes ) の代替え
157//                                      .add( "class"           ,clm.getDbType() );                     // 4.0.0 (2005/01/31)
158//
159//              // 6.1.1.0 (2015/01/17) TagBufferの連結記述
160//              // 7.0.5.1 (2019/09/27) optionAttributes が二重に設定されていたため、削除
161//              tagBuffer.add( XHTMLTag.textareaAttri( attributes ) );
162////                             .add( attributes.get( "optionAttributes" ) );          // 6.0.4.0 (2014/11/28)
163
164                // AUTOAREA は、パラメータに、rows-maxRow,cols-maxCols 設定 がある。
165                final int rowAdrs = rows1.indexOf( '-' );       // rows-maxRow 設定時 '-' がなければ、ただのrows
166                if( rowAdrs > 0 ) {
167                        // ※ 注意: rows1 に substring した値をセットすると、maxRowSize が取れなくなる。
168                        rows2 = rows1.substring( 0,rowAdrs );
169                        maxRowSize = Integer.parseInt( rows1.substring( rowAdrs+1 ) );
170                        rows1 = rows2 ;
171                }
172
173                final int colAdrs = cols1.indexOf( '-' );       // cols-maxCols 設定時 '-' がなければ、ただのcols
174                if( colAdrs > 0 ) {
175                        // ※ 注意: cols1 に substring した値をセットすると、maxColSize が取れなくなる。
176                        cols2 = cols1.substring( 0,colAdrs );
177                        maxColSize = Integer.parseInt( cols1.substring( colAdrs+1 ) );
178                        cols1 = cols2;
179                }
180
181                // 6.1.1.0 (2015/01/17) 固定値の設定
182                if( maxRowSize > 0 ) {
183                        autoArea = "autoArea( this," + maxRowSize + " );" ;
184                }
185        }
186
187        /**
188         * 各オブジェクトから自分のインスタンスを返します。
189         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
190         * まかされます。
191         *
192         * @param       clm     DBColumnオブジェクト
193         *
194         * @return      CellEditorオブジェクト
195         */
196        public CellEditor newInstance( final DBColumn clm ) {
197                return new Editor_AUTOAREA( clm );
198        }
199
200        /**
201         * データの編集用文字列を返します。
202         *
203         * @og.rev 4.3.7.2 (2009/06/15) 属性でidが出力される場合は、idを出力しない
204         * @og.rev 5.1.2.0 (2010/01/01) 先頭の'_'による書き込み制御を行わない。(他のクラスとの実装の共通化)
205         * @og.rev 5.1.7.0 (2010/06/01) 動的プルダウン実装見直し
206         * @og.rev 8.5.6.1 (2024/03/29) Editor_TEXTAREA を継承します。
207         * @og.rev 8.5.6.1 (2024/03/29) Editor_AUTOAREA のメソッドをstatic化してColumnUtilに移動します。
208         *
209         * @param   value 値
210         *
211         * @return  データの編集用文字列
212         * @og.rtnNotNull
213         */
214        @Override
215        public String getValue( final String value ) {
216//              final int[] rowcol = getRowsCols( value,cols1,rows1 );
217                final String[] rowcol = ColumnUtil.getRowsCols( value,cols1,rows1,maxColSize,maxRowSize );              // 8.5.6.1 (2024/03/29)
218
219//              // 6.1.1.0 (2015/01/17) TagBufferの連結記述
220//              return new TagBuffer( "textarea" )
221//                                              .add( "name"    , name )
222//                                              // 8.5.4.2 (2024/01/12) import static … を個別に記述
223//                                              .add( "id"              , name , StringUtil.isNull( attributes.get( "id" ) ) )          // 4.3.7.2 (2009/06/15)
224////                                            .add( "cols"    , String.valueOf( rowcol[COL] ) )
225////                                            .add( "rows"    , String.valueOf( rowcol[ROW] ) )
226//                                              .add( "cols"    , rowcol[COL] )                                 // 8.5.6.1 (2024/03/29)
227//                                              .add( "rows"    , rowcol[ROW] )                                 // 8.5.6.1 (2024/03/29)
228//                                              .add( "onkeyup" , autoArea )
229//                                              .add( tagBuffer.makeTag() )
230//                                              .addBody( value )
231//                                              .makeTag();
232
233                return makeTagBuffer( name,rowcol[COL],rowcol[ROW] )
234                                        .add( "id"              , name , StringUtil.isNull( attributes.get( "id" ) ) )          // 4.3.7.2 (2009/06/15)
235                                        .add( "onkeyup" , autoArea )
236                                        .addBody( value )
237                                        .makeTag();
238        }
239
240        /**
241         * name属性を変えた、データ表示/編集用のHTML文字列を作成します。
242         * テーブル上の name に 行番号を付加して、名前_行番号 で登録するキーを作成し、
243         * リクエスト情報を1つ毎のフィールドで処理できます。
244         *
245         * @og.rev 4.3.7.2 (2009/06/15) 属性でidが出力される場合は、idを出力しない
246         * @og.rev 5.1.2.0 (2010/01/01) 先頭の'_'による書き込み制御を行わない。(他のクラスとの実装の共通化)
247         * @og.rev 5.1.7.0 (2010/06/01) 動的プルダウン実装見直し
248         * @og.rev 8.5.6.1 (2024/03/29) Editor_TEXTAREA を継承します。
249         * @og.rev 8.5.6.1 (2024/03/29) Editor_AUTOAREA のメソッドをstatic化してColumnUtilに移動します。
250         *
251         * @param   row   行番号
252         * @param   value 値
253         *
254         * @return  データ表示/編集用の文字列
255         * @og.rtnNotNull
256         */
257        @Override
258        public String getValue( final int row,final String value ) {
259//              final int[] rowcol   = getRowsCols( value,cols2,rows2 );
260                final String[] rowcol = ColumnUtil.getRowsCols( value,cols2,rows2,maxColSize,maxRowSize );              // 8.5.6.1 (2024/03/29)
261                final String newName = name + HybsSystem.JOINT_STRING + row;
262
263//              // 6.1.1.0 (2015/01/17) TagBufferの連結記述
264//              return new TagBuffer( "textarea" )
265//                                              .add( "name"    , newName )
266//                                              // 8.5.4.2 (2024/01/12) import static … を個別に記述
267//                                              .add( "id"              , newName , StringUtil.isNull( attributes.get( "id" ) ) )               // 4.3.7.2 (2009/06/15)
268////                                            .add( "cols"    , String.valueOf( rowcol[COL] ) )
269////                                            .add( "rows"    , String.valueOf( rowcol[ROW] ) )
270//                                              .add( "cols"    , rowcol[COL] )                                 // 8.5.6.1 (2024/03/29)
271//                                              .add( "rows"    , rowcol[ROW] )                                 // 8.5.6.1 (2024/03/29)
272//                                              .add( "onkeyup" , autoArea )
273//                                              .add( tagBuffer.makeTag() )
274//                                              .addBody( value )
275//                                              .makeTag( row,value );
276
277                return makeTagBuffer( newName,rowcol[COL],rowcol[ROW] )
278                                        .add( "id"              , newName , StringUtil.isNull( attributes.get( "id" ) ) )               // 4.3.7.2 (2009/06/15)
279                                        .add( "onkeyup" , autoArea )
280                                        .addBody( value )
281                                        .makeTag( row,value );
282        }
283
284//      /**
285//       * 自動表示する行列の数を求めます。
286//       * 行数は、引数の文字列中に含まれる 改行コードの個数を求めます。
287//       * 列数は、各行数のなかの最大桁数より求めます。これには半角、全角が含まれる為、
288//       * 半角換算での文字数ということになります。
289//       * 行数と列数が、初期設定の行数と列数より小さい場合は、初期設定値が使用されます。
290//       *
291//       * @og.rev 8.5.6.1 (2024/03/29) Editor_AUTOAREA のメソッドをstatic化してColumnUtilに移動します。
292//       *
293//       * @param   value       表示文字列
294//       * @param   cols        最小カラム数
295//       * @param   rows        最小行数
296//       *
297//       * @return      自動計算した行列の配列
298//       * @og.rtnNotNull
299//       */
300////    private int[] getRowsCols( final String value, final int cols, final int rows ) {
301//      private String[] getRowsCols( final String value, final String cols, final String rows ) {              // 8.5.6.1 (2024/03/29)
302//              if( value == null ) {
303////                    return new int[] { cols,rows };                                 // 6.3.9.1 (2015/11/27) 逆だったが、今まで大丈夫だった?
304//                      return new String[] { cols, rows };
305//              }
306//
307//              final StringTokenizer token = new StringTokenizer( value, "\n", true );
308//
309//              int cntRow = 1;
310//              int maxCol = 0;
311//              while( token.hasMoreTokens() ) {
312//                      final String val = token.nextToken();
313//                      if( "\n".equals( val ) ) { cntRow++; }
314//                      else {
315//                              final byte[] byteValue = StringUtil.makeByte( val,CODE );       // 3.5.5.3 (2004/04/09)
316//                              final int byteSize = byteValue.length;
317//                              if( maxColSize > 0 && byteSize > maxColSize ) {         // 最大列数
318//                                      cntRow += byteSize / maxColSize;
319//                                      maxCol = maxColSize ;
320//                              }
321//                              else if( byteSize > maxCol ) { maxCol = byteSize; }
322//                      }
323//                      if( maxRowSize > 0 && cntRow >= maxRowSize ) {          // 最大行数
324//                              cntRow = maxRowSize;
325//                              break;
326//                      }
327//              }
328//
329//              maxCol += 2;    // マージン。フォントや画面サイズに影響する為、比率のほうがよい?
330//
331//              // 6.3.9.1 (2015/11/27) Found 'DD'-anomaly for variable(PMD)
332//              maxCol = Math.max( Integer.parseInt( cols ),maxCol );           // COL
333//              cntRow = Math.max( Integer.parseInt( rows ),cntRow );           // ROW
334//
335//              //                                              COL                                                     ROW
336//              return new String[] { String.valueOf( maxCol ), String.valueOf( cntRow ) };
337//
338////            //                                      COL                                                     ROW
339////            return new int[] { Math.max( cols,maxCol ) , Math.max( rows,cntRow ) } ;        // 6.3.9.1 (2015/11/27)
340//      }
341}