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.hayabusa.db;
017
018import org.opengion.hayabusa.common.HybsSystem;
019import org.opengion.hayabusa.common.SystemManager;
020import org.opengion.fukurou.util.Cleanable;
021
022import java.util.Map;
023import java.util.HashMap;
024import java.util.Locale ;
025
026/**
027 * DBCellRenderer/DBCellEditor オブジェクトを取得する為に使用する,ファクトリクラスです。
028 *
029 *  DBCell オブジェクト の識別ID を元に、DBCellFactory.newInstance( String id )
030 * メソッドで,DBCell オブジェクトを取得します。
031 *
032 * @og.group データ表示
033 * @og.group データ編集
034 *
035 * @version  4.0
036 * @author   Kazuhiko Hasegawa
037 * @since    JDK5.0,
038 */
039public final class DBCellFactory {
040        private static final String DEFAULT_RENDERER    = "LABEL";
041        private static final String DEFAULT_EDITOR              = "TEXT";
042        private static final Map<String,CellRenderer>   rMap    = new HashMap<String,CellRenderer>();
043        private static final Map<String,CellEditor>             eMap    = new HashMap<String,CellEditor>();
044
045        // 4.0.0 (2005/01/31) Cleanable インターフェースによる初期化処理
046        static {
047                Cleanable clr = new Cleanable() {
048                        public void clear() {
049                                DBCellFactory.clear();
050                        }
051                };
052
053                SystemManager.addCleanable( clr );
054        }
055
056        /**
057         *  デフォルトコンストラクターをprivateにして、
058         *  オブジェクトの生成をさせないようにする。
059         *
060         */
061        private DBCellFactory() {
062        }
063
064        /**
065         * 識別id に応じた DBCell オブジェクトを取得します。
066         *
067         * @og.rev 2.1.2.1 (2002/11/27) id が指定されていない場合の デフォルトを指定するように変更。
068         * @og.rev 3.1.1.1 (2003/04/03) DBCell のファクトリクラスに DBColumn オブジェクトを渡す。
069         * @og.rev 3.1.2.1 (2003/04/10) synchronized の方法を修正。
070         * @og.rev 3.5.6.0 (2004/06/18) 各種プラグイン関連付け設定を、システムパラメータ に記述します。
071         * @og.rev 4.0.0.0 (2005/01/31) キーの指定を、Renderer. から、Renderer_ に変更します。
072         *
073         * @param   id DBCell インターフェースを実装したサブクラスの識別id
074         * @param       clm     DBColumnオブジェクト
075         *
076         * @return  DBCellオブジェクト
077         */
078        public static CellRenderer newRenderer( final String id,final DBColumn clm ) {
079                String type = ( id == null ) ? DEFAULT_RENDERER : id.toUpperCase(Locale.JAPAN) ;
080                String cls = HybsSystem.sys( "Renderer_" + type );              // 4.0.0 (2005/01/31)
081
082                CellRenderer cell;
083                synchronized( rMap ) {
084                        cell = rMap.get( type );
085                        if( cell == null ) {
086                                cell = (CellRenderer)HybsSystem.newInstance( cls );     // 3.5.5.3 (2004/04/09)
087                                rMap.put( type,cell );
088                        }
089                }
090                return cell.newInstance( clm );
091        }
092
093        /**
094         * 識別id に応じた DBCell オブジェクトを取得します。
095         *
096         * @og.rev 2.1.2.1 (2002/11/27) id が指定されていない場合の デフォルトを指定するように変更。
097         * @og.rev 3.1.1.1 (2003/04/03) DBCell のファクトリクラスに DBColumn オブジェクトを渡す。
098         * @og.rev 3.1.2.1 (2003/04/10) synchronized の方法を修正。
099         * @og.rev 3.5.6.0 (2004/06/18) 各種プラグイン関連付け設定を、システムパラメータ に記述します。
100         * @og.rev 4.0.0.0 (2005/01/31) キーの指定を、Editor. から、Editor_ に変更します。
101         *
102         * @param   id DBCell インターフェースを実装したサブクラスの識別id
103         * @param       clm     DBColumnオブジェクト
104         *
105         * @return  DBCellオブジェクト
106         */
107        public static CellEditor newEditor( final String id,final DBColumn clm ) {
108                String type = ( id == null ) ? DEFAULT_EDITOR : id.toUpperCase(Locale.JAPAN) ;
109                String cls = HybsSystem.sys( "Editor_" + type );                // 4.0.0 (2005/01/31)
110
111                CellEditor cell;
112                synchronized( eMap ) {
113                        cell = eMap.get( type );
114                        if( cell == null ) {
115                                cell = (CellEditor)HybsSystem.newInstance( cls );       // 3.5.5.3 (2004/04/09)
116                                eMap.put( type,cell );
117                        }
118                }
119                return cell.newInstance( clm );
120        }
121
122        /**
123         * DBCell オブジェクトをプールからすべて削除します。
124         * システム全体を初期化するときや、動作が不安定になったときに行います。
125         * プールの方法自体が,一種のキャッシュ的な使いかたしかしていない為,
126         * 実行中でも、いつでもプールを初期化できます。
127         *
128         * @og.rev 3.1.1.1 (2003/04/03) キャッシュクリアメソッドを新規追加。
129         * @og.rev 3.1.2.1 (2003/04/10) synchronized の方法を修正。
130         */
131        public static void clear() {
132                synchronized( rMap ) { rMap.clear(); }
133                synchronized( eMap ) { eMap.clear(); }
134        }
135}