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.column;
017    
018    import org.opengion.hayabusa.common.HybsSystem;
019    import org.opengion.hayabusa.common.HybsSystemException;
020    import org.opengion.hayabusa.db.AbstractEditor;
021    import org.opengion.hayabusa.db.CellEditor;
022    import org.opengion.hayabusa.db.DBColumn;
023    import org.opengion.hayabusa.db.Selection;
024    import org.opengion.hayabusa.db.SelectionFactory;
025    import org.opengion.fukurou.util.StringFormat;
026    import org.opengion.fukurou.util.XHTMLTag;
027    import org.opengion.fukurou.util.Attributes;
028    import org.opengion.fukurou.util.TagBuffer;
029    
030    /**
031     * カラãƒ??編é›?ƒ‘ラメーターの?³?±?¬æ–??実行結果より、ã?ルãƒ?‚¦ンメニューを作æ?して
032     * 編é›?�™るå?合に使用するエãƒ?‚£タークラスですã?
033     *
034     * 編é›?ƒ‘ラメータには、ã?ルãƒ?‚¦ンメニューを作æ?するための、SQLæ–?‚’記述しますã?
035     * こã?SQLæ–??、select KEY,LABEL from xx ?¥?¥?¥ とã�?�†構文で、KEY部åˆ?�¨LABEL部åˆ?�Œ
036     * 選択されますã?<del>現時点では、それ以降ã?検索結果は無視されますが、å°?�¥çš?�ª拡張ã�?
037     * あるため、検索は、KEYとLABELの?’つにしてくださいã€?/del>
038     * 第ä¸?‚«ラãƒ??キー、第二カラãƒ??ラベルでこã??’つはå¿??ですã?第三カラãƒ??短縮ラベルã€?
039     * 第四カラãƒ??グルーãƒ?optgroup)、第五カラãƒ??色付け等に使ã�?‚¯ラスですã?
040     * 短縮ラベルが設定されてã�?‚‹場合ã?ä¸?¦§でこã?エãƒ?‚£タが適用されると短縮ラベル表示ã‚?
041     * した上でマウスオーバã?時ã?ãƒ??ルチップで通常のラベルを表示しますã?<br/>
042     *
043     * å�?‚«ラãƒ??値(value値)に、AAA:BBB:CCC:DDD とã�?�†値を設定できますã?これはã€?
044     * $1,$2,$3,$4 に割り当てなおして、QUERYを実行しますã?またã?$1 は、本来の値としてã€?
045     * メニューの初期値設定等に使用しますã?上記ã?例では、AAA がå?で、それ以降ã?ã€?
046     * 引数になりますã?
047     * 又ã?$Cには自åˆ??身のカラãƒ?��を割り当てますã?
048     * こã?機è?を使用すれば、動çš?ƒ¡ニューを行ごとに条件を変えて作æ?することã�?
049     * 可能になりますã?
050     * 例:select KEY,LABEL from xx where KUBUN='$2' and CDK='$3'
051     * さらに、å?のæ–?­—å?"AAA:BBB:CCC:DDD"はã€?0 に割り当てられますã?割り当てがなã�?
052     * 変数はã€?"(ゼロæ–?­—å?)として、扱われますã?
053     *
054     * 編é›?ƒ‘ラメータに"SEQ"と記述することで正方向にしか選べなã�?‚·ークメニューを実現できますã?
055     * これにより、シーケンスにスãƒ??タスをé?に挙げてã�?��ような、ã?ルãƒ?‚¦ンメニュー
056     * を作æ?することがå?来ますã?(é€?�«戻れなã�?ƒ¡ニュー)
057     *
058     * カラãƒ??表示にå¿?¦�な属æ?は, DBColumn オブジェクãƒ?より取り出しますã?
059     * こã?クラスは、DBColumn オブジェクト毎に?‘つ作æ?されますã?
060     *
061     * @og.rev 3.2.3.0 (2003/06/06) 新規作æ?
062     * @og.rev 3.4.0.1 (2003/09/03) ?¤?¢検索をリアルタイãƒ?�«変更ã€?
063     * @og.rev 4.3.6.0 (2009/04/01) eventColumn対å¿?
064     * @og.rev 5.4.3.6 (2012/01/19) コメント変更
065     * @og.group ãƒ??タ編é›?
066     *
067     * @version  4.0
068     * @author       Kazuhiko Hasegawa
069     * @since    JDK5.0,
070     */
071    public class Editor_DBMENU extends AbstractEditor {
072            //* こã?プログラãƒ??VERSIONæ–?­—å?を設定しますã?       {@value} */
073            private static final String VERSION = "5.5.1.0 (2012/04/03)" ;
074    
075            private final String query ;
076            private final String dbid ;
077            private final String lang ;                             // 4.0.0 (2006/11/15)
078            private final boolean addNoValue ;              // 3.5.5.7 (2004/05/10)
079            private final boolean seqFlag ;                 // 3.6.0.6 (2004/10/22)
080            private final String useSLabel ;        // 5.5.1.0 (2012/04/03)
081    
082            /**
083             * ãƒ?ƒ•ォルトコンストラクターã€?
084             * こã?コンストラクターで、基本オブジェクトを作æ?しますã?
085             *
086             * @og.rev 3.4.0.1 (2003/09/03) 初期値でQUERYæ–?‚’キープするã?
087             * @og.rev 3.6.0.6 (2004/10/22) シーケンスアクセス機è?(seqFlag)を追åŠ?�—まã�?
088             * @og.rev 5.5.1.0 (2012/04/03) Slabel対å¿?
089             *
090             */
091            public Editor_DBMENU() {
092                    // 4.3.4.4 (2009/01/01)
093    //              super();
094                    query   = null;
095                    dbid    = null;
096                    lang    = null;                 // 4.0.0 (2006/11/15)
097                    addNoValue = false;             // 3.5.5.7 (2004/05/10)
098                    seqFlag    = false;             // 3.6.0.6 (2004/10/22)
099                    useSLabel = "auto";             // 5.5.1.0 (2012/04/03)
100            }
101    
102            /**
103             * コンストラクターã€?
104             *
105             * @og.rev 3.3.1.1 (2003/07/03) name , attributes 属æ?ã‚?final にするã€?
106             * @og.rev 3.4.0.1 (2003/09/03) 継承の親å…??変更に伴ã�?®Ÿè£??移動ã?
107             * @og.rev 3.5.5.7 (2004/05/10) addNoValue 属æ?を追åŠ?�—ますã?
108             * @og.rev 3.5.5.9 (2004/06/07) editorParam 属æ?ã�?null の場合ã?、エラーとしますã?
109             * @og.rev 3.5.6.0 (2004/06/18) XHTMLTag の å†?ƒ¨配å? SELECT_KEY を隠蔽しますã?
110             * @og.rev 3.6.0.6 (2004/10/22) シーケンスアクセス機è?(seqFlag)を追åŠ?�—まã�?
111             * @og.rev 5.1.7.0 (2010/06/01) 動的プルãƒ?‚¦ン実è£?¦‹直ã�?
112             * @og.rev 5.5.1.0 (2012/04/03) Slabel対å¿?
113             *
114             * @param       clm     DBColumnオブジェクãƒ?
115             */
116            private Editor_DBMENU( final DBColumn clm ) {
117            //      super( clm );
118                    name            = clm.getName();
119                    addNoValue      = clm.isAddNoValue() ;          // 3.5.5.7 (2004/05/10)
120                    query           = clm.getEditorParam();
121                    dbid            = clm.getDbid();
122                    lang            = clm.getLang();                        // 4.0.0 (2006/11/15)
123                    seqFlag         = false;                // 3.6.0.6 (2004/10/22)
124    //              eventColumn = clm.getEventColumn(); // 4.3.6.0 (2009/04/01)
125    //              eventURL        = clm.getEventURL();    // 4.3.6.0 (2009/04/01)
126    //              editor          = clm.getEditor();              // 4.3.6.0 (2009/04/01)
127                    useSLabel = clm.getUseSLabel() ;        // 5.5.1.0 (2012/04/03)
128    
129                    // 3.5.5.9 (2004/06/07)
130                    if( query == null || query.length() == 0 ) {
131                            String errMsg = "DBMENU Editor では、編é›?ƒ‘ラメータはå¿??ですã?"
132                                            + " name=[" + name + "]" + HybsSystem.CR ;
133                            throw new HybsSystemException( errMsg );
134                    }
135    
136                    String  disabled = clm.isWritable() ? null : "disabled" ;
137    
138                    attributes = new Attributes();
139                    attributes.set( "disabled"      ,disabled );
140    
141                    attributes.addAttributes( clm.getEditorAttributes() );
142                    optAttr = attributes.get( "optionAttributes" );
143                    tagBuffer.add( XHTMLTag.selectAttri( attributes ) );
144            }
145    
146            /**
147             * å�?‚ªブジェクトからè?åˆ??インスタンスを返しますã?
148             * 自åˆ??身をキャãƒ?‚·ュするのかã?新たに作æ?するのかã?、各サブクラスの実è£?�«
149             * まかされますã?
150             *
151             * @param       clm     DBColumnオブジェクãƒ?
152             *
153             * @return      CellEditorオブジェクãƒ?
154             */
155            public CellEditor newInstance( final DBColumn clm ) {
156                    return new Editor_DBMENU( clm );
157            }
158    
159            /**
160             * ãƒ??タの編é›?”¨æ–?­—å?を返しますã?
161             *
162             * ここでは、AAA:BBB:CCC:DDD とã�?�†値をã?$1,$2,$3,$4 に割り当てなおしてã€?
163             * QUERYを実行しますã?またã?$1 は、本来の値として、メニューの初期値設定等に
164             * 使用しますã?上記ã?例では、AAA がå?で、それ以降ã?、引数になりますã?
165             * さらに、å?のæ–?­—å?"AAA:BBB:CCC:DDD"はã€?0 に割り当てられますã?割り当てがなã�?
166             * 変数はã€?"(ゼロæ–?­—å?)として、扱われますã?
167             * 又ã?$Cには自åˆ??身のカラãƒ?��を割り当てますã?
168             *
169             * @og.rev 3.4.0.1 (2003/09/03) リアルタイãƒ?�§値を作æ?する様に変更ã€?
170             * @og.rev 3.5.5.5 (2004/04/23) 新規に Attributes オブジェクトを作æ?する方式を止めますã?
171             * @og.rev 4.3.7.2 (2009/06/15) 属æ?でidがå?力される場合ã?、idをå?力しなã�?
172             * @og.rev 5.1.3.0 (2010/02/01) ä¸?¦§表示のみで、ツールチップ表示を行うã€?
173             * @og.rev 5.1.7.0 (2010/06/01) 動的プルãƒ?‚¦ン実è£?¦‹直ã�?
174             * @og.rev 5.5.1.0 (2012/04/03) Slabel対å¿?
175             *
176             * @param       value 入力å?
177             *
178             * @return      ãƒ??タの編é›?”¨æ–?­—å?
179             */
180            @Override
181            public String getValue( final String value ) {
182    //              final boolean uslbl = !"true".equals(useSLabel) ? false : true; // 5.5.1.0 (2012/04/03)
183                    final boolean uslbl = "true".equalsIgnoreCase( useSLabel );             // 5.5.1.0 (2012/04/03)
184    
185                    TagBuffer tag = new TagBuffer( "select" );
186                    tag.add( "name"    , name );
187                    if( attributes.get( "id" ) == null || attributes.get( "id" ).length() == 0 ) { // 4.3.7.2 (2009/06/15)
188                            tag.add( "id"      , name ); // 4.3.6.0 (2009/04/01)
189                    }
190                    tag.add( tagBuffer.makeTag() );
191                    tag.add( optAttr );             // 3.5.5.8 (2004/05/20)
192    
193                    // 5.1.3.0 (2010/02/01)
194    //              tag = getOption( tag,value );
195    //              tag = getOption( tag,value,false );
196                    tag = getOption( tag,value,uslbl ); // 5.5.1.0 (2012/04/03)
197    
198                    return tag.makeTag();
199    //              return tag.makeTag() + createEventColumnJS( name, editor, -1, eventURL ); // 4.3.6.0 (2009/04/01)
200            }
201    
202            /**
203             * name属æ?を変えたã?ãƒ??タ表示/編é›?”¨のHTMLæ–?­—å?を作æ?しますã?
204             * ãƒ??ブル上ã? name に 行番号を付加して、名前_行番号 で登録するキーを作æ?ã�?
205             * リクエスト情報を1つ毎ã?フィールドで処ç�?�§きますã?
206             *
207             * ここでは、AAA:BBB:CCC:DDD とã�?�†値をã?$1,$2,$3,$4 に割り当てなおしてã€?
208             * QUERYを実行しますã?またã?$1 は、本来の値として、メニューの初期値設定等に
209             * 使用しますã?上記ã?例では、AAA がå?で、それ以降ã?、引数になりますã?
210             * さらに、å?のæ–?­—å?"AAA:BBB:CCC:DDD"はã€?0 に割り当てられますã?割り当てがなã�?
211             * 変数はã€?"(ゼロæ–?­—å?)として、扱われますã?
212             * 又ã?$Cには自åˆ??身のカラãƒ?��を割り当てますã?
213             *
214             * @og.rev 2.0.0.3 (2002/09/26) optionAttributes 属æ?に "$i" を使ã�?�¨そã?行数に置き換る機è?を追åŠ??
215             * @og.rev 3.4.0.1 (2003/09/03) リアルタイãƒ?�§値を作æ?する様に変更ã€?
216             * @og.rev 3.5.5.0 (2004/03/12) 名前と行番号の区åˆ?‚Š記号("__")をã?HybsSystem.JOINT_STRING  に変更ã€?
217             * @og.rev 3.5.5.5 (2004/04/23) 新規に Attributes オブジェクトを作æ?する方式を止めますã?
218             * @og.rev 4.3.7.2 (2009/06/15) 属æ?でidがå?力される場合ã?、idをå?力しなã�?
219             * @og.rev 5.1.3.0 (2010/02/01) ä¸?¦§表示のみで、ツールチップ表示を行うã€?
220             * @og.rev 5.1.7.0 (2010/06/01) 動的プルãƒ?‚¦ン実è£?¦‹直ã�?
221             * @og.rev 5.5.1.0 (2012/04/03) Slabel対å¿?
222             *
223             * @param       row   行番号
224             * @param       value 入力å?
225             *
226             * @return      ãƒ??タ表示/編é›?”¨のæ–?­—å?
227             */
228            @Override
229            public String getValue( final int row,final String value ) {
230    //              final boolean uslbl = ("auto".equals(useSLabel) || "true".equals(useSLabel) ) ? true : false; // 5.5.1.0 (2012/04/03)
231                    final boolean uslbl = "auto".equalsIgnoreCase( useSLabel ) || "true".equalsIgnoreCase( useSLabel );             // 5.5.1.0 (2012/04/03)
232    
233                    TagBuffer tag = new TagBuffer( "select" );
234                    String newName = name + HybsSystem.JOINT_STRING + row; // 4.3.6.0 (2009/04/01)
235                    //tag.add( "name"    , name + HybsSystem.JOINT_STRING + row );
236                    tag.add( "name", newName ); // 4.3.6.0 (2009/04/01)
237                    if( attributes.get( "id" ) == null || attributes.get( "id" ).length() == 0 ) { // 4.3.7.2 (2009/06/15)
238                            tag.add( "id"    , newName ); // 4.3.6.0 (2009/04/01)
239                    }
240                    tag.add( tagBuffer.makeTag() );
241                    tag.add( optAttr );             // 3.5.5.8 (2004/05/20)
242    
243                    // 5.1.3.0 (2010/02/01)
244    //              tag = getOption( tag,value );
245    //              tag = getOption( tag,value,true );
246                    tag = getOption( tag,value,uslbl );
247    
248                    return tag.makeTag();
249    //              return tag.makeTag() + createEventColumnJS( name, editor, row, eventURL ); // 4.3.6.0 (2009/04/01)
250            }
251    
252            /**
253             * 初期値が選択済みの 選択肢(オプション)をTagBuffer に反映しますã?
254             * こã?オプションは、引数の値をå?期å?とするオプションタグ作æ?しã?TagBuffer
255             * に値を設定して返しますã?
256             *
257             * ここでは、AAA:BBB:CCC:DDD とã�?�†値をã?$1,$2,$3,$4 に割り当てなおしてã€?
258             * QUERYを実行しますã?またã?$1 は、本来の値として、メニューの初期値設定等に
259             * 使用しますã?上記ã?例では、AAA がå?で、それ以降ã?、引数になりますã?
260             * さらに、å?のæ–?­—å?"AAA:BBB:CCC:DDD"はã€?0 に割り当てられますã?割り当てがなã�?
261             * 変数はã€?"(ゼロæ–?­—å?)として、扱われますã?
262             * 又ã?$Cには自åˆ??身のカラãƒ?��を割り当てますã?
263             *
264             * @og.rev 3.5.5.7 (2004/05/10) getOption( String value )のå»?­¢を受けて、新規作æ?
265             * @og.rev 3.6.0.6 (2004/10/22) シーケンスアクセス機è?(seqFlag)を追åŠ?�—まã�?
266             * @og.rev 4.0.0.0 (2006/11/15) SelectionFactory に lang 属æ?を追åŠ?�—ますã?
267             * @og.rev 4.3.4.0 (2008/12/01) $Cのカラãƒ?��置換えを追åŠ?
268             * @og.rev 5.1.3.0 (2010/02/01) ä¸?¦§表示のみで、ツールチップ表示を行うã€?
269             *
270             * @param       buf    タグæ–?­—å?のバッファー
271             * @param       value  選択されてã�?‚‹値
272             * @param   useShortLabel ラベル(短)をã?ースとしたオプション表示を行うかどã�?�‹ã€?
273             *
274             * @return      オプションタグ
275             */
276            private TagBuffer getOption( final TagBuffer buf,final String value,final boolean useShortLabel ) {
277    
278                    // StringFormat format = new StringFormat( query,value);
279                    StringFormat format = new StringFormat( query, value, name ); // 4.3.4.0 (2008/12/01)
280                    String newQuery = format.format();
281                    String newValue = format.getValue();
282    
283                    Selection selection = SelectionFactory.newDBSelection( newQuery, dbid, lang );
284                    boolean useMultiSelect = selection.useMultiSelect();
285                    if( useMultiSelect ) {
286                            buf.add( "onkeydown", "setKeySelect(this);" );
287                    }
288    
289                    if( addNoValue ) {
290                            // 5.1.3.0 (2010/02/01)
291    //                      buf.setBody( Selection.NO_VALUE_OPTION + selection.getOption( newValue, seqFlag ) );
292                            buf.setBody( Selection.NO_VALUE_OPTION + selection.getOption( newValue, seqFlag, useShortLabel ) );
293                    }
294                    else {
295                            // 5.1.3.0 (2010/02/01)
296    //                      buf.setBody( selection.getOption( newValue, seqFlag ) );
297                            buf.setBody( selection.getOption( newValue, seqFlag, useShortLabel ) );
298                    }
299    
300                    return buf;
301            }
302    }