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.column; 017 018import org.opengion.hayabusa.common.HybsSystem; 019import org.opengion.hayabusa.common.HybsSystemException; 020import org.opengion.hayabusa.db.AbstractEditor; 021import org.opengion.hayabusa.db.CellEditor; 022import org.opengion.hayabusa.db.DBColumn; 023import org.opengion.hayabusa.db.SelectionCellEditor; // 6.2.2.0 (2015/03/27) 024import org.opengion.hayabusa.db.Selection; 025import org.opengion.hayabusa.db.SelectionFactory; 026import org.opengion.fukurou.util.StringFormat; 027import org.opengion.fukurou.util.XHTMLTag; 028import org.opengion.fukurou.util.Attributes; 029import org.opengion.fukurou.util.TagBuffer; 030 031import static org.opengion.fukurou.util.StringUtil.isNull; // 6.1.1.0 (2015/01/17) 032 033/** 034 * カラムの編集パラメーターのSQL文の実行結果より、プルダウンメニューを作成して 035 * 編集する場合に使用するエディタークラスです。 036 * 037 * 編集パラメータには、プルダウンメニューを作成するための、SQL文を記述します。 038 * このSQL文は、select KEY,LABEL from xx ・・・ という構文で、KEY部分とLABEL部分が 039 * 選択されます。 040 * 第一カラムはキー、第二カラムはラベルでこの2つは必須です。第三カラムは短縮ラベル、 041 * 第四カラムはグループ(optgroup)、第五カラムは色付け等に使うクラスです。 042 * 第五カラムの指定方法には、3通りあります。 043 * ここでは、 044 * ① "=" を含む場合は、そのままセット。(style='AAAA' など) 045 * ② disabled 単体の場合は、disabled="disabled" をセット 046 * ③ それ以外は、class= の後に、引数をセット 047 * します。 048 * 短縮ラベルが設定されている場合、一覧でこのエディタが適用されると短縮ラベル表示を 049 * した上でマウスオーバー時はツールチップで通常のラベルを表示します。 050 * 051 * 各カラムの値(value値)に、AAA:BBB:CCC:DDD という値を設定できます。これは、 052 * $1,$2,$3,$4 に割り当てなおして、QUERYを実行します。また、$1 は、本来の値として、 053 * メニューの初期値設定等に使用します。上記の例では、AAA が値で、それ以降は、 054 * 引数になります。 055 * 又、$Cには自分自身のカラム名を割り当てます。 056 * この機能を使用すれば、動的メニューを行ごとに条件を変えて作成することが 057 * 可能になります。 058 * 例:select KEY,LABEL from xx where KUBUN='$2' and CDK='$3' 059 * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない 060 * 変数は、""(ゼロ文字列)として、扱われます。 061 * 062 * 編集パラメータに"SEQ"と記述することで正方向にしか選べないシークメニューを実現できます。 063 * これにより、シーケンスにステータスを順に挙げていくような、プルダウンメニュー 064 * を作成することが出来ます。(逆に戻れないメニュー) 065 * 066 * カラムの表示に必要な属性は, DBColumn オブジェクト より取り出します。 067 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。 068 * 069 * @og.rev 3.2.3.0 (2003/06/06) 新規作成 070 * @og.rev 3.4.0.1 (2003/09/03) DB検索をリアルタイムに変更。 071 * @og.rev 4.3.6.0 (2009/04/01) eventColumn対応 072 * @og.rev 5.4.3.6 (2012/01/19) コメント変更 073 * @og.rev 6.2.2.0 (2015/03/27) SelectionCellEditor I/Fを追加 074 * @og.group データ編集 075 * 076 * @version 4.0 077 * @author Kazuhiko Hasegawa 078 * @since JDK5.0, 079 */ 080public class Editor_DBMENU extends AbstractEditor implements SelectionCellEditor { 081 /** このプログラムのVERSION文字列を設定します。 {@value} */ 082 private static final String VERSION = "6.9.5.0 (2018/04/23)" ; 083 084 private final String query ; 085 private final String dbid ; 086 private final String lang ; // 4.0.0 (2006/11/15) 087 private final boolean addNoValue ; // 3.5.5.7 (2004/05/10) 088 private final boolean seqFlag ; // 3.6.0.6 (2004/10/22) 089 private final String useSLabel ; // 5.5.1.0 (2012/04/03) 090 private final String addKeyLabel ; // 6.2.0.0 (2015/02/27) キー:ラベル形式 091 092 /** 093 * デフォルトコンストラクター。 094 * このコンストラクターで、基本オブジェクトを作成します。 095 * 096 * @og.rev 3.4.0.1 (2003/09/03) 初期値でQUERY文をキープする。 097 * @og.rev 3.6.0.6 (2004/10/22) シーケンスアクセス機能(seqFlag)を追加します 098 * @og.rev 5.5.1.0 (2012/04/03) Slabel対応 099 * @og.rev 6.2.0.0 (2015/02/27) キー:ラベル形式で表示するかどうかを、指定できるようにします。 100 */ 101 public Editor_DBMENU() { 102 super(); // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor 103 // 4.3.4.4 (2009/01/01) 104 query = null; 105 dbid = null; 106 lang = null; // 4.0.0 (2006/11/15) 107 addNoValue = false; // 3.5.5.7 (2004/05/10) 108 seqFlag = false; // 3.6.0.6 (2004/10/22) 109 useSLabel = "auto"; // 5.5.1.0 (2012/04/03) 110 addKeyLabel = null; // 6.2.0.0 (2015/02/27) キー:ラベル形式 111 } 112 113 /** 114 * コンストラクター。 115 * 116 * @og.rev 3.3.1.1 (2003/07/03) name , attributes 属性を final にする。 117 * @og.rev 3.4.0.1 (2003/09/03) 継承の親元の変更に伴う実装の移動。 118 * @og.rev 3.5.5.7 (2004/05/10) addNoValue 属性を追加します。 119 * @og.rev 3.5.5.9 (2004/06/07) editorParam 属性が null の場合は、エラーとします。 120 * @og.rev 3.5.6.0 (2004/06/18) XHTMLTag の 内部配列 SELECT_KEY を隠蔽します。 121 * @og.rev 3.6.0.6 (2004/10/22) シーケンスアクセス機能(seqFlag)を追加します 122 * @og.rev 5.1.7.0 (2010/06/01) 動的プルダウン実装見直し 123 * @og.rev 5.5.1.0 (2012/04/03) Slabel対応 124 * @og.rev 6.0.4.0 (2014/11/28) optionAttributes は、コンストラクタで設定します。 125 * @og.rev 6.0.4.0 (2014/11/28) useMultiSelect は、selection ではなく、colomn から取得する。 126 * @og.rev 6.2.0.0 (2015/02/27) キー:ラベル形式で表示するかどうかを、指定できるようにします。 127 * @og.rev 6.4.4.2 (2016/04/01) nameのfinal化 128 * @og.rev 6.9.5.0 (2018/04/23) useMultiSelect 廃止 129 * 130 * @param clm DBColumnオブジェクト 131 */ 132 private Editor_DBMENU( final DBColumn clm ) { 133 // super(); // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor 134 super( clm ); // 6.4.4.2 (2016/04/01) nameのfinal化 135 // name = clm.getName(); 136 addNoValue = clm.isAddNoValue() ; // 3.5.5.7 (2004/05/10) 137 query = clm.getEditorParam(); 138 dbid = clm.getDbid(); 139 lang = clm.getLang(); // 4.0.0.0 (2006/11/15) 140 seqFlag = false; // 3.6.0.6 (2004/10/22) 141 useSLabel = clm.getUseSLabel() ; // 5.5.1.0 (2012/04/03) 142 addKeyLabel = clm.getAddKeyLabel(); // 6.2.0.0 (2015/02/27) キー:ラベル形式 143 144 // 3.5.5.9 (2004/06/07) 145 if( query == null || query.isEmpty() ) { 146 final String errMsg = "DBMENU Editor では、編集パラメータは必須です。" 147 + " name=[" + name + "]" + CR ; 148 throw new HybsSystemException( errMsg ); 149 } 150 151 final String disabled = clm.isWritable() ? null : "disabled" ; 152 153 // 6.1.1.0 (2015/01/17) Attributesの連結記述 154 attributes = new Attributes() 155 .set( "disabled" ,disabled ) 156 .set( clm.getEditorAttributes() ); // #addAttributes( Attributes ) の代替え 157 158// // 6.9.5.0 (2018/04/23) useMultiSelect 廃止 159// // 6.1.1.0 (2015/01/17) TagBufferの連結記述 160// tagBuffer.add( XHTMLTag.selectAttri( attributes ) ) 161// .add( attributes.get( "optionAttributes" ) ) // 6.0.4.0 (2014/11/28) 162// .add( "onkeydown" , "setKeySelect(this);" , clm.useMultiSelect() ); // 6.0.4.0 (2014/11/28) 163 164 // 6.1.1.0 (2015/01/17) TagBufferの連結記述 165 tagBuffer.add( XHTMLTag.selectAttri( attributes ) ) 166 .add( attributes.get( "optionAttributes" ) ); // 6.0.4.0 (2014/11/28) 167 } 168 169 /** 170 * 各オブジェクトから自分のインスタンスを返します。 171 * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に 172 * まかされます。 173 * 174 * @param clm DBColumnオブジェクト 175 * 176 * @return CellEditorオブジェクト 177 * @og.rtnNotNull 178 */ 179 public CellEditor newInstance( final DBColumn clm ) { 180 return new Editor_DBMENU( clm ); 181 } 182 183 /** 184 * データの編集用文字列を返します。 185 * 186 * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、 187 * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に 188 * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。 189 * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない 190 * 変数は、""(ゼロ文字列)として、扱われます。 191 * 又、$Cには自分自身のカラム名を割り当てます。 192 * 193 * @og.rev 3.4.0.1 (2003/09/03) リアルタイムで値を作成する様に変更。 194 * @og.rev 3.5.5.5 (2004/04/23) 新規に Attributes オブジェクトを作成する方式を止めます。 195 * @og.rev 4.3.7.2 (2009/06/15) 属性でidが出力される場合は、idを出力しない 196 * @og.rev 5.1.3.0 (2010/02/01) 一覧表示のみで、ツールチップ表示を行う。 197 * @og.rev 5.1.7.0 (2010/06/01) 動的プルダウン実装見直し 198 * @og.rev 5.5.1.0 (2012/04/03) Slabel対応 199 * 200 * @param value 入力値 201 * 202 * @return データの編集用文字列 203 * @og.rtnNotNull 204 */ 205 @Override 206 public String getValue( final String value ) { 207 final boolean useSlbl = "true".equalsIgnoreCase( useSLabel ); // 5.5.1.0 (2012/04/03) 208 209 // select タグの作成 210 // 6.1.1.0 (2015/01/17) TagBufferの連結記述 211 return getOption( 212 new TagBuffer( "select" ) 213 .add( "name" , name ) // 4.3.6.0 (2009/04/01) 214 .add( "id" , name , isNull( attributes.get( "id" ) ) ) // 4.3.7.2 (2009/06/15) 215 .add( tagBuffer.makeTag() ) 216 , value 217 , useSlbl 218 ).makeTag() ; 219 220 } 221 222 /** 223 * name属性を変えた、データ表示/編集用のHTML文字列を作成します。 224 * テーブル上の name に 行番号を付加して、名前_行番号 で登録するキーを作成し, 225 * リクエスト情報を1つ毎のフィールドで処理できます。 226 * 227 * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、 228 * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に 229 * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。 230 * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない 231 * 変数は、""(ゼロ文字列)として、扱われます。 232 * 又、$Cには自分自身のカラム名を割り当てます。 233 * 234 * @og.rev 2.0.0.3 (2002/09/26) optionAttributes 属性に "$i" を使うとその行数に置き換る機能を追加。 235 * @og.rev 3.4.0.1 (2003/09/03) リアルタイムで値を作成する様に変更。 236 * @og.rev 3.5.5.0 (2004/03/12) 名前と行番号の区切り記号("__")を、HybsSystem.JOINT_STRING に変更。 237 * @og.rev 3.5.5.5 (2004/04/23) 新規に Attributes オブジェクトを作成する方式を止めます。 238 * @og.rev 4.3.7.2 (2009/06/15) 属性でidが出力される場合は、idを出力しない 239 * @og.rev 5.1.3.0 (2010/02/01) 一覧表示のみで、ツールチップ表示を行う。 240 * @og.rev 5.1.7.0 (2010/06/01) 動的プルダウン実装見直し 241 * @og.rev 5.5.1.0 (2012/04/03) Slabel対応 242 * @og.rev 6.4.5.3 (2016/05/13) value は、コロン区切りの先頭だけ分離する。 243 * 244 * @param row 行番号 245 * @param value 入力値 246 * 247 * @return データ表示/編集用の文字列 248 * @og.rtnNotNull 249 */ 250 @Override 251 public String getValue( final int row,final String value ) { 252 final boolean useSlbl = "auto".equalsIgnoreCase( useSLabel ) || "true".equalsIgnoreCase( useSLabel ); // 5.5.1.0 (2012/04/03) 253 final String newName = name + HybsSystem.JOINT_STRING + row; // 4.3.6.0 (2009/04/01) 254 final String newValue = StringFormat.getValue( value ); // 6.4.5.3 (2016/05/13) コロン区切りの先頭だけ 255 256 // select タグの作成 257 // 6.1.1.0 (2015/01/17) TagBufferの連結記述 258 return getOption( 259 new TagBuffer( "select" ) 260 .add( "name" , newName ) // 4.3.6.0 (2009/04/01) 261 .add( "id" , newName , isNull( attributes.get( "id" ) ) ) // 4.3.7.2 (2009/06/15) 262 .add( tagBuffer.makeTag() ) 263 , value 264 , useSlbl 265 ).makeTag( row,newValue ) ; // 6.4.5.3 (2016/05/13) ※ 元は、makeTag() でしたが、バグ? の可能性がある。 266 267 } 268 269 /** 270 * 初期値が選択済みの 選択肢(オプション)をTagBuffer に反映します。 271 * このオプションは、引数の値を初期値とするオプションタグ作成し、TagBuffer 272 * に値を設定して返します。 273 * 274 * ここでは、AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てなおして、 275 * QUERYを実行します。また、$1 は、本来の値として、メニューの初期値設定等に 276 * 使用します。上記の例では、AAA が値で、それ以降は、引数になります。 277 * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てがない 278 * 変数は、""(ゼロ文字列)として、扱われます。 279 * 又、$Cには自分自身のカラム名を割り当てます。 280 * 281 * @og.rev 3.5.5.7 (2004/05/10) getOption( String value )の廃止を受けて、新規作成 282 * @og.rev 3.6.0.6 (2004/10/22) シーケンスアクセス機能(seqFlag)を追加します 283 * @og.rev 4.0.0.0 (2006/11/15) SelectionFactory に lang 属性を追加します。 284 * @og.rev 4.3.4.0 (2008/12/01) $Cのカラム名置換えを追加 285 * @og.rev 5.1.3.0 (2010/02/01) 一覧表示のみで、ツールチップ表示を行う。 286 * @og.rev 6.0.4.0 (2014/11/28) useMultiSelect は、selection ではなく、colomn から取得する。 287 * @og.rev 6.2.0.0 (2015/02/27) キー:ラベル形式で表示するかどうかを、指定できるようにします。 288 * 289 * @param buf タグ文字列のバッファー 290 * @param value 選択されている値 291 * @param useSlbl ラベル(短)をベースとしたオプション表示を行うかどうか。 292 * 293 * @return オプションタグ 294 * @og.rtnNotNull 295 */ 296 private TagBuffer getOption( final TagBuffer buf,final String value,final boolean useSlbl ) { 297 298 // StringFormat format = new StringFormat( query,value); 299 final StringFormat format = new StringFormat( query, value, name ); // 4.3.4.0 (2008/12/01) 300 final String newQuery = format.format(); 301 final String newValue = format.getValue(); 302 303 // 6.2.0.0 (2015/02/27) キー:ラベル形式 304 final Selection selection = SelectionFactory.newDBSelection( newQuery, dbid, lang, addKeyLabel ); 305 // 6.0.4.0 (2014/11/28) useMultiSelect は、selection ではなく、colomn から取得する。 306 307 // 6.1.1.0 (2015/01/17) TagBufferの連結記述 308 return buf.addBody( Selection.NO_VALUE_OPTION , addNoValue ) // 5.5.1.0 (2012/04/03) 309 .addBody( selection.getOption( newValue, seqFlag, useSlbl ) ); 310 311 } 312}