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