Typo: inspect_jpegs, not inspect-jpegs in form
[privoxy.git] / src / java / org / privoxy / activityconsole / StatWidget.java
1 /*********************************************************************
2  *
3  * File        :  $Source$
4  *
5  * Purpose     :  A graphical element (specifically, a JPanel) that 
6  *                displays a little box of color and a number.  When
7  *                tweaked with a new number, the color changes for a
8  *                set period of time, then resets to the previous 
9  *                color.
10  *
11  * Copyright   :  Written by and Copyright (C) 2003 the SourceForge
12  *                Privoxy team. http://www.privoxy.org/
13  *
14  *                Based on the Internet Junkbuster originally written
15  *                by and Copyright (C) 1997 Anonymous Coders and
16  *                Junkbusters Corporation.  http://www.junkbusters.com
17  *
18  *                This program is free software; you can redistribute it
19  *                and/or modify it under the terms of the GNU General
20  *                Public License as published by the Free Software
21  *                Foundation; either version 2 of the License, or (at
22  *                your option) any later version.
23  *
24  *                This program is distributed in the hope that it will
25  *                be useful, but WITHOUT ANY WARRANTY; without even the
26  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
27  *                PARTICULAR PURPOSE.  See the GNU General Public
28  *                License for more details.
29  *
30  *                The GNU General Public License should be included with
31  *                this file.  If not, you can view it at
32  *                http://www.gnu.org/copyleft/gpl.html
33  *                or write to the Free Software Foundation, Inc., 59
34  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
35  *
36  * Revisions   :
37  *    $Log$
38  *********************************************************************/
39
40 package org.privoxy.activityconsole;
41
42 import java.awt.*;
43 import javax.swing.*;
44 import javax.swing.table.*;
45
46 /** 
47  * A graphical element that displays a little box of color and a number.
48  * @author Last Modified By: $Author$
49  * @version $Rev$-$Date$$State$
50  */
51 public class StatWidget extends JPanel implements Comparable
52 {
53   private static final String
54     COPYRIGHT = org.privoxy.activityconsole.Copyright.COPYRIGHT;
55
56   private JPanel _statusIcon;
57   private JLabel _valueLabel;
58   private int _status;
59   private int _value;
60   private int _row, _col;
61   private JTable _table = null;
62   private StatWidgetThread _thread;
63   Color activityColor = Color.yellow;
64   Color inactivityColor = getBackground().darker();
65
66   public StatWidget(int initialValue, int statusDurationMillis)
67   {
68     _value = initialValue;
69     _statusIcon = new JPanel();
70     _statusIcon.setBackground(inactivityColor);
71     _valueLabel = new JLabel(new Integer(initialValue).toString());
72     this.setLayout(new GridBagLayout());
73     ActivityConsoleGuiUtil.constrain(this, _statusIcon,
74                               1, 1, // X, Y Coordinates
75                               1, 1, // Grid width, height
76                               GridBagConstraints.NONE,  // Fill value
77                               GridBagConstraints.WEST,  // Anchor value
78                               0.0,0.0,  // Weight X, Y
79                               0, 1, 0, 1 ); // Top, left, bottom, right insets
80     ActivityConsoleGuiUtil.constrain(this, _valueLabel,
81                               2, 1, // X, Y Coordinates
82                               1, 1, // Grid width, height
83                               GridBagConstraints.HORIZONTAL,  // Fill value
84                               GridBagConstraints.WEST,  // Anchor value
85                               1.0,0.0,  // Weight X, Y
86                               0, 1, 0, 1 ); // Top, left, bottom, right insets
87     _thread = new StatWidgetThread(this, statusDurationMillis);
88     _thread.start();
89   }
90
91   public void updateValue(int newValue)
92   {
93     if (_value != newValue)
94     {
95       _value = newValue;
96       _valueLabel.setText(new Integer(newValue).toString());
97       _statusIcon.setBackground(activityColor);
98       if (_table != null)
99         ((AbstractTableModel)_table.getModel()).fireTableCellUpdated(_row,_table.convertColumnIndexToModel(_col));
100       _thread.interrupt();
101     }
102   }
103
104   public void setRowColTable(int row, int col, JTable table)
105   {
106     _row = row;
107     _col = col;
108     _table = table;
109   }
110
111   public int getRow()
112   {
113     return _row;
114   }
115
116   public int getCol()
117   {
118     return _col;
119   }
120
121   public int getValue()
122   {
123     return _value;
124   }
125
126   public void timerPop()
127   {
128     _statusIcon.setBackground(inactivityColor);
129     if (_table != null)
130       ((AbstractTableModel)_table.getModel()).fireTableCellUpdated(_row,_table.convertColumnIndexToModel(_col));
131   }
132
133   public int compareTo(Object compare)
134   {
135     int compareValue = ((StatWidget)compare).getValue();
136     if (_value < compareValue)
137       return 1;
138     else if (_value == compareValue)
139       return 0;
140     else
141       return -1;
142   }
143 }