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.Locale;
019import java.util.Map;
020import java.util.HashMap;
021
022import org.opengion.hayabusa.db.AbstractRenderer;
023import org.opengion.hayabusa.db.CellRenderer;
024import org.opengion.hayabusa.db.DBColumn;
025import org.opengion.fukurou.util.StringUtil ;                                           // 6.2.2.0 (2015/03/27)
026
027/**
028 * ICON レンデラーは、カラムのファイル名の拡張子からアイコンファイルのイメージタグを作成します。
029 * イメージデータは、jsp/image/thumb を使用します。
030 *
031 * 実質的には、アイコンではなく、サムネイルとして利用します。
032 * 
033 * 縦横比をそのままに、縦か横の最大値に画像サイズを合わせるには、
034 * style="max-width:100; max-height:100;" をセットすることで対応できます。
035 * class="ICON" 属性を出力しておきますので、CSSファイルで記述してください。
036 *
037 * (例:)
038 *<pre>
039 *  &lt;style type="text/css"&gt;
040 *      img.ICON { max-width:100px; max-height:100px; }
041 *  &lt;/style&gt;
042 *</pre>
043 *
044 * このクラスは、不変オブジェクトとして、共有されます。
045 *
046 * @og.rev 5.6.5.1 (2013/06/14) 新規作成
047 *
048 * @og.group データ表示
049 *
050 * @version  4.0
051 * @author       Kazuhiko Hasegawa
052 * @since    JDK5.0,
053 */
054public class Renderer_ICON extends AbstractRenderer {
055        /** このプログラムのVERSION文字列を設定します。   {@value} */
056        private static final String VERSION = "6.4.2.0 (2016/01/29)" ;
057
058        private static final CellRenderer DB_CELL = new Renderer_ICON() ;
059
060        private static final String DOC_VIEW = "../image/thumb/docview.png" ;           // その他のアイコン
061
062        // アイコンファイルに割り当てられる拡張子とファイルの関連(MAP)情報
063        /** staticイニシャライザ後、読み取り専用にするので、ConcurrentHashMap を使用しません。 */
064        private static final Map<String,String> ICON_MAP ;
065        static {
066                ICON_MAP = new HashMap<>();
067
068                ICON_MAP.put( "doc"             ,       "../image/thumb/doc.png" );
069                ICON_MAP.put( "docx"    ,       "../image/thumb/doc.png" );
070                ICON_MAP.put( "xls"             ,       "../image/thumb/xls.png" );
071                ICON_MAP.put( "xlsx"    ,       "../image/thumb/xls.png" );
072                ICON_MAP.put( "xlsm"    ,       "../image/thumb/xls.png" );      // 6.2.2.0 (2015/03/27) マクロ付Excel(.xlsm)対応
073                ICON_MAP.put( "ppt"             ,       "../image/thumb/ppt.png" );
074                ICON_MAP.put( "pptx"    ,       "../image/thumb/ppt.png" );
075                ICON_MAP.put( "pdf"             ,       "../image/thumb/pdf.png" );
076                ICON_MAP.put( "txt"             ,       "../image/thumb/text.png" );
077                ICON_MAP.put( "zip"             ,       "../image/thumb/zip.png" );
078        }
079
080        /**
081         * デフォルトコンストラクター
082         *
083         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
084         */
085        public Renderer_ICON() { super(); }             // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
086
087        /**
088         * 各オブジェクトから自分のインスタンスを返します。
089         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
090         * まかされます。
091         *
092         * @param       clm     DBColumnオブジェクト
093         *
094         * @return      CellRendererオブジェクト
095         * @og.rtnNotNull
096         */
097        public CellRenderer newInstance( final DBColumn clm ) {
098                return DB_CELL;
099        }
100
101        /**
102         * データの表示用文字列を返します。
103         *
104         * @og.rev 6.0.2.4 (2014/10/17) img タグに、title 属性追記
105         * @og.rev 6.2.2.0 (2015/03/27) BRと\nを相互に変換する処理を追加
106         * @og.rev 6.2.2.3 (2015/04/10) htmlフィルターに、BR→改行処理機能を追加。
107         *
108         * @param       value 入力値
109         *
110         * @return      データの表示用文字列
111         * @og.rtnNotNull
112         */
113        @Override
114        public String getValue( final String value ) {
115                String icon = null;
116
117                if( value != null ) {
118                        String sufix = null;
119                        final int idx = value.lastIndexOf('.');         // 6.0.2.5 (2014/10/31) refactoring
120                        if( idx >= 0 ) {
121                                sufix = value.substring( idx+1 ).toLowerCase( Locale.JAPAN );
122                                icon = ICON_MAP.get( sufix );
123                        }
124                }
125
126                if( icon == null ) { icon = DOC_VIEW; }
127
128                final String title = StringUtil.htmlFilter( value,true );
129                return "<img class=\"ICON\" src=\"" + icon + "\" alt=\"" + title + "\" title=\"" + title + "\" />" ;
130        }
131
132        /**
133         * データ出力用の文字列を作成します。
134         * ファイル等に出力する形式を想定しますので、HTMLタグを含まない
135         * データを返します。
136         * 基本は、#getValue( String ) をそのまま返します。
137         *
138         * @og.rev 6.0.4.0 (2014/11/28) データ出力用のレンデラー
139         *
140         * @param   value 入力値
141         *
142         * @return  データ出力用の文字列
143         * @og.rtnNotNull
144         * @see         #getValue( String )
145         */
146        @Override
147        public String getWriteValue( final String value ) {
148                return ( value == null ) ? "" : value;
149        }
150}