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.viewer;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.eclipse.jface.viewers.ILabelProviderListener;
22  import org.eclipse.jface.viewers.ITableLabelProvider;
23  import org.eclipse.swt.graphics.Image;
24  import org.seasar.framework.beans.BeanDesc;
25  import org.seasar.framework.beans.PropertyDesc;
26  import org.seasar.framework.beans.factory.BeanDescFactory;
27  import org.seasar.uruma.annotation.BindingLabel;
28  
29  /**
30   * 汎用的な {@link ITableLabelProvider} の実装クラスです。<br />
31   * <p>
32   * {@link GenericTableLabelProvider#setTargetClass(Class) setTargetClass()}
33   * メソッドで設定されたクラスの中から {@link BindingLabel} アノテーションが付加されたフィールドを、テーブルのカラムとして表示します。
34   * </p>
35   * 
36   * @author y-komori
37   */
38  public class GenericTableLabelProvider implements ITableLabelProvider,
39          TargetClassHoldingProvider {
40      protected Map<String, Integer> columnNoMap = new HashMap<String, Integer>();
41  
42      protected Map<Integer, PropertyDesc> columnMap = new HashMap<Integer, PropertyDesc>();
43  
44      /*
45       * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object,
46       *      int)
47       */
48      public Image getColumnImage(final Object element, final int columnIndex) {
49          return null;
50      }
51  
52      /*
53       * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object,
54       *      int)
55       */
56      public String getColumnText(final Object element, final int columnIndex) {
57          PropertyDesc pd = columnMap.get(columnIndex);
58          if (pd != null && pd.isReadable()) {
59              Object value = pd.getValue(element);
60  
61              return (value != null) ? value.toString() : "";
62          } else {
63              return "";
64          }
65      }
66  
67      public void addListener(final ILabelProviderListener listener) {
68          // Do nothing.
69      }
70  
71      public void dispose() {
72          // Do nothing.
73      }
74  
75      public boolean isLabelProperty(final Object element, final String property) {
76          return false;
77          // Do nothing.
78      }
79  
80      public void removeListener(final ILabelProviderListener listener) {
81          // Do nothing.
82      }
83  
84      /*
85       * @see org.seasar.uruma.viewer.TargetClassHoldingProvider#setTargetClass(java.lang.Class)
86       */
87      public void setTargetClass(final Class<?> clazz) {
88          BeanDesc desc = BeanDescFactory.getBeanDesc(clazz);
89          for (String columnName : columnNoMap.keySet()) {
90              if (desc.hasPropertyDesc(columnName)) {
91                  PropertyDesc pd = desc.getPropertyDesc(columnName);
92                  columnMap.put(columnNoMap.get(columnName), pd);
93              }
94          }
95      }
96  
97      /**
98       * カラム名とカラム番号の対応を設定します。<br />
99       * 
100      * @param columnNo
101      *            カラム番号
102      * @param columnName
103      *            カラム名
104      */
105     public void addColumnMap(final int columnNo, final String columnName) {
106         columnNoMap.put(columnName, columnNo);
107     }
108 }