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