KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > designer > swing > status > StatusDisplay


1 package com.opensymphony.workflow.designer.swing.status;
2
3 import java.awt.*;
4 import java.awt.event.ActionEvent JavaDoc;
5 import java.awt.event.ActionListener JavaDoc;
6 import javax.swing.*;
7 import javax.swing.border.EmptyBorder JavaDoc;
8
9 import com.opensymphony.workflow.designer.ResourceManager;
10
11 /**
12  * @author Hani Suleiman (hani@formicary.net)
13  * Date: Dec 24, 2003
14  * Time: 5:05:32 PM
15  */

16 public class StatusDisplay extends DisplayItem
17 {
18   private JLabel status;
19   private JLabel progressStatus;
20   private JProgressBar progress;
21   private JButton cancel;
22   private CancelListener cancelListener;
23
24   public static interface CancelListener
25   {
26     public void cancelPerformed();
27   }
28
29   public StatusDisplay()
30   {
31     setLayout(new CardLayout());
32     add(createStatus(), "Status");
33     add(createProgressBar(), "Progress");
34     showStatus();
35   }
36
37   public void showStatus()
38   {
39     ((CardLayout)getLayout()).show(this, "Status");
40   }
41
42   public void showProgress()
43   {
44     ((CardLayout)getLayout()).show(this, "Progress");
45   }
46
47   private Component createProgressBar()
48   {
49     JPanel mainPanel = new JPanel();
50     mainPanel.setOpaque(false);
51     cancelListener = null;
52     mainPanel.setLayout(new BorderLayout(3, 0));
53     progressStatus = new JLabel("");
54     progressStatus.setVerticalAlignment(1);
55     progressStatus.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 4));
56     progress = new JProgressBar();
57     progress.setMaximum(100);
58     cancel = new JButton(ResourceManager.getString("cancel"));
59     cancel.setOpaque(false);
60     cancel.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 4));
61     cancel.addActionListener(new ActionListener JavaDoc()
62     {
63       public void actionPerformed(ActionEvent JavaDoc actionevent)
64       {
65         if(StatusDisplay.this.cancelListener != null)
66           StatusDisplay.this.cancelListener.cancelPerformed();
67       }
68     });
69     cancel.setRequestFocusEnabled(false);
70     cancel.setFocusable(false);
71     JPanel barPanel = new JPanel(new BorderLayout(0, 0));
72     barPanel.setOpaque(false);
73     barPanel.setBorder(new EmptyBorder JavaDoc(3, 3, 3, 3));
74     barPanel.add(progress, BorderLayout.CENTER);
75     JPanel progressPanel = new JPanel(new BorderLayout(3, 0));
76     progressPanel.setOpaque(false);
77     progressPanel.add(progressStatus, BorderLayout.LINE_START);
78     progressPanel.add(barPanel, BorderLayout.CENTER);
79     mainPanel.add(progressPanel, BorderLayout.CENTER);
80     mainPanel.add(cancel, BorderLayout.LINE_END);
81     setCancelListener(null);
82     return mainPanel;
83   }
84
85   private Component createStatus()
86   {
87     status = new JLabel();
88     status.setVerticalAlignment(1);
89     status.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 4));
90     return status;
91   }
92
93   public void setStatus(final String JavaDoc string)
94   {
95     if(SwingUtilities.isEventDispatchThread())
96     {
97       status.setText(string);
98       showStatus();
99       return;
100     }
101     Runnable JavaDoc runnable = new Runnable JavaDoc()
102     {
103       public void run()
104       {
105         StatusDisplay.this.status.setText(string);
106         showStatus();
107       }
108     };
109     SwingUtilities.invokeLater(runnable);
110   }
111
112   public void setStatusIcon(final Icon icon)
113   {
114     if(SwingUtilities.isEventDispatchThread())
115     {
116       status.setIcon(icon);
117       showStatus();
118       return;
119     }
120     Runnable JavaDoc runnable = new Runnable JavaDoc()
121     {
122       public void run()
123       {
124         StatusDisplay.this.status.setIcon(icon);
125         showStatus();
126       }
127     };
128     SwingUtilities.invokeLater(runnable);
129   }
130
131   public void setProgressStatus(final String JavaDoc string)
132   {
133     if(SwingUtilities.isEventDispatchThread())
134     {
135       showProgress();
136       progressStatus.setText(string);
137       return;
138     }
139     Runnable JavaDoc runnable = new Runnable JavaDoc()
140     {
141       public void run()
142       {
143         showProgress();
144         StatusDisplay.this.progressStatus.setText(string);
145       }
146     };
147     SwingUtilities.invokeLater(runnable);
148   }
149
150   /**
151    * @param amount A value between 0 and 100 (inclusive)
152    */

153   public void setProgress(final int amount)
154   {
155     if(SwingUtilities.isEventDispatchThread())
156     {
157       showProgress();
158       progress.setValue(amount);
159       return;
160     }
161     Runnable JavaDoc runnable = new Runnable JavaDoc()
162     {
163       public void run()
164       {
165         showProgress();
166         StatusDisplay.this.progress.setValue(amount);
167       }
168     };
169     SwingUtilities.invokeLater(runnable);
170   }
171
172
173   public void setCancelListener(CancelListener c)
174   {
175     if(c == null)
176     {
177       cancel.setVisible(false);
178     }
179     else
180     {
181       cancel.setVisible(true);
182     }
183     cancelListener = c;
184   }
185
186   public String JavaDoc getItemName()
187   {
188     return "Status";
189   }
190
191   public Dimension getPreferredSize()
192   {
193     return new Dimension(200, super.getPreferredSize().height);
194   }
195
196   public void setIndeterminate(boolean bool)
197   {
198     progress.setIndeterminate(bool);
199   }
200
201   public void setProgressBarWidth(int i)
202   {
203     progress.setMaximumSize(new Dimension(i, progress.getPreferredSize().height));
204   }
205 }
206
Popular Tags