Typo: inspect_jpegs, not inspect-jpegs in form
[privoxy.git] / src / java / org / privoxy / activityconsole / SortButtonRenderer.java
1 /*********************************************************************
2  *
3  * File        :  $Source$
4  *
5  * Purpose     :  Swing details of rendering a column header as a button.
6  *
7  * Copyright   :  Written by and Copyright (C) 2003 the SourceForge
8  *                Privoxy team. http://www.privoxy.org/
9  *
10  *                Based on the Internet Junkbuster originally written
11  *                by and Copyright (C) 1997 Anonymous Coders and
12  *                Junkbusters Corporation.  http://www.junkbusters.com
13  *
14  *                This program is free software; you can redistribute it
15  *                and/or modify it under the terms of the GNU General
16  *                Public License as published by the Free Software
17  *                Foundation; either version 2 of the License, or (at
18  *                your option) any later version.
19  *
20  *                This program is distributed in the hope that it will
21  *                be useful, but WITHOUT ANY WARRANTY; without even the
22  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
23  *                PARTICULAR PURPOSE.  See the GNU General Public
24  *                License for more details.
25  *
26  *                The GNU General Public License should be included with
27  *                this file.  If not, you can view it at
28  *                http://www.gnu.org/copyleft/gpl.html
29  *                or write to the Free Software Foundation, Inc., 59
30  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
31  *
32  * Revisions   :
33  *    $Log$
34  *********************************************************************/
35
36 package org.privoxy.activityconsole;
37
38 import java.util.*;
39 import java.awt.*;
40 import javax.swing.*;
41 import javax.swing.table.*;
42
43 /** 
44  * Swing details of rendering a column header as a button.
45  * @author Last Modified By: $Author$
46  * @version $Rev$-$Date$$State$
47  */
48 public class SortButtonRenderer extends JButton implements TableCellRenderer
49 {
50
51   private static final String
52   COPYRIGHT = org.privoxy.activityconsole.Copyright.COPYRIGHT;
53
54   public static final int NONE = 0;
55   public static final int DOWN = 1;
56   public static final int UP   = 2;
57
58   int pushedColumn;
59   Hashtable state;
60   JButton downButton,upButton;
61
62   public SortButtonRenderer()
63   {
64     pushedColumn   = -1;
65     state = new Hashtable();
66
67     setMargin(new Insets(0,0,0,0));
68     setHorizontalTextPosition(LEFT);
69     setIcon(new BlankIcon());
70
71     downButton = new JButton();
72     downButton.setMargin(new Insets(0,0,0,0));
73     downButton.setHorizontalTextPosition(LEFT);
74     downButton.setIcon(new BevelArrowIcon(BevelArrowIcon.DOWN, false, false));
75     downButton.setPressedIcon(new BevelArrowIcon(BevelArrowIcon.DOWN, false, true));
76
77     upButton = new JButton();
78     upButton.setMargin(new Insets(0,0,0,0));
79     upButton.setHorizontalTextPosition(LEFT);
80     upButton.setIcon(new BevelArrowIcon(BevelArrowIcon.UP, false, false));
81     upButton.setPressedIcon(new BevelArrowIcon(BevelArrowIcon.UP, false, true));
82
83   }
84
85   public Component getTableCellRendererComponent(JTable table, Object value,
86                                                  boolean isSelected, boolean hasFocus, int row, int column)
87   {
88     JButton button = this;
89     Object obj = state.get(new Integer(column));
90     if (obj != null)
91     {
92       if (((Integer)obj).intValue() == DOWN)
93       {
94         button = downButton;
95       }
96       else
97       {
98         button = upButton;
99       }
100     }
101     button.setText((value ==null) ? "" : value.toString());
102     boolean isPressed = (column == pushedColumn);
103     button.getModel().setPressed(isPressed);
104     button.getModel().setArmed(isPressed);
105     return button;
106   }
107
108   public void setPressedColumn(int col)
109   {
110     pushedColumn = col;
111   }
112
113   public void setSelectedColumn(int col)
114   {
115     if (col < 0) return;
116     Integer value = null;
117     Object obj = state.get(new Integer(col));
118     if (obj == null)
119     {
120       value = new Integer(DOWN);
121     }
122     else
123     {
124       if (((Integer)obj).intValue() == DOWN)
125       {
126         value = new Integer(UP);
127       }
128       else
129       {
130         value = new Integer(DOWN);
131       }
132     }
133     state.clear();
134     state.put(new Integer(col), value);
135   } 
136
137   public int getState(int col)
138   {
139     int retValue;
140     Object obj = state.get(new Integer(col));
141     if (obj == null)
142     {
143       retValue = NONE;
144     }
145     else
146     {
147       if (((Integer)obj).intValue() == DOWN)
148       {
149         retValue = DOWN;
150       }
151       else
152       {
153         retValue = UP;
154       }
155     }
156     return retValue;
157   }
158 }