View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.operation.catches.species.frequency;
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.ui.swing.content.operation.catches.species.frequency.actions.DeleteSpeciesFrequencyLogRowAction;
26  import jaxx.runtime.swing.JAXXWidgetUtil;
27  
28  import javax.swing.AbstractCellEditor;
29  import javax.swing.JButton;
30  import javax.swing.JLabel;
31  import javax.swing.JPanel;
32  import javax.swing.JTable;
33  import javax.swing.UIManager;
34  import javax.swing.table.TableCellEditor;
35  import javax.swing.table.TableCellRenderer;
36  import java.awt.BorderLayout;
37  import java.awt.Color;
38  import java.awt.Component;
39  import java.awt.Font;
40  
41  /**
42   * Component to render and delete log items from logs table.
43   *
44   * @author Kevin Morin (Code Lutin)
45   * @since 3.8
46   */
47  public class SpeciesFrequencyLogCellComponent extends JPanel {
48  
49      private static final long serialVersionUID = -3050615446905949687L;
50  
51      private JLabel label = new JLabel();
52  
53      public SpeciesFrequencyLogCellComponent(DeleteSpeciesFrequencyLogRowAction action) {
54  
55          setLayout(new BorderLayout());
56  
57          Font defaultFont = UIManager.getFont("Table.font");
58          label.setFont(defaultFont);
59          label.setOpaque(false);
60          add(label, BorderLayout.CENTER);
61  
62          JButton deleteButton = new JButton();
63          deleteButton.setIcon(JAXXWidgetUtil.createActionIcon("delete"));
64          deleteButton.setBorderPainted(false);
65          deleteButton.setBorder(null);
66          deleteButton.setBackground(null);
67          label.setOpaque(false);
68          add(deleteButton, BorderLayout.EAST);
69          if (action != null) {
70              deleteButton.setAction(action);
71          }
72  
73      }
74  
75      public void setData(String data) {
76          label.setText(data);
77      }
78  
79      public static TableCellRenderer newRender() {
80          return new FrequencyLogCellRenderer();
81      }
82  
83      public static TableCellEditor newEditor(SpeciesFrequencyUI speciesFrequencyUI) {
84          return new FrequencyLogCellEditor(speciesFrequencyUI);
85      }
86  
87      public static class FrequencyLogCellEditor extends AbstractCellEditor implements TableCellEditor {
88  
89          private static final long serialVersionUID = 1L;
90  
91          protected final SpeciesFrequencyLogCellComponent component;
92  
93          private SpeciesFrequencyLogRowModel row;
94  
95          public FrequencyLogCellEditor(SpeciesFrequencyUI speciesFrequencyUI) {
96              component = new SpeciesFrequencyLogCellComponent(new DeleteSpeciesFrequencyLogRowAction(speciesFrequencyUI, this));
97          }
98  
99          public void setRow(SpeciesFrequencyLogRowModel row) {
100             this.row = row;
101         }
102 
103         public SpeciesFrequencyLogRowModel getRow() {
104             return row;
105         }
106 
107         @Override
108         public Component getTableCellEditorComponent(JTable table,
109                                                      Object value,
110                                                      boolean isSelected,
111                                                      int row,
112                                                      int column) {
113 
114             SpeciesFrequencyLogsTableModel tableModel = (SpeciesFrequencyLogsTableModel) table.getModel();
115             SpeciesFrequencyLogRowModel editRow = tableModel.getEntry(row);
116             setRow(editRow);
117 
118             String data = (String) value;
119             component.setData(data);
120 
121             return component;
122         }
123 
124         @Override
125         public Object getCellEditorValue() {
126             return null;
127         }
128 
129     }
130 
131     public static class FrequencyLogCellRenderer implements TableCellRenderer {
132 
133         protected final SpeciesFrequencyLogCellComponent component;
134 
135         public FrequencyLogCellRenderer() {
136             component = new SpeciesFrequencyLogCellComponent(null);
137         }
138 
139         @Override
140         public Component getTableCellRendererComponent(JTable table,
141                                                        Object value,
142                                                        boolean isSelected,
143                                                        boolean hasFocus,
144                                                        int row,
145                                                        int column) {
146             String data = (String) value;
147             component.setData(data);
148 
149             component.setBackground(Color.WHITE);
150 
151             return component;
152         }
153 
154     }
155 
156 }