source: src/main/java/weka/gui/WekaTaskMonitor.java @ 4

Last change on this file since 4 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 4.9 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 *    WekaTaskMonitor.java
19 *    Copyright (C) 2000 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui;
24
25import java.awt.BorderLayout;
26import java.awt.Dimension;
27import java.awt.Image;
28import java.awt.Toolkit;
29
30import javax.swing.BorderFactory;
31import javax.swing.ImageIcon;
32import javax.swing.JLabel;
33import javax.swing.JPanel;
34import javax.swing.SwingConstants;
35
36/**
37 * This panel records the number of weka tasks running and displays a
38 * simple bird animation while their are active tasks
39 *
40 * @author Mark Hall (mhall@cs.waikato.ac.nz)
41 * @version $Revision: 5198 $
42 */
43public class WekaTaskMonitor
44  extends JPanel
45  implements TaskLogger {
46
47  /** for serialization */
48  private static final long serialVersionUID = 508309816292197578L;
49
50  /** The number of running weka threads */
51  private int m_ActiveTasks = 0;
52
53  /** The label for displaying info */
54  private JLabel m_MonitorLabel;
55
56  /** The icon for the stationary bird */
57  private ImageIcon m_iconStationary;
58
59  /** The icon for the animated bird */
60  private ImageIcon m_iconAnimated;
61
62  /** True if their are active tasks */
63  private boolean m_animating = false;
64 
65  /**
66   * Constructor
67   */
68  public WekaTaskMonitor() {
69    java.net.URL imageURL = 
70      this.getClass().getClassLoader().getResource("weka/gui/weka_stationary.gif");
71
72    if (imageURL != null) {
73      Image pic = Toolkit.getDefaultToolkit().getImage(imageURL);
74      imageURL = 
75        this.getClass().getClassLoader().getResource("weka/gui/weka_animated.gif");
76      Image pic2 = Toolkit.getDefaultToolkit().getImage(imageURL); 
77   
78      /*    Image pic = Toolkit.getDefaultToolkit().
79            getImage(ClassLoader.getSystemResource("weka/gui/weka_stationary.gif"));
80            Image pic2 = Toolkit.getDefaultToolkit().
81            getImage(ClassLoader.getSystemResource("weka/gui/weka_animated.gif")); */
82
83      m_iconStationary = new ImageIcon(pic); 
84      m_iconAnimated = new ImageIcon(pic2);
85    }
86   
87    m_MonitorLabel = new JLabel(" x "+m_ActiveTasks,m_iconStationary,SwingConstants.CENTER);
88    /*
89    setBorder(BorderFactory.createCompoundBorder(
90              BorderFactory.createTitledBorder("Weka Tasks"),
91              BorderFactory.createEmptyBorder(0, 5, 5, 5)
92              ));
93    */
94    setLayout(new BorderLayout());
95    Dimension d = m_MonitorLabel.getPreferredSize();
96    m_MonitorLabel.setPreferredSize(new Dimension(d.width+15,d.height));
97    m_MonitorLabel.setMinimumSize(new Dimension(d.width+15,d.height));
98    add(m_MonitorLabel, BorderLayout.CENTER);
99   
100
101  }
102
103  /**
104   * Tells the panel that a new task has been started
105   */
106  public synchronized void taskStarted() {
107    m_ActiveTasks++;
108    updateMonitor();
109  }
110
111  /**
112   * Tells the panel that a task has completed
113   */
114  public synchronized void taskFinished() {
115    m_ActiveTasks--;
116    if (m_ActiveTasks < 0) {
117      m_ActiveTasks = 0;
118    }
119    updateMonitor();
120  }
121
122  /**
123   * Updates the number of running tasks an the status of the bird
124   * image
125   */
126  private void updateMonitor() {
127    m_MonitorLabel.setText(" x "+m_ActiveTasks);
128    if (m_ActiveTasks > 0 && !m_animating) {
129      m_MonitorLabel.setIcon(m_iconAnimated);
130      m_animating = true;
131    }
132
133    if (m_ActiveTasks == 0 && m_animating) {
134      m_MonitorLabel.setIcon(m_iconStationary);
135      m_animating = false;
136    }
137  }
138
139  /**
140   * Main method for testing this class
141   */
142  public static void main(String [] args) {
143   
144    try {
145      final javax.swing.JFrame jf = new javax.swing.JFrame();
146      jf.getContentPane().setLayout(new BorderLayout());
147      final WekaTaskMonitor tm = new WekaTaskMonitor();
148      tm.setBorder(BorderFactory.createCompoundBorder(
149                   BorderFactory.createTitledBorder("Weka Tasks"),
150                   BorderFactory.createEmptyBorder(0, 5, 5, 5)
151                   ));
152      jf.getContentPane().add(tm, BorderLayout.CENTER);
153      jf.addWindowListener(new java.awt.event.WindowAdapter() {
154        public void windowClosing(java.awt.event.WindowEvent e) {
155          jf.dispose();
156          System.exit(0);
157        }
158      });
159      jf.pack();
160      jf.setVisible(true);
161      tm.taskStarted();
162    } catch (Exception ex) {
163      ex.printStackTrace();
164      System.err.println(ex.getMessage());
165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.