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 java.util.Set; 019import java.util.HashSet; 020import java.util.Arrays; 021 022import org.opengion.hayabusa.common.HybsSystemException; 023import org.opengion.hayabusa.resource.CodeData; 024import static org.opengion.fukurou.system.HybsConst.CR ; // 6.1.0.0 (2014/12/26) 025import static org.opengion.fukurou.system.HybsConst.BUFFER_LARGE; // 6.1.0.0 (2014/12/26) refactoring 026 027/** 028 * データのコード情報を取り扱うクラスです。 029 * 030 * コードのキーとラベルの情報から、HTMLのチェックボックスを作成するための オプション 031 * タグを作成したり、与えられたキーをもとに、チェック済みのオプションタグを作成したり 032 * します。 033 * 034 * ※ このクラスは、CHBOX 用ではなく、CHBOX2 用です。 035 * 036 * @og.group 選択データ制御 037 * @og.rev 6.4.4.0 (2016/03/11) 新規追加 038 * 039 * @version 6.4 040 * @author Kazuhiko Hasegawa 041 * @since JDK8.0, 042 */ 043public class Selection_CHBOX extends Selection_NULL { 044 private final CodeData codeData ; 045 046 /** 047 * コンストラクター 048 * 049 * @og.rev 6.4.4.0 (2016/03/11) 新規追加 050 * 051 * @param cdData コードデータオブジェクト 052 * 053 */ 054 public Selection_CHBOX( final CodeData cdData ) { 055 super(); // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor 056 if( cdData == null ) { 057 final String errMsg = "コードリソースが定義されていません。" + CR ; 058 throw new HybsSystemException( errMsg ); 059 } 060 061 codeData = cdData ; 062 } 063 064 /** 065 * 初期値が選択済みの 選択肢(オプション)を返します。 066 * このオプションは、引数の値を初期値とするオプションタグを返します。 067 * ※ このクラスでは実装されていません。 068 * 069 * @og.rev 6.4.4.0 (2016/03/11) CHBOX2は、コードリソースも使用できるように変更。 070 * 071 * @param selectValue 選択されている値 072 * @param seqFlag シーケンスアクセス機能の指定 073 * @param useShortLabel 短ラベルの指定 074 * 075 * @return オプションタグ 076 */ 077 @Override 078 public String getOption( final String selectValue,final boolean seqFlag, final boolean useShortLabel ) { 079 final String errMsg = "このクラスでは実装されていません。"; 080 throw new UnsupportedOperationException( errMsg ); 081 } 082 083 /** 084 * 初期値が選択済みの 選択肢(オプション)を返します。 085 * このオプションは、引数の値を初期値とするオプションタグを返します。 086 * 選択されている値は、複数指定が可能です。CSV形式データとして渡された場合は、 087 * 個別に分解して、ラベル化します。 088 * 089 * @og.rev 6.4.4.0 (2016/03/11) CHBOX2は、コードリソースも使用できるように変更。 090 * @og.rev 7.0.1.0 (2018/10/15) XHTML → HTML5 対応(空要素の、"/>" 止めを、">" に変更します)。 091 * @og.rev 7.2.6.1 (2020/07/17) codeGroupが使えるように、isUseを判定する。 092 * 093 * @param name ラジオの name 094 * @param selectValue 選択されている値 095 * @param useLabel ラベル表示の有無 [true:有/false:無] 096 * 097 * @return オプションタグ 098 * @og.rtnNotNull 099 */ 100 @Override 101 public String getOption( final String name,final String selectValue,final boolean useLabel ) { 102 // 選択値を、カンマで分解(split)して、Listを作成し、HashSetに渡している。存在チェック用 103 final Set<String> valSet = selectValue == null 104 ? new HashSet<>() 105 : new HashSet<>( Arrays.asList( selectValue.split( "," ) ) ); 106 107 final String inputTag = "<input type=\"checkbox\" name=\"" + name + "\" value=\"" ; 108 final StringBuilder buf = new StringBuilder( BUFFER_LARGE ); 109 final int size = codeData.getSize(); 110 for( int i=0; i<size; i++ ) { 111 if( ! codeData.isUse(i) ) { continue; } // 7.2.6.1 (2020/07/17) 112 113 final String value = codeData.getCodeKey(i); 114 if( useLabel ) { buf.append( "<label>" ); } 115 buf.append( inputTag ).append( value ).append( '"' ); // 6.0.2.5 (2014/10/31) char を append する。 116 if( valSet.contains( value ) ) { 117 buf.append( " checked=\"checked\"" ); 118 } 119// buf.append( "/>" ); 120 buf.append( '>' ); // 7.0.1.0 (2018/10/15) 121 if( useLabel ) { buf.append( codeData.getShortLabel(i) ).append( ' ' ).append( "</label>" ); } 122 } 123 124 return buf.toString(); 125 } 126 127 /** 128 * 選択肢(value)に対するラベルを返します。 129 * 選択肢(value)が、存在しなかった場合は、選択肢そのものを返します。 130 * このメソッドでは、短縮ラベルを返すかどうかを指定するフラグを指定します。 131 * getValueLabel( XX,false ) は、getValueLabel( XX ) と同じです。 132 * 133 * @og.rev 6.4.4.0 (2016/03/11) CHBOX2は、コードリソースも使用できるように変更。 134 * 135 * @param selectValue 選択肢の値 136 * @param isSLbl 短縮ラベルを使用する [true:使用する/false:しない] 137 * 138 * @return 選択肢のラベル 139 * @see #getValueLabel( String ) 140 */ 141 @Override 142 public String getValueLabel( final String selectValue,final boolean isSLbl ) { 143 // 選択値を、カンマで分解(split)して、Listを作成し、HashSetに渡している。存在チェック用 144 final Set<String> valSet = selectValue == null 145 ? new HashSet<>() 146 : new HashSet<>( Arrays.asList( selectValue.split( "," ) ) ); 147 148 final StringBuilder buf = new StringBuilder( BUFFER_LARGE ); 149 final int size = codeData.getSize(); 150 for( int i=0; i<size; i++ ) { 151 final String value = codeData.getCodeKey(i); 152 if( valSet.contains( value ) ) { 153 buf.append( '■' ); 154 } 155 else { 156 buf.append( '□' ); 157 } 158 159 if( isSLbl ) { buf.append( codeData.getShortLabel(i) ).append( ' ' ); } 160 } 161 return buf.toString(); 162 } 163}