View Javadoc

1   package com.explosion.datastream.exql.gui.table;
2   
3   import java.awt.Component;
4   import java.awt.Font;
5   
6   import javax.swing.BorderFactory;
7   import javax.swing.Icon;
8   import javax.swing.JLabel;
9   import javax.swing.JTable;
10  import javax.swing.UIManager;
11  import javax.swing.border.Border;
12  import javax.swing.table.JTableHeader;
13  import javax.swing.table.TableCellRenderer;
14  
15  import org.apache.log4j.LogManager;
16  import org.apache.log4j.Logger;
17  
18  import com.explosion.expfmodules.rdbmsconn.dbom.DBEntity;
19  import com.explosion.expfmodules.rdbmsconn.dbom.DBEntityBuffer;
20  import com.explosion.utilities.exception.ExceptionManagerFactory;
21  
22  /*
23   * =============================================================================
24   * 
25   * Copyright 2004 Stephen Cowx
26   * 
27   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
28   * use this file except in compliance with the License. You may obtain a copy of
29   * the License at
30   * 
31   * http://www.apache.org/licenses/LICENSE-2.0
32   * 
33   * Unless required by applicable law or agreed to in writing, software
34   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
35   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
36   * License for the specific language governing permissions and limitations under
37   * the License.
38   * 
39   * =============================================================================
40   */
41  
42  /***
43   * @author Stephen Created on Apr 20, 2004
44   */
45  public class Sort_HeaderRenderer extends JLabel implements TableCellRenderer
46  {
47      private static final int NOT_SORTED = 0;
48      private static final int ASCENDING = 1;
49      private static final int DESCENDING = -1;
50      private DBEntity entity = null;
51      private DBEntityBuffer buffer;
52      private static Logger log = LogManager.getLogger(Sort_HeaderRenderer.class);
53  
54      private JTable table;
55      private Border selectedBorder;
56      private Border normalBorder;
57      private Font selectedFont;
58      private Font normalFont;
59  
60      public Sort_HeaderRenderer(JTable table)
61      {
62          this.table = table;
63          normalBorder = UIManager.getBorder("TableHeader.cellBorder");
64          selectedBorder = BorderFactory.createRaisedBevelBorder();
65          JTableHeader header = table.getTableHeader();
66          normalFont = header.getFont();
67          selectedFont = normalFont.deriveFont(normalFont.getStyle() | Font.BOLD);
68          setForeground(header.getForeground());
69          setBackground(header.getBackground());
70          setOpaque(true);
71      }
72  
73      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
74      {
75  
76          setFont(normalFont);
77          setBorder(normalBorder);
78          String label = value == null ? "" : value.toString();
79          setText(label);
80  
81          setHorizontalTextPosition(JLabel.LEFT);
82          
83          boolean pk = false;
84          if (entity != null && entity.getColumn(label) != null)
85              pk = entity.getColumn(label).isPrimaryKey();
86          //log.debug("Setting icon on " + this.hashCode());
87          this.setIcon(getHeaderRendererIcon(label, getFont().getSize() + 10, pk));
88          
89          return this; 
90      }
91  
92      /***
93       * Returns the icon to use
94       * 
95       * @param columnName
96       * @param size
97       * @return
98       */
99      private Icon getHeaderRendererIcon(String columnName, int size, boolean isKey)
100     {
101 
102         try
103         {
104             int sortDirection = buffer.getSortDirection(columnName);
105             int sortIndex = buffer.getSortIndex(columnName);
106             if (sortDirection == NOT_SORTED)
107             {
108                 return null;
109             }
110 
111             return new Arrow(columnName, sortDirection == DESCENDING, size, sortIndex);
112         }
113         catch (Exception e)
114         {
115             ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while initialising table cell renderer.");
116             return null;
117         }
118     }
119 
120     /***
121      * @return Returns the entity.
122      */
123     public DBEntity getEntity()
124     {
125         return entity;
126     }
127 
128     /***
129      * @param entity The entity to set.
130      */
131     public void setEntity(DBEntity entity)
132     {
133         this.entity = entity;
134         this.buffer = entity.getDBEntityBuffer();
135     }
136 }
137