View Javadoc
1   package fr.ifremer.tutti.ui.swing.util.caracteristics;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
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   * Renderer for the values of the caracteristics of the fishing operations.
40   * The renderer depends on the caracteristic value type.
41   *
42   * @author Kevin Morin - kmorin@codelutin.com
43   * @since 0.3
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              // should be render a null value ?
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                      // use default text renderer with align at east
89                      renderer = table.getDefaultRenderer(Object.class);
90                      numericType = true;
91                      break;
92                  default:
93                  case TEXT:
94                      // use default text renderer
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 }