1 /* @(#)TCInfo.java	1.2 02/10/24 21:03:24 */
2 package tilecachetool;
3 
4 import java.util.Hashtable;
5 import java.awt.Color;
6 import java.awt.BorderLayout;
7 import java.awt.FlowLayout;
8 import javax.swing.JLabel;
9 import javax.swing.JPanel;
10 import javax.swing.JSlider;
11 import javax.swing.event.ChangeEvent;
12 import javax.swing.event.ChangeListener;
13 import javax.swing.border.BevelBorder;
14 
15 import com.lightcrafts.jai.utils.LCTileCache;
16 
17 /**
18  * <p>Title: Tile Cache Monitoring Tool</p>
19  * <p>Description: Monitors and displays JAI Tile Cache activity.</p>
20  * <p>Copyright: Copyright (c) 2002</p>
21  * <p>    All Rights Reserved</p>
22  * <p>Company: Virtual Visions Software, Inc.</p>
23  *
24  * @author Dennis Sigel
25  * @version 1.01
26  */
27 
28 public final class TCInfo extends JPanel
29                           implements ChangeListener {
30 
31     private JSlider memoryCapacitySlider;
32     private JSlider memoryThresholdSlider;
33     private JLabel  memoryCapacityLabel;
34     private JLabel  memoryThresholdLabel;
35 
36     private LCTileCache cache = null;
37     private Statistics statistics;
38 
39     private float memoryUsage;
40     private float memoryCapacity;
41     private int percentTCM;
42 
43     private static final Color BACKGROUND = new Color(180, 180, 220);
44     private static final Color LIGHT_BLUE = new Color(200, 200, 230);
45     private static final BevelBorder BEVEL_BORDER = new BevelBorder(BevelBorder.LOWERED);
46 
47 
48     /**
49      * Default constructor
50      */
TCInfo()51     public TCInfo() {
52         setLayout( new FlowLayout(FlowLayout.LEFT, 10, 1));
53 
54         // controller for tile cache memory capacity
55         JPanel p1 = new JPanel();
56         p1.setLayout( new BorderLayout() );
57         p1.setBackground(LIGHT_BLUE);
58 
59         // use initial arbitrary values (adjusted later)
60         memoryCapacityLabel = new JLabel("16 MB");
61         memoryCapacityLabel.setBackground(LIGHT_BLUE);
62         memoryCapacityLabel.setForeground(Color.black);
63         p1.add(memoryCapacityLabel, BorderLayout.NORTH);
64 
65         memoryCapacitySlider = new JSlider(JSlider.VERTICAL, 0, 128, 16);
66 
67         memoryCapacitySlider.setBorder(BEVEL_BORDER);
68         memoryCapacitySlider.createStandardLabels(64, 0);
69         memoryCapacitySlider.setBackground(BACKGROUND);
70         memoryCapacitySlider.setForeground(Color.black);
71         memoryCapacitySlider.setPaintLabels(true);
72         memoryCapacitySlider.setPaintTicks(true);
73         memoryCapacitySlider.setMajorTickSpacing(32);
74         memoryCapacitySlider.setMinorTickSpacing(8);
75         memoryCapacitySlider.setSnapToTicks(false);
76         memoryCapacitySlider.addChangeListener(this);
77 
78         p1.add(memoryCapacitySlider);
79         add(p1);
80 
81         // controller for tile cache threshold
82         JPanel p2 = new JPanel();
83         p2.setLayout( new BorderLayout() );
84         p2.setBackground(LIGHT_BLUE);
85 
86         memoryThresholdLabel = new JLabel("75%");
87         memoryThresholdLabel.setBackground(LIGHT_BLUE);
88         memoryThresholdLabel.setForeground(Color.black);
89         p2.add(memoryThresholdLabel, BorderLayout.NORTH);
90 
91         memoryThresholdSlider = new JSlider(JSlider.VERTICAL, 0, 100, 75);
92 
93         memoryThresholdSlider.setBorder(BEVEL_BORDER);
94         Hashtable mt_labels = new Hashtable();
95 
96         for ( int j = 0; j <= 100; j+=10 ) {
97             JLabel label = new JLabel(j + "%");
98             label.setForeground(Color.black);
99             mt_labels.put(new Integer(j), label);
100         }
101 
102         memoryThresholdSlider.setBackground(BACKGROUND);
103         memoryThresholdSlider.setForeground(Color.black);
104         memoryThresholdSlider.setPaintLabels(true);
105         memoryThresholdSlider.setLabelTable(mt_labels);
106         memoryThresholdSlider.setPaintTicks(true);
107         memoryThresholdSlider.setMajorTickSpacing(10);
108         memoryThresholdSlider.setMinorTickSpacing(5);
109         memoryThresholdSlider.setSnapToTicks(true);
110         memoryThresholdSlider.addChangeListener(this);
111 
112         p2.add(memoryThresholdSlider);
113         add(p2);
114     }
115 
116     /**
117      * Set cache to a SunTileCache
118      * @param c is a sun tile cache object
119      * @throws IllegalArgumentException is <code>c</code> is <code>null</code>
120      * @throws IllegalArgumentException if <code>c</code> not an instance of SunTileCache
121      * @since TCT 1.0
122      */
setTileCache(LCTileCache c)123     public void setTileCache(LCTileCache c) {
124         if ( c == null ) {
125             throw new IllegalArgumentException("cache cannot be null.");
126         }
127 
128         if ( c instanceof LCTileCache ) {
129             cache = c;
130         } else {
131             throw new IllegalArgumentException("cache not an instance of SunTileCache");
132         }
133 
134         memoryCapacitySlider.setValue((int)(cache.getMemoryCapacity() / (1024L * 1024L)));
135         memoryThresholdSlider.setValue((int)(100.0F * cache.getMemoryThreshold()));
136     }
137 
138     /**
139      * Provide access to the statistics object
140      * @param stats a Statistics object
141      * @since TCT 1.0
142      */
setStatistics(Statistics stats)143     public void setStatistics(Statistics stats) {
144         statistics = stats;
145     }
146 
147     /**
148      * set memory capacity slider range to proper values.  If the value
149      * is below the current cache capacity, max_mem will be set to the
150      * current cache capacity.
151      *
152      * @param max_mem maximum slider value for cache memory capacity
153      * @throws IllegalArgumentException if <code>max_mem</code> is .le. 0
154      * @since TCT 1.0
155      */
setMemoryCapacitySliderMaximum(int max_mem)156      public void setMemoryCapacitySliderMaximum(int max_mem) {
157          if ( max_mem <= 0L ) {
158              throw new IllegalArgumentException("max_mem must be greater than 0");
159          }
160 
161          int mem_cap = (int) (cache.getMemoryCapacity() / (1024L * 1024L));
162 
163          if ( max_mem < mem_cap ) {
164              max_mem = mem_cap;
165          }
166 
167          memoryCapacitySlider.setMaximum((int) max_mem);
168      }
169 
170     /**
171      * Responce to slider changes
172      */
stateChanged(ChangeEvent e)173     public void stateChanged(ChangeEvent e) {
174         JSlider slider = (JSlider) e.getSource();
175 
176         if ( slider == memoryCapacitySlider ) {
177             memoryCapacityLabel.setText( slider.getValue() + " MB" );
178             cache.setMemoryCapacity( (long) slider.getValue() * 1024L * 1024L );
179 
180             if ( statistics != null ) {
181                 memoryCapacity = (float) cache.getMemoryCapacity();
182                 memoryUsage    = (float) cache.getCacheMemoryUsed();
183                 percentTCM = (int)((100.0F * memoryUsage / memoryCapacity) + 0.5F);
184                 statistics.set(percentTCM);
185             }
186         } else if ( slider == memoryThresholdSlider ) {
187             memoryThresholdLabel.setText( slider.getValue() + "%" );
188             cache.setMemoryThreshold( (float)slider.getValue() / 100.0F );
189         }
190     }
191 }
192 
193