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 fr.ifremer.tutti.persistence.entities.data.Attachment;
26  import jaxx.runtime.SwingUtil;
27  import org.apache.commons.collections4.CollectionUtils;
28  import org.nuiton.decorator.Decorator;
29  
30  import javax.swing.JComponent;
31  import javax.swing.JTable;
32  import javax.swing.UIManager;
33  import javax.swing.table.DefaultTableCellRenderer;
34  import java.awt.Color;
35  import java.awt.Font;
36  import java.util.List;
37  
38  import static org.nuiton.i18n.I18n.n;
39  import static org.nuiton.i18n.I18n.t;
40  
41  /**
42   * Renderer of a attachement editor in a table cell.
43   *
44   * @author Kevin Morin - kmorin@codelutin.com
45   * @author Tony Chemit - chemit@codelutin.com
46   * @since 1.0.2
47   */
48  public class AttachmentCellRenderer extends DefaultTableCellRenderer {
49  
50      public static final String TEXT_PATTERN = "<html><body>%s</body></html>";
51  
52      private static final long serialVersionUID = 1L;
53  
54      private final String noneText;
55  
56      private final Decorator<Attachment> decorator;
57  
58      private Font defaulfFont;
59  
60      private Font selectedFont;
61  
62      public static AttachmentCellRenderer newRender(Decorator<Attachment> decorator) {
63          return new AttachmentCellRenderer(decorator);
64      }
65  
66      protected AttachmentCellRenderer(Decorator<Attachment> decorator) {
67          setHorizontalAlignment(CENTER);
68          setIcon(SwingUtil.createActionIcon("edit-attachment"));
69          this.noneText = n("tutti.attachmentEditor.none.tip");
70          this.decorator = decorator;
71      }
72  
73      @Override
74      protected void setValue(Object value) {
75          // do nothing
76      }
77  
78      @Override
79      public JComponent getTableCellRendererComponent(JTable table,
80                                                      Object value,
81                                                      boolean isSelected,
82                                                      boolean hasFocus,
83                                                      int row,
84                                                      int column) {
85  
86          if (defaulfFont == null) {
87              defaulfFont = UIManager.getFont("Table.font");
88              selectedFont = defaulfFont.deriveFont(Font.BOLD);
89          }
90  
91          List<Attachment> attachments = (List<Attachment>) value;
92  
93          String toolTipTextValue;
94  
95          if (CollectionUtils.isEmpty(attachments)) {
96  
97              // use HTML to show the tooltip in italic
98              toolTipTextValue = "<i>" + t(noneText) + "</i>";
99  
100 
101         } else {
102 
103             StringBuilder sb = new StringBuilder();
104             for (Attachment attachment : attachments) {
105                 sb.append("<br/>").append(decorator.toString(attachment));
106             }
107             // use html to display the tooltip on several lines
108             toolTipTextValue = sb.substring(5);
109         }
110         String textValue = ButtonAttachment.getButtonText(attachments);
111         boolean editable = table.isCellEditable(row, column);
112         toolTipTextValue = String.format(TEXT_PATTERN, toolTipTextValue);
113         setEnabled(editable);
114         setText(textValue);
115         setToolTipText(toolTipTextValue);
116         setBackground(null);
117         setForeground(Color.BLACK);
118 
119         if (isSelected) {
120             setFont(selectedFont);
121         } else {
122             setFont(defaulfFont);
123         }
124 
125         return this;
126     }
127 }