View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.uruma.binding.enables;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.eclipse.jface.viewers.ComboViewer;
22  import org.eclipse.jface.viewers.TableViewer;
23  import org.eclipse.jface.viewers.TreeViewer;
24  import org.seasar.uruma.binding.enables.impl.ViewerEnablesDependingListener;
25  import org.seasar.uruma.context.WidgetHandle;
26  import org.seasar.uruma.exception.EnablesDependingException;
27  import org.seasar.uruma.util.AssertionUtil;
28  import org.seasar.uruma.util.ClassUtil;
29  
30  /**
31   * {@link EnablesDependingListener} を取得するためのファクトリクラスです。<br />
32   * 
33   * @author y-komori
34   */
35  public class EnablesDependingListenerFactory {
36  
37      private static final Map<Class<?>, Class<? extends EnablesDependingListener>> listenerMap = new HashMap<Class<?>, Class<? extends EnablesDependingListener>>();
38  
39      static {
40          addListener(TableViewer.class, ViewerEnablesDependingListener.class);
41          addListener(TreeViewer.class, ViewerEnablesDependingListener.class);
42          addListener(ComboViewer.class, ViewerEnablesDependingListener.class);
43      }
44  
45      private EnablesDependingListenerFactory() {
46  
47      }
48  
49      /**
50       * <code>target</code> のもつウィジットに対応する {@link EnablesDependingListener}
51       * のインスタンスを取得します。<br />
52       * 
53       * @param target
54       *            ターゲットウィジットをもつ {@link WidgetHandle}
55       * @param enabled
56       *            イネーブル状態を変化させるウィジットをもつ {@link WidgetHandle}
57       * @param type
58       *            選択状態を表す {@link EnablesForType}
59       * @return {@link EnablesDependingListener} のインスタンス
60       */
61      public static EnablesDependingListener getListener(
62              final WidgetHandle target, final WidgetHandle enabled,
63              final EnablesForType type) {
64          Class<? extends EnablesDependingListener> listenerType = listenerMap
65                  .get(target.getWidgetClass());
66  
67          if (listenerType != null) {
68              return ClassUtil.newInstance(listenerType, target, enabled, type);
69          } else {
70              throw new EnablesDependingException(target.getWidgetClass());
71          }
72      }
73  
74      /**
75       * <code>clazz</code> に対応する {@link EnablesDependingListener}
76       * の型マッピングを追加します。<br />
77       * 
78       * @param clazz
79       *            ターゲットの {@link Class} オブジェクト
80       * @param listenerType
81       *            {@link EnablesDependingListener} の型
82       */
83      public static void addListener(final Class<?> clazz,
84              final Class<? extends EnablesDependingListener> listenerType) {
85          AssertionUtil.assertNotNull("clazz", clazz);
86          AssertionUtil.assertNotNull("listenerType", listenerType);
87  
88          listenerMap.put(clazz, listenerType);
89      }
90  }