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