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         *
083         * @param   name         ラジオの name
084         * @param   selectValue  選択されている値
085         * @param   useLabel     ラベル表示の有無 [true:有/false:無]
086         *
087         * @return  オプションタグ
088         * @og.rtnNotNull
089         */
090        @Override
091        public String getOption( final String name,final String selectValue,final boolean useLabel ) {
092                final String inputTag = "<input type=\"radio\" name=\"" + name + "\" value=\"" ;
093                final StringBuilder buf = new StringBuilder( BUFFER_LARGE );
094                final int size = codeData.getSize();
095                for( int i=0; i<size; i++ ) {
096                        final String value = codeData.getCodeKey(i);
097                        if( useLabel ) { buf.append( "<label>" ); }
098                        buf.append( inputTag ).append( value ).append( '"' );           // 6.0.2.5 (2014/10/31) char を append する。
099                        if( value.equals( selectValue ) ) {
100                                buf.append( " checked=\"checked\"" );
101                        }
102                        buf.append( "/>" );
103                        if( useLabel ) { buf.append( codeData.getShortLabel(i) ).append( "</label>" ); }
104                }
105                return buf.toString();
106        }
107
108        /**
109         * 選択肢(value)に対するラベルを返します。
110         * 選択肢(value)が、存在しなかった場合は、選択肢そのものを返します。
111         * このメソッドでは、短縮ラベルを返すかどうかを指定するフラグを指定します。
112         * getValueLabel( XX,false ) は、getValueLabel( XX ) と同じです。
113         *
114         * @og.rev 4.0.0.0 (2005/11/30) を追加
115         *
116         * @param       selectValue     選択肢の値
117         * @param       isSLbl  短縮ラベルを使用する [true:使用する/false:しない]
118         *
119         * @return  選択肢のラベル
120         * @see     #getValueLabel( String )
121         */
122        @Override
123        public String getValueLabel( final String selectValue,final boolean isSLbl ) {
124                // マッチするアドレスを探す。
125                final int selected = codeData.getAddress( selectValue );
126
127                // 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
128                return selected < 0
129                                        ? selectValue                   // マッチしなければ、選択肢そのものを返す。
130                                        : isSLbl
131                                                ? codeData.getShortLabel(selected)
132                                                : codeData.getLongLabel(selected);
133
134        }
135
136}