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.plugin.column;
017
018import java.util.StringTokenizer;
019
020import org.opengion.fukurou.util.StringUtil;
021// import org.opengion.fukurou.db.DBUtil;
022
023/**
024 * plugin.column パッケージ内で使用する、ユーティリティーメソッドを集めたクラスです。
025 *
026 * 特に関連する機能を集めたのではなく、単に共有できそうな処理を集めました。
027 *
028 * @og.rev 8.5.6.1 (2024/03/29) クラス名を RowSizeUtil → ColumnUtil に変更します。
029 * @og.group その他
030 *
031 * @version  8.5
032 * @author   Kazuhiko Hasegawa
033 * @since    JDK21.0,
034 */
035// /* default */ final class RowSizeUtil {
036/* default */ final class ColumnUtil {
037        /** このプログラムのVERSION文字列を設定します。   {@value} */
038        private static final String VERSION = "8.5.6.1 (2024/03/29)" ;
039
040        private static final String CODE = "Windows-31J";
041
042        /**
043         * デフォルトコンストラクターをprivateにして、
044         * オブジェクトの生成をさせないようにする。
045         *
046         * @og.rev 8.5.5.1 (2024/02/29) spotbugs CT_CONSTRUCTOR_THROW(コンストラクタで、Excweptionを出さない)
047         */
048//      private RowSizeUtil() { super(); }              // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
049        private ColumnUtil() { super(); }               // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
050
051//      /**
052//       * フィールドサイズを求める簡易メソッド。
053//       *
054//       * コンストラクターで、Exception を出さないための処理。
055//       *
056//       * @og.rev 8.5.5.1 (2024/02/29) spotbugs CT_CONSTRUCTOR_THROW(コンストラクタで、Excweptionを出さない)
057//       * @og.rev 8.5.6.1 (2024/03/29) 未使用なので廃止
058//       *
059//       * @param       tsize   TotalSize
060//       * @param       fsize   フィールドサイズ
061//       * @return      トータルサイズ/フィールドサイズ + 1
062//       */
063//      public static int getRowSizeInt( final int tsize , final String fsize ) {
064//              int rtnSize = 5;
065//              try {
066//                      final int r1 = tsize / Integer.parseInt( fsize ) + 1;
067//                      if( r1 <= 5 ) {
068//                              rtnSize = r1;
069//                      }
070//              }
071//              catch( final Throwable th ) {
072//                      final String errMsg = "フィールドサイズが数値変換できません。fsize=[" + fsize + "]" ;
073//                      System.err.println( errMsg );
074//              }
075//              return rtnSize;
076//      }
077
078        /**
079         * フィールドサイズを求める簡易メソッド。
080         *
081         * コンストラクターで、Exception を出さないための処理。
082         *
083         * @og.rev 8.5.5.1 (2024/02/29) spotbugs CT_CONSTRUCTOR_THROW(コンストラクタで、Excweptionを出さない)
084         *
085         * @param       tsize   TotalSize
086         * @param       fsize   フィールドサイズ
087         * @return      トータルサイズ/フィールドサイズ + 1
088         */
089        public static String getRowSize( final int tsize , final String fsize ) {
090                String rtnSize = "5";
091                try {
092                        final int r1 = tsize / Integer.parseInt( fsize ) + 1;
093                        if( r1 <= 5 ) {
094                                rtnSize = String.valueOf( r1 );
095                        }
096                }
097                catch( final Throwable th ) {
098                        final String errMsg = "フィールドサイズが数値変換できません。fsize=[" + fsize + "]" ;
099                        System.err.println( errMsg );
100                }
101                return rtnSize;
102        }
103
104        /**
105         * 自動表示する行列の数を求めます。
106         * 行数は、引数の文字列中に含まれる 改行コードの個数を求めます。
107         * 列数は、各行数のなかの最大桁数より求めます。これには半角、全角が含まれる為、
108         * 半角換算での文字数ということになります。
109         * 行数と列数が、初期設定の行数と列数より小さい場合は、初期設定値が使用されます。
110         *
111         * @og.rev 8.5.6.1 (2024/03/29) Editor_AUTOAREA のメソッドをstatic化してColumnUtilに移動します。
112         *
113         * @param   value       表示文字列
114         * @param   cols        最小カラム数
115         * @param   rows        最小行数
116         * @param   maxColSize  最大カラム数
117         * @param   maxRowSize  最大行数
118         *
119         * @return      自動計算した行列の配列
120         * @og.rtnNotNull
121         */
122//      private int[] getRowsCols( final String value, final int cols, final int rows ) {
123        public static String[] getRowsCols( final String value, final String cols, final String rows
124                                                                                ,final int maxColSize, final int maxRowSize ) {
125                if( value == null ) {
126//                      return new int[] { cols,rows };                                 // 6.3.9.1 (2015/11/27) 逆だったが、今まで大丈夫だった?
127                        return new String[] { cols, rows };
128                }
129
130                final StringTokenizer token = new StringTokenizer( value, "\n", true );
131
132                int cntRow = 1;
133                int maxCol = 0;
134                while( token.hasMoreTokens() ) {
135                        final String val = token.nextToken();
136                        if( "\n".equals( val ) ) { cntRow++; }
137                        else {
138                                final byte[] byteValue = StringUtil.makeByte( val,CODE );       // 3.5.5.3 (2004/04/09)
139                                final int byteSize = byteValue.length;
140                                if( maxColSize > 0 && byteSize > maxColSize ) {         // 最大列数
141                                        cntRow += byteSize / maxColSize;
142                                        maxCol = maxColSize ;
143                                }
144                                else if( byteSize > maxCol ) { maxCol = byteSize; }
145                        }
146                        if( maxRowSize > 0 && cntRow >= maxRowSize ) {          // 最大行数
147                                cntRow = maxRowSize;
148                                break;
149                        }
150                }
151
152                maxCol += 2;    // マージン。フォントや画面サイズに影響する為、比率のほうがよい?
153
154                // 6.3.9.1 (2015/11/27) Found 'DD'-anomaly for variable(PMD)
155                maxCol = Math.max( Integer.parseInt( cols ),maxCol );           // COL
156                cntRow = Math.max( Integer.parseInt( rows ),cntRow );           // ROW
157
158                //                                              COL                                                     ROW
159                return new String[] { String.valueOf( maxCol ), String.valueOf( cntRow ) };
160
161//              //                                      COL                                                     ROW
162//              return new int[] { Math.max( cols,maxCol ) , Math.max( rows,cntRow ) } ;        // 6.3.9.1 (2015/11/27)
163        }
164}