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;
025
026/**
027 * ICON レンデラーは、カラムのファイル名の拡張子からアイコンファイルのイメージタグを作成します。
028 * イメージデータは、jsp/image/thumb を使用します。
029 *
030 * 実質的には、アイコンではなく、サムネイルとして利用します。
031 * 
032 * 縦横比をそのままに、縦か横の最大値に画像サイズを合わせるには、
033 * style="max-width:100; max-height:100;" をセットすることで対応できます。
034 * class="ICON" 属性を出力しておきますので、CSSファイルで記述してください。
035 *
036 * (例:)
037 *<pre>
038 *  &lt;style type="text/css"&gt;
039 *      img.ICON { max-width:100px; max-height:100px; }
040 *  &lt;/style&gt;
041 *</pre>
042 *
043 * このクラスは、不変オブジェクトとして、共有されます。
044 *
045 * @og.rev 5.6.5.1 (2013/06/14) 新規作成
046 *
047 * @og.group データ表示
048 *
049 * @version  4.0
050 * @author       Kazuhiko Hasegawa
051 * @since    JDK5.0,
052 */
053public class Renderer_ICON extends AbstractRenderer {
054        //* このプログラムのVERSION文字列を設定します。   {@value} */
055        private static final String VERSION = "5.6.5.1 (2013/06/14)" ;
056
057        private static final CellRenderer dbCell = new Renderer_ICON() ;
058
059        // アイコンファイルに割り当てられる拡張子とファイルの関連(MAP)情報
060        private static final Map<String,String> ICON_MAP ;
061        static {
062                ICON_MAP = new HashMap<String,String>();
063
064                ICON_MAP.put( "doc"             ,       "../image/thumb/doc.png" );
065                ICON_MAP.put( "docx"    ,       "../image/thumb/doc.png" );
066                ICON_MAP.put( "xls"             ,       "../image/thumb/xls.png" );
067                ICON_MAP.put( "xlsx"    ,       "../image/thumb/xls.png" );
068                ICON_MAP.put( "ppt"             ,       "../image/thumb/ppt.png" );
069                ICON_MAP.put( "pptx"    ,       "../image/thumb/ppt.png" );
070                ICON_MAP.put( "pdf"             ,       "../image/thumb/pdf.png" );
071                ICON_MAP.put( "txt"             ,       "../image/thumb/text.png" );
072                ICON_MAP.put( "zip"             ,       "../image/thumb/zip.png" );
073        }
074        private static final String DOC_VIEW = "../image/thumb/docview.png" ;           // その他のアイコン
075
076        /**
077         * 各オブジェクトから自分のインスタンスを返します。
078         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
079         * まかされます。
080         *
081         * @param       clm     DBColumnオブジェクト
082         *
083         * @return      CellRendererオブジェクト
084         */
085        public CellRenderer newInstance( final DBColumn clm ) {
086                return dbCell;
087        }
088
089        /**
090         * データの表示用文字列を返します。
091         *
092         * @param       value 入力値
093         *
094         * @return      データの表示用文字列
095         */
096        @Override
097        public String getValue( final String value ) {
098                String icon = null;
099
100                if( value != null ) {
101                        String sufix = null;
102                        int idx = value.lastIndexOf(".");
103                        if( idx >= 0 ) {
104                                sufix = value.substring( idx+1 ).toLowerCase( Locale.JAPAN );
105                                icon = ICON_MAP.get( sufix );
106                        }
107                }
108
109                if( icon == null ) { icon = DOC_VIEW; }
110
111                return "<img class=\"ICON\" src=\"" + icon + "\" alt=\"" + value + "\" />" ;
112        }
113}