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.hayabusa.common.HybsSystem;                                                              // 8.0.0.0 (2021/07/31) Delete
019import org.opengion.hayabusa.db.AbstractRenderer;
020import org.opengion.hayabusa.db.CellRenderer;
021import org.opengion.hayabusa.db.DBColumn;
022import org.opengion.fukurou.util.StringFormat;
023import org.opengion.fukurou.util.StringUtil;
024import org.opengion.fukurou.util.TagBuffer;
025
026/**
027 * URLCALL レンデラーは、汎用ボタンにURLをクリックする機能を適用するクラスです。
028 *
029 * ボタンのラベルは、ラベルリソースから取得します。それ以外に値に設定された文字列から、
030 * 変数 $1,$2,$3,$4 を適用できます。
031 * AAA:BBB:CCC:DDD という値を、$1,$2,$3,$4 に割り当てます。
032 *
033 * <button name="CC" id="CC" type="button" onclick="window.open('URL','CC_FRM').close();" >CC</button>
034 * <iframe style="display:none;" name="CC_FRM"><!-- --></iframe>
035 *
036 * window.open で、すぐに閉じるのと、iframe を 見えなくすることで、ajax と同じような感じで実行できる。
037 *
038 * ※ 色々と試行錯誤した結果、window.open + 見えない iframe 方式で行います。
039 *
040 * 不採用案1:ajaxによる非同期通信
041 *  <button name="AA" id="AA" type="button" onclick="ajaxCall('URL');" >ラベルAA</button>
042 *  default.js に ajaxCall を用意して、非同期にURLを呼び出す。
043 *  IE11 では、localhost 等から呼び出せない(セキュリティ)を低にすれば動作する。Microsoft Edge では実行可能。
044 *  将来的には、こちらの方法になる可能性は大きい
045 *
046 * 不採用案2:location.href 遷移
047 *  <button name="BB" id="BB" type="button" onclick="location.href='URL'" >ラベルBB</button>
048 *  どうしても、URLに飛んで画面遷移してしまう。return false; を入れてもすでに遷移してしまう。
049 *
050 * @og.rev 7.4.2.0 (2021/05/14) 新規作成
051 * @og.group データ表示
052 *
053 * @version     7.4
054 * @author      Kazuhiko Hasegawa
055 * @since       JDK11,
056 */
057public class Renderer_URLCALL extends AbstractRenderer {
058        /** このプログラムのVERSION文字列を設定します。 {@value} */
059        private static final String VERSION = "8.5.3.0 (2023/09/08)" ;
060
061//      private static final CellRenderer DB_CELL = new Renderer_URLCALL() ;
062
063        private String  name;
064        private String  label;
065        private String  param;
066
067        /**
068         * デフォルトコンストラクター。
069         * このコンストラクターで、基本オブジェクトを作成します。
070         *
071         */
072        public Renderer_URLCALL() {
073                super();
074        }
075
076        /**
077         * DBColumnオブジェクトを指定したprivateコンストラクター。
078         *
079         * @og.rev 8.5.3.0 (2023/09/08) DynamicAttributes対応
080         *
081         * @param       clm     DBColumnオブジェクト
082         */
083        private Renderer_URLCALL( final DBColumn clm ) {
084                super();
085
086                name  = clm.getName();
087                label = clm.getLabel();
088//              param = StringUtil.nvalAdd( clm.getRendererParam() ,
089//                                                                      clm.getRendererAttributes().get( "optionAttributes" ) );
090                // 8.5.3.0 (2023/09/08) optionAttributesの使い方が分からない為、optionAttributesのセットを廃止
091                param = StringUtil.nval( clm.getRendererParam() , "" );
092        }
093
094        /**
095         * 各オブジェクトから自分のインスタンスを返します。
096         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
097         * まかされます。
098         *
099         * @param       clm     DBColumnオブジェクト
100         * @return      CellRendererオブジェクト
101         * @og.rtnNotNull
102         */
103        public CellRenderer newInstance( final DBColumn clm ) {
104                return new Renderer_URLCALL( clm );
105        }
106
107        /**
108         * データの表示用文字列を返します。
109         *
110         * @og.rev 7.4.2.0 (2021/05/14) 新規作成
111         *
112         * @param       value   入力値
113         * @return      データの表示用文字列
114         * @og.rtnNotNull
115         */
116        @Override
117        public String getValue( final String value ) {
118                return makeButton( name, value );
119        }
120
121        /**
122         * データ出力用の文字列を作成します。
123         * ファイル等に出力する形式を想定しますので、HTMLタグを含まない
124         * データを返します。
125         * 基本は、#getValue( String ) をそのまま返します。
126         *
127         * @og.rev 7.4.2.0 (2021/05/14) 新規作成
128         *
129         * @param       value   入力値
130         * @return      データ出力用の文字列
131         * @og.rtnNotNull
132         * @see         #getValue( String )
133         */
134        @Override
135        public String getWriteValue( final String value ) {
136                return value == null ? "" : value;
137        }
138
139        /**
140         * データの表示用文字列を返します。
141         *
142         * @og.rev 7.4.2.0 (2021/05/14) 新規作成
143         * @og.rev 8.5.2.0 (2023/07/14) iframeのnameに "_FRM" 追加
144         *
145         * @param       name    カラム名
146         * @param       value   入力値 表示するファイルのアドレス
147         * @return      データ表示用の文字列
148         * @og.rtnNotNull
149         */
150        private String makeButton( final String name,final String value ) {
151                final String newVal = new StringFormat( param, value, name ).format();  // $C は name に置き換える。
152
153                final String button = new TagBuffer( "button" )
154                                        .add( "name"    , name  )
155                                        .add( "id"              , name  )
156                                        .add( "type"    , "button"      )
157                                        .add( "onclick" , "window.open('" + newVal + "','" + name + "_FRM').close();" )
158                                        .addBody( label )                                                                                       // ボタンの表示
159                                        .makeTag();
160
161                final String iframe = new TagBuffer( "iframe" )
162//                                      .add( "name"    , name  )
163                                        .add( "name"    , name + "_FRM" )                                                       // 8.5.2.0 (2023/07/14) Modify
164                                        .add( "style"   , "display:none;" )
165                                        .makeTag();
166
167                return button + iframe;
168        }
169}