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 org.opengion.hayabusa.db.AbstractRenderer;
019import org.opengion.hayabusa.db.CellRenderer;
020import org.opengion.hayabusa.db.DBColumn;
021import org.opengion.fukurou.util.StringUtil ;                                           // 6.2.2.0 (2015/03/27)
022
023/**
024 * SLABEL レンデラーは、桁数の長いデータをコンパクトに表示させる
025 * LABEL レンデラーの類似クラスです。
026 *
027 * 全角2Byte / 半角および半角カタカナを 1Byte で簡易計算し、指定の
028 * 桁数でカットします。
029 * 初期値は、20Byteで、桁数は、表示パラメータ(RENDERER_PARAM)で指定します。
030 * 文字をカットした場合は、最後に『...』を追加し、カット前の文字を title 属性に
031 * 設定することで、マウスをカット後の文字に載せると、カット前の値がチップ表示
032 * されます。
033 * <span title="カット前の値">カット文字...</span>
034 * カットされなかった場合は、元の文字がそのまま表示されます。
035 *
036 *  カラムの表示に必要な属性は, DBColumn オブジェクト より取り出します。
037 * このクラスは、表示パラメータになにも指定しない(デフォルト)場合は、
038 * すべて同一のオブジェクトを返します。それ以外は、DBColumn オブジェクト毎に1つ作成されます。
039 *
040 * @og.rev 3.5.6.2 (2004/07/05) 新規作成
041 * @og.group データ表示
042 *
043 * @version  4.0
044 * @author       Kazuhiko Hasegawa
045 * @since    JDK5.0,
046 */
047public class Renderer_SLABEL extends AbstractRenderer {
048        /** このプログラムのVERSION文字列を設定します。   {@value} */
049        private static final String VERSION = "6.9.7.0 (2018/05/14)" ;
050
051        private static final CellRenderer DB_CELL = new Renderer_SLABEL() ;     // 20Byteでカット
052        private final int cutSize;
053
054        /**
055         * デフォルトコンストラクター。
056         * このコンストラクターで、基本オブジェクトを作成します。
057         *
058         */
059        public Renderer_SLABEL() {
060                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
061                cutSize = 20;
062        }
063
064        /**
065         * カットサイズを指定する、コンストラクター。
066         *
067         * @og.rev 6.9.7.0 (2018/05/14) カットサイズを指定する。
068         *
069//       * @param       clm     DBColumnオブジェクト
070         * @param       size    カットサイズ
071         */
072//      private Renderer_SLABEL( final DBColumn clm ) {
073        private Renderer_SLABEL( final String size ) {
074                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
075//              final String param = clm.getRendererParam();
076//              cutSize = Integer.parseInt( param );
077                cutSize = Integer.parseInt( size );
078        }
079
080        /**
081         * 各オブジェクトから自分のインスタンスを返します。
082         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
083         * まかされます。
084         *
085         * @param       clm     DBColumnオブジェクト
086         *
087         * @return      CellRendererオブジェクト
088         * @og.rtnNotNull
089         */
090        public CellRenderer newInstance( final DBColumn clm ) {
091                final String param = clm.getRendererParam();
092
093                // 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
094                // 反転させたので注意
095//              return param == null || param.isEmpty() ? DB_CELL : new Renderer_SLABEL( clm );
096                return param == null || param.isEmpty() ? DB_CELL : new Renderer_SLABEL( param );
097        }
098
099        /**
100         * データの表示用文字列を返します。
101         *
102         * 全角2Byte / 半角および半角カタカナを 1Byte で簡易計算し、指定の
103         * 桁数でカットします。
104         * 初期値は、20Byteで、桁数は、表示パラメータ(RENDERER_PARAM)で指定します。
105         *
106         * @og.rev 6.2.2.0 (2015/03/27) BRと\nを相互に変換する処理を追加
107         * @og.rev 6.2.2.3 (2015/04/10) htmlフィルターに、BR→改行処理機能を追加。
108         *
109         * @param       value 入力値
110         *
111         * @return      データの表示用文字列
112         */
113        @Override
114        public String getValue( final String value ) {
115
116                // 簡易的処理。すべてが全角であっても、制限以内である。
117                final int len      = value.length();
118                if( len*2 <= cutSize ) { return value; }
119
120                int byteSize = 0;
121                int adrs;
122                for( adrs=0; adrs<len && byteSize<cutSize ; adrs++ ) {
123                        final char ch = value.charAt(adrs);
124                        if( ch <= 0x7f || ch >= 0xff61 && ch <= 0xff9f ) {                      // 6.9.7.0 (2018/05/14) PMD Useless parentheses.
125                                byteSize ++;
126                        }
127                        else {
128                                byteSize +=2;
129                        }
130                }
131
132                // 正確にカウントした結果、制限以内であったため。
133                if( adrs==len && byteSize<=cutSize ) {
134                        return value;
135                }
136                else if( byteSize>cutSize ) {           // オーバーした場合
137                        adrs-- ;
138                }
139
140                final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE )
141                        .append( "<span title=\"" )
142                        .append( StringUtil.htmlFilter( value,true ) )
143                        .append( "\">" )
144                        .append( value.substring( 0,adrs ) )    // 切り出し
145                        .append( "...</span>" );
146
147                return buf.toString();
148        }
149
150        /**
151         * データ出力用の文字列を作成します。
152         * ファイル等に出力する形式を想定しますので、HTMLタグを含まない
153         * データを返します。
154         * SLABEL ですが、出力時はカットしません。
155         *
156         * @og.rev 6.0.4.0 (2014/11/28) データ出力用のレンデラー
157         *
158         * @param   value 入力値
159         *
160         * @return  データ出力用の文字列
161         * @og.rtnNotNull
162         * @see         #getValue( String )
163         */
164        @Override
165        public String getWriteValue( final String value ) {
166                return ( value == null ) ? "" : value;
167        }
168}