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