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
018// import org.opengion.fukurou.util.Attributes;
019// import org.opengion.fukurou.util.StringUtil;
020// import org.opengion.fukurou.util.TagBuffer;
021// import org.opengion.fukurou.util.XHTMLTag;
022import org.opengion.hayabusa.common.HybsSystem;
023// import org.opengion.hayabusa.db.AbstractEditor;
024import org.opengion.hayabusa.db.CellEditor;
025import org.opengion.hayabusa.db.DBColumn;
026
027import static org.opengion.fukurou.util.StringUtil.nval;
028
029/**
030 * TEXTRICH エディターは、カラムのデータをリッチテキストで編集する場合に
031 * 使用するクラスです。
032 * サイズ指定はsize1,size2で高さ,幅がpxで設定されます。
033 * 初期値は250,600です。
034 *
035 * optionAttibutes属性にcleditorの設定が可能です。
036 * 詳細は下記ページを参照してください。
037 * http://www.premiumsoftware.net/cleditor/gettingstarted
038 *
039 * @og.rev      5.9.32.0 (2018/05/02) 新規作成
040 * @og.rev      5.10.1.0 (2018/06/29) クリアボタンとエラー画面からの戻る場合の対応
041 * @og.group データ編集
042 *
043 * @version  5
044 * @author   T.OTA
045 * @since    JDK5.0,
046 */
047public class Editor_RICHTEXT extends Editor_TEXTAREA {
048        //* このプログラムのVERSION文字列を設定します。   {@value} */
049        private static final String VERSION = "6.9.6.0 (2018/05/07)" ;
050
051        /**
052         * デフォルトコンストラクター。
053         * このコンストラクターで、基本オブジェクトを作成します。
054         */
055        public Editor_RICHTEXT() { super(); }           // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
056
057//      // デフォルトの値設定
058//      private void defaultSet() {
059//              size1 = "250";
060//              size2 = "600";
061//      }
062
063        /**
064         * コンストラクター。
065         *
066         * @og.rev      5.10.1.0 (2018/06/29) クリアボタンとエラー画面からの戻る場合の対応
067         *
068         * @param       clm     DBColumnオブジェクト
069         */
070        private Editor_RICHTEXT( final DBColumn clm ) {
071                super( clm );
072
073                // size に、"height,width" を指定できるように変更
074                // 2018/06/29 MODIFY size(ViewLength)のみ取得するように変更
075                // String param = StringUtil.nval( clm.getEditorParam(),clm.getViewLength() );
076                final String param = clm.getViewLength();
077                if( param != null && param.length() != 0 ) {
078                        final int st = param.indexOf( ',' );
079                        if( st > 0 ) {
080                                cols1 = param.substring( 0, st );
081                                cols2 = param.substring( st + 1);
082                        }
083                }
084
085                // ※ 超絶特殊。未設定時は、super で、size1,2 に設定されているので、その値で判定します。
086//              if( cols1 == size1 ) { cols1 = "250"; }
087//              if( cols2 == size2 ) { cols2 = "600"; }
088                if( cols1 == null || cols1.equals( size1 ) ) { cols1 = "250"; }         // 6.9.8.0 (2018/05/28) FindBugs: String オブジェクトを == や != を使用して比較している
089                if( cols2 == null || cols2.equals( size2 ) ) { cols2 = "600"; }         // 6.9.8.0 (2018/05/28) FindBugs: String オブジェクトを == や != を使用して比較している
090
091//              String  disabled = clm.isWritable() ? null : "disabled" ;
092//
093//              // size に、"height,width" を指定できるように変更
094//              String param = StringUtil.nval( clm.getEditorParam(),clm.getViewLength() );
095//              if( param != null && param.length() != 0 ) {
096//                      int st = param.indexOf( ',' );
097//                      if( st > 0 ) {
098//                              size1 = param.substring( 0, st );
099//                              size2 = param.substring( st + 1);
100//                      }else {
101//                              defaultSet();
102//                      }
103//              }else {
104//                      defaultSet();
105//              }
106//
107//              attributes = new Attributes();
108//              attributes.addAttributes( clm.getEditorAttributes() );
109//              tagBuffer.add( XHTMLTag.textareaAttri( attributes ) );
110        }
111
112        /**
113         * 各オブジェクトから自分のインスタンスを返します。
114         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
115         * まかされます。
116         *
117         * @param       clm     DBColumnオブジェクト
118         *
119         * @return      CellEditorオブジェクト
120         */
121        @Override
122        public CellEditor newInstance( final DBColumn clm ) {
123                return new Editor_RICHTEXT( clm );
124        }
125
126        /**
127         * データの編集用文字列を返します。
128         *
129         * @param   value 入力値
130         *
131         * @return  データの編集用文字列
132         */
133        @Override
134        public String getValue( final String value ) {
135                final String id = nval( attributes.get( "id" ) , name );
136
137                return super.getValue( value ) + createCLEditorSc( id );
138
139//              String id = "";
140//
141//              TagBuffer tag = new TagBuffer( "textarea" );
142//              tag.add( "name"    , name );
143//
144//              id = attributes.get( "id" );
145//              optAttr = attributes.get( "optionAttributes" );
146//              if( id == null || id.length() == 0 ) {
147//                      tag.add( "id"      , name );
148//                      id = name;
149//              }
150//              tag.add( tagBuffer.makeTag() );
151//              tag.add( optAttr );
152//              tag.addBody( value );
153//
154//              return tag.makeTag() + createCLEditorSc(id);
155        }
156
157        /**
158         * name属性を変えた、データ表示/編集用のHTML文字列を作成します。
159         * テーブル上の name に 行番号を付加して、名前_行番号 で登録するキーを作成し,
160         * リクエスト情報を1つ毎のフィールドで処理できます。
161         *
162         * @param   row   行番号
163         * @param   value 入力値
164         *
165         * @return  データ表示/編集用の文字列
166         */
167        @Override
168        public String getValue( final int row,final String value ) {
169                final String newName = name + HybsSystem.JOINT_STRING + row;
170                final String id = nval( attributes.get( "id" ) , newName );
171
172                return super.getValue( row,value ) + createCLEditorSc( id );
173
174//              String id = "";
175//
176//              TagBuffer tag = new TagBuffer( "textarea" );
177//              String newName = name + HybsSystem.JOINT_STRING + row;
178//              tag.add( "name"    , newName );
179//              id = attributes.get( "id" );
180//              if( id == null || id.length() == 0 ) {
181//                      tag.add( "id"      , newName );
182//                      id = newName;
183//              }
184//
185//              tag.add( tagBuffer.makeTag() );
186//              tag.add( optAttr );
187//              tag.addBody( value );
188//
189//              return tag.makeTag( row,value ) + createCLEditorSc(id);
190        }
191
192        /**
193         * CLEditorスクリプトを生成します。
194         *
195         * @og.rev      5.10.1.0 (2018/06/29) クリアボタンとエラー画面からの戻る場合の対応
196         *
197         * @param   id ID値
198         *
199         * @return  CLEditorスクリプト
200         */
201        private String createCLEditorSc( final String id ) {
202
203                final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE )
204                                                .append( "<script type='text/javascript'>" )
205                                                .append( "var trg = $('#" ).append( id ).append( "').cleditor({" )
206                                                .append( "bodyStyle:''" )
207                                                .append( ",height:"             ).append( cols1 )
208                                                .append( ",width:"              ).append( cols2 )
209                                                .append( ",controls: 'bold size | color highlight | removeformat | link unlink | undo redo'" );
210
211                final String attr = attributes.get( "optionAttributes" );
212                if( attr != null && attr.length() > 0 ) {
213                        buf.append( ',' ).append( attr );
214                }
215                buf.append( "})[0];" )
216                        // editorをtextareaに反映(この処理で更新なしの場合も、><の文字ががエンコードされる。)
217                        .append( "trg.updateTextArea();" );
218                // readonly属性が設定されている場合は、変更不可。
219                if( "readonly".equals( attributes.get( "readonly" ) ) ) {
220                        buf.append( "trg.disable('true');" )
221                                // linkは新規ウィンドウに表示
222                                .append( "$('#" ).append( id ).append( "').next('iframe').contents().find('a').attr('target','_blank');" );
223                }
224                // 2018/06/29 ADD START
225                // クリアボタン押下時の動作(textareaの値をiframeに反映して、初期状態に戻す)
226                // reset後に反映するために、setTimeoutで遅延
227                buf.append("$('input[type=reset]').click(function(){setTimeout(function(){trg.updateFrame()},10);});")
228                        // エラー画面からの戻る対応。textareaの値をiframeに反映する。
229                        .append("$(function(){trg.updateFrame();});")
230                        // 2018/06/29 ADD END
231                        .append( "</script>" );
232
233                return buf.toString();
234
235//              StringBuilder js = new StringBuilder();
236//              js.append("<script type='text/javascript'>");
237//              js.append("var trg = $('#").append(id).append("').cleditor({");
238//              js.append("bodyStyle:''");
239//              js.append(",height:").append(size1);
240//              js.append(",width:").append(size2);
241//              js.append(",controls: 'bold size | color highlight | removeformat | link unlink | undo redo'");
242//              String attr = attributes.get( "optionAttributes" );
243//              if(attr != null && attr.length() > 0) {
244//                      js.append(",").append(attr);
245//              }
246//              js.append("})[0];");
247//              // editorをtextareaに反映(この処理で更新なしの場合も、><の文字ががエンコードされる。)
248//              js.append("trg.updateTextArea();");
249//              // readonly属性が設定されている場合は、変更不可。
250//              if("readonly".equals(attributes.get("readonly"))) {
251//                      js.append("trg.disable('true');");
252//                      // linkは新規ウィンドウに表示
253//                      js.append("$('#").append(id).append("').next('iframe').contents().find('a').attr('target','_blank');");
254//              }
255//              js.append("</script>");
256//
257//              return js.toString();
258        }
259}