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 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.border.LineBorder;
32  import javax.swing.table.DefaultTableCellRenderer;
33  import java.awt.Color;
34  import java.awt.Font;
35  
36  import static org.nuiton.i18n.I18n.n;
37  import static org.nuiton.i18n.I18n.t;
38  
39  /**
40   * To render a comment in a table cell.
41   */
42  public class CommentCellRenderer extends DefaultTableCellRenderer {
43  
44      /**
45       * Constant <code>TEXT_PATTERN="<html><body>%s</body></html>"</code>
46       */
47      private static final String TEXT_PATTERN = "<html><body>%s</body></html>";
48  
49      private static final long serialVersionUID = 1L;
50  
51      private final String noneText;
52  
53      private Font defaultFont;
54  
55      private Font selectedFont;
56  
57      /**
58       * <p>newRenderer.</p>
59       *
60       * @return a {@link CommentCellRenderer} object.
61       */
62      public static CommentCellRenderer newRenderer() {
63          return new CommentCellRenderer();
64      }
65  
66      /**
67       * <p>Constructor for CommentCellRenderer.</p>
68       */
69      protected CommentCellRenderer() {
70          setHorizontalAlignment(CENTER);
71          setIcon(SwingUtil.createActionIcon("edit-no-comment"));
72          this.noneText = n("quadrige3.commentEditor.none.tip");
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      protected void setValue(Object value) {
80          // do nothing
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public JComponent getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
88  
89          String comment = (String) value;
90  
91          String toolTipTextValue;
92  
93          if (StringUtils.isBlank(comment)) {
94  
95              // use HTML to show the tooltip in italic
96              toolTipTextValue = "<i>" + t(noneText) + "</i>";
97              this.setIcon(SwingUtil.createActionIcon("edit-no-comment"));
98          } else {
99  
100             // use html to display the tooltip on several lines
101             toolTipTextValue = String.valueOf(value).replace("\n", "<br/>");
102             this.setIcon(SwingUtil.createActionIcon("edit-comment"));
103 
104         }
105         toolTipTextValue = String.format(TEXT_PATTERN, toolTipTextValue);
106         setToolTipText(toolTipTextValue);
107         setBackground(null);
108         setBorder(hasFocus ? new LineBorder(Color.BLACK) : null);
109 
110         if (defaultFont == null) {
111             defaultFont = UIManager.getFont("Table.font");
112             selectedFont = defaultFont.deriveFont(Font.BOLD);
113         }
114 
115         if (isSelected) {
116             setFont(selectedFont);
117         } else {
118             setFont(defaultFont);
119         }
120 
121         return this;
122     }
123 }