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