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.db.AbstractEditor;
020import org.opengion.hayabusa.db.CellEditor;
021import org.opengion.hayabusa.db.DBColumn;
022import org.opengion.hayabusa.db.SelectionCellEditor;                                    // 6.2.2.0 (2015/03/27)
023import org.opengion.hayabusa.db.Selection;
024import org.opengion.hayabusa.db.SelectionFactory;                                               // 6.0.4.0 (2014/11/28)
025import org.opengion.fukurou.util.XHTMLTag;
026import org.opengion.fukurou.util.Attributes;
027import org.opengion.fukurou.util.TagBuffer;
028
029import static org.opengion.fukurou.util.StringUtil.isNull;                              // 6.1.1.0 (2015/01/17)
030
031/**
032 * INMENU エディターは、コードリソースに対応したプルダウンメニューと、
033 * テキストフィールドによる入力の両方をサポートする、編集に使用するクラスです。
034 *
035 * JavaScript によりテキストフィールドとメニュー(コンボボックス)を重ね合わせて
036 * 表示しておき、メニューで選択した値を、テキストフィールドに設定しています。
037 * このエディタを使用するには、jsp/common/inputMenu.js を予め使用できるように
038 * 設定しておく必要があります。
039 *
040 * 編集パラメータに"SEQ"と記述することで正方向にしか選べないプルダウンメニューを実現できます。
041 * これにより、シーケンスにステータスを順に挙げていくような、プルダウンメニュー
042 * を作成することが出来ます。(逆に戻れないメニュー)
043 *
044 * このエディタはeventColumnに対応していません。
045 *
046 *  カラムの表示に必要な属性は, DBColumn オブジェクト より取り出します。
047 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。
048 *
049 * @og.rev 3.5.6.2 (2004/07/05) 新規作成
050 * @og.rev 6.2.2.0 (2015/03/27) SelectionCellEditor I/Fを追加
051 * @og.group データ編集
052 *
053 * @version  4.0
054 * @author       Kazuhiko Hasegawa
055 * @since    JDK5.0,
056 */
057public class Editor_INMENU extends AbstractEditor implements SelectionCellEditor {
058        /** このプログラムのVERSION文字列を設定します。   {@value} */
059        private static final String VERSION = "7.0.5.1 (2019/09/27)" ;
060
061        // 8.1.0.0 (2021/12/28) HTML5 準拠に見直し(<script> type属性削除)
062//      private static final String SEL1 = "<script type=\"text/javascript\">makeInputMenu('" ;
063        private static final String SEL1 = "<script>makeInputMenu('" ;
064        private static final String SEL2 = "');</script>" ;
065
066        /** セレクションオブジェクト */
067        private final Selection selection       ;               // 6.0.4.0 (2014/11/28) サブクラスからの参照を廃止
068        private final boolean addNoValue        ;
069        private final boolean seqFlag           ;               // 3.6.0.6 (2004/10/22)
070        private final String useSLabel          ;               // 6.2.0.0 (2015/02/27) SLABEL 対応
071
072        private final TagBuffer selTagBuffer = new TagBuffer() ;
073
074        private String errMsg   ;                                       // 6.0.4.0 (2014/11/28)
075
076        /**
077         * デフォルトコンストラクター。
078         * このコンストラクターで、基本オブジェクトを作成します。
079         *
080         * @og.rev 3.6.0.6 (2004/10/22) シーケンスアクセス機能(seqFlag)を追加します
081         * @og.rev 6.2.0.0 (2015/02/27) SLABEL 対応
082         */
083        public Editor_INMENU() {
084                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
085                // 4.3.4.4 (2009/01/01)
086                selection       = null;
087                addNoValue      = false;                // 3.5.5.7 (2004/05/10)
088                seqFlag         = false;                // 3.6.0.6 (2004/10/22)
089                useSLabel       = "auto";               // 6.2.0.0 (2015/02/27) SLABEL 対応
090        }
091
092        /**
093         * コンストラクター。
094         *
095         * @og.rev 3.6.0.6 (2004/10/22) シーケンスアクセス機能(seqFlag)を追加します
096         * @og.rev 4.0.0.0 (2005/01/31) SelectionFactory ではなく、直接 Selection_CODE を作成。
097         * @og.rev 4.0.0.0 (2006/11/24) TextField分の属性設定
098         * @og.rev 4.0.0.0 (2007/11/07) SelectionオブジェクトをDBColumnから取得
099         * @og.rev 5.6.3.0 (2013/04/01) プルダウンのonChangeの設定場所を変更
100         * @og.rev 6.0.4.0 (2014/11/28) selection が null の場合、警告表示します。
101         * @og.rev 6.2.0.0 (2015/02/27) SLABEL 対応
102         * @og.rev 6.2.0.0 (2015/02/27) キー:ラベル形式で表示するかどうかを、指定できるようにします。
103         * @og.rev 7.0.5.1 (2019/09/27) optionAttributes が二重に設定されていたため、削除
104         *
105         * @param       clm     DBColumnオブジェクト
106         */
107        protected Editor_INMENU( final DBColumn clm ) {
108                super( clm );
109                tagBuffer.add( XHTMLTag.inputAttri( attributes ) );
110
111                addNoValue      = clm.isAddNoValue() ;          // 3.5.5.7 (2004/05/10)
112                seqFlag         = "SEQ".equals( clm.getEditorParam() ); // 3.6.0.6 (2004/10/22)
113                useSLabel       = clm.getUseSLabel() ;          // 6.2.0.0 (2015/02/27) SLABEL 対応
114
115                final String addKeyLabel = clm.getAddKeyLabel();                // 6.2.0.0 (2015/02/27) キー:ラベル形式
116                final String disabled    = clm.isWritable() ? null : "disabled" ;
117
118                // 6.1.1.0 (2015/01/17) Attributesの連結記述
119                final Attributes selAttri = new Attributes()
120                                .set( "disabled" ,disabled )
121                                .set( clm.getEditorAttributes() )                       // #addAttributes( Attributes ) の代替え
122                                .set( "onChange" ,"selChanged(this);" );        // INMENU 特有のJavaScript 5.6.3.0 (2013/04/01) 場所移動
123
124                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
125                // 7.0.5.1 (2019/09/27) optionAttributes が二重に設定されていたため、削除
126                selTagBuffer.add( XHTMLTag.selectAttri( selAttri ) );
127//                                      .add( selAttri.get( "optionAttributes" ) );                             // 6.0.4.0 (2014/11/28)
128
129                // 6.0.4.0 (2014/11/28) selection は、Column から取得するのではなく、Factory で作成する。
130                selection = SelectionFactory.newSelection( "MENU",clm.getCodeData(),addKeyLabel );      // 6.2.0.0 (2015/02/27)
131
132                // 6.0.4.0 (2014/11/28) selection が null の場合、警告表示します。
133                if( selection == null ) {
134                        errMsg = "codeData が未設定です。"
135                                                                + " name="  + name
136                                                                + " label=" + clm.getLabel()
137                                                                + " editType="  + clm.getEditor() ;
138                        System.out.println( errMsg );
139                }
140        }
141
142        /**
143         * 各オブジェクトから自分のインスタンスを返します。
144         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
145         * まかされます。
146         *
147         * @param       clm     DBColumnオブジェクト
148         *
149         * @return      CellEditorオブジェクト
150         * @og.rtnNotNull
151         */
152        public CellEditor newInstance( final DBColumn clm ) {
153                return new Editor_INMENU( clm );
154        }
155
156        /**
157         * データの編集用文字列を返します。
158         *
159         * @og.rev 3.6.0.6 (2004/10/22) シーケンスアクセス機能(seqFlag)を追加します
160         * @og.rev 3.8.5.3 (2006/06/30) 位置を絶対位置指定(position:absolute;)
161         * @og.rev 4.3.7.2 (2009/06/15) 属性でidが出力される場合は、idを出力しない
162         * @og.rev 6.0.4.0 (2014/11/28) selection が null の場合、INMENU は作成しません。
163         * @og.rev 6.2.0.0 (2015/02/27) SLABEL 対応
164         *
165         * @param       value 入力値
166         *
167         * @return      データの編集用文字列
168         * @og.rtnNotNull
169         */
170        @Override
171        public String getValue( final String value ) {
172                // input タグの作成
173                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
174                final String intag = new TagBuffer( "input" )
175                                                .add( "name"    , name )
176                                                .add( "id"              , name , isNull( attributes.get( "id" ) ) )             // 4.3.7.2 (2009/06/15)
177                                                .add( "value"   , value )
178                                                .add( "size"    , size1 )
179                                                .add( tagBuffer.makeTag() )
180                                                .makeTag();
181
182                // 6.0.4.0 (2014/11/28) selection が null の場合、INMENU は作成しません。
183                if( selection == null ) {
184                        return intag + CR;
185                }
186
187                final boolean useSlbl = "true".equalsIgnoreCase( useSLabel );           // 6.2.0.0 (2015/02/27)
188
189                // select タグの作成
190                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
191                final String seltag = new TagBuffer( "select" )
192                                                .add( "id"              , name + ".sel" )                       // INMENU 特有のJavaScript用のキー
193                                                .add( "style"   , "position:absolute;" )        // 3.8.5.3 (2006/06/30) 位置を絶対位置指定
194                                                .add( selTagBuffer.makeTag() )
195                                                .addBody( Selection.NO_VALUE_OPTION , addNoValue )                                      // 5.5.1.0 (2012/04/03)
196                                                .addBody( selection.getOption( value,seqFlag,useSlbl ) )                        // 6.2.0.0 (2015/02/27) SLABEL 対応
197                                                .makeTag();
198
199                return intag + CR + seltag + CR + SEL1 + name + SEL2;
200        }
201
202        /**
203         * name属性を変えた、データ表示/編集用のHTML文字列を作成します。
204         * テーブル上の name に 行番号を付加して、名前_行番号 で登録するキーを作成し,
205         * リクエスト情報を1つ毎のフィールドで処理できます。
206         *
207         * @og.rev 3.6.0.6 (2004/10/22) シーケンスアクセス機能(seqFlag)を追加します
208         * @og.rev 3.8.5.1 (2006/04/28) makeInputMenu 呼び出し時の引数記述ミスを修正
209         * @og.rev 3.8.5.3 (2006/06/30) 位置を絶対位置指定(position:absolute;)
210         * @og.rev 4.3.7.2 (2009/06/15) 属性でidが出力される場合は、idを出力しない
211         * @og.rev 6.0.4.0 (2014/11/28) selection が null の場合、INMENU は作成しません。
212         * @og.rev 6.2.0.0 (2015/02/27) SLABEL 対応
213         *
214         * @param       row   行番号
215         * @param       value 入力値
216         *
217         * @return      データ表示/編集用の文字列
218         * @og.rtnNotNull
219         */
220        @Override
221        public String getValue( final int row,final String value ) {
222
223                final String name2 = name + HybsSystem.JOINT_STRING + row ;
224
225                // input タグの作成
226                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
227                final String intag = new TagBuffer( "input" )
228                                                .add( "name"    , name2 )
229                                                .add( "id"              , name2 , isNull( attributes.get( "id" ) ) )            // INMENU 特有のJavaScript用のキー
230                                                .add( "value"   , value )
231                                                .add( "size"    , size2 )
232                                                .add( tagBuffer.makeTag() )
233                                                .makeTag( row,value );
234
235                // 6.0.4.0 (2014/11/28) selection が null の場合、INMENU は作成しません。
236                if( selection == null ) {
237                        return intag + CR;
238                }
239
240                final boolean useSlbl = "auto".equalsIgnoreCase( useSLabel ) || "true".equalsIgnoreCase( useSLabel );           // 6.2.0.0 (2015/02/27)
241
242                // select タグの作成
243                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
244                final String seltag = new TagBuffer( "select" )
245                                                .add( "id"              , name2 + ".sel" )                      // INMENU 特有のJavaScript用のキー
246                                                .add( "style"   , "position:absolute;" )        // 3.8.5.3 (2006/06/30) 位置を絶対位置指定
247                                                .add( selTagBuffer.makeTag() )
248                                                .addBody( Selection.NO_VALUE_OPTION , addNoValue )                                      // 5.5.1.0 (2012/04/03)
249                                                .addBody( selection.getOption( value,seqFlag,useSlbl ) )                        // 6.2.0.0 (2015/02/27) SLABEL 対応
250                                                .makeTag( row,value );
251
252                return intag + CR + seltag + CR + SEL1 + name2 + SEL2;
253        }
254}