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