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.UUID;
019
020// import org.opengion.fukurou.util.StringUtil;
021import org.opengion.fukurou.util.TagBuffer;
022import org.opengion.hayabusa.db.AbstractRenderer;
023import org.opengion.hayabusa.db.CellRenderer;
024import org.opengion.hayabusa.db.DBColumn;
025
026/**
027 * TEXTAREA レンデラは、カラムのデータをリッチテキストで表示する場合に
028 * 使用するクラスです。
029 * readonlyのテキストエリアでname属性は付けません。(データは送信されません)
030 * エリアの縦、横サイズはエディタのリッチテキストと同様にして算出されます。
031 *
032 * カラムの表示に必要な属性は, DBColumn オブジェクト より取り出します。
033 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。
034 *
035 * @og.rev 5.9.32.0 (2018/05/02) 新規作成
036 * @og.rev 5.10.1.0 (2018/06/29) size指定の処理を変更。
037 * @og.group データ編集
038 *
039 * @version  4.0
040 * @author   Takahashi Masakazu
041 * @since    JDK5.0,
042 */
043public class Renderer_RICHTEXT extends AbstractRenderer {
044        //* このプログラムのVERSION文字列を設定します。   {@value} */
045        private static final String VERSION = "6.9.6.0 (2018/05/07)" ;
046
047//      private final TagBuffer tagBuffer = new TagBuffer();
048
049        private  String         size1 = "150";
050        private  String         size2 = "300";
051
052        /**
053         * デフォルトコンストラクター。
054         * このコンストラクターで、基本オブジェクトを作成します。
055         *
056         */
057        public Renderer_RICHTEXT() { super(); }         // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
058
059//      // デフォルトの値設定
060//      private void defaultSet() {
061//              size1 = "150";
062//              size2 = "300";
063//      }
064
065        /**
066         * DBColumnオブジェクトを指定したprivateコンストラクター。
067         *
068         * textareaのサイズを決めるため、sizeとrowを決定する
069         * editorの計算を移植。
070         *
071         * @param       clm     DBColumnオブジェクト
072         */
073        private Renderer_RICHTEXT( final DBColumn clm ) {
074                super();
075
076                // size に、"height,width" を指定できるように変更
077                // 2018/06/21 MODIFY size(ViewLength)のみ取得するように変更
078//              final String param = StringUtil.nval( clm.getEditorParam(),clm.getViewLength() );
079                final String param = clm.getViewLength();
080                if( param != null && param.length() != 0 ) {
081                        final int st = param.indexOf( ',' );
082                        if( st > 0 ) {
083                                size1 = param.substring( 0, st );
084                                size2 = param.substring( st + 1);
085//                      }else {
086//                              defaultSet();
087                        }
088//              }else {
089//                      defaultSet();
090                }
091        }
092
093        /**
094         * 各オブジェクトから自分のインスタンスを返します。
095         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
096         * まかされます。
097         *
098         * @param       clm     DBColumnオブジェクト
099         *
100         * @return      CellEditorオブジェクト
101         */
102        public CellRenderer newInstance( final DBColumn clm ) {
103                return new Renderer_RICHTEXT( clm );
104        }
105
106        /**
107         * データの表示用文字列を返します。
108         *
109         * @param   value 入力値
110         *
111         * @return  データの表示用文字列
112         */
113        @Override
114        public String getValue( final String value ) {
115                // uuidをidとして使用する
116                final String uuid = UUID.randomUUID().toString();
117
118                return new TagBuffer( "textarea" )
119                                        .add( "id" , uuid )
120        //                              .add( tagBuffer.makeTag() )
121                                        .addBody( value )
122                                        .makeTag()
123                                + createCLEditorSc( uuid );
124
125//              // uuidをidとして使用する
126//              String uuid = UUID.randomUUID().toString();
127//
128//              TagBuffer tag = new TagBuffer( "textarea" );
129//              tag.add( "id" , uuid);
130//              tag.add( tagBuffer.makeTag() );
131//              tag.addBody( value );
132//
133//              return tag.makeTag() + createCLEditorSc(uuid);
134        }
135
136        /**
137         * データの表示用文字列を返します。
138         *
139         * @param   row   行番号
140         * @param   value 入力値
141         *
142         * @return  データ表示用の文字列
143         */
144        @Override
145        public String getValue( final int row,final String value ) {
146                // uuidをidとして使用する
147                final String uuid = UUID.randomUUID().toString();
148
149                return new TagBuffer( "textarea" )
150                                        .add( "id" , uuid )
151        //                              .add( tagBuffer.makeTag() )
152                                        .addBody( value )
153                                        .makeTag( row,value )
154                                + createCLEditorSc( uuid );
155
156//              // uuidをidとして使用する
157//              String uuid = UUID.randomUUID().toString();
158//              TagBuffer tag = new TagBuffer( "textarea" );
159//              tag.add( "id" , uuid);
160//              tag.add( tagBuffer.makeTag() );
161//              tag.addBody( value );
162//
163//              return tag.makeTag( row,value ) + createCLEditorSc(uuid);
164        }
165
166        /**
167         * CLEditorスクリプトを生成します。
168         *
169         * @param   id ID値
170         *
171         * @return  CLEditorスクリプト
172         */
173        private String createCLEditorSc( final String id ) {
174                return new StringBuilder( BUFFER_MIDDLE )
175                        .append( "<script type='text/javascript'>" )
176                        .append( "var trg = $('#").append( id ).append( "').cleditor({" )
177                        .append( "height:" ).append( size1 )
178                        .append( ",width:" ).append( size2 )
179                        .append( ",controls:''" )
180                        .append( ",bodyStyle:'background-color: transparent;'})[0];" )
181                        .append( "trg.disable('true');" )               // 操作の無効化
182                        .append( "trg.$toolbar.remove();" )     // メニューバーの削除
183                        .append( "trg.$main.css('background-color','transparent');" )           // bodyのstyle設定
184                        // linkは新規ウィンドウに表示
185                        .append( "$('#").append(id).append( "').next('iframe').contents().find('a').attr('target','_blank');" )
186                        .append( "</script>" )
187                        .toString();
188
189//              StringBuilder js = new StringBuilder();
190//              js.append("<script type='text/javascript'>");
191//              js.append("var trg = $('#").append(id).append("').cleditor({");
192//              js.append("height:").append(size1);
193//              js.append(",width:").append(size2);
194//              js.append(",controls:''");
195//              js.append(",bodyStyle:'background-color: transparent;'})[0];");
196//              js.append("trg.disable('true');");      // 操作の無効化
197//              js.append("trg.$toolbar.remove();");    // メニューバーの削除
198//              js.append("trg.$main.css('background-color','transparent');");  // bodyのstyle設定
199//              // linkは新規ウィンドウに表示
200//              js.append("$('#").append(id).append("').next('iframe').contents().find('a').attr('target','_blank');");
201//              js.append("</script>");
202//              return js.toString();
203        }
204}