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