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     */
016    package org.opengion.plugin.view;
017    
018    import java.util.List;
019    
020    import org.opengion.fukurou.util.StringUtil;
021    import org.opengion.hayabusa.common.HybsSystem;
022    import org.opengion.hayabusa.common.HybsSystemException;
023    import org.opengion.hayabusa.db.DBTableModel;
024    import org.opengion.hayabusa.html.TableFormatter;
025    
026    /**
027     * JavaScript のãƒ?ƒªー階層を持ったテーブル表示を行う、ツリーãƒ??ブル表示クラスですã?
028     *
029     * AbstractViewForm により、setter/getterメソãƒ?ƒ‰のãƒ?ƒ•ォルト実è£?‚’提供してã�?�¾すã?
030     * 各HTMLのタグにå¿?¦�な setter/getterメソãƒ?ƒ‰のみ?Œ追åŠ?®š義してã�?�¾すã?
031     *
032     * AbstractViewForm を継承してã�?‚‹為,ロケールに応じたラベルをå?力させる事が出来ますã?
033     *
034     * @og.group 画面表示
035     *
036     * @version  4.0
037     * @author   Hiroki Nakamura
038     * @since    JDK5.0,
039     */
040    public class ViewForm_HTMLCustomTreeBOM extends ViewForm_HTMLTable  {
041            //* こã?プログラãƒ??VERSIONæ–?­—å?を設定しますã?       {@value} */
042            private static final String VERSION = "5.1.6.0 (2010/05/01)" ;
043    
044    //      public static final String COLUMN_LEVEL_KEY = "COLUMN_LEVEL";   // 5.1.9.0 (2010/08/01) å»?­¢
045    
046            private TableFormatter          headerFormat    = null;
047            private TableFormatter[]        bodyFormats             = null;
048            private int                                     bodyFormatsCount = 0;
049    
050            private static final int BODYFORMAT_MAX_COUNT = 10;
051    
052            // 4.3.4.4 (2009/01/01)
053    //      /**
054    //       * ãƒ?ƒ•ォルトコンストラクター
055    //       *
056    //       */
057    //      public ViewForm_HTMLCustomTreeBOM() {
058    //              super();
059    //      }
060    
061            /**
062             * DBTableModel から HTMLæ–?­—å?を作æ?して返しますã?
063             * startNo(表示開始位置)から、pageSize(表示件数)までのViewæ–?­—å?を作æ?しますã?
064             * 表示残りãƒ??タã�?pageSize 以下ã?場合ã?,残りのãƒ??タをすべて出力しますã?
065             *
066             * @og.rev 4.3.1.0 (2008/09/08) フォーマットが設定されてã�?�ªã�??合ã?エラー追åŠ?
067             *
068             * @param  stNo     表示開始位置
069             * @param  pgSize   表示件数
070             *
071             * @return  DBTableModelから作æ?されã�?HTMLæ–?­—å?
072             */
073            @Override
074            public String create( final int stNo, final int pgSize )  {
075                    // こã?クラスでは、テーブル全ãƒ??タを使用しますã?
076                    if( getRowCount() == 0 ) { return ""; } // 暫定å?置
077    
078                    // 4.3.1.0 (2008/09/08)
079                    if( headerFormat == null ) {
080                            String errMsg = "ViewTagで canUseFormat() = true の場合ã?Formatter はå¿??ですã?";
081                            throw new HybsSystemException( errMsg );
082                    }
083    
084                    int startNo = 0;
085                    int pageSize = getRowCount();
086    
087                    int lastNo = getLastNo( startNo, pageSize );
088    
089                    StringBuilder out = new StringBuilder( HybsSystem.BUFFER_LARGE );
090    
091                    headerFormat.makeFormat( getDBTableModel() );
092    
093                    if( bodyFormatsCount == 0 ) {
094                            bodyFormats[0] = headerFormat ;
095                            bodyFormatsCount ++ ;
096                    }
097                    else {
098                            for( int i=0; i<bodyFormatsCount; i++ ) {
099                                    bodyFormats[i].makeFormat( getDBTableModel() );
100                            }
101                    }
102    
103                    out.append( getHeader() );
104    
105                    int level;
106                    boolean isFld;
107                    for( int row=startNo; row<lastNo; row++ ) {
108                            // カラãƒ?=?�ã?、レベルを指定するã?
109                            level = Integer.parseInt( getValueLabel(row,0) );
110                            isFld = false;
111                            if( row+1<lastNo ) {
112                                    int nextLevel = Integer.parseInt( getValueLabel(row+1,0) );
113                                    isFld = ( level < nextLevel ) ? true : false ;
114                            }
115                            out.append( getLevelScript( level,isFld ) );
116    
117                            // 開å§?
118                            for( int i=0; i<bodyFormatsCount; i++ ) {
119                                    TableFormatter bodyFormat = bodyFormats[i];
120    
121                                    int cl = 0;
122                                    for( ; cl < bodyFormat.getLocationSize(); cl++ ) {
123                                            String fmt = bodyFormat.getFormat(cl);
124                                            int loc = bodyFormat.getLocation(cl);
125                                            if( ! bodyFormat.isNoClass() && loc >= 0 ) {
126                                                    StringBuilder newtg = new StringBuilder( HybsSystem.BUFFER_LARGE );
127                                                    newtg.append("<td class=\"");
128                                                    newtg.append( getColumnDbType(loc) );
129                                                    newtg.append("\" ");
130                                                    String tdclass = newtg.toString();
131                                                    fmt = StringUtil.replace( bodyFormat.getFormat(cl) ,"<td", tdclass );
132                                            }
133                                            out.append( fmt );
134                                            if( loc >= 0 ) {
135                                                    switch( bodyFormat.getType(cl) ) {
136                                                    case '#' : out.append( getColumnLabel(loc) );           break;
137                                                    case '$' : out.append( getRendererValue(row,loc) );     break;
138                                                    case '!' : out.append( getValue(row,loc) );                     break;
139                                                    default  : out.append( getValueLabel(row,loc) );        break;
140                                                    }
141                                            }
142                                    }
143                                    out.append( StringUtil.replace( bodyFormat.getFormat(cl), "</tr>", "" ) );
144                            }
145                            // 終äº?
146    
147                            out.append( "', '', 'gold')" );
148                            if( level != 0 ) {
149                                    out.append( ")" );
150                            }
151                            out.append( HybsSystem.CR );
152                    }
153                    out.append( getFutter() );
154    
155                    return out.toString();
156            }
157    
158            /**
159             * DBTableModel から ãƒ??ブルのヘッãƒ??タグæ–?­—å?を作æ?して返しますã?
160             * JavaScript の TreeBody では、JavaScriptに関連する定義もこのヘッãƒ??に
161             * 含めますã?
162             *
163             * @return  ãƒ??ブルのヘッãƒ??タグæ–?­—å?
164             */
165            @Override
166            protected String getHeader() {
167                    StringBuilder buf = new StringBuilder( HybsSystem.BUFFER_MIDDLE );
168    
169                    buf.append("<table border=\"0\" cellspacing=\"2\" cellpadding=\"0\"  summary=\"bomTable\" id=\"viewTable\">"); // 3.9.0.1 (2007/12/18)
170                    buf.append( HybsSystem.CR );
171                    buf.append("<script type=\"text/javascript\">");
172                    buf.append( HybsSystem.CR );
173                    buf.append("<!--");
174                    buf.append( HybsSystem.CR );
175                    buf.append("aux0 = gFld('");
176                    // 開å§?
177                    int cl = 0;
178                    for( ; cl < headerFormat.getLocationSize(); cl++ ) {
179                            buf.append( StringUtil.replace( headerFormat.getFormat(cl) ,"td","th" ));
180                            int loc = headerFormat.getLocation(cl);
181                            if( loc >= 0 ) { buf.append( getColumnLabel(loc) ); }
182                            // ヘッãƒ??フォーマット部では、何もしませんã€?
183                    }
184                    buf.append( StringUtil.replace( StringUtil.replace( headerFormat.getFormat(cl) ,"td","th" ), "</tr>", "" ) );
185                    // 終äº?
186    
187                    buf.append("', '', 'gold')");
188                    buf.append( HybsSystem.CR );
189    
190                    return buf.toString();
191            }
192    
193            /**
194             * DBTableModel から ãƒ??ブルのフッタータグæ–?­—å?を作æ?して返しますã?
195             * JavaScript の TreeBody では、JavaScriptに関連する定義もこのフッターに
196             * 含めますã?
197             *
198             * @return  ãƒ??ブルのフッタータグæ–?­—å?
199             */
200            protected String getFutter() {
201                    StringBuilder buf = new StringBuilder( HybsSystem.BUFFER_MIDDLE );
202    
203                    buf.append("initializeDocument()").append( HybsSystem.CR );
204                    buf.append("//-->").append( HybsSystem.CR );
205                    buf.append("</script>").append( HybsSystem.CR );
206                    buf.append("</table>").append( HybsSystem.CR );
207    
208                    return buf.toString();
209            }
210    
211            /**
212             * 行ã?レベルに応じã�?JavaScript関数のヘッãƒ??部åˆ?‚’返しますã?
213             *
214             * @og.rev 3.5.2.1 (2003/10/27) JavaScript å†??ãƒ?ƒ–ルコーãƒ??ションをシングルコーãƒ??ションに変更するã€?
215             *
216             * @param       lvl             ãƒ?ƒªーのレベル
217             * @param       isFld   フォルãƒ?�‹どã�?�‹[true:フォルãƒ?false:æœ?¸‹層]
218             *
219             * @return  JavaScript関数のヘッãƒ??部åˆ?
220             */
221            private String getLevelScript( final int lvl,final boolean isFld ) {
222    
223                    String auxX = "\taux" + ( lvl );
224                    String auxY = "aux" + ( lvl-1 );
225    
226                    final String rtn ;
227                    if( isFld ) {
228                            rtn = auxX + " = insFld(" + auxY + ", gFld('";
229                    }
230                    else {
231                            rtn = "\tinsFld(" + auxY + ", gLnk('CONTENTS','";
232                    }
233    
234                    return rtn;
235            }
236    
237            /**
238             * フォーマットを設定しますã?
239             *
240             * @param       list    TableFormatterのリスãƒ?
241             */
242            @Override
243            public void setFormatterList( final List<TableFormatter> list ) {         // 4.3.3.6 (2008/11/15) Generics警告対å¿?
244                    bodyFormats = new TableFormatter[BODYFORMAT_MAX_COUNT];
245    
246                    bodyFormatsCount = 0;
247                    for( int i=0; i<list.size(); i++ ) {
248                            TableFormatter format = list.get( i );          // 4.3.3.6 (2008/11/15) Generics警告対å¿?
249                            switch( format.getFormatType() ) {
250                            case TYPE_HEAD : headerFormat = format; break;
251                            case TYPE_BODY : bodyFormats[bodyFormatsCount++] = format; break;
252                            default : String errMsg = "FormatterType の定義外ã?値が指定されましたã€?;
253                            // 4.3.4.4 (2009/01/01)
254                                              throw new HybsSystemException( errMsg );
255                            }
256                    }
257    
258                    if( headerFormat == null ) {
259                            String errMsg = "og:thead タグの、フォーマットã?æŒ?®šã?å¿??ですã?";
260                            throw new HybsSystemException( errMsg );
261                    }
262            }
263    
264            /**
265             * フォーマットメソãƒ?ƒ‰を使用できるかどã�?�‹を問ã�?�ˆわせますã?
266             *
267             * @return  使用可能(true)/ 使用不可能 (false)
268             */
269            @Override
270            public boolean canUseFormat() {
271                    return true;
272            }
273    
274            /**
275             * ビューで表示したカラãƒ??ä¸?¦§をカンマ区åˆ?‚Šで返しますã?
276             *
277             * @og.rev 5.1.6.0 (2010/05/01) 新規追åŠ?
278             *
279             * @return      ビューで表示したカラãƒ??ä¸?¦§
280             */
281            @Override
282            public String getViewClms() {
283                    DBTableModel table = getDBTableModel();
284                    StringBuilder buf = new StringBuilder( HybsSystem.BUFFER_MIDDLE );
285                    for( int i=0; i<headerFormat.getLocationSize(); i++ ) {
286                            if( buf.length() > 0 ) { buf.append( ',' ); }
287                            buf.append( table.getColumnName( headerFormat.getLocation( i ) ) );
288                    }
289                    return buf.toString();
290            }
291    }