View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.table.renderer;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (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 Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  import org.apache.commons.lang3.StringUtils;
28  
29  import javax.swing.*;
30  import javax.swing.table.TableCellRenderer;
31  import java.awt.Color;
32  import java.awt.Component;
33  import java.awt.Font;
34  
35  /**
36   * <p>ButtonCellRenderer class.</p>
37   *
38   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
39   * @param <T>
40   */
41  public class ButtonCellRenderer<T> implements TableCellRenderer {
42  
43      private final boolean hideValue;
44  
45      private Font defaultFont;
46  
47      private Font selectedFont;
48  
49      protected final Icon icon;
50  
51      /**
52       * <p>Constructor for ButtonCellRenderer.</p>
53       */
54      public ButtonCellRenderer() {
55          this(null, false);
56      }
57  
58      /**
59       * <p>Constructor for ButtonCellRenderer.</p>
60       *
61       * @param hideValue a boolean.
62       */
63      public ButtonCellRenderer(boolean hideValue) {
64          this(null, hideValue);
65      }
66  
67      /**
68       * <p>Constructor for ButtonCellRenderer.</p>
69       *
70       * @param icon a {@link Icon} object.
71       * @param hideValue a boolean.
72       */
73      public ButtonCellRenderer(Icon icon, boolean hideValue) {
74          this.icon = icon;
75          this.hideValue = hideValue;
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      @SuppressWarnings("unchecked")
81      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
82  
83          JButton button = new JButton();
84  
85          button.setBackground(null);
86          button.setForeground(Color.BLACK);
87          button.setHorizontalAlignment(SwingConstants.CENTER);
88  
89          T object = (T) value;
90  
91          if (hideValue) {
92              button.setText(null);
93          } else {
94              String text = getText(table, object, row, column);
95              button.setText(text != null ? text : value.toString());
96          }
97          Icon icon = getIcon(table, object, row, column);
98          if (icon != null) {
99              button.setIcon(icon);
100         }
101         String toolTipText = getToolTipText(table, object, row, column);
102         if (StringUtils.isNotBlank(toolTipText)) {
103             button.setToolTipText(toolTipText);
104         }
105 
106         boolean editable = table.isCellEditable(row, column);
107         button.setEnabled(editable);
108 
109         if (defaultFont == null) {
110             defaultFont = UIManager.getFont("Table.font");
111             selectedFont = defaultFont.deriveFont(Font.BOLD);
112         }
113 
114         if (isSelected) {
115             button.setFont(selectedFont);
116         } else {
117             button.setFont(defaultFont);
118         }
119 
120         return button;
121     }
122 
123     /**
124      * <p>getText.</p>
125      *
126      * @param table a {@link JTable} object.
127      * @param value a T object.
128      * @param row a int.
129      * @param column a int.
130      * @return a {@link String} object.
131      */
132     protected String getText(JTable table, T value, int row, int column) {
133         return null;
134     }
135 
136     /**
137      * <p>Getter for the field <code>icon</code>.</p>
138      *
139      * @param table a {@link JTable} object.
140      * @param value a T object.
141      * @param row a int.
142      * @param column a int.
143      * @return a {@link Icon} object.
144      */
145     protected Icon getIcon(JTable table, T value, int row, int column) {
146         return this.icon;
147     }
148 
149     /**
150      * <p>getToolTipText.</p>
151      *
152      * @param table a {@link JTable} object.
153      * @param value a T object.
154      * @param row a int.
155      * @param column a int.
156      * @return a {@link String} object.
157      */
158     protected String getToolTipText(JTable table, T value, int row, int column) {
159         return null;
160     }
161 }