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.hayabusa.db;
017
018import org.opengion.hayabusa.common.HybsSystemException;
019import org.opengion.hayabusa.resource.CodeData;
020import static org.opengion.fukurou.system.HybsConst.CR ;                                // 6.1.0.0 (2014/12/26)
021import static org.opengion.fukurou.system.HybsConst.BUFFER_LARGE;               // 6.1.0.0 (2014/12/26) refactoring
022
023/**
024 * データのコード情報を取り扱うクラスです。
025 *
026 * コードのキーとラベルの情報から、HTMLのメニューやリストを作成するための オプション
027 * タグを作成したり、与えられたキーをもとに、チェック済みのオプションタグを作成したり
028 * します。
029 *
030 * @og.group 選択データ制御
031 *
032 * @version  4.0
033 * @author   Kazuhiko Hasegawa
034 * @since    JDK5.0,
035 */
036public class Selection_RADIO extends Selection_NULL {
037        private final CodeData codeData ;
038
039        /**
040         * コンストラクター
041         *
042         * @param       cdData  コードデータオブジェクト
043         *
044         */
045        public Selection_RADIO( final CodeData cdData ) {
046                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
047                if( cdData == null ) {
048                        final String errMsg = "コードリソースが定義されていません。" + CR ;
049                        throw new HybsSystemException( errMsg );
050                }
051
052                codeData = cdData ;
053        }
054
055        /**
056         * 初期値が選択済みの 選択肢(オプション)を返します。
057         * このオプションは、引数の値を初期値とするオプションタグを返します。
058         * ※ このクラスでは実装されていません。
059         *
060         * @og.rev 5.1.3.0 (2010/02/01) 追加
061         *
062         * @param       selectValue     選択されている値
063         * @param       seqFlag シーケンスアクセス機能の指定
064         * @param       useShortLabel   短ラベルの指定
065         *
066         * @return  オプションタグ
067         */
068        @Override
069        public String getOption( final String selectValue,final boolean seqFlag, final boolean useShortLabel ) {
070                final String errMsg = "このクラスでは実装されていません。";
071                throw new UnsupportedOperationException( errMsg );
072        }
073
074        /**
075         * 初期値が選択済みの 選択肢(オプション)を返します。
076         * このオプションは、引数の値を初期値とするオプションタグを返します。
077         *
078         * @og.rev 2.1.0.1 (2002/10/17) 選択リストを、正方向にしか選べないようにする sequenceFlag を導入する
079         * @og.rev 3.5.6.3 (2004/07/12) キャッシュを利用せず毎回タグを作成します。
080         * @og.rev 3.8.6.0 (2006/09/29) useLabel 属性 追加
081         * @og.rev 6.2.2.4 (2015/04/24) メソッド変更。旧 #getRadio( String , String , boolean )
082         * @og.rev 7.0.1.0 (2018/10/15) XHTML → HTML5 対応(空要素の、"/>" 止めを、">" に変更します)。
083         * @og.rev 7.2.6.1 (2020/07/17) codeGroupが使えるように、isUseを判定する。
084         *
085         * @param   name         ラジオの name
086         * @param   selectValue  選択されている値
087         * @param   useLabel     ラベル表示の有無 [true:有/false:無]
088         *
089         * @return  オプションタグ
090         * @og.rtnNotNull
091         */
092        @Override
093        public String getOption( final String name,final String selectValue,final boolean useLabel ) {
094                final String inputTag = "<input type=\"radio\" name=\"" + name + "\" value=\"" ;
095                final StringBuilder buf = new StringBuilder( BUFFER_LARGE );
096                final int size = codeData.getSize();
097                for( int i=0; i<size; i++ ) {
098                        if( ! codeData.isUse(i) ) { continue; }         // 7.2.6.1 (2020/07/17)
099
100                        final String value = codeData.getCodeKey(i);
101                        if( useLabel ) { buf.append( "<label>" ); }
102                        buf.append( inputTag ).append( value ).append( '"' );           // 6.0.2.5 (2014/10/31) char を append する。
103                        if( value.equals( selectValue ) ) {
104                                buf.append( " checked=\"checked\"" );
105                        }
106//                      buf.append( "/>" );
107                        buf.append( '>' );                              // 7.0.1.0 (2018/10/15)
108                        if( useLabel ) { buf.append( codeData.getShortLabel(i) ).append( "</label>" ); }
109                }
110                return buf.toString();
111        }
112
113        /**
114         * 選択肢(value)に対するラベルを返します。
115         * 選択肢(value)が、存在しなかった場合は、選択肢そのものを返します。
116         * このメソッドでは、短縮ラベルを返すかどうかを指定するフラグを指定します。
117         * getValueLabel( XX,false ) は、getValueLabel( XX ) と同じです。
118         *
119         * @og.rev 4.0.0.0 (2005/11/30) を追加
120         *
121         * @param       selectValue     選択肢の値
122         * @param       isSLbl  短縮ラベルを使用する [true:使用する/false:しない]
123         *
124         * @return  選択肢のラベル
125         * @see     #getValueLabel( String )
126         */
127        @Override
128        public String getValueLabel( final String selectValue,final boolean isSLbl ) {
129                // マッチするアドレスを探す。
130                final int selected = codeData.getAddress( selectValue );
131
132                // 6.4.1.1 (2016/01/16) PMD refactoring. A method should have only one exit point, and that should be the last statement in the method
133                return selected < 0
134                                        ? selectValue                   // マッチしなければ、選択肢そのものを返す。
135                                        : isSLbl
136                                                ? codeData.getShortLabel(selected)
137                                                : codeData.getLongLabel(selected);
138        }
139
140}