1 package fr.ifremer.allegro.obsdeb.ui.swing.util.table;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import com.google.common.base.Preconditions;
27 import fr.ifremer.allegro.obsdeb.service.ObsdebTechnicalException;
28 import org.apache.commons.beanutils.NestedNullException;
29 import org.apache.commons.beanutils.PropertyUtils;
30 import org.apache.commons.lang3.StringUtils;
31 import org.nuiton.jaxx.application.swing.table.ColumnIdentifier;
32
33 import java.lang.reflect.InvocationTargetException;
34
35
36
37
38
39
40
41 public class ObsdebColumnIdentifier<R> extends ColumnIdentifier<R> {
42
43 private static final long serialVersionUID = 1L;
44
45 private final Class<?> propertyType;
46 private final String decoratorName;
47 private final boolean mandatory;
48
49 public static <R> ObsdebColumnIdentifier<R> newId(String propertyName, String headerI18nKey, String headerTipI18nKey, Class<?> propertyType) {
50 return new ObsdebColumnIdentifier<R>(propertyName, headerI18nKey, headerTipI18nKey, propertyType, null, false);
51 }
52
53 public static <R> ObsdebColumnIdentifier<R> newId(String propertyName, String headerI18nKey, String headerTipI18nKey, Class<?> propertyType,
54 String decoratorName) {
55 return new ObsdebColumnIdentifier<R>(propertyName, headerI18nKey, headerTipI18nKey, propertyType, decoratorName, false);
56 }
57
58 public static <R> ObsdebColumnIdentifier<R> newId(String propertyName, String headerI18nKey, String headerTipI18nKey, Class<?> propertyType,
59 boolean mandatory) {
60 return new ObsdebColumnIdentifier<R>(propertyName, headerI18nKey, headerTipI18nKey, propertyType, null, mandatory);
61 }
62
63 public static <R> ObsdebColumnIdentifier<R> newId(String propertyName, String headerI18nKey, String headerTipI18nKey, Class<?> propertyType,
64 String decoratorName, boolean mandatory) {
65 return new ObsdebColumnIdentifier<R>(propertyName, headerI18nKey, headerTipI18nKey, propertyType, decoratorName, mandatory);
66 }
67
68 public static <R> ObsdebColumnIdentifier<R> newReadOnlyId(String propertyName, String headerI18nKey, String headerTipI18nKey, Class<?> propertyType) {
69 return new ObsdebColumnIdentifier<R>(propertyName, headerI18nKey, headerTipI18nKey, propertyType, null, false) {
70
71 private static final long serialVersionUID = 1L;
72
73 @Override
74 public void setValue(R entry, Object value) {
75
76 }
77 };
78 }
79
80 protected ObsdebColumnIdentifier(String propertyName, String headerI18nKey, String headerTipI18nKey, Class<?> propertyType, String decoratorName, boolean mandatory) {
81 super(propertyName, headerI18nKey, headerTipI18nKey);
82 this.propertyType = propertyType;
83 this.decoratorName = decoratorName;
84 this.mandatory = mandatory;
85 }
86
87 public Class<?> getPropertyType() {
88 return propertyType;
89 }
90
91 public String getDecoratorName() {
92 return decoratorName;
93 }
94
95 public boolean isMandatory() {
96 return mandatory;
97 }
98
99 @Override
100 public Object getValue(R entry) {
101 Object result = null;
102 if (StringUtils.isNotBlank(getPropertyName()) && entry != null) {
103 try {
104 result = PropertyUtils.getProperty(entry, getPropertyName());
105 } catch (NestedNullException e) {
106 result = null;
107 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
108 throw new ObsdebTechnicalException(String.format("Property %s not found on object of type %s", getPropertyName(), entry.getClass().getName()), e);
109 }
110 }
111 return result;
112 }
113
114 @Override
115 public void setValue(R entry, Object value) {
116 Preconditions.checkNotNull(entry);
117 if (StringUtils.isNotBlank(getPropertyName())) {
118 try {
119 PropertyUtils.setProperty(entry, getPropertyName(), value);
120 } catch (NestedNullException e) {
121 throw new ObsdebTechnicalException(String.format("Nested property %s is null on object of type %s", getPropertyName(), entry.getClass().getName()), e);
122 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
123 throw new ObsdebTechnicalException(String.format("Property %s not found on object of type %s", getPropertyName(), entry.getClass().getName()), e);
124 }
125 }
126 }
127 }