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.fukurou.util.XHTMLTag;
023import org.opengion.fukurou.util.Attributes;
024import org.opengion.fukurou.util.TagBuffer;
025
026import org.opengion.hayabusa.db.Selection;                                                              // 6.4.4.0 (2016/03/11)
027import org.opengion.hayabusa.db.SelectionFactory;                                               // 6.4.4.0 (2016/03/11)
028
029/**
030 * CHBOX2 エディターは、カラムのデータをチェックボックスで編集する場合に使用するクラスです。
031 *
032 * このエディターは、CHBOXとは異なりチェックボックス特有の制御を全く行いません。
033 * 特性としては、typeがcheckboxであるという1点を除いて、TEXT エディターと同じです
034 *
035 * ラベル表示が必要な場合は、編集パラメータに、"useLabel"と
036 * 記述しておくことで、ラベルを出力することが可能です。
037 *
038 * カラムの表示に必要な属性は, DBColumn オブジェクト より取り出します。
039 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。
040 *
041 * 6.4.4.0 (2016/03/11)
042 * チェックボックスとして、チェックした値を送信する機能に、コードリソースの値を
043 * 指定できるようにします。これは、プルダウンメニューのマルチプルと同じ効果が
044 * 期待できます。
045 * コードリソースは、無くてもかまいません。
046 *
047 * @og.group データ編集
048 *
049 * @version  6.4
050 * @author       Kazuhiko Hasegawa
051 * @since    JDK8.0,
052 */
053public class Editor_CHBOX2 extends AbstractEditor {
054        /** このプログラムのVERSION文字列を設定します。   {@value} */
055        private static final String VERSION = "6.4.4.0 (2016/03/11)" ;
056
057        // 6.4.4.0 (2016/03/11) CHBOX2 は、Selection は、必須ではなく、定義されていれば使う程度
058        private final Selection selection ;             // 6.4.4.0 (2016/03/11)
059        private final boolean writable  ;
060        private final boolean useLabel  ;               // 4.3.3.0 (2008/10/01)
061
062        /**
063         * デフォルトコンストラクター。
064         * このコンストラクターで、基本オブジェクトを作成します。
065         *
066         * @og.rev 6.4.4.0 (2016/03/11) CHBOX2は、コードリソースも使用できるように変更。
067         */
068        public Editor_CHBOX2() {
069                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
070                selection = null;
071                writable  = false;
072                useLabel  = false;
073        }
074
075        /**
076         * コンストラクター。
077         *
078         * @og.rev 6.3.3.0 (2015/07/25) maxlength は不要なので削除
079         * @og.rev 6.4.4.0 (2016/03/11) CHBOX2は、コードリソースも使用できるように変更。
080         *
081         * @param       clm     DBColumnオブジェクト
082         */
083        private Editor_CHBOX2( final DBColumn clm ) {
084                super( clm );
085                attributes.set( "type"          , "checkbox"    );
086                attributes.set( "NO_MAXLEN"     , "true"                );                      // 6.3.3.0 (2015/07/25) maxlength は不要なので削除
087
088                tagBuffer.add( XHTMLTag.inputAttri( attributes ) );
089
090                // CHBOX2 は、コードリソース(selection)が存在しない場合もありうる。
091                final String addKeyLabel = clm.getAddKeyLabel();                // 6.2.0.0 (2015/02/27) キー:ラベル形式
092                selection = SelectionFactory.newSelection( "CHBOX" , clm.getCodeData(), addKeyLabel );
093
094                // 6.4.4.0 (2016/03/11) CHBOX2は、コードリソースも使用できるように変更。
095                writable = clm.isWritable();
096
097                // 6.1.1.0 (2015/01/17) Attributesの連結記述
098                attributes = new Attributes()
099                                        .set( clm.getEditorAttributes() )                               // #addAttributes( Attributes ) の代替え
100                                        .add( "class","CHBOX" );
101
102                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
103                tagBuffer.add( XHTMLTag.inputAttri( attributes ) )
104                                 .add( attributes.get( "optionAttributes" ) );          // 6.0.4.0 (2014/11/28)
105
106                useLabel = "useLabel".equalsIgnoreCase( clm.getEditorParam() );
107        }
108
109        /**
110         * 各オブジェクトから自分のインスタンスを返します。
111         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
112         * まかされます。
113         *
114         * @param       clm     DBColumnオブジェクト
115         *
116         * @return      CellEditorオブジェクト
117         * @og.rtnNotNull
118         */
119        public CellEditor newInstance( final DBColumn clm ) {
120                return new Editor_CHBOX2( clm );
121        }
122
123        /**
124         * データの編集用文字列を返します。
125         *
126         * @og.rev 6.4.4.0 (2016/03/11) チェックボックスの横に値をラベル表示します
127         *
128         * @param       value 値
129         *
130         * @return      データの編集用文字列
131         * @og.rtnNotNull
132         */
133        @Override
134        public String getValue( final String value ) {
135                final String chbox ;
136
137                if( selection == null ) {
138                        chbox = new StringBuilder( BUFFER_MIDDLE )
139                                                        .append( "<label>" )
140                                                        .append( super.getValue( value ) )
141                                                        .append( value )
142                                                        .append( "</label>" )
143                                                        .toString();
144                }
145                else {
146                        if( writable ) {
147                                chbox = selection.getOption( name,value,true );                 // 6.2.2.4 (2015/04/24)
148                        }
149                        else {
150                                chbox = selection.getValueLabel( value,true );                  // 6.2.2.4 (2015/04/24) useLabel
151                        }
152                }
153
154                return new TagBuffer( "pre" )
155                                        .add( tagBuffer.makeTag() )
156                                        .addBody( chbox )
157                                        .makeTag();
158        }
159
160        /**
161         * name属性を変えた、データ表示/編集用のHTML文字列を作成します。
162         * テーブル上の name に 行番号を付加して、名前_行番号 で登録するキーを作成し,
163         * リクエスト情報を1つ毎のフィールドで処理できます。
164         *
165         * @og.rev 6.4.4.0 (2016/03/11) チェックボックスの横に値をラベル表示します。
166         *
167         * @param       row   行番号
168         * @param       value 値
169         *
170         * @return      データ表示/編集用の文字列
171         * @og.rtnNotNull
172         */
173        @Override
174        public String getValue( final int row,final String value ) {
175                final String chbox ;
176
177                if( selection == null ) {
178                        chbox = new StringBuilder( BUFFER_MIDDLE )
179                                                .append( "<label>" )
180                                                .append( super.getValue( row,value ) )
181                                                .append( useLabel ? value : "" )
182                                                .append( "</label>" )
183                                                .toString();
184                }
185                else {
186                        if( writable ) {
187                                chbox = selection.getOption( name + HybsSystem.JOINT_STRING + row,value,useLabel );             // 6.2.2.4 (2015/04/24)
188                }
189                else {
190                                chbox = selection.getValueLabel( value,useLabel );              // 6.2.2.4 (2015/04/24)
191                        }
192                }
193
194                return new TagBuffer( "pre" )
195                                        .add( tagBuffer.makeTag() )
196                                        .addBody( chbox )
197                                        .makeTag( row,value );
198        }
199}