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.common.HybsSystem;
019import org.opengion.hayabusa.db.AbstractRenderer;
020import org.opengion.hayabusa.db.CellRenderer;
021import org.opengion.hayabusa.db.DBColumn;
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 = "4.0.0.0 (2005/08/31)" ;
050
051        private static final CellRenderer dbCell = new Renderer_SLABEL() ;      // 20Byteでカット
052        private final int cutSize;
053
054        /**
055         * デフォルトコンストラクター。
056         * このコンストラクターで、基本オブジェクトを作成します。
057         *
058         */
059        public Renderer_SLABEL() {
060                cutSize = 20;
061        }
062
063        /**
064         * デフォルトコンストラクター。
065         *
066         * @param       clm     DBColumnオブジェクト
067         */
068        private Renderer_SLABEL( final DBColumn clm ) {
069                String param = clm.getRendererParam();
070                cutSize = Integer.parseInt( param );
071        }
072
073        /**
074         * 各オブジェクトから自分のインスタンスを返します。
075         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
076         * まかされます。
077         *
078         * @param       clm     DBColumnオブジェクト
079         *
080         * @return      CellRendererオブジェクト
081         */
082        public CellRenderer newInstance( final DBColumn clm ) {
083                String param = clm.getRendererParam();
084
085                if( param != null && param.length() > 0 ) {
086                        return new Renderer_SLABEL( clm );
087                }
088                else {
089                        return dbCell;
090                }
091        }
092
093        /**
094         * データの表示用文字列を返します。
095         *
096         * 全角2Byte / 半角および半角カタカナを 1Byte で簡易計算し、指定の
097         * 桁数でカットします。
098         * 初期値は、20Byteで、桁数は、表示パラメータ(RENDERER_PARAM)で指定します。
099         *
100         * @param       value 入力値
101         *
102         * @return      データの表示用文字列
103         */
104        @Override
105        public String getValue( final String value ) {
106
107                // 簡易的処理。すべてが全角であっても、制限以内である。
108                int len      = value.length();
109                if( len*2 <= cutSize ) { return value; }
110
111                int byteSize = 0;
112                int adrs;
113                for( adrs=0; adrs<len && byteSize<cutSize ; adrs++ ) {
114                        char ch = value.charAt(adrs);
115                        if( ch <= 0x7f || ( ch >= 0xff61 && ch <= 0xff9f ) ) {
116                                byteSize ++;
117                        }
118                        else {
119                                byteSize +=2;
120                        }
121                }
122
123                // 正確にカウントした結果、制限以内であったため。
124                if( adrs==len && byteSize<=cutSize ) {
125                        return value;
126                }
127                else if( byteSize>cutSize ) {                // オーバーした場合
128                        adrs-- ;
129                }
130
131                StringBuilder buf = new StringBuilder( HybsSystem.BUFFER_SMALL );
132                buf.append( "<span title=\"" );
133                buf.append( value );
134                buf.append( "\">" );
135                buf.append( value.substring( 0,adrs ) );        // 切り出し
136                buf.append( "...</span>" );
137
138                return buf.toString();
139        }
140}