Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
GenericLabelProvider |
|
| 0.0;0 |
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.lang.reflect.Field; | |
19 | ||
20 | import org.eclipse.jface.viewers.ILabelProvider; | |
21 | import org.eclipse.jface.viewers.ILabelProviderListener; | |
22 | import org.eclipse.swt.graphics.Image; | |
23 | import org.seasar.framework.beans.BeanDesc; | |
24 | import org.seasar.framework.beans.factory.BeanDescFactory; | |
25 | import org.seasar.framework.util.FieldUtil; | |
26 | import org.seasar.uruma.annotation.BindingLabel; | |
27 | ||
28 | /** | |
29 | * 汎用的な {@link ILabelProvider} の実装クラスです。<br /> | |
30 | * <p> | |
31 | * {@link GenericLabelProvider#setTargetClass(Class) setTargetClass()} | |
32 | * メソッドで設定されたクラスの中から {@link BindingLabel} アノテーションが付加されたフィールドをテーブルのカラムとして表示します。 | |
33 | * </p> | |
34 | * <p> | |
35 | * {@link BindingLabel} アノテーションが付加されたフィールドが存在しない場合、<code>toString()</code> | |
36 | * メソッドの戻り値をラベルに設定します。 | |
37 | * </p> | |
38 | * <p> | |
39 | * {@link BindingLabel} アノテーションが付加されたフィールドが複数存在する場合、一番最初のフィールドが有効となります。 | |
40 | * </p> | |
41 | * | |
42 | * @author y-komori | |
43 | */ | |
44 | 32 | public class GenericLabelProvider implements ILabelProvider, |
45 | TargetClassHoldingProvider { | |
46 | protected Field labelField; | |
47 | ||
48 | /* | |
49 | * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object) | |
50 | */ | |
51 | public Image getImage(final Object element) { | |
52 | 0 | return null; |
53 | } | |
54 | ||
55 | /* | |
56 | * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object) | |
57 | */ | |
58 | public String getText(final Object element) { | |
59 | 124 | if (labelField != null) { |
60 | 0 | Object value = FieldUtil.get(labelField, element); |
61 | 0 | return (value != null) ? value.toString() : ""; |
62 | } else { | |
63 | 124 | return element.toString(); |
64 | } | |
65 | } | |
66 | ||
67 | /* | |
68 | * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener) | |
69 | */ | |
70 | public void addListener(final ILabelProviderListener listener) { | |
71 | // Do nothing. | |
72 | 28 | } |
73 | ||
74 | /* | |
75 | * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose() | |
76 | */ | |
77 | public void dispose() { | |
78 | // Do nothing. | |
79 | 28 | } |
80 | ||
81 | /* | |
82 | * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object, | |
83 | * java.lang.String) | |
84 | */ | |
85 | public boolean isLabelProperty(final Object element, final String property) { | |
86 | // Do nothing. | |
87 | 0 | return false; |
88 | } | |
89 | ||
90 | /* | |
91 | * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener) | |
92 | */ | |
93 | public void removeListener(final ILabelProviderListener listener) { | |
94 | // Do nothing. | |
95 | 28 | } |
96 | ||
97 | /* | |
98 | * @see org.seasar.uruma.viewer.TargetClassHoldingProvider#setTargetClass(java.lang.Class) | |
99 | */ | |
100 | public void setTargetClass(final Class<?> clazz) { | |
101 | 12 | BeanDesc desc = BeanDescFactory.getBeanDesc(clazz); |
102 | 12 | int fieldSize = desc.getFieldSize(); |
103 | 96 | for (int i = 0; i < fieldSize; i++) { |
104 | 84 | Field field = desc.getField(i); |
105 | 84 | BindingLabel annotation = field.getAnnotation(BindingLabel.class); |
106 | 84 | if (annotation != null) { |
107 | 0 | labelField = field; |
108 | 0 | break; |
109 | } | |
110 | } | |
111 | 12 | } |
112 | } |