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