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.2.2.3 (2015/04/10)" ;
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         * @param       clm     DBColumnオブジェクト
068         */
069        private Renderer_SLABEL( final DBColumn clm ) {
070                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
071                final String param = clm.getRendererParam();
072                cutSize = Integer.parseInt( param );
073        }
074
075        /**
076         * 各オブジェクトから自分のインスタンスを返します。
077         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
078         * まかされます。
079         *
080         * @param       clm     DBColumnオブジェクト
081         *
082         * @return      CellRendererオブジェクト
083         * @og.rtnNotNull
084         */
085        public CellRenderer newInstance( final DBColumn clm ) {
086                final String param = clm.getRendererParam();
087
088                // 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
089                // 反転させたので注意
090                return param == null || param.isEmpty() ? DB_CELL : new Renderer_SLABEL( clm );
091
092        }
093
094        /**
095         * データの表示用文字列を返します。
096         *
097         * 全角2Byte / 半角および半角カタカナを 1Byte で簡易計算し、指定の
098         * 桁数でカットします。
099         * 初期値は、20Byteで、桁数は、表示パラメータ(RENDERER_PARAM)で指定します。
100         *
101         * @og.rev 6.2.2.0 (2015/03/27) BRと\nを相互に変換する処理を追加
102         * @og.rev 6.2.2.3 (2015/04/10) htmlフィルターに、BR→改行処理機能を追加。
103         *
104         * @param       value 入力値
105         *
106         * @return      データの表示用文字列
107         */
108        @Override
109        public String getValue( final String value ) {
110
111                // 簡易的処理。すべてが全角であっても、制限以内である。
112                final int len      = value.length();
113                if( len*2 <= cutSize ) { return value; }
114
115                int byteSize = 0;
116                int adrs;
117                for( adrs=0; adrs<len && byteSize<cutSize ; adrs++ ) {
118                        final char ch = value.charAt(adrs);
119                        if( ch <= 0x7f || ( ch >= 0xff61 && ch <= 0xff9f ) ) {
120                                byteSize ++;
121                        }
122                        else {
123                                byteSize +=2;
124                        }
125                }
126
127                // 正確にカウントした結果、制限以内であったため。
128                if( adrs==len && byteSize<=cutSize ) {
129                        return value;
130                }
131                else if( byteSize>cutSize ) {           // オーバーした場合
132                        adrs-- ;
133                }
134
135                final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE )
136                        .append( "<span title=\"" )
137                        .append( StringUtil.htmlFilter( value,true ) )
138                        .append( "\">" )
139                        .append( value.substring( 0,adrs ) )    // 切り出し
140                        .append( "...</span>" );
141
142                return buf.toString();
143        }
144
145        /**
146         * データ出力用の文字列を作成します。
147         * ファイル等に出力する形式を想定しますので、HTMLタグを含まない
148         * データを返します。
149         * SLABEL ですが、出力時はカットしません。
150         *
151         * @og.rev 6.0.4.0 (2014/11/28) データ出力用のレンデラー
152         *
153         * @param   value 入力値
154         *
155         * @return  データ出力用の文字列
156         * @og.rtnNotNull
157         * @see         #getValue( String )
158         */
159        @Override
160        public String getWriteValue( final String value ) {
161                return ( value == null ) ? "" : value;
162        }
163}