View Javadoc
1   package fr.ifremer.quadrige2.ui.swing.common.table;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 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.jdesktop.swingx.JXTableHeader;
28  
29  import javax.swing.JTextPane;
30  import javax.swing.SwingConstants;
31  import javax.swing.UIManager;
32  import javax.swing.table.DefaultTableCellRenderer;
33  import javax.swing.table.TableCellRenderer;
34  import javax.swing.table.TableColumnModel;
35  import javax.swing.text.html.HTMLEditorKit;
36  import java.awt.Color;
37  import java.awt.Dimension;
38  
39  /**
40   * An implementation of JXTableHeader that add some enhancements:
41   * <ul><li>allow multiline header text (html render) and auto resize</li>
42   * <li>force header text on top</li></ul>
43   * Created by Ludovic on 13/05/2015.
44   */
45  public class SwingTableHeader extends JXTableHeader {
46  
47      /**
48       * <p>Constructor for SwingTableHeader.</p>
49       *
50       * @param columnModel a {@link TableColumnModel} object.
51       */
52      public SwingTableHeader(TableColumnModel columnModel) {
53          super(columnModel);
54      }
55  
56      /**
57       * {@inheritDoc}
58       *
59       * the preferred size (specially the height) is calculated by render the header value in a JTextPane
60       * in order to fit the content in the column width
61       */
62      @Override
63      protected Dimension getPreferredSize(Dimension pref) {
64          int height = pref.height;
65          for (int i = 0; i < getColumnModel().getColumnCount(); i++) {
66  
67              // calculate text height
68              JTextPane pane = new JTextPane();
69              pane.setEditorKit(new HTMLEditorKit());
70              pane.setSize(new Dimension(getColumnModel().getColumn(i).getWidth(), 1000));
71              pane.setOpaque(true);
72              pane.setFont(table.getFont());
73              pane.setText((String) getColumnModel().getColumn(i).getHeaderValue());
74              height = Math.max(height, pane.getPreferredSize().height);
75          }
76          pref.height = height;
77          return pref;
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public TableCellRenderer getDefaultRenderer() {
83          DefaultTableCellRenderer defaultRenderer = (DefaultTableCellRenderer) super.getDefaultRenderer();
84          defaultRenderer.setVerticalAlignment(SwingConstants.TOP);
85          defaultRenderer.setHorizontalAlignment(SwingConstants.CENTER);
86          return defaultRenderer;
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      public Color getForeground() {
92  
93          Color color = UIManager.getColor("thematicLabelColor");
94          return color != null ? color : super.getForeground();
95      }
96  }