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         * コンストラクター
067         * textareaのサイズを決めるため、sizeとrowを決定する
068         * editorの計算を移植。
069         *
070         * @param       clm     DBColumnオブジェクト
071         */
072        private Renderer_RICHTEXT( final DBColumn clm ) {
073                super();
074
075                // size に、"height,width" を指定できるように変更
076                // 2018/06/21 MODIFY size(ViewLength)のみ取得するように変更
077//              final String param = StringUtil.nval( clm.getEditorParam(),clm.getViewLength() );
078                final String param = clm.getViewLength();
079                if( param != null && param.length() != 0 ) {
080                        final int st = param.indexOf( ',' );
081                        if( st > 0 ) {
082                                size1 = param.substring( 0, st );
083                                size2 = param.substring( st + 1);
084//                      }else {
085//                              defaultSet();
086                        }
087//              }else {
088//                      defaultSet();
089                }
090        }
091
092        /**
093         * 各オブジェクトから自分のインスタンスを返します。
094         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
095         * まかされます。
096         *
097         * @param       clm     DBColumnオブジェクト
098         *
099         * @return      CellEditorオブジェクト
100         */
101        public CellRenderer newInstance( final DBColumn clm ) {
102                return new Renderer_RICHTEXT( clm );
103        }
104
105        /**
106         * データの表示用文字列を返します。
107         *
108         * @param   value 入力値
109         *
110         * @return  データの表示用文字列
111         */
112        @Override
113        public String getValue( final String value ) {
114                // uuidをidとして使用する
115                final String uuid = UUID.randomUUID().toString();
116
117                return new TagBuffer( "textarea" )
118                                        .add( "id" , uuid )
119        //                              .add( tagBuffer.makeTag() )
120                                        .addBody( value )
121                                        .makeTag()
122                                + createCLEditorSc( uuid );
123
124//              // uuidをidとして使用する
125//              String uuid = UUID.randomUUID().toString();
126//
127//              TagBuffer tag = new TagBuffer( "textarea" );
128//              tag.add( "id" , uuid);
129//              tag.add( tagBuffer.makeTag() );
130//              tag.addBody( value );
131//
132//              return tag.makeTag() + createCLEditorSc(uuid);
133        }
134
135        /**
136         * データの表示用文字列を返します。
137         *
138         * @param   row   行番号
139         * @param   value 入力値
140         *
141         * @return  データ表示用の文字列
142         */
143        @Override
144        public String getValue( final int row,final String value ) {
145                // uuidをidとして使用する
146                final String uuid = UUID.randomUUID().toString();
147
148                return new TagBuffer( "textarea" )
149                                        .add( "id" , uuid )
150        //                              .add( tagBuffer.makeTag() )
151                                        .addBody( value )
152                                        .makeTag( row,value )
153                                + createCLEditorSc( uuid );
154
155//              // uuidをidとして使用する
156//              String uuid = UUID.randomUUID().toString();
157//              TagBuffer tag = new TagBuffer( "textarea" );
158//              tag.add( "id" , uuid);
159//              tag.add( tagBuffer.makeTag() );
160//              tag.addBody( value );
161//
162//              return tag.makeTag( row,value ) + createCLEditorSc(uuid);
163        }
164
165        /**
166         * CLEditorスクリプトを生成します。
167         *
168         * @param   id ID値
169         *
170         * @return  CLEditorスクリプト
171         */
172        private String createCLEditorSc( final String id ) {
173                return new StringBuilder( BUFFER_MIDDLE )
174                        .append( "<script type='text/javascript'>" )
175                        .append( "var trg = $('#").append( id ).append( "').cleditor({" )
176                        .append( "height:" ).append( size1 )
177                        .append( ",width:" ).append( size2 )
178                        .append( ",controls:''" )
179                        .append( ",bodyStyle:'background-color: transparent;'})[0];" )
180                        .append( "trg.disable('true');" )               // 操作の無効化
181                        .append( "trg.$toolbar.remove();" )     // メニューバーの削除
182                        .append( "trg.$main.css('background-color','transparent');" )           // bodyのstyle設定
183                        // linkは新規ウィンドウに表示
184                        .append( "$('#").append(id).append( "').next('iframe').contents().find('a').attr('target','_blank');" )
185                        .append( "</script>" )
186                        .toString();
187
188//              StringBuilder js = new StringBuilder();
189//              js.append("<script type='text/javascript'>");
190//              js.append("var trg = $('#").append(id).append("').cleditor({");
191//              js.append("height:").append(size1);
192//              js.append(",width:").append(size2);
193//              js.append(",controls:''");
194//              js.append(",bodyStyle:'background-color: transparent;'})[0];");
195//              js.append("trg.disable('true');");      // 操作の無効化
196//              js.append("trg.$toolbar.remove();");    // メニューバーの削除
197//              js.append("trg.$main.css('background-color','transparent');");  // bodyのstyle設定
198//              // linkは新規ウィンドウに表示
199//              js.append("$('#").append(id).append("').next('iframe').contents().find('a').attr('target','_blank');");
200//              js.append("</script>");
201//              return js.toString();
202        }
203}