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 jaxx.runtime.SwingUtil;
26  import org.apache.commons.lang3.StringUtils;
27  
28  import javax.swing.JComponent;
29  import javax.swing.JTable;
30  import javax.swing.UIManager;
31  import javax.swing.table.DefaultTableCellRenderer;
32  import java.awt.Font;
33  
34  import static org.nuiton.i18n.I18n.n;
35  import static org.nuiton.i18n.I18n.t;
36  
37  /**
38   * To render a comment in a table cell.
39   *
40   * @author Tony Chemit - chemit@codelutin.com
41   * @since 1.1
42   */
43  public class CommentCellRenderer extends DefaultTableCellRenderer {
44  
45      public static final String TEXT_PATTERN = "<html><body>%s</body></html>";
46  
47      private static final long serialVersionUID = 1L;
48  
49      private final String noneText;
50  
51      protected Font defaulfFont;
52  
53      protected Font selectedFont;
54  
55  
56      public static CommentCellRenderer newRender() {
57          return new CommentCellRenderer();
58      }
59  
60      protected CommentCellRenderer() {
61          setHorizontalAlignment(CENTER);
62          setIcon(SwingUtil.createActionIcon("edit-comment"));
63          this.noneText = n("tutti.commentEditor.none.tip");
64      }
65  
66      @Override
67      protected void setValue(Object value) {
68          // do nothing
69      }
70  
71      @Override
72      public JComponent getTableCellRendererComponent(JTable table,
73                                                      Object value,
74                                                      boolean isSelected,
75                                                      boolean hasFocus,
76                                                      int row,
77                                                      int column) {
78  
79          String comment = (String) value;
80  
81          String toolTipTextValue;
82  
83          if (StringUtils.isEmpty(comment)) {
84  
85              // use HTML to show the tooltip in italic
86              toolTipTextValue = "<i>" + t(noneText) + "</i>";
87  
88  
89          } else {
90  
91              // use html to display the tooltip on several lines
92              toolTipTextValue = String.valueOf(value).replace("\n", "<br/>");
93  
94          }
95          boolean editable = table.isCellEditable(row, column);
96          toolTipTextValue = String.format(TEXT_PATTERN, toolTipTextValue);
97          setEnabled(editable);
98          setToolTipText(toolTipTextValue);
99          setBackground(null);
100 
101         if (defaulfFont == null) {
102             defaulfFont = UIManager.getFont("Table.font");
103             selectedFont = defaulfFont.deriveFont(Font.BOLD);
104         }
105 
106         if (isSelected) {
107             setFont(selectedFont);
108         } else {
109             setFont(defaulfFont);
110         }
111 
112         return this;
113     }
114 }