View Javadoc
1   package fr.ifremer.tutti.ui.swing.util.comment;
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 com.google.common.base.Preconditions;
26  import fr.ifremer.tutti.persistence.entities.CommentAware;
27  import fr.ifremer.tutti.ui.swing.TuttiUIContext;
28  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiBeanUIModel;
29  import fr.ifremer.tutti.ui.swing.util.TuttiUI;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.nuiton.jaxx.application.swing.table.AbstractApplicationTableModel;
33  
34  import javax.swing.AbstractCellEditor;
35  import javax.swing.JTable;
36  import javax.swing.border.LineBorder;
37  import javax.swing.event.CellEditorListener;
38  import javax.swing.event.ChangeEvent;
39  import javax.swing.table.TableCellEditor;
40  import java.awt.Color;
41  import java.awt.Component;
42  import java.util.EventObject;
43  
44  /**
45   * To edit a comment in a table cell using a nice dialog for this.
46   *
47   * @author Tony Chemit - chemit@codelutin.com
48   * @since 1.1
49   */
50  public class CommentCellEditor extends AbstractCellEditor implements TableCellEditor {
51  
52      private static final long serialVersionUID = 1L;
53  
54      /** Logger. */
55      private static final Log log = LogFactory.getLog(CommentCellEditor.class);
56  
57      public static TableCellEditor newEditor(TuttiUI ui) {
58  
59          return new CommentCellEditor(ui.getHandler().getContext());
60      }
61  
62      protected JTable table;
63  
64      protected AbstractApplicationTableModel<AbstractTuttiBeanUIModel> tableModel;
65  
66      protected Integer rowIndex;
67  
68      protected Integer columnIndex;
69  
70      protected final ButtonComment editorButton;
71  
72      public CommentCellEditor(TuttiUIContext context) {
73  
74          this.editorButton = new ButtonComment(context, null);
75          this.editorButton.setBorder(new LineBorder(Color.BLACK));
76          addCellEditorListener(new CellEditorListener() {
77              @Override
78              public void editingStopped(ChangeEvent e) {
79                  editorButton.setSelected(false);
80              }
81  
82              @Override
83              public void editingCanceled(ChangeEvent e) {
84                  editorButton.setSelected(false);
85              }
86          });
87      }
88  
89      @Override
90      public Component getTableCellEditorComponent(JTable table,
91                                                   Object value,
92                                                   boolean isSelected,
93                                                   int row,
94                                                   int column) {
95          this.table = table;
96          this.tableModel = (AbstractApplicationTableModel<AbstractTuttiBeanUIModel>) table.getModel();
97  
98          rowIndex = row;
99          columnIndex = column;
100 
101         CommentAware model = (CommentAware) tableModel.getEntry(row);
102 
103         editorButton.init(model);
104 
105         return editorButton;
106     }
107 
108     @Override
109     public boolean shouldSelectCell(EventObject anEvent) {
110         return true;
111     }
112 
113     @Override
114     public Object getCellEditorValue() {
115 
116         CommentAware model = editorButton.getBean();
117         Preconditions.checkNotNull(model, "No model found in editor.");
118 
119         Object result = model.getComment();
120         if (log.isDebugEnabled()) {
121             log.debug("editor value: " + result);
122         }
123 
124         return result;
125     }
126 
127     @Override
128     public boolean stopCellEditing() {
129         boolean b = super.stopCellEditing();
130         if (b) {
131             editorButton.setBean(null);
132         }
133         return b;
134     }
135 
136     @Override
137     public void cancelCellEditing() {
138         editorButton.setBean(null);
139         super.cancelCellEditing();
140     }
141 }