View Javadoc
1   package fr.ifremer.dali.ui.swing.content.extraction.filters;
2   
3   /*
4    * #%L
5    * Dali :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import fr.ifremer.quadrige3.ui.core.dto.QuadrigeBean;
27  import fr.ifremer.quadrige3.ui.swing.table.renderer.CollectionDecoratorCellRenderer;
28  import fr.ifremer.dali.decorator.DecoratorService;
29  import fr.ifremer.dali.dto.configuration.filter.FilterDTO;
30  import fr.ifremer.dali.dto.configuration.programStrategy.ProgramDTO;
31  import fr.ifremer.dali.dto.data.survey.CampaignDTO;
32  import fr.ifremer.dali.dto.referential.*;
33  import fr.ifremer.dali.dto.referential.pmfm.PmfmDTO;
34  import fr.ifremer.dali.dto.system.extraction.ExtractionPeriodDTO;
35  import org.apache.commons.collections4.CollectionUtils;
36  
37  import javax.swing.JComponent;
38  import javax.swing.JTable;
39  import javax.swing.table.DefaultTableCellRenderer;
40  import java.awt.Component;
41  import java.util.List;
42  
43  /**
44   * Specific cell renderer for extraction filters
45   * Created by Ludovic on 08/12/2015.
46   */
47  public class FilterElementsCellRenderer extends DefaultTableCellRenderer {
48  
49      private static final String SEPARATOR = "; ";
50  
51      private final CollectionDecoratorCellRenderer periodRenderer;
52      private final CollectionDecoratorCellRenderer programRenderer;
53      private final CollectionDecoratorCellRenderer locationRenderer;
54      private final CollectionDecoratorCellRenderer campaignRenderer;
55      private final CollectionDecoratorCellRenderer taxonRenderer;
56      private final CollectionDecoratorCellRenderer taxonGroupRenderer;
57      private final CollectionDecoratorCellRenderer departmentRenderer;
58      private final CollectionDecoratorCellRenderer pmfmRenderer;
59      private final CollectionDecoratorCellRenderer samplingEquipmentRenderer;
60  
61      /**
62       * <p>Constructor for FilterElementsCellRenderer.</p>
63       *
64       * @param decoratorService a {@link fr.ifremer.dali.decorator.DecoratorService} object.
65       */
66      public FilterElementsCellRenderer(DecoratorService decoratorService) {
67          periodRenderer = new CollectionDecoratorCellRenderer(decoratorService.getDecoratorByType(ExtractionPeriodDTO.class), SEPARATOR);
68          programRenderer = new CollectionDecoratorCellRenderer(decoratorService.getDecoratorByType(ProgramDTO.class, DecoratorService.CODE), SEPARATOR);
69          locationRenderer = new CollectionDecoratorCellRenderer(decoratorService.getDecoratorByType(LocationDTO.class), SEPARATOR);
70          campaignRenderer = new CollectionDecoratorCellRenderer(decoratorService.getDecoratorByType(CampaignDTO.class), SEPARATOR);
71          taxonRenderer = new CollectionDecoratorCellRenderer(decoratorService.getDecoratorByType(TaxonDTO.class), SEPARATOR);
72          taxonGroupRenderer = new CollectionDecoratorCellRenderer(decoratorService.getDecoratorByType(TaxonGroupDTO.class), SEPARATOR);
73          departmentRenderer = new CollectionDecoratorCellRenderer(decoratorService.getDecoratorByType(DepartmentDTO.class), SEPARATOR);
74          pmfmRenderer = new CollectionDecoratorCellRenderer(decoratorService.getDecoratorByType(PmfmDTO.class, DecoratorService.NAME), SEPARATOR);
75          samplingEquipmentRenderer = new CollectionDecoratorCellRenderer(decoratorService.getDecoratorByType(SamplingEquipmentDTO.class, DecoratorService.NAME), SEPARATOR);
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
81  
82          if (value instanceof FilterDTO) {
83              CollectionDecoratorCellRenderer renderer = null;
84              List<? extends QuadrigeBean> elements = ((FilterDTO) value).getElements();
85              if (CollectionUtils.isNotEmpty(elements)) {
86                  // get the element type
87                  QuadrigeBean firstElement = elements.get(0);
88                  if (firstElement instanceof ExtractionPeriodDTO) {
89                      renderer = periodRenderer;
90                  } else if (firstElement instanceof ProgramDTO) {
91                      renderer = programRenderer;
92                  } else if (firstElement instanceof CampaignDTO) {
93                      renderer = campaignRenderer;
94                  } else if (firstElement instanceof LocationDTO) {
95                      renderer = locationRenderer;
96                  } else if (firstElement instanceof TaxonDTO) {
97                      renderer = taxonRenderer;
98                  } else if (firstElement instanceof TaxonGroupDTO) {
99                      renderer = taxonGroupRenderer;
100                 } else if (firstElement instanceof DepartmentDTO) {
101                     renderer = departmentRenderer;
102                 } else if (firstElement instanceof PmfmDTO) {
103                     renderer = pmfmRenderer;
104                 } else if (firstElement instanceof SamplingEquipmentDTO) {
105                     renderer = samplingEquipmentRenderer;
106                 }
107 
108                 if (renderer != null) {
109                     return renderer.getTableCellRendererComponent(table, elements, isSelected, hasFocus, row, column);
110                 }
111             }
112         }
113 
114         // default empty value
115         Component component = super.getTableCellRendererComponent(table, null, isSelected, hasFocus, row, column);
116         if (component instanceof JComponent) {
117             // Force reset the tooltip
118             ((JComponent) component).setToolTipText(null);
119         }
120         return component;
121     }
122 }