View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.table.renderer;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 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  
27  import javax.swing.AbstractCellEditor;
28  import javax.swing.JCheckBox;
29  import javax.swing.JTable;
30  import javax.swing.table.TableCellEditor;
31  import javax.swing.table.TableCellRenderer;
32  import java.awt.Component;
33  
34  /**
35   * <p>LabeledCheckBoxEditor class.</p>
36   *
37   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
38   */
39  public class LabeledCheckBoxEditor extends AbstractCellEditor implements TableCellEditor {
40  
41      private final JCheckBox editor;
42  
43      /**
44       * <p>Constructor for LabeledCheckBoxEditor.</p>
45       */
46      public LabeledCheckBoxEditor() {
47          super();
48          editor = new JCheckBox();
49          editor.addActionListener(e -> LabeledCheckBoxEditor.this.stopCellEditing());
50      }
51  
52      /** {@inheritDoc} */
53      @Override
54      public final boolean stopCellEditing() {
55          // default behavior
56          fireEditingStopped();
57          return true;
58      }
59      
60      /** {@inheritDoc} */
61      @Override
62      public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
63          
64          TableCellRenderer renderer = table.getCellRenderer(row, column);
65          if (renderer instanceof LabeledCheckBoxRenderer) {
66              JCheckBox renderComponent = (JCheckBox) renderer.getTableCellRendererComponent(table, value, isSelected, true, row, column);
67  
68              editor.setHorizontalAlignment(renderComponent.getHorizontalAlignment());
69              editor.setOpaque(renderComponent.isOpaque());
70              editor.setBackground(renderComponent.getBackground());
71              editor.setBorder(renderComponent.getBorder());
72  
73          }
74  
75          LabeledCheckBoxBean bean;
76          if (value instanceof LabeledCheckBoxBean) {
77              bean = (LabeledCheckBoxBean) value;
78          } else {
79              bean = new LabeledCheckBoxBean(isSelected, null);
80          }
81  
82          editor.setSelected(bean.isChecked());
83          editor.setText(bean.getText());
84  
85          return editor;
86      }
87  
88      /** {@inheritDoc} */
89      @Override
90      public Object getCellEditorValue() {
91          return new LabeledCheckBoxBean(editor.isSelected(), editor.getText());
92      }
93  
94      /**
95       * <p>Getter for the field <code>editor</code>.</p>
96       *
97       * @return a {@link JCheckBox} object.
98       */
99      public JCheckBox getEditor() {
100         return editor;
101     }
102 
103 }