1/*
2 * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25
26package com.sun.java.swing.ui;
27
28import java.awt.*;
29import java.awt.event.*;
30import javax.swing.*;
31
32public class StatusBar extends JPanel
33    implements ActionListener, MouseListener
34{
35
36    public StatusBar()
37    {
38        setLayout(new FlowLayout(0));
39        setBorder(BorderFactory.createEtchedBorder());
40        progressBar = new JProgressBar(0, 0, 100);
41        progressBar.setPreferredSize(new Dimension(60, progressBar.getPreferredSize().height + 2));
42        progressBar.setVisible(false);
43        label = new JLabel("                                                                                        ");
44        preferredSize = new Dimension(getWidth(label.getText()), 2 * getFontHeight());
45        add(progressBar);
46        add(label);
47    }
48
49    public static StatusBar getInstance()
50    {
51        if(statusBar == null)
52            statusBar = new StatusBar();
53        return statusBar;
54    }
55
56    public static void setInstance(StatusBar sb)
57    {
58        statusBar = sb;
59    }
60
61    protected int getWidth(String s)
62    {
63        FontMetrics fm = getFontMetrics(getFont());
64        if(fm == null)
65            return 0;
66        else
67            return fm.stringWidth(s);
68    }
69
70    protected int getFontHeight()
71    {
72        FontMetrics fm = getFontMetrics(getFont());
73        if(fm == null)
74            return 0;
75        else
76            return fm.getHeight();
77    }
78
79    public Dimension getPreferredSize()
80    {
81        return preferredSize;
82    }
83
84    public void setMessage(String message)
85    {
86        label.setText(message);
87        label.repaint();
88    }
89
90    public void startBusyBar()
91    {
92        forward = true;
93        if(timer == null)
94        {
95            setMessage("");
96            progressBar.setVisible(true);
97            timer = new Timer(15, this);
98            timer.start();
99        }
100    }
101
102    public void stopBusyBar()
103    {
104        if(timer != null)
105        {
106            timer.stop();
107            timer = null;
108        }
109        setMessage("");
110        progressBar.setVisible(false);
111        progressBar.setValue(0);
112    }
113
114    public void actionPerformed(ActionEvent evt)
115    {
116        int value = progressBar.getValue();
117        if(forward)
118        {
119            if(value < 100)
120            {
121                progressBar.setValue(value + 1);
122            } else
123            {
124                forward = false;
125                progressBar.setValue(value - 1);
126            }
127        } else
128        if(value > 0)
129        {
130            progressBar.setValue(value - 1);
131        } else
132        {
133            forward = true;
134            progressBar.setValue(value + 1);
135        }
136    }
137
138    public void mouseClicked(MouseEvent mouseevent)
139    {
140    }
141
142    public void mousePressed(MouseEvent mouseevent)
143    {
144    }
145
146    public void mouseReleased(MouseEvent mouseevent)
147    {
148    }
149
150    public void mouseExited(MouseEvent evt)
151    {
152        setMessage("");
153    }
154
155    public void mouseEntered(MouseEvent evt)
156    {
157        if(evt.getSource() instanceof AbstractButton)
158        {
159            AbstractButton button = (AbstractButton)evt.getSource();
160            Action action = button.getAction();
161            if(action != null)
162            {
163                String message = (String)action.getValue("LongDescription");
164                setMessage(message);
165            }
166        }
167    }
168
169    private static final int PROGRESS_MAX = 100;
170    private static final int PROGRESS_MIN = 0;
171    private JLabel label;
172    private Dimension preferredSize;
173    private JProgressBar progressBar;
174    private Timer timer;
175    private boolean forward;
176    private static StatusBar statusBar;
177
178}
179