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.hayabusa.db.AbstractEditor;
019import org.opengion.hayabusa.db.CellEditor;
020import org.opengion.hayabusa.db.DBColumn;
021import org.opengion.fukurou.util.XHTMLTag;
022import org.opengion.hayabusa.common.HybsSystem;
023import org.opengion.fukurou.util.Attributes;
024import org.opengion.fukurou.util.StringUtil;
025import org.opengion.fukurou.util.TagBuffer;
026
027/**
028 * OCR1 エディターは、tesseract.js を利用した、File APIで取り込んだイメージを
029 * 文字に変換して テキストエリアに書き出すクラスです。
030 *
031 * 基本的な構造は、FilaAPI、画像表示用 img、進捗(progressbar)、textarea で構成されます。
032 * textarea の name 以外は、固定です。よって、各ページに、1つしか設定できません。
033 *
034 *    <input type='file' name='imgfile' id='imgfile' accept='image/*'
035 *        onChange='readOCR(this)' ><br />
036 *    <img id='preview'><br />
037 *    <progress id='progressbar' min='0' max='1' value='0' > </progress><br />
038 *    <textarea name='≪カラム名≫' id='outdata' rows='10'cols='80'> </textarea>
039 *
040 * script に CDNサービス を使うと、無線環境(iPad等)ではものすごく遅くなったため、ローカルに配置することにします。
041 * <script src="https://unpkg.com/tesseract.js"><!-- --></script>
042 *
043 * script は、tesseract.min.js を使います。現在、1画面1つしかカメラは使えません。
044 * これらは、使用する画面に、組み込んでください。
045 * <script src="{@SYS.JSP}/option/tesseract.min.js"><!-- --></script>
046 * <script src="{@SYS.JSP}/option/videocamera.js"><!-- --></script>
047 *
048 * を使用するページに設定します。
049 *
050 * @og.rev 7.4.2.1 (2021/05/21) 新規作成
051 * @og.group データ編集
052 *
053 * @version  7.4
054 * @author   Kazuhiko Hasegawa
055 * @since    JDK11.0,
056 */
057public class Editor_OCR1 extends AbstractEditor {
058        /** このプログラムのVERSION文字列を設定します。   {@value} */
059        private static final String VERSION = "7.4.2.1 (2021/05/21)" ;
060
061//      // 7.4.2.2 (2021/05/28) システム定数のJSPを使用します。(※ SYS.JSP + SYS.IMAGE_DIR)
062//      private static final String JSP_OPT =  HybsSystem.sys( "JSP" ) + "/option/" ;
063
064//      private static final String JS_SRC = "<script src='" + JSP_OPT + "tesseract.min.js' ><!-- --></script>"
065//                                                              + CR +   "<script src='" + JSP_OPT + "videocamera.js' ><!-- --></script>" ;
066
067        private static final String BASE_HTML =
068                                                                        "<input type='file' name='imgfile' id='imgfile' accept='image/*' onChange='readOCR(this)' ><br />"
069                                                        + CR +  "<img id='preview'><br />"
070                                                        + CR +  "<progress id='progressbar' min='0' max='1' value='0' > </progress><br />" ;
071
072        /** 列1 */ protected  String     cols1   ;
073        /** 列2 */ protected  String     cols2   ;
074        /** 行1 */ protected  String     rows1   ;
075        /** 行2 */ protected  String     rows2   ;
076
077        /**
078         * デフォルトコンストラクター。
079         * このコンストラクターで、基本オブジェクトを作成します。
080         *
081         * @og.rev 7.4.2.1 (2021/05/21) 新規作成
082         *
083         */
084        public Editor_OCR1() { super(); }               // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
085
086        /**
087         * コンストラクター。
088         *
089         * @og.rev 7.4.2.1 (2021/05/21) 新規作成
090         *
091         * @param       clm     DBColumnオブジェクト
092         */
093        protected Editor_OCR1( final DBColumn clm ) {
094                super( clm );
095                final String  disabled = clm.isWritable() ? null : "disabled" ;
096
097                final int r1 = clm.getTotalSize()/Integer.parseInt(size1) + 1;
098                rows1 = String.valueOf( r1 );
099
100                final int r2 = clm.getTotalSize()/Integer.parseInt(size2) + 1;
101                rows2 = String.valueOf( r2 );
102
103                // size に、"rows,cols" を指定できるように変更
104                final String param = StringUtil.nval( clm.getEditorParam(),clm.getViewLength() );
105                if( param != null && param.length() != 0 ) {
106                        final int st = param.indexOf( ',' );
107                        if( st > 0 ) {
108                                rows1 = param.substring( 0,st );
109                                rows2 = rows1 ;
110                                cols1 = param.substring( st+1 );
111                                cols2 = cols1;
112                        }
113                }
114
115                // size1,size2 を使わずに、cols1,cols2 を使用。
116                if( cols1 == null || cols2 == null ) {
117                        cols1   = size1  ;
118                        cols2   = size2  ;
119                }
120
121                // Attributesの連結記述
122                attributes = new Attributes()
123                                        .set( "disabled"        , disabled )
124                                        .set( clm.getEditorAttributes() )                               // #addAttributes( Attributes ) の代替え
125                                        .add( "class"           , clm.getDbType() );
126
127                tagBuffer.add( XHTMLTag.textareaAttri( attributes ) );
128        }
129
130        /**
131         * 各オブジェクトから自分のインスタンスを返します。
132         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
133         * まかされます。
134         *
135         * @og.rev 7.4.2.1 (2021/05/21) 新規作成
136         *
137         * @param       clm     DBColumnオブジェクト
138         *
139         * @return      CellEditorオブジェクト
140         * @og.rtnNotNull
141         */
142        public CellEditor newInstance( final DBColumn clm ) {
143                return new Editor_OCR1( clm );
144        }
145
146        /**
147         * データの編集用文字列を返します。
148         *
149         * @og.rev 7.4.2.1 (2021/05/21) 新規作成
150         *
151         * @param   value 入力値
152         *
153         * @return  データの編集用文字列
154         * @og.rtnNotNull
155         */
156        @Override
157        public String getValue( final String value ) {
158                // TagBufferの連結記述
159                return  BASE_HTML
160                        +       new TagBuffer( "textarea" )
161                                        .add( "name"    , name )
162                                        .add( "id"              , "outdata" )                           // ID 固定です。
163                                        .add( "cols"    , cols2 )
164                                        .add( "rows"    , rows2 )
165                                        .add( tagBuffer.makeTag() )
166                                        .addBody( value )
167                                        .makeTag();
168        }
169
170        /**
171         * name属性を変えた、データ表示/編集用のHTML文字列を作成します。
172         * テーブル上の name に 行番号を付加して、名前_行番号 で登録するキーを作成し、
173         * リクエスト情報を1つ毎のフィールドで処理できます。
174         *
175         * @og.rev 7.4.2.1 (2021/05/21) 新規作成
176         *
177         * @param   row   行番号
178         * @param   value 入力値
179         *
180         * @return  データ表示/編集用の文字列
181         * @og.rtnNotNull
182         */
183        @Override
184        public String getValue( final int row,final String value ) {
185                final String newName = name + HybsSystem.JOINT_STRING + row;
186
187                // TagBufferの連結記述
188                return  BASE_HTML
189                        +       new TagBuffer( "textarea" )
190                                        .add( "name"    , newName )
191                                        .add( "id"              , "outdata" )                           // ID 固定です。
192                                        .add( "cols"    , cols2 )
193                                        .add( "rows"    , rows2 )
194                                        .add( tagBuffer.makeTag() )
195                                        .addBody( value )
196                                        .makeTag( row,value );
197        }
198}