1 package fr.ifremer.tutti.ui.swing.util.caracteristics;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
26 import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue;
27 import fr.ifremer.tutti.service.DecoratorService;
28 import fr.ifremer.tutti.ui.swing.TuttiUIContext;
29 import jaxx.runtime.swing.renderer.DecoratorTableCellRenderer;
30 import org.nuiton.decorator.Decorator;
31
32 import javax.swing.JLabel;
33 import javax.swing.JTable;
34 import javax.swing.SwingConstants;
35 import javax.swing.table.TableCellRenderer;
36 import java.awt.Component;
37
38
39
40
41
42
43
44
45 public class CaracteristicValueRenderer implements TableCellRenderer {
46
47 protected int caracteristicColumn;
48
49 protected Decorator<CaracteristicQualitativeValue> decorator;
50
51 public CaracteristicValueRenderer(TuttiUIContext context) {
52 this(0, context);
53 }
54
55 public CaracteristicValueRenderer(int caracteristicColumn, TuttiUIContext context) {
56 super();
57 this.caracteristicColumn = caracteristicColumn;
58 DecoratorService decoratorService = context.getDecoratorService();
59 decorator = decoratorService.getDecoratorByType(CaracteristicQualitativeValue.class);
60 }
61
62 public Component getTableCellRendererComponent(JTable table,
63 Object value,
64 boolean isSelected,
65 boolean hasFocus,
66 int row,
67 int column) {
68 TableCellRenderer renderer;
69
70 Caracteristic caracteristic = (Caracteristic)
71 table.getModel().getValueAt(row, caracteristicColumn);
72
73 boolean numericType = false;
74 if (caracteristic == null) {
75
76
77 renderer = table.getDefaultRenderer(Object.class);
78
79 } else {
80 switch (caracteristic.getCaracteristicType()) {
81
82 case QUALITATIVE:
83 renderer = new DecoratorTableCellRenderer(decorator);
84 break;
85
86 case NUMBER:
87
88
89 renderer = table.getDefaultRenderer(Object.class);
90 numericType = true;
91 break;
92 default:
93 case TEXT:
94
95 renderer = table.getDefaultRenderer(Object.class);
96 }
97 }
98
99 Component result = renderer.getTableCellRendererComponent(
100 table, value, isSelected, hasFocus, row, column);
101
102 if (numericType) {
103
104 if (result instanceof JLabel) {
105 JLabel jLabel = (JLabel) result;
106 jLabel.setHorizontalAlignment(SwingConstants.RIGHT);
107
108 }
109 } else {
110 if (result instanceof JLabel) {
111 JLabel jLabel = (JLabel) result;
112 jLabel.setHorizontalAlignment(SwingConstants.LEFT);
113 }
114 }
115
116 return result;
117 }
118
119 }