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.fukurou.util.Attributes;
019import org.opengion.fukurou.util.StringUtil;
020import org.opengion.fukurou.util.TagBuffer;
021import org.opengion.fukurou.util.XHTMLTag;
022import org.opengion.hayabusa.common.HybsSystem;
023import org.opengion.hayabusa.db.AbstractEditor;
024import org.opengion.hayabusa.db.CellEditor;
025import org.opengion.hayabusa.db.DBColumn;
026
027/**
028 * TEXTRICH エディターは、カラムのデータをリッチテキストで編集する場合に
029 * 使用するクラスです。
030 * サイズ指定はsize1,size2で高さ,幅がpxで設定されます。
031 * 初期値は250,600です。
032 *
033 * optionAttibutes属性にcleditorの設定が可能です。
034 * 詳細は下記ページを参照してください。
035 * http://www.premiumsoftware.net/cleditor/gettingstarted
036 *
037 * @og.rev 5.9.32.0 (2018/05/02) 新規作成
038 * @og.rev      5.10.1.0 (2018/06/29) クリアボタンとエラー画面からの戻る場合の対応
039 * @og.group データ編集
040 *
041 * @version  5
042 * @author   T.OTA
043 * @since    JDK5.0,
044 */
045public class Editor_RICHTEXT extends AbstractEditor {
046        //* このプログラムのVERSION文字列を設定します。   {@value} */
047        private static final String VERSION = "" ;
048
049        /**
050         * デフォルトコンストラクター。
051         * このコンストラクターで、基本オブジェクトを作成します。
052         *
053         *
054         */
055        public Editor_RICHTEXT() {
056        }
057
058        // デフォルトの値設定
059        private void defaultSet() {
060                size1 = "250";
061                size2 = "600";
062        }
063
064        /**
065         * コンストラクター。
066         *
067         * @param       clm     DBColumnオブジェクト
068         */
069        private Editor_RICHTEXT( final DBColumn clm ) {
070                super( clm );
071                String  disabled = clm.isWritable() ? null : "disabled" ;
072
073                // size に、"height,width" を指定できるように変更
074                String param = StringUtil.nval( clm.getEditorParam(),clm.getViewLength() );
075                if( param != null && param.length() != 0 ) {
076                        int st = param.indexOf( ',' );
077                        if( st > 0 ) {
078                                size1 = param.substring( 0, st );
079                                size2 = param.substring( st + 1);
080                        }else {
081                                defaultSet();
082                        }
083                }else {
084                        defaultSet();
085                }
086
087                attributes = new Attributes();
088                attributes.addAttributes( clm.getEditorAttributes() );
089                tagBuffer.add( XHTMLTag.textareaAttri( attributes ) );
090        }
091
092        /**
093         * 各オブジェクトから自分のインスタンスを返します。
094         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
095         * まかされます。
096         *
097         * @param       clm     DBColumnオブジェクト
098         *
099         * @return      CellEditorオブジェクト
100         */
101        public CellEditor newInstance( final DBColumn clm ) {
102                return new Editor_RICHTEXT( clm );
103        }
104
105        /**
106         * データの編集用文字列を返します。
107         *
108         * @param   value 入力値
109         *
110         * @return  データの編集用文字列
111         */
112        @Override
113        public String getValue( final String value ) {
114                String id = "";
115
116                TagBuffer tag = new TagBuffer( "textarea" );
117                tag.add( "name"    , name );
118
119                id = attributes.get( "id" );
120                optAttr = attributes.get( "optionAttributes" );
121                if( id == null || id.length() == 0 ) {
122                        tag.add( "id"      , name );
123                        id = name;
124                }
125                tag.add( tagBuffer.makeTag() );
126                tag.add( optAttr );
127                tag.setBody( value );
128
129                return tag.makeTag() + createCLEditorSc(id);
130        }
131
132        /**
133         * name属性を変えた、データ表示/編集用のHTML文字列を作成します。
134         * テーブル上の name に 行番号を付加して、名前_行番号 で登録するキーを作成し,
135         * リクエスト情報を1つ毎のフィールドで処理できます。
136         *
137         * @param   row   行番号
138         * @param   value 入力値
139         *
140         * @return  データ表示/編集用の文字列
141         */
142        @Override
143        public String getValue( final int row,final String value ) {
144                String id = "";
145
146                TagBuffer tag = new TagBuffer( "textarea" );
147                String newName = name + HybsSystem.JOINT_STRING + row;
148                tag.add( "name"    , newName );
149                id = attributes.get( "id" );
150                if( id == null || id.length() == 0 ) {
151                        tag.add( "id"      , newName );
152                        id = newName;
153                }
154
155                tag.add( tagBuffer.makeTag() );
156                tag.add( optAttr );
157                tag.setBody( value );
158
159                return tag.makeTag( row,value ) + createCLEditorSc(id);
160        }
161
162        // CLEditorスクリプトの生成
163        private String createCLEditorSc(String id) {
164
165                StringBuilder js = new StringBuilder();
166                js.append("<script type='text/javascript'>");
167                js.append("var trg = $('#").append(id).append("').cleditor({");
168                js.append("bodyStyle:''");
169                js.append(",height:").append(size1);
170                js.append(",width:").append(size2);
171                js.append(",controls: 'bold size | color highlight | removeformat | link unlink | undo redo'");
172                String attr = attributes.get( "optionAttributes" );
173                if(attr != null && attr.length() > 0) {
174                        js.append(",").append(attr);
175                }
176                js.append("})[0];");
177                // editorをtextareaに反映(この処理で更新なしの場合も、><の文字ががエンコードされる。)
178                js.append("trg.updateTextArea();");
179                // readonly属性が設定されている場合は、変更不可。
180                if("readonly".equals(attributes.get("readonly"))) {
181                        js.append("trg.disable('true');");
182                        // linkは新規ウィンドウに表示
183                        js.append("$('#").append(id).append("').next('iframe').contents().find('a').attr('target','_blank');");
184                }
185                js.append("</script>");
186
187                return js.toString();
188        }
189}