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.fukurou.system.LogWriter;
019import static org.opengion.fukurou.system.HybsConst.CR ;                                // 6.1.0.0 (2014/12/26)
020import static org.opengion.fukurou.system.HybsConst.BUFFER_MIDDLE;      // 6.1.0.0 (2014/12/26) refactoring
021
022/**
023 * データのコード情報を取り扱うクラスです。
024 *
025 * 開始、終了、ステップの情報から、HTMLのメニューやリストを作成するための オプション
026 * タグを作成したり、与えられたキーをもとに、チェック済みのオプションタグを作成したりします。
027 * 
028 * ここでは、数字(連番)の自動生成を行います。パラメータで、開始、終了、ステップを指定します。
029 * パラメータの初期値は、開始(1)、終了(10)、ステップ(1) です。
030 * 
031 * 例:1,10,1    → 1,2,3,4,5,6,7,8,9,10 のプルダウン
032 * 例:10,100,10 → 10,20,30,40,50,60,70,80,90,100 のプルダウン
033 * 例:-5,5,1    → -5,-4,-3,-2,-1,0,1,2,3,4,5 のプルダウン
034 * 例:5,-5,-2   → 5,3,1,-1,-3,-5 のプルダウン
035 *
036 * @og.group 選択データ制御
037 * @og.rev 5.6.1.1 (2013/02/08) 新規追加
038 *
039 * @version  4.0
040 * @author   Kazuhiko Hasegawa
041 * @since    JDK5.0,
042 */
043public class Selection_NUM extends Selection_NULL {
044        private final String   CACHE ;
045        private final String   ST_ED_STEP ;
046
047        /**
048         * コンストラクター
049         *
050         * 引数は、開始、終了、ステップです。
051         * パラメータの初期値は、開始(1)、終了(10)、ステップ(1) です。
052         *
053         * @og.rev 6.2.6.0 (2015/06/19) type別Selectionの場合、ラベルリソースを使用する為、言語を引数で渡す。
054         * @og.rev 6.3.4.0 (2015/08/01) Selection_NUM の引数から、lang 属性を削除します。
055         *
056         * @param       editPrm 開始、終了、[ステップ]を表す引数(例:1,10,1)
057         */
058        public Selection_NUM( final String editPrm ) {
059                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
060        //      if( param.length < 2 ) {
061        //      final String errMsg = "引数は、開始、終了、[ステップ] です。最低でも2個必要です。";
062        //              throw new IllegalArgumentException( errMsg );
063        //      }
064
065                final String[] param = editPrm == null ? new String[0] : editPrm.split( "," ) ;
066
067                final int start = param.length > 0 ? Integer.parseInt( param[0].trim() ) : 1;
068                final int end   = param.length > 1 ? Integer.parseInt( param[1].trim() ) : 10 ;
069                final int step  = param.length > 2 ? Integer.parseInt( param[2].trim() ) : 1;
070
071                if( step == 0 ) {
072                        final String errMsg = "ステップ に 0 は指定できません。無限ループします。";
073                        throw new IllegalArgumentException( errMsg );
074                }
075
076                final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE );
077
078                // ステップの正負による判定の違い。while( Math.signum( end-start ) * step >= 0.0 ) で、判る?
079                // 終了条件は、含む(val<=end)
080                int val  = start;
081                final int sign = step > 0 ? 1 : -1 ;    // ステップの符号。
082                while( (end - val) * sign >= 0 ) {
083                        buf.append( "<option value=\"" ).append( val )
084                                .append( "\">" ).append( val ).append( "</option>" );           // 6.0.2.5 (2014/10/31) char を append する。
085                        val += step;
086                }
087
088                CACHE = buf.toString();
089                ST_ED_STEP = "Start=" + start + " , End=" + end + " , Step=" + step ;
090        }
091
092        /**
093         * 初期値が選択済みの 選択肢(オプション)を返します。
094         * このオプションは、引数の値を初期値とするオプションタグを返します。
095         * このメソッドでは、引数のuseShortLabelがtrueに指定された場合に、ラベル(短)をベースとした
096         * ツールチップ表示を行います。
097         *
098         * @param   selectValue  選択されている値
099         * @param   seqFlag  シーケンスアクセス機能 [true:ON/false:OFF]
100         * @param   useShortLabel ラベル(短)をベースとしたオプション表示を行うかどうか。(未使用)
101         *
102         * @return  オプションタグ
103         * @og.rtnNotNull
104         */
105        @Override
106        public String getOption( final String selectValue,final boolean seqFlag, final boolean useShortLabel ) {
107                // マッチするアドレスを探す。
108                final int selected = CACHE.indexOf( "\"" + selectValue + "\"" );
109
110                if( selected < 0 ) {
111                        if( selectValue != null && selectValue.length() > 0 ) {
112                                final String errMsg = "数字範囲に存在しない値が指定されました。"
113                                                        + " value=[" + selectValue + "]"
114                                                        + CR + ST_ED_STEP ;
115                                LogWriter.log( errMsg );
116                        }
117                        return CACHE;
118                }
119                else {
120                        // "値" 文字列の位置が、selected なので、値の文字数+2までが、前半部分になる。
121                        final int indx = selected + selectValue.length() + 2 ;
122
123                        final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE );
124                        // 3.6.0.6 (2004/10/22) シーケンスアクセス機能を指定する seqFlag を導入
125                        if( seqFlag ) {
126                                buf.append( "<option value=\"" ).append( selectValue ).append( '"' );           // 6.0.2.5 (2014/10/31) char を append する。
127                        }
128                        else {
129                                buf.append( CACHE.substring( 0,indx ) );
130                        }
131                        buf.append( " selected=\"selected\"" )
132                                .append( CACHE.substring( indx ) );
133                        return buf.toString() ;
134                }
135        }
136
137        /**
138         * 選択肢(value)に対するラベルを返します。
139         * 選択肢(value)が、存在しなかった場合は、選択肢そのものを返します。
140         * このメソッドでは、短縮ラベルを返すかどうかを指定するフラグを指定します。
141         * getValueLabel( XX,false ) は、getValueLabel( XX ) と同じです。
142         *
143         * @og.rev 4.0.0.0 (2005/11/30) を追加
144         *
145         * @param       selectValue     選択肢の値
146         * @param       isSLbl  短縮ラベルを [true:使用する/false:しない] (未使用)
147         *
148         * @return  選択肢のラベル
149         * @see     #getValueLabel( String )
150         */
151        @Override
152        public String getValueLabel( final String selectValue,final boolean isSLbl ) {
153                // あろうがなかろうが、選択肢そのものを返します。
154                return selectValue;
155        }
156
157}