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    }