View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.rt.swingmonitor.ui;
22  
23  import net.sf.jour.util.*;
24  import javax.swing.JLabel;
25  import javax.swing.JPanel;
26  import javax.swing.JProgressBar;
27  import javax.swing.border.BevelBorder;
28  
29  import java.awt.BorderLayout;
30  import java.awt.Dimension;
31  
32  import net.sf.jour.rt.agent.EventProcessor;
33  
34  import org.apache.log4j.Logger;
35  
36  /***
37   * Provide SystemInfo information.
38   *
39   * Created on 05.12.2004
40   * Contributing Author(s):
41   *
42   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
43   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
44   *
45   * @author vlads
46   * @version $Revision: 1.5 $ ($Author: vlads $)  $Date: 2004/12/16 05:35:52 $
47   */
48  public class StatusBar extends JPanel implements Runnable
49  {
50  
51      private static final Logger log = Logger.getLogger(StatusBar.class);
52  
53      private JLabel status = new JLabel();
54  
55      private JProgressBar progressBar = new JProgressBar();
56  
57      /***
58       * Constructor StatusBar
59       *  
60       */
61      public StatusBar() {
62  
63          setLayout(new BorderLayout());
64          status.setBorder(new BevelBorder(BevelBorder.LOWERED));
65          add(status, BorderLayout.CENTER);
66          progressBar.setMaximumSize(new Dimension(150, 20));
67          progressBar.setBorder(new BevelBorder(BevelBorder.LOWERED));
68          add(progressBar, BorderLayout.EAST);
69          
70          new Thread(this).start();
71      }
72  
73     /***
74       * Method getProgressBar
75       */
76      public JProgressBar getProgressBar() {
77          return progressBar;
78      }
79  
80      /***
81       * Method setText
82       */
83      public void setStatusText(String text) {
84          status.setText("  " + text);
85      }
86      
87      long countTotal;
88      long countAcepted;
89      long noChangeCount;
90      long lastEvent;
91      
92      public void run() {
93          while (true) {
94              try {
95                  EventProcessor p = EventProcessor.getInstance();
96                  
97                  boolean changeDetected = false;
98                  
99                  long delta = 0;
100                 if ((countTotal != p.getCountTotal()) ||
101                      (countAcepted != p.getCountAcepted())) {
102                     changeDetected = true;
103                     if (lastEvent != 0) {
104                         delta = p.getCountTotal() - countTotal;
105                         long time = System.currentTimeMillis() - lastEvent;
106                         if (time != 0) {
107                             delta = (1000 * delta) / time;
108                         }
109                     }
110                     countTotal = p.getCountTotal();
111                     countAcepted = p.getCountAcepted();
112                     noChangeCount = 0;
113                     lastEvent = System.currentTimeMillis();
114                 } else {
115                     noChangeCount ++;
116                     if (noChangeCount < 5) {
117                         changeDetected = true;
118                     }
119                 }
120                 
121                 if (changeDetected) {
122                     StringBuffer msg = new StringBuffer();
123                     if (noChangeCount > 3) {
124                         msg.append("Processed ");
125                     } else {
126                         msg.append("Processing ");
127                         if (delta > 0) {
128                             msg.append(delta).append(" events/sec ");
129                         }
130                     }
131                     msg.append("total ").append(countTotal);
132                     msg.append(", acepted for staistic ");
133                     if (countTotal != 0) {
134                         msg.append(" ").append(StringUtil.percent(countAcepted, countTotal));
135                         msg.append("%");
136                     }
137                     msg.append(" ").append(countAcepted);
138 
139                     setStatusText(msg.toString());
140 
141                     Thread.sleep(1000);
142                 } else {
143                     Thread.sleep(3000);
144                 }
145             } catch (Exception e) {
146                 log.error("Thread error", e);
147             }
148         }
149     }
150 }