View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.protocol.rtp;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2016 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolSpeciesRowModel;
28  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolSpeciesTableModel;
29  import jaxx.runtime.SwingUtil;
30  
31  import javax.swing.JComponent;
32  import javax.swing.JTable;
33  import javax.swing.UIManager;
34  import javax.swing.table.DefaultTableCellRenderer;
35  import java.awt.Color;
36  import java.awt.Font;
37  
38  import static org.nuiton.i18n.I18n.n;
39  import static org.nuiton.i18n.I18n.t;
40  
41  /**
42   * Created on 14/01/16.
43   *
44   * @author Tony Chemit - chemit@codelutin.com
45   */
46  public class RtpCellRenderer extends DefaultTableCellRenderer {
47  
48      public static final String TEXT_PATTERN = "<html><body>%s</body></html>";
49  
50      private static final long serialVersionUID = 1L;
51  
52      private final String withoutRtp;
53  
54      private final String withRtp;
55  
56      private Font defaultFont;
57  
58      private Font selectedFont;
59  
60      public RtpCellRenderer() {
61          setHorizontalAlignment(CENTER);
62          setIcon(SwingUtil.createActionIcon("edit-rtp"));
63          this.withoutRtp = n("tutti.editRtp.withoutRtp.tip");
64          this.withRtp = n("tutti.editRtp.withRtp.tip");
65      }
66  
67      @Override
68      protected void setValue(Object value) {
69          // do nothing
70      }
71  
72      @Override
73      public JComponent getTableCellRendererComponent(JTable table,
74                                                      Object value,
75                                                      boolean isSelected,
76                                                      boolean hasFocus,
77                                                      int row,
78                                                      int column) {
79  
80          if (defaultFont == null) {
81              defaultFont = UIManager.getFont("Table.font");
82              selectedFont = defaultFont.deriveFont(Font.BOLD);
83          }
84  
85          int rowIndex = table.convertRowIndexToModel(row);
86          EditProtocolSpeciesRowModel rowModel = ((EditProtocolSpeciesTableModel) table.getModel()).getEntry(rowIndex);
87  
88          boolean useRtp = rowModel.isUseRtp();
89  
90          String toolTipTextValue;
91          String textValue;
92          if (!useRtp) {
93  
94              // use HTML to show the tooltip in italic
95              toolTipTextValue = "<i>" + t(withoutRtp) + "</i>";
96              textValue = t(withoutRtp);
97  
98  
99          } else {
100             toolTipTextValue = t("tutti.editRtp.tooltip",
101                                  rowModel.getRtpMale().getA(), rowModel.getRtpMale().getB(),
102                                  rowModel.getRtpFemale().getA(), rowModel.getRtpFemale().getB(),
103                                  rowModel.getRtpUndefined().getA(), rowModel.getRtpUndefined().getB());
104             textValue = t(withRtp);
105 
106         }
107         boolean editable = table.isCellEditable(row, column);
108         toolTipTextValue = String.format(TEXT_PATTERN, toolTipTextValue);
109         setEnabled(editable);
110         setText(textValue);
111         setToolTipText(toolTipTextValue);
112         setBackground(null);
113         setForeground(Color.BLACK);
114 
115         if (isSelected) {
116             setFont(selectedFont);
117         } else {
118             setFont(defaultFont);
119         }
120 
121         return this;
122     }
123 }