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.hayabusa.db.Selection;
022import org.opengion.hayabusa.db.SelectionFactory;               // 5.7.3.0 (2014/02/07)
023
024/**
025 * BITBOX レンデラーは、カラムのデータをコードリソースに対応した
026 * チェックボックスで表示する場合に使用するクラスです。
027 *
028 * カラムの表示に必要な属性は、DBColumn オブジェクト より取り出します。
029 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。
030 *
031 * 一覧表示する場合は、通常は、□(0)か■(1)のみ表示されます。
032 * ラベル表示が必要な場合は、表示パラメータに、"useLabel"と
033 * 記述しておくことで、ラベルを出力することが可能です。
034 *
035 * @og.rev 6.2.2.4 (2015/04/24) 新規作成
036 * @og.group データ表示
037 *
038 * @version  6.2
039 * @author       Kazuhiko Hasegawa
040 * @since    JDK8.0,
041 */
042public class Renderer_BITBOX extends AbstractRenderer {
043        /** このプログラムのVERSION文字列を設定します。   {@value} */
044        private static final String VERSION = "6.4.6.0 (2016/05/27)" ;
045
046        private final Selection selection       ;
047        private final boolean useLabel  ;               // 4.3.3.0 (2008/10/01)
048        private final boolean useKeyLabel ;             // 6.2.0.0 (2015/02/27) キー:ラベル形式
049
050        private String errMsg   ;                               // 6.0.4.0 (2014/11/28)
051
052        /**
053         * デフォルトコンストラクター。
054         * このコンストラクターで、基本オブジェクトを作成します。
055         *
056         */
057        public Renderer_BITBOX() {
058                super();                                        // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
059                selection       = null;
060                useLabel        = false;                // 4.3.3.0 (2008/10/01)
061                useKeyLabel = true;
062        }
063
064        /**
065         * DBColumnオブジェクトを指定したprivateコンストラクター。
066         *
067         * @og.rev 6.2.2.4 (2015/04/24) 新規追加
068         * @og.rev 6.4.6.0 (2016/05/27) getEditorParam → getRendererParam に修正
069         *
070         * @param       clm     DBColumnオブジェクト
071         */
072        private Renderer_BITBOX( final DBColumn clm ) {
073                super();                // 6.4.1.1 (2016/01/16) PMD refactoring. It is a good practice to call super() in a constructor
074                useKeyLabel = "true".equalsIgnoreCase( clm.getAddKeyLabel() ) ;         // 値:ラベル形式
075
076                // 5.7.3.0 (2014/02/07) SelectionFactory 対応
077                final String addKeyLabel = clm.getAddKeyLabel();                                        // 6.2.0.0 (2015/02/27) キー:ラベル形式
078                selection = SelectionFactory.newSelection( "BITBOX" , clm.getCodeData() , addKeyLabel );
079
080                // 6.0.4.0 (2014/11/28) selection が null の場合、Selection_NULL を作成します。
081                if( selection == null ) {
082                        errMsg = "codeData が未設定です。"
083                                                                + " name="  + clm.getName()
084                                                                + " label=" + clm.getLabel()
085                                                                + " rendType="  + clm.getRenderer() ;
086                        System.out.println( errMsg );
087                }
088
089                useLabel = "useLabel".equalsIgnoreCase( clm.getRendererParam() ) ;                      // 6.4.6.0 (2016/05/27)
090        }
091
092        /**
093         * 各オブジェクトから自分のインスタンスを返します。
094         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
095         * まかされます。
096         *
097         * @og.rev 6.2.2.4 (2015/04/24) 新規追加
098         *
099         * @param       clm     DBColumnオブジェクト
100         *
101         * @return      CellRendererオブジェクト
102         * @og.rtnNotNull
103         */
104        public CellRenderer newInstance( final DBColumn clm ) {
105                return new Renderer_BITBOX( clm );
106        }
107
108        /**
109         * データの表示用文字列を返します。
110         *
111         * @og.rev 6.2.2.4 (2015/04/24) 新規追加
112         *
113         * @param   value 入力値
114         *
115         * @return  データの表示用文字列
116         * @og.rtnNotNull
117         */
118        @Override
119        public String getValue( final String value ) {
120                // 6.0.4.0 (2014/11/28) selection が null の場合、警告表示します。
121                if( selection == null ) {
122                        return "<span class=\"error\">" + errMsg + " value=" + value + "</span>";
123                }
124
125                return "<pre class=\"BITBOX\">" + selection.getValueLabel( value,true ) + "</pre>" ;
126        }
127
128        /**
129         * データの一覧表示用文字列を返します。
130         * 一覧表示のため、useLabel が有効です。
131         *
132         * @og.rev 6.2.2.4 (2015/04/24) 新規追加
133         *
134         * @param       row   行番号
135         * @param       value 入力値
136         *
137         * @return      データ表示/編集用の文字列
138         * @og.rtnNotNull
139         */
140        @Override
141        public String getValue( final int row,final String value ) {
142                // 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
143                return selection == null
144                                        ? "<span class=\"error\">" + errMsg + " value=" + value + " row=" + row + "</span>"
145                                        : "<pre class=\"BITBOX\">" + selection.getValueLabel( value,useLabel ) + "</pre>" ;
146        }
147
148        /**
149         * データ出力用の文字列を作成します。
150         * ファイル等に出力する形式を想定しますので、HTMLタグを含まない
151         * データを返します。
152         * 基本は、#getValue( String ) をそのまま返します。
153         *
154         * @og.rev 6.2.2.4 (2015/04/24) 新規追加
155         *
156         * @param   value 入力値
157         *
158         * @return  データ出力用の文字列
159         * @see         #getValue( String )
160         */
161        @Override
162        public String getWriteValue( final String value ) {
163                // 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
164                return selection == null
165                                        ? value
166                                        : useKeyLabel
167                                                ? value + ':' + selection.getValueLabel( value,useLabel )
168                                                : selection.getValueLabel( value,useLabel );
169        }
170}