View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.table;
21  
22  import java.awt.Color;
23  import java.awt.Component;
24  import java.awt.Graphics;
25  import java.util.Hashtable;
26  
27  import javax.swing.Icon;
28  import javax.swing.JLabel;
29  import javax.swing.JTable;
30  import javax.swing.SwingUtilities;
31  import javax.swing.UIManager;
32  import javax.swing.table.TableCellRenderer;
33  
34  /***
35   * Don't use this class directly. Use 
36   * <code>TableHeaderListener.install()</code> instead.
37   *
38   * <p>If an instance of this class is set as the header renderer for a table, 
39   * 
40   *
41   * @see xnap.gui.table.TableHeaderListener
42   */
43  public class SortButtonRenderer implements TableCellRenderer
44  {
45  
46      public static final Icon downIcon 
47  	= new BevelArrowIcon(BevelArrowIcon.DOWN, false);
48      public static final Icon upIcon 
49  	= new BevelArrowIcon(BevelArrowIcon.UP, false);
50  
51      //--- Data Field(s) ---
52  
53      private Hashtable icons = new Hashtable();
54      private TableCellRenderer renderer;
55      private JLabel label;
56      private int selectedColumn = -1;
57  
58      //--- Constructor(s) ---
59      
60      /***
61       * Constructs a SortButtonRenderer. The <code>renderer</code> is used
62       * to renderer the header. This needs to be an instance of 
63       * {@link javax.swing.JLabel JLabel} for the sort arrow to be displayed.
64       *
65       * <p>This implementation uses the setIcon() method of JLabel to display 
66       * the arrow icon. Make sure you do not use the icon of the renderer.
67       *
68       * @param renderer the default renderer
69       */
70      public SortButtonRenderer(TableCellRenderer renderer)
71      {
72  	this.renderer = renderer;
73  	if (renderer instanceof JLabel) {
74  	    label = (JLabel)renderer;
75  	    label.setHorizontalTextPosition(SwingUtilities.LEFT);
76  	}
77  	else {
78  	    // avoid null pointer exception if cr is not instanceof JLabel
79  	    label = new JLabel();
80  	}
81      }
82  
83      //--- Method(s) ---
84   
85      public Component getTableCellRendererComponent(JTable table, Object value,
86                                           boolean isSelected, boolean hasFocus,
87                                           int row, int column) 
88      {
89  	// set the 
90  	label.setIcon(getIcon(column));
91  
92  	Component c = renderer.getTableCellRendererComponent
93  	    (table, value, isSelected, hasFocus, row, column);
94  
95  	if (column == selectedColumn) {
96  	    label.setBackground(UIManager.getColor("controlShadow"));
97  	}
98  
99          return c;
100     }
101 
102     public void selectColumn(int col)
103     {
104 	selectedColumn = col;
105     }
106 
107     /***
108      * Prints the arrow icon to the column at index <code>col</code>.
109      */
110     public void setSortedColumn(int col, boolean ascending) 
111     {
112         icons.clear();
113 	if (ascending) {
114 	    icons.put(new Integer(col), downIcon);
115 	}
116 	else {
117 	    icons.put(new Integer(col), upIcon);
118 	}
119     }
120 
121     /***
122      * Returns the icon of col.  
123      */
124     public Icon getIcon(int col) 
125     {
126         return (Icon)icons.get(new Integer(col));
127     }
128 }
129 
130 /***
131  * Provides a small arrow shaped icon.
132  */
133 class BevelArrowIcon implements Icon {
134     
135     public static final int UP    = 0;
136     public static final int DOWN  = 1;
137     
138     private Color edge1;
139     private Color edge2;
140     private int direction;
141 
142     public BevelArrowIcon(int direction, boolean isPressedView) 
143     {
144 	this.direction = direction;
145 
146 	if (isPressedView) {
147 	    edge1 = UIManager.getColor("controlDkShadow");
148 	    edge2 = UIManager.getColor("controlLtHighlight");
149 	} 
150 	else {
151 	    edge1 = UIManager.getColor("controlShadow");
152 	    edge2 = UIManager.getColor("controlHighlight");
153 	}
154     }
155 
156     public int getIconHeight() 
157     {
158 	return 7;
159     }
160     
161     public int getIconWidth() 
162     {
163 	return 8;
164     }
165 		
166     public void paintIcon(Component c, Graphics g, int x, int y) 
167     {
168 	switch (direction) {
169 	case DOWN:
170 	    g.setColor(edge2);
171 	    g.drawLine(x + 5, y + 7, x + 8, y);
172 	    g.setColor(edge1);
173 	    g.drawLine(x, y, x + 8, y);
174 	    g.drawLine(x, y, x + 4, y + 7);
175 	    break;
176 	case UP:
177 	    g.setColor(edge1);
178 	    g.drawLine(x, y + 6, x + 4, y);
179 	    g.setColor(edge2);
180 	    g.drawLine(x, y + 7, x + 8, y + 7);
181 	    g.drawLine(x + 5, y, x + 8, y + 7);
182 	    break;
183 	}
184     }	
185 
186 }
187