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.view; 017 018import java.util.List; 019import java.util.regex.Pattern; // 6.4.3.3 (2016/03/04) 020 021import org.opengion.hayabusa.common.HybsSystemException; 022import org.opengion.fukurou.util.StringUtil; 023import org.opengion.hayabusa.html.TableFormatter; 024 025/** 026 * ヘッダ、フッタ、ボディを指定して作成する、自由レイアウトが可能な、カスタムテーブル表示クラスです。 027 * このクラスは、ViewForm_HTMLFormatTable クラスの代替えとしても使用できます。 028 * その場合は、thead のみ指定すれば、同じフォームが tbody にも適用されます。 029 * これは、まさに、ViewForm_HTMLFormatTable と同じです。 030 * 031 * AbstractViewForm により、setter/getterメソッドのデフォルト実装を提供しています。 032 * 各HTMLのタグに必要な setter/getterメソッドのみ,追加定義しています。 033 * 034 * AbstractViewForm を継承している為,ロケールに応じたラベルを出力させる事が出来ます。 035 * 036 * @og.group 画面表示 037 * 038 * @version 4.0 039 * @author Kazuhiko Hasegawa 040 * @since JDK5.0, 041 */ 042public class ViewForm_HTMLCustomTable extends ViewForm_HTMLTable { 043 /** このプログラムのVERSION文字列を設定します。 {@value} */ 044 private static final String VERSION = "6.8.2.0 (2017/10/13)" ; 045 046 // 6.4.3.3 (2016/03/04) class属性(ColumnのDBType)置換で、td属性に[カラム]があると、誤ってそちらがセットされてしまう対応。 047 // ※ 正規表現の「.」は、改行を除く任意の文字なので、改行を含みたい場合は、「[\\s\\S]」とします。 048 private static final Pattern TD_BODY = Pattern.compile( "[\\s\\S]*<td[^>]*>[\\s]*" ); // 6.4.4.2 (2016/04/01) tdタグが完了して、BODY部分の解析に入る。 049 private static final Pattern TD_END = Pattern.compile( "[^<]*>[^<>]*" ); // 何らかのタグが完了して、BODY部分の解析に入る。 050 051 // 3.5.4.0 (2003/11/25) TableFormatter クラス追加 052 // 4.3.1.0 (2008/09/08) protectedに変更 053 /** ヘッダーフォーマット変数 */ 054 protected TableFormatter headerFormat ; 055 /** ボディーフォーマット配列変数 */ 056 protected TableFormatter[] bodyFormats ; 057 /** フッターフォーマット変数 */ 058 protected TableFormatter footerFormat ; 059 /** ボディーフォーマット数 */ 060 protected int bodyFormatsCount; 061 062 // 3.5.4.6 (2004/01/30) 初期値変更 063 /** ボディーフォーマット最大数 初期値:{@value} */ 064 protected static final int BODYFORMAT_MAX_COUNT = 15; // 5.7.6.2 (2014/05/16) 065 066 /** 067 * デフォルトコンストラクター 068 * 069 * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor. 070 */ 071 public ViewForm_HTMLCustomTable() { super(); } // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。 072 073 /** 074 * DBTableModel から HTML文字列を作成して返します。 075 * startNo(表示開始位置)から、pageSize(表示件数)までのView文字列を作成します。 076 * 表示残りデータが pageSize 以下の場合は,残りのデータをすべて出力します。 077 * 078 * @og.rev 3.5.0.0 (2003/09/17) BODY要素の noClass 属性を追加。 079 * @og.rev 3.5.0.0 (2003/09/17) <tr>属性は、元のフォーマットのまま使用します。 080 * @og.rev 3.5.2.0 (2003/10/20) ヘッダー繰り返し属性( headerSkipCount )を採用 081 * @og.rev 3.5.3.1 (2003/10/31) skip属性を採用。headerLine のキャッシュクリア 082 * @og.rev 3.5.4.0 (2003/11/25) TableFormatter クラスを使用するように変更。 083 * @og.rev 3.5.5.0 (2004/03/12) systemFormat(例:[KEY.カラム名]形式等)の対応 084 * @og.rev 3.5.5.0 (2004/03/12) No 欄そのものの作成判断ロジックを追加 085 * @og.rev 3.5.5.7 (2004/05/10) [#カラム名] , [$カラム名] に対応 086 * @og.rev 3.5.6.0 (2004/06/18) '!' 値のみ 追加 既存の '$' は、レンデラー 087 * @og.rev 3.5.6.2 (2004/07/05) makeFormat 処理をgetTableHead メソッドから移動 088 * @og.rev 3.5.6.4 (2004/07/16) ヘッダーとボディー部をJavaScriptで分離 089 * @og.rev 4.0.0.0 (2005/01/31) 新規作成(getColumnClassName ⇒ getColumnDbType) 090 * @og.rev 3.7.0.3 (2005/03/01) getBgColorCycleClass に、選択行マーカーを採用 091 * @og.rev 4.3.1.0 (2008/09/08) フォーマットが設定されていない場合のエラー追加・編集行のみを表示する属性(isSkipNoEdit)追加 092 * @og.rev 4.3.3.0 (2008/10/01) noTransition属性対応 093 * @og.rev 4.3.7.4 (2009/07/01) tbodyタグの入れ子を解消(FireFox対応) 094 * @og.rev 6.2.0.0 (2015/02/27) フォーマット系の noDisplay 対応 095 * @og.rev 6.4.3.3 (2016/03/04) class属性(ColumnのDBType)置換で、td属性に[カラム]があると、誤ってそちらがセットされてしまう対応。 096 * @og.rev 6.4.3.4 (2016/03/11) tdに、[カラム]が無いケースで、次の[カラム]のクラス属性が、前方すべてのtdにセットされてしまう対応。 097 * @og.rev 6.4.4.2 (2016/04/01) TableFormatterのタイプ別値取得処理の共通部をまとめる。 098 * @og.rev 6.4.5.0 (2016/04/08) メソッド変更( getColumnDbType(int) → getClassName(int) ) 099 * @og.rev 6.8.1.1 (2017/07/22) ckboxTD変数は、<td> から <td に変更します(タグの最後が記述されていない状態でもらう)。 100 * @og.rev 6.8.1.1 (2017/07/22) 行番号のtdに、個別に class="S9" を追加するための useS9 変数を追加。 101 * 102 * @param startNo 表示開始位置 103 * @param pageSize 表示件数 104 * 105 * @return DBTableModelから作成された HTML文字列 106 * @og.rtnNotNull 107 */ 108 @Override 109 public String create( final int startNo, final int pageSize ) { 110 if( getRowCount() == 0 ) { return ""; } // 暫定処置 111 112 // 4.3.1.0 (2008/09/08) 113 if( headerFormat == null ) { 114 final String errMsg = "ViewTagで canUseFormat() = true の場合、Formatter は必須です。"; 115 throw new HybsSystemException( errMsg ); 116 } 117 118 headerLine = null; // 3.5.3.1 (2003/10/31) キャッシュクリア 119 120 final int lastNo = getLastNo( startNo, pageSize ); 121 final int blc = getBackLinkCount(); 122 final int hsc = getHeaderSkipCount(); // 3.5.2.0 (2003/10/20) 123 int hscCnt = 1; // 3.5.2.0 (2003/10/20) 124 125 headerFormat.makeFormat( getDBTableModel() ); // 3.5.6.2 (2004/07/05) 移動 126 // 6.2.0.0 (2015/02/27) フォーマット系の noDisplay 対応 127 setFormatNoDisplay( headerFormat ); 128 129 final StringBuilder out = new StringBuilder( BUFFER_LARGE ) 130 .append( getCountForm( startNo,pageSize ) ) 131 .append( getHeader() ); 132 133 if( bodyFormatsCount == 0 ) { 134 bodyFormats[0] = headerFormat ; 135 bodyFormatsCount ++ ; 136 } 137 else { 138 for( int i=0; i<bodyFormatsCount; i++ ) { 139 bodyFormats[i].makeFormat( getDBTableModel() ); 140 // 6.2.0.0 (2015/02/27) フォーマット系の noDisplay 対応 141 setFormatNoDisplay( bodyFormats[i] ); 142 } 143 } 144 145 int bgClrCnt = 0; 146 for( int row=startNo; row<lastNo; row++ ) { 147 if( isSkip( row ) || isSkipNoEdit( row ) ) { continue; } // 4.3.1.0 (2008/09/08) 148 for( int i=0; i<bodyFormatsCount; i++ ) { 149 final TableFormatter bodyFormat = bodyFormats[i]; 150 if( ! bodyFormat.isUse( row,getDBTableModel() ) ) { continue; } // 3.5.4.0 (2003/11/25) 151 out.append("<tbody").append( getBgColorCycleClass( bgClrCnt++,row ) ); 152 if( isNoTransition() ) { // 4.3.3.0 (2008/10/01) 153 out.append( getHiddenRowValue( row ) ); 154 } 155 out.append('>') // 6.0.2.5 (2014/10/31) char を append する。 156 .append( bodyFormat.getTrTag() ); 157 158 // 3.5.5.0 (2004/03/12) No 欄そのものの作成判断追加 159 if( isNumberDisplay() ) { 160 final String ckboxTD = "<td" + bodyFormat.getRowspan(); // 6.8.1.1 (2017/07/22) 161 out.append( makeCheckbox( ckboxTD,row,blc,true ) ); // 6.8.1.1 (2017/07/22) 行番号のtdに、個別に class="S9" を追加 162 } 163 164 int cl = 0; 165 boolean isTdBody = false; // 6.4.3.3 (2016/03/04) tdBody処理を行ってよいかどうかのフラグ 166 for( ; cl<bodyFormat.getLocationSize(); cl++ ) { 167 String fmt = bodyFormat.getFormat(cl); 168 final int loc = bodyFormat.getLocation(cl); // 3.5.5.0 (2004/03/12) 169 if( ! bodyFormat.isNoClass() && loc >= 0 ) { // 3.5.5.7 (2004/05/10) 170 if( isTdBody && TD_END.matcher( fmt ).matches() ) { // 6.4.3.3 (2016/03/04) tdBody許可があり、TDが閉じた場合。 171 isTdBody = false; 172 final int idx = out.lastIndexOf( "<td" ); 173 if( idx >= 0 ) { 174 final String tdclass = " class=\"" + getClassName(loc) + "\" "; // 6.4.5.0 (2016/04/08) 175 out.insert( idx+3, tdclass ); // +3 は、"<td" の後ろという意味。fmtでなくそれ以前に戻る必要がある。 176 } 177 } 178 179 if( TD_BODY.matcher( fmt ).matches() ) { // 6.4.3.3 (2016/03/04) TDが閉じた場合。 180 // 6.4.3.4 (2016/03/11) tdに、[カラム]が無いケースで、次の[カラム]のクラス属性が、前方すべてのtdにセットされてしまう対応。 181 // 上の処理と統一しなければいけないが、とりあえず暫定対応 182 final int idx = fmt.lastIndexOf( "<td" ); 183 if( idx >= 0 ) { // matchしてるので、あるはず 184 final String tdclass = " class=\"" + getClassName(loc) + "\" "; // 6.4.5.0 (2016/04/08) 185 fmt = fmt.substring( 0,idx+3 ) + tdclass + fmt.substring( idx+3 ) ; 186 } 187 isTdBody = false; // 6.4.3.3 (2016/03/04) これは、要らない・・・はず。 188 } 189 else { 190 isTdBody = true; // TDが閉じていない。 191 } 192 } 193 out.append( fmt ); // 3.5.0.0 194 195 // 3.5.5.7 (2004/05/10) #,$ 対応 196 if( loc >= 0 ) { 197 // 6.4.4.2 (2016/04/01) 処理の共通部をまとめる。 198 out.append( getTypeCaseValue( bodyFormat.getType(cl),row,loc ) ); 199 } 200 else { 201 out.append( bodyFormat.getSystemFormat(row,loc) ); 202 } 203 } 204 out.append( bodyFormat.getFormat(cl) ) 205 .append("</tbody>").append( CR ); 206 } 207 208 // 3.5.2.0 (2003/10/20) ヘッダー繰り返し属性( headerSkipCount )を採用 209 if( hsc > 0 && hscCnt % hsc == 0 ) { 210 out.append("<tbody class=\"row_h\" >") 211 .append( getHeadLine() ) 212 .append("</tbody>"); 213 hscCnt = 1; 214 } 215 else { 216 hscCnt ++ ; 217 } 218 } 219 220 if( footerFormat != null ) { 221 // 6.3.9.0 (2015/11/06) 引数にTableFormatterを渡して、処理の共有化を図る。 222 out.append( getTableFoot( footerFormat ) ); 223 } 224 225 out.append("</table>").append( CR ) 226 .append( getScrollBarEndDiv() ); // 3.8.0.3 (2005/07/15) 227 228 return out.toString(); 229 } 230 231 /** 232 * 内容をクリア(初期化)します。 233 * 234 * @og.rev 3.1.1.0 (2003/03/28) 同期メソッド(synchronized付き)を非同期に変更する。 235 * @og.rev 3.5.0.0 (2003/09/17) Noカラムに、表示を全て消せるように、class 属性を追加。 236 * @og.rev 3.5.4.0 (2003/11/25) TableFormatter クラスを使用するように変更。 237 * 238 */ 239 @Override 240 public void clear() { 241 super.clear(); 242 headerFormat = null; 243 bodyFormats = null; 244 footerFormat = null; 245 bodyFormatsCount = 0; 246 } 247 248 /** 249 * DBTableModel から テーブルのタグ文字列を作成して返します。 250 * 251 * @og.rev 3.5.0.0 (2003/09/17) <tr>属性は、元のフォーマットのまま使用します。 252 * @og.rev 3.5.1.0 (2003/10/03) Noカラムに、numberType 属性を追加 253 * @og.rev 3.5.2.0 (2003/10/20) ヘッダー繰り返し部をgetHeadLine()へ移動 254 * @og.rev 3.5.3.1 (2003/10/31) VERCHAR2 を VARCHAR2 に修正。 255 * @og.rev 3.5.4.0 (2003/11/25) TableFormatter クラスを使用するように変更。 256 * @og.rev 3.5.5.0 (2004/03/12) No 欄そのものの作成判断ロジックを追加 257 * @og.rev 3.5.6.2 (2004/07/05) makeFormat 処理をcreate メソッドの頭に移動 258 * @og.rev 3.5.6.5 (2004/08/09) thead に、id="header" を追加 259 * @og.rev 4.0.0.0 (2005/01/31) DBColumn の 属性(CLS_NM)から、DBTYPEに変更 260 * @og.rev 5.9.1.2 (2015/10/23) 自己終了警告対応 261 * @og.rev 6.4.4.1 (2016/03/18) NUMBER_DISPLAYを、static final 定数化します。 262 * @og.rev 6.4.9.0 (2016/07/23) colgroupのHTML5対応(No欄) 263 * @og.rev 6.4.9.1 (2016/08/05) colgroupのHTML5対応(No欄)時の対応ミス修正 264 * @og.rev 6.8.1.0 (2017/07/14) HTML5対応ヘッダー出力設定時に、ブラウザを互換設定したときの対応。 265 * @og.rev 6.8.2.0 (2017/10/13) makeNthChildの廃止と、makeCheckboxで、個別にclass指定するように変更。 266 * 267 * @return テーブルのタグ文字列 268 * @og.rtnNotNull 269 */ 270 @Override 271 protected String getTableHead() { 272 final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE ); 273 274 // 3.5.5.0 (2004/03/12) No 欄そのものの作成判断追加 275 if( isNumberDisplay() ) { 276 // 6.4.9.0 (2016/07/23) colgroupのHTML5対応(No欄) 277 buf.append( NUMBER_DISPLAY ); // 6.8.1.0 (2017/07/14) HTML5ネイティブ時でも、出力します。 278 // 6.8.1.0 (2017/07/14) HTML5対応ヘッダー出力設定時に、ブラウザを互換設定したときの対応。 279 // 6.8.2.0 (2017/10/13) makeNthChildの廃止と、makeCheckboxで、個別にclass指定するように変更。 280 // if( !useIE7Header ) { 281 // buf.append( "<style type=\"text/css\">" ).append( CR ); 282 // makeNthChild( buf,2,"BIT" ); 283 // makeNthChild( buf,3,"S9" ); 284 // buf.append( "</style>" ).append( CR ); // 6.4.9.1 (2016/08/05) 285 // } 286 // 5.9.1.2 (2015/10/23) 自己終了警告対応 287 // 6.4.4.1 (2016/03/18) NUMBER_DISPLAYを、static final 定数化します。 288 } 289 290 // 3.5.2.0 (2003/10/20) ヘッダー繰り返し部をgetHeadLine()へ移動 291 buf.append("<thead id=\"header\">").append( CR ) // 3.5.6.5 (2004/08/09) 292 .append( getHeadLine() ) 293 .append("</thead>").append( CR ); 294 295 return buf.toString(); 296 } 297 298 /** 299 * ヘッダー繰り返し部を、getTableHead()メソッドから分離。 300 * 301 * @og.rev 3.5.6.2 (2004/07/05) HTMLCustomTableScrollBar 用に新規作成 302 * @og.rev 6.1.2.0 (2015/01/24) キャッシュを返すのを、#getHeadLine() に移動。 303 * 304 * @return テーブルのタグ文字列 305 * @og.rtnNotNull 306 */ 307 @Override 308 protected String getHeadLine() { 309 if( headerLine == null ) { // キャッシュになければ、設定する。 310 headerLine = getHeadLine( "<th" + headerFormat.getRowspan() ) ; 311 } 312 313 return headerLine ; 314 } 315 316 /** 317 * ヘッダー繰り返し部を、getTableHead()メソッドから分離。 318 * 319 * @og.rev 3.5.2.0 (2003/10/20) 新規作成 320 * @og.rev 3.5.4.0 (2003/11/25) TableFormatter クラスを使用するように変更。 321 * @og.rev 3.5.4.3 (2004/01/05) useCheckControl 属性の機能を追加 322 * @og.rev 3.5.4.6 (2004/01/30) numberType="none" 時の処理を追加(Noラベルを出さない) 323 * @og.rev 3.5.4.7 (2004/02/06) ヘッダーにソート機能用のリンクを追加します。 324 * @og.rev 3.5.5.0 (2004/03/12) systemFormat(例:[KEY.カラム名]形式等)の対応 325 * @og.rev 3.5.5.0 (2004/03/12) No 欄そのものの作成判断ロジックを追加 326 * @og.rev 3.5.6.2 (2004/07/05) HTMLCustomTableScrollBar 用に引数追加 327 * @og.rev 3.7.0.1 (2005/01/31) 全件チェックコントロール処理変更 328 * @og.rev 6.1.2.0 (2015/01/24) キャッシュを返すのを、#getHeadLine() に移動。 329 * @og.rev 6.4.3.4 (2016/03/11) ヘッダーでもTableFormatterのType(#,$,!)に対応した値を出すようにする。 330 * @og.rev 6.4.4.2 (2016/04/01) TableFormatterのタイプ別値取得処理の共通部をまとめる。 331 * 332 * @param thTag タグの文字列 333 * 334 * @return テーブルのタグ文字列 335 * @og.rtnNotNull 336 */ 337 @Override 338 protected String getHeadLine( final String thTag ) { 339 340 final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE ); 341 342 buf.append( headerFormat.getTrTag() ).append( CR ); 343 344 // 3.5.5.0 (2004/03/12) No 欄そのものの作成判断追加 345 if( isNumberDisplay() ) { 346 // 6.1.2.0 (2015/01/24) thTag に、headerFormat.getRowspan() を含ませて受け取る。 347 if( isUseCheckControl() && "checkbox".equals( getSelectedType() ) ) { 348 buf.append( thTag ).append( "></th>" ) 349 .append( thTag ).append( '>' ).append( getAllCheckControl() ).append( "</th>" ) // 6.0.2.5 (2014/10/31) char を append する。 350 .append( thTag ).append( '>' ).append( getNumberHeader() ).append( "</th>" ); // 6.0.2.5 (2014/10/31) char を append する。 351 } 352 else { 353 buf.append( thTag ).append( " colspan=\"3\">" ).append( getNumberHeader() ).append( "</th>" ); // 6.0.2.5 (2014/10/31) char を append する。 354 } 355 } 356 357 int cl = 0; 358 for( ; cl<headerFormat.getLocationSize(); cl++ ) { 359 buf.append( StringUtil.replace( headerFormat.getFormat(cl) ,"td","th" )); 360 final int loc = headerFormat.getLocation(cl); 361 // 6.4.3.4 (2016/03/11) ヘッダーでもTableFormatterのType(#,$,!)に対応した値を出すようにする。 362 // if( loc >= 0 ) { buf.append( getSortedColumnLabel(loc) ); } 363 if( loc >= 0 ) { 364 // 6.4.4.2 (2016/04/01) 処理の共通部をまとめる。 365 buf.append( getTypeCaseValue( headerFormat.getType(cl),-1,loc ) ); 366 } 367 } 368 buf.append( StringUtil.replace( headerFormat.getFormat(cl) ,"td","th" ) ).append( CR ); 369 370 return buf.toString(); // 6.1.2.0 (2015/01/24) 371 } 372 373 /** 374 * フォーマットを設定します。 375 * 376 * @og.rev 3.5.4.0 (2003/11/25) 新規作成 377 * @og.rev 3.5.4.4 (2004/01/16) 配列の最大数を変更 378 * @og.rev 3.5.5.5 (2004/04/23) headerFormat が定義されていない場合はエラー 379 * 380 * @param list TableFormatterのリスト 381 */ 382 @Override 383 public void setFormatterList( final List<TableFormatter> list ) { // 4.3.3.6 (2008/11/15) Generics警告対応 384 bodyFormats = new TableFormatter[BODYFORMAT_MAX_COUNT]; 385 386 bodyFormatsCount = 0; 387 for( int i=0; i<list.size(); i++ ) { 388 final TableFormatter format = list.get( i ); // 4.3.3.6 (2008/11/15) Generics警告対応 389 390 switch( format.getFormatType() ) { 391 case TYPE_HEAD : headerFormat = format; break; 392 case TYPE_BODY : bodyFormats[bodyFormatsCount++] = format; break; 393 case TYPE_FOOT : footerFormat = format; break; 394 default : final String errMsg = "FormatterType の定義外の値が指定されました。"; 395 // 4.3.4.4 (2009/01/01) 396 throw new HybsSystemException( errMsg ); 397 } 398 } 399 400 // 3.5.5.5 (2004/04/23) headerFormat が定義されていない場合はエラー 401 if( headerFormat == null ) { 402 final String errMsg = "h:thead タグの、フォーマットの指定は必須です。"; 403 throw new HybsSystemException( errMsg ); 404 } 405 } 406 407 /** 408 * フォーマットメソッドを使用できるかどうかを問い合わせます。 409 * 410 * @return 使用可能(true)/ 使用不可能 (false) 411 */ 412 @Override 413 public boolean canUseFormat() { 414 return true; 415 } 416 417 /** 418 * ビューで表示したカラムの一覧をCSV形式で返します。 419 * 420 * @og.rev 5.1.6.0 (2010/05/01) 新規追加 421 * @og.rev 6.2.0.1 (2015/03/06) TableFormatter#getLocation(int)の有効判定 422 * @og.rev 6.4.3.4 (2016/03/11) getViewClms(TableFormatter) を使用して表示されたカラム一覧を求めます。 423 * 424 * @return ビューで表示したカラムの一覧 425 * @og.rtnNotNull 426 */ 427 @Override 428 public String getViewClms() { 429 return getViewClms( headerFormat ); 430 } 431}