View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.table.comment;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: UI Swing Common
6    * %%
7    * Copyright (C) 2017 - 2019 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (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 Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  
25  import fr.ifremer.quadrige3.core.dao.technical.Assert;
26  import fr.ifremer.quadrige3.ui.core.dto.CommentAware;
27  import fr.ifremer.quadrige3.ui.swing.ApplicationUI;
28  import fr.ifremer.quadrige3.ui.swing.table.AbstractRowUIModel;
29  import fr.ifremer.quadrige3.ui.swing.table.AbstractTableModel;
30  import jaxx.runtime.SwingUtil;
31  import org.apache.commons.lang3.StringUtils;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.nuiton.jaxx.application.bean.JavaBeanObjectUtil;
35  
36  import javax.swing.AbstractCellEditor;
37  import javax.swing.JTable;
38  import javax.swing.border.LineBorder;
39  import javax.swing.event.CellEditorListener;
40  import javax.swing.event.ChangeEvent;
41  import javax.swing.table.TableCellEditor;
42  import java.awt.Color;
43  import java.awt.Component;
44  import java.util.EventObject;
45  
46  /**
47   * To edit a comment in a table cell using a nice dialog for this.
48   */
49  public class CommentCellEditor extends AbstractCellEditor implements TableCellEditor {
50  
51      private static final long serialVersionUID = 1L;
52  
53      /**
54       * Logger.
55       */
56      private static final Log LOG = LogFactory.getLog(CommentCellEditor.class);
57  
58      /**
59       * <p>newEditor.</p>
60       *
61       * @param ui       a {@link ApplicationUI} object.
62       * @param property a {@link String} object.
63       * @return a {@link TableCellEditor} object.
64       */
65      public static TableCellEditor newEditor(ApplicationUI ui, String property, String titleI18n, boolean editable) {
66          return new CommentCellEditor(ui, property, titleI18n, editable);
67      }
68  
69      /**
70       * <p>newEditor.</p>
71       *
72       * @param ui a {@link ApplicationUI} object.
73       * @return a {@link TableCellEditor} object.
74       */
75      public static TableCellEditor newEditor(ApplicationUI ui) {
76          return new CommentCellEditor(ui);
77      }
78  
79      protected JTable table;
80  
81      protected AbstractTableModel<AbstractRowUIModel<?, ?>> tableModel;
82  
83      protected Integer rowIndex;
84  
85      protected Integer columnIndex;
86  
87      private final ButtonComment editorButton;
88  
89      private final boolean editable;
90  
91      /**
92       * <p>Constructor for CommentCellEditor.</p>
93       *
94       * @param ui a {@link fr.ifremer.quadrige3.ui.swing.Application} object.
95       */
96      public CommentCellEditor(ApplicationUI ui) {
97          this(ui, null, null, true);
98      }
99  
100     /**
101      * <p>Constructor for CommentCellEditor.</p>
102      *
103      * @param ui       a {@link ApplicationUI} object.
104      * @param property a {@link String} object.
105      */
106     public CommentCellEditor(ApplicationUI ui, String property, String titleI18n, boolean editable) {
107 
108         this.editorButton = new ButtonComment(ui, null, property, titleI18n);
109         this.editorButton.setBorder(new LineBorder(Color.BLACK));
110         addCellEditorListener(new CellEditorListener() {
111             @Override
112             public void editingStopped(ChangeEvent e) {
113                 editorButton.setSelected(false);
114             }
115 
116             @Override
117             public void editingCanceled(ChangeEvent e) {
118                 editorButton.setSelected(false);
119             }
120         });
121         this.editable = editable;
122     }
123 
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     @SuppressWarnings("unchecked")
129     public Component getTableCellEditorComponent(JTable table,
130                                                  Object value,
131                                                  boolean isSelected,
132                                                  int row,
133                                                  int column) {
134         this.table = table;
135         this.tableModel = (AbstractTableModel<AbstractRowUIModel<?, ?>>) table.getModel();
136 
137         rowIndex = row;
138         columnIndex = column;
139         String comment = (String) value;
140 
141         AbstractRowUIModel rowModel = tableModel.getEntry(table.convertRowIndexToModel(row));
142         boolean isEditable = editable && rowModel.isEditable();
143         CommentAware commentAwareModel = null;
144         if (rowModel instanceof CommentAware) {
145             commentAwareModel = (CommentAware) rowModel;
146         }
147 
148         editorButton.init(commentAwareModel, isEditable);
149 
150         if (StringUtils.isEmpty(StringUtils.trim(comment))) {
151             this.editorButton.setIcon(SwingUtil.createActionIcon("edit-no-comment"));
152         } else {
153             this.editorButton.setIcon(SwingUtil.createActionIcon("edit-comment"));
154         }
155 
156         return editorButton;
157     }
158 
159     /**
160      * {@inheritDoc}
161      */
162     @Override
163     public boolean shouldSelectCell(EventObject anEvent) {
164         return true;
165     }
166 
167     /**
168      * {@inheritDoc}
169      */
170     @Override
171     public Object getCellEditorValue() {
172 
173         CommentAware model = editorButton.getBean();
174         Assert.notNull(model, "No model found in editor.");
175 
176         Object result = editorButton.getProperty() == null ? model.getComment() : JavaBeanObjectUtil.getProperty(model, editorButton.getProperty());
177         if (LOG.isDebugEnabled()) {
178             LOG.debug("editor value: " + result);
179         }
180 
181         return result;
182     }
183 
184     /**
185      * {@inheritDoc}
186      */
187     @Override
188     public boolean stopCellEditing() {
189         boolean b = super.stopCellEditing();
190         if (b) {
191             editorButton.setBean(null);
192         }
193         editorButton.setSelected(false);
194         return b;
195     }
196 
197     /**
198      * {@inheritDoc}
199      */
200     @Override
201     public void cancelCellEditing() {
202         editorButton.setBean(null);
203         editorButton.setSelected(false);
204         super.cancelCellEditing();
205     }
206 }