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.2.0 (2023/07/14)" ;
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         * @param       clm     DBColumnオブジェクト
080         */
081        private Renderer_URLCALL( final DBColumn clm ) {
082                super();
083
084                name  = clm.getName();
085                label = clm.getLabel();
086                param = StringUtil.nvalAdd( clm.getRendererParam() ,
087                                                                        clm.getRendererAttributes().get( "optionAttributes" ) );
088        }
089
090        /**
091         * 各オブジェクトから自分のインスタンスを返します。
092         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
093         * まかされます。
094         *
095         * @param       clm     DBColumnオブジェクト
096         * @return      CellRendererオブジェクト
097         * @og.rtnNotNull
098         */
099        public CellRenderer newInstance( final DBColumn clm ) {
100                return new Renderer_URLCALL( clm );
101        }
102
103        /**
104         * データの表示用文字列を返します。
105         *
106         * @og.rev 7.4.2.0 (2021/05/14) 新規作成
107         *
108         * @param       value   入力値
109         * @return      データの表示用文字列
110         * @og.rtnNotNull
111         */
112        @Override
113        public String getValue( final String value ) {
114                return makeButton( name, value );
115        }
116
117        /**
118         * データ出力用の文字列を作成します。
119         * ファイル等に出力する形式を想定しますので、HTMLタグを含まない
120         * データを返します。
121         * 基本は、#getValue( String ) をそのまま返します。
122         *
123         * @og.rev 7.4.2.0 (2021/05/14) 新規作成
124         *
125         * @param       value 入力値
126         *
127         * @return      データ出力用の文字列
128         * @og.rtnNotNull
129         * @see         #getValue( String )
130         */
131        @Override
132        public String getWriteValue( final String value ) {
133                return value == null ? "" : value;
134        }
135
136        /**
137         * データの表示用文字列を返します。
138         *
139         * @og.rev 7.4.2.0 (2021/05/14) 新規作成
140         * @og.rev 8.5.2.0 (2023/07/14) iframeのnameに "_FRM" 追加
141         *
142         * @param       name    カラム名
143         * @param       value   入力値 表示するファイルのアドレス
144         * @return      データ表示用の文字列
145         * @og.rtnNotNull
146         */
147        private String makeButton( final String name,final String value ) {
148                final String newVal = new StringFormat( param, value, name ).format();  // $C は name に置き換える。
149
150                final String button = new TagBuffer( "button" )
151                                        .add( "name"    , name  )
152                                        .add( "id"              , name  )
153                                        .add( "type"    , "button"      )
154                                        .add( "onClick" , "window.open('" + newVal + "','" + name + "_FRM').close();" )
155                                        .addBody( label )                                                                                       // ボタンの表示
156                                        .makeTag();
157
158                final String iframe = new TagBuffer( "iframe" )
159//                                      .add( "name"    , name  )
160                                        .add( "name"    , name + "_FRM" )                                                       // 8.5.2.0 (2023/07/14) Modify
161                                        .add( "style"   , "display:none;" )
162                                        .makeTag();
163
164                return button + iframe;
165        }
166}