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.rev 6.2.2.4 (2015/04/24) 新規作成 031 * @og.group 選択データ制御 032 * 033 * @version 4.0 034 * @author Kazuhiko Hasegawa 035 * @since JDK5.0, 036 */ 037public class Selection_BITBOX extends Selection_NULL { 038 private final CodeData codeData ; 039 040 /** 041 * コンストラクター 042 * 043 * @og.rev 6.2.2.4 (2015/04/24) 新規追加 044 * 045 * @param cdData コードデータオブジェクト 046 */ 047 public Selection_BITBOX( final CodeData cdData ) { 048 super(); // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor 049 if( cdData == null ) { 050 final String errMsg = "コードリソースが定義されていません。" + CR ; 051 throw new HybsSystemException( errMsg ); 052 } 053 054 codeData = cdData ; 055 } 056 057 /** 058 * 初期値が選択済みの 選択肢(オプション)を返します。 059 * このオプションは、引数の値を初期値とするオプションタグを返します。 060 * ※ このクラスでは実装されていません。 061 * 062 * @og.rev 6.2.2.4 (2015/04/24) 新規追加 063 * 064 * @param selectValue 選択されている値 065 * @param seqFlag シーケンスアクセス機能の指定 066 * @param useShortLabel 短ラベルの指定 067 * 068 * @return オプションタグ 069 */ 070 @Override 071 public String getOption( final String selectValue,final boolean seqFlag, final boolean useShortLabel ) { 072 final String errMsg = "このクラスでは実装されていません。"; 073 throw new UnsupportedOperationException( errMsg ); 074 } 075 076 /** 077 * 初期値が選択済みの 選択肢(オプション)を返します。 078 * このオプションは、引数の値を初期値とするオプションタグを返します。 079 * 080 * @og.rev 6.2.2.4 (2015/04/24) 新規追加 081 * @og.rev 6.4.4.0 (2016/03/11) ラベルとラジオボタンの間に、全角スペースをひとつ入れます。 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 String value = null; 093 try { 094 final String inputTag = "<input type=\"checkbox\" name=\"" + name + "\" value=\"" ; 095 final StringBuilder buf = new StringBuilder( BUFFER_LARGE ); 096 final int size = codeData.getSize(); 097 final int selVal = selectValue == null || selectValue.isEmpty() ? 0 : Integer.parseInt( selectValue ); // 数値化して、論理和の準備をする。 098 for( int i=0; i<size; i++ ) { 099 value = codeData.getCodeKey(i); 100 final int val = value == null || value.isEmpty() ? 0 : Integer.parseInt( value ); // 数値化して、論理和の準備をする。 101 if( useLabel ) { buf.append( "<label>" ); } 102 buf.append( inputTag ).append( value ).append( '"' ); // 6.0.2.5 (2014/10/31) char を append する。 103 if( ( val & selVal ) != 0 ) { // 論理積 104 buf.append( " checked=\"checked\"" ); 105 } 106 buf.append( "/>" ); 107 if( useLabel ) { buf.append( codeData.getShortLabel(i) ).append( ' ' ).append( "</label>" ); } 108 } 109 return buf.toString(); 110 } 111 catch( final NumberFormatException ex ) { // 文字列が解析可能な数値を含まない場合 112 final String errMsg = "BITBOXで、数値変換できませんでした。 " + CR 113 + " name=[" + name + ", selectValue=[" + selectValue + "] , value=[" + value + "]" + CR 114 + ex.getMessage() ; 115 return errMsg; 116 } 117 } 118 119 /** 120 * 選択肢(value)に対するラベルを返します。 121 * 選択肢(value)が、存在しなかった場合は、選択肢そのものを返します。 122 * このメソッドでは、短縮ラベルを返すかどうかを指定するフラグを指定します。 123 * getValueLabel( XX,false ) は、getValueLabel( XX ) と同じです。 124 * 125 * @og.rev 6.2.2.4 (2015/04/24) 新規追加 126 * 127 * @param selectValue 選択肢の値 128 * @param isSLbl 短縮ラベルを [true:使用する/false:しない] (未使用) 129 * 130 * @return 選択肢のラベル 131 * @see #getValueLabel( String ) 132 */ 133 @Override 134 public String getValueLabel( final String selectValue,final boolean isSLbl ) { 135 String value = null; 136 try { 137 final StringBuilder buf = new StringBuilder( BUFFER_LARGE ); 138 final int size = codeData.getSize(); 139 // 6.4.2.1 (2016/02/05) PMD refactoring. Useless parentheses. 140 final int selVal = selectValue == null || selectValue.isEmpty() ? 0 : Integer.parseInt( selectValue ); // 数値化して、論理積の準備をする。 141 for( int i=0; i<size; i++ ) { 142 value = codeData.getCodeKey(i); 143 // 6.4.2.1 (2016/02/05) PMD refactoring. Useless parentheses. 144 final int val = value == null || value.isEmpty() ? 0 : Integer.parseInt( value ); // 数値化して、論理積の準備をする。 145 if( ( val & selVal ) == 0 ) { // 論理積 146 buf.append( '□' ); 147 } 148 else { 149 buf.append( '■' ); 150 } 151 152 if( isSLbl ) { buf.append( codeData.getShortLabel(i) ).append( ' ' ); } 153 } 154 return buf.toString(); 155 } 156 catch( final NumberFormatException ex ) { // 文字列が解析可能な数値を含まない場合 157 final String errMsg = "BITBOXで、数値変換できませんでした。 " + CR 158 + " selectValue=[" + selectValue + "] , value=[" + value + "]" + CR 159 + ex.getMessage() ; 160 return errMsg; 161 } 162 } 163 164}