View Javadoc
1   package fr.ifremer.tutti.ui.swing.util.attachment;
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.ui.swing.TuttiUIContext;
27  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiBeanUIModel;
28  import fr.ifremer.tutti.ui.swing.util.TuttiUI;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.nuiton.jaxx.application.swing.table.AbstractApplicationTableModel;
32  
33  import javax.swing.AbstractCellEditor;
34  import javax.swing.JTable;
35  import javax.swing.border.LineBorder;
36  import javax.swing.event.CellEditorListener;
37  import javax.swing.event.ChangeEvent;
38  import javax.swing.table.TableCellEditor;
39  import java.awt.Color;
40  import java.awt.Component;
41  import java.util.EventObject;
42  
43  /**
44   * To edit attachments from a table cell.
45   *
46   * @author Tony Chemit - chemit@codelutin.com
47   * @author Kevin Morin - kmorin@codelutin.com
48   * @since 1.0.2
49   */
50  public class AttachmentCellEditor extends AbstractCellEditor implements TableCellEditor {
51  
52      private static final long serialVersionUID = 1L;
53  
54      /** Logger. */
55      private static final Log log = LogFactory.getLog(AttachmentCellEditor.class);
56  
57      public static TableCellEditor newEditor(TuttiUI ui) {
58  
59          return new AttachmentCellEditor(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 ButtonAttachment editorButton;
71  
72      public AttachmentCellEditor(TuttiUIContext context) {
73  
74          this.editorButton = new ButtonAttachment(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         AttachmentModelAware model =
102                 (AttachmentModelAware) tableModel.getEntry(row);
103 
104         editorButton.init(model);
105 
106         return editorButton;
107     }
108 
109     @Override
110     public boolean shouldSelectCell(EventObject anEvent) {
111         return false;
112     }
113 
114     @Override
115     public Object getCellEditorValue() {
116 
117         AttachmentModelAware model = editorButton.getBean();
118         Preconditions.checkNotNull(model, "No model found in editor.");
119 
120         Object result = model.getAttachment();
121         if (log.isDebugEnabled()) {
122             log.debug("editor value: " + result);
123         }
124 
125         return result;
126     }
127 
128     @Override
129     public boolean stopCellEditing() {
130         boolean b = super.stopCellEditing();
131         if (b) {
132             editorButton.setBean(null);
133         }
134         return b;
135     }
136 
137     @Override
138     public void cancelCellEditing() {
139         editorButton.setBean(null);
140         super.cancelCellEditing();
141     }
142 }