View Javadoc

1   /*
2    * $HeadURL: $
3    * $Date: $
4    * $Revision: $
5    * $Author: $
6    * 
7    * Copyright (c) 2005 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  package com.mindtree.techworks.insight.gui.renderers;
25  
26  import java.awt.Component;
27  import java.awt.Color;
28  import java.awt.Graphics;
29  
30  import javax.swing.Icon;
31  import javax.swing.JLabel;
32  import javax.swing.JTable;
33  import javax.swing.table.TableCellRenderer;
34  
35  import com.mindtree.techworks.insight.model.LogEventTableModel;
36  import com.mindtree.techworks.insight.model.ModelDataSorter;
37  
38  
39  /**
40  *
41  * The <code>SortableHeaderRenderer</code> class renders a sortable header with
42  * appropriate arrow indications to indicate the order of sort, if any
43  *
44  * @author  Regunath B
45  * @version 1.0, 04/11/13
46  */
47  public class SortableHeaderRenderer implements TableCellRenderer {
48  
49  	/*
50  	 * The default TableCellRenderer for the JTable
51  	 */
52  	private TableCellRenderer tableCellRenderer;
53  
54      public SortableHeaderRenderer(TableCellRenderer tableCellRenderer) {
55          this.tableCellRenderer = tableCellRenderer;
56      }
57  
58      /**
59       * TableCellRenderer interface implementation
60       * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
61       */
62      public Component getTableCellRendererComponent(JTable table, 
63                                                     Object value,
64                                                     boolean isSelected, 
65                                                     boolean hasFocus,
66                                                     int row, 
67                                                     int column) {
68          Component component = tableCellRenderer.getTableCellRendererComponent(table, 
69                  value, isSelected, hasFocus, row, column);
70          if (component instanceof JLabel) {
71              JLabel label = (JLabel)component;
72              label.setHorizontalTextPosition(JLabel.LEFT);
73              int modelColumn = table.convertColumnIndexToModel(column);
74              label.setIcon(getHeaderRendererIcon(table, modelColumn, label.getFont().getSize()));
75          }
76          return component;
77      }
78      
79      /**
80       * Private helper method to get the Icon to be rendered in the header of the
81       * JTable 
82       */
83      private Icon getHeaderRendererIcon(JTable table, int column, int size) {
84      	LogEventTableModel tableModel = (LogEventTableModel)table.getModel();
85      	int sortOrder = tableModel.getSortOrder(column);
86          return (sortOrder == ModelDataSorter.NOTSORTED ? null : new Arrow(sortOrder == ModelDataSorter.DESCENDING, size));
87      }   
88  
89      /**
90       * Icon implementation that displays an arrow in the table header.
91       * The direction of the arrow indicates the sort order 
92       */
93      private static class Arrow implements Icon {
94          private boolean descending;
95          private int size;
96  //        private int priority;
97  
98          /**
99           * Cosbtructor for this class
100          * @param descending indicator that indicates a descending sort if true is specified
101          * @param size size of the Arrow - width and height are the same
102          */
103         public Arrow(boolean descending, int size) {
104             this.descending = descending;
105             this.size = size;
106         }
107 
108         /**
109          * Overriden superclass method
110          * @see javax.swing.Icon#paintIcon(java.awt.Component, java.awt.Graphics, int, int)
111          */
112         public void paintIcon(Component c, Graphics g, int x, int y) {
113             Color color = c == null ? Color.GRAY : c.getBackground();             
114             int dx = (int)(size/2);
115             int dy = descending ? dx : -dx;
116             // Align icon (roughly) with font baseline. 
117             y = y + 5*size/6 + (descending ? -dy : 0);
118             int shift = descending ? 1 : -1;
119             g.translate(x, y);
120 
121             // Right diagonal. 
122             g.setColor(color.darker());
123             g.drawLine(dx / 2, dy, 0, 0);
124             g.drawLine(dx / 2, dy + shift, 0, shift);
125             
126             // Left diagonal. 
127             g.setColor(color.brighter());
128             g.drawLine(dx / 2, dy, dx, 0);
129             g.drawLine(dx / 2, dy + shift, dx, shift);
130             
131             // Horizontal line. 
132             if (descending) {
133                 g.setColor(color.darker().darker());
134             } else {
135                 g.setColor(color.brighter().brighter());
136             }
137             g.drawLine(dx, 0, 0, 0);
138 
139             g.setColor(color);
140             g.translate(-x, -y);
141         }
142         
143         /**
144          * Icon interface method implementation
145          * @see javax.swing.Icon#getIconWidth()
146          */
147         public int getIconWidth() {
148             return size;
149         }
150 
151         /**
152          * Icon interface method implementation
153          * @see javax.swing.Icon#getIconHeight()
154          */
155         public int getIconHeight() {
156             return size;
157         }
158         
159     }
160 	
161 }