001: package com.opensymphony.workflow.designer.swing.status;
002:
003: import java.awt.*;
004: import java.awt.event.ActionEvent;
005: import java.awt.event.ActionListener;
006: import javax.swing.*;
007: import javax.swing.border.EmptyBorder;
008:
009: import com.opensymphony.workflow.designer.ResourceManager;
010:
011: /**
012: * @author Hani Suleiman (hani@formicary.net)
013: * Date: Dec 24, 2003
014: * Time: 5:05:32 PM
015: */
016: public class StatusDisplay extends DisplayItem {
017: private JLabel status;
018: private JLabel progressStatus;
019: private JProgressBar progress;
020: private JButton cancel;
021: private CancelListener cancelListener;
022:
023: public static interface CancelListener {
024: public void cancelPerformed();
025: }
026:
027: public StatusDisplay() {
028: setLayout(new CardLayout());
029: add(createStatus(), "Status");
030: add(createProgressBar(), "Progress");
031: showStatus();
032: }
033:
034: public void showStatus() {
035: ((CardLayout) getLayout()).show(this , "Status");
036: }
037:
038: public void showProgress() {
039: ((CardLayout) getLayout()).show(this , "Progress");
040: }
041:
042: private Component createProgressBar() {
043: JPanel mainPanel = new JPanel();
044: mainPanel.setOpaque(false);
045: cancelListener = null;
046: mainPanel.setLayout(new BorderLayout(3, 0));
047: progressStatus = new JLabel("");
048: progressStatus.setVerticalAlignment(1);
049: progressStatus.setBorder(BorderFactory.createEmptyBorder(0, 4,
050: 0, 4));
051: progress = new JProgressBar();
052: progress.setMaximum(100);
053: cancel = new JButton(ResourceManager.getString("cancel"));
054: cancel.setOpaque(false);
055: cancel.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 4));
056: cancel.addActionListener(new ActionListener() {
057: public void actionPerformed(ActionEvent actionevent) {
058: if (StatusDisplay.this .cancelListener != null)
059: StatusDisplay.this .cancelListener.cancelPerformed();
060: }
061: });
062: cancel.setRequestFocusEnabled(false);
063: cancel.setFocusable(false);
064: JPanel barPanel = new JPanel(new BorderLayout(0, 0));
065: barPanel.setOpaque(false);
066: barPanel.setBorder(new EmptyBorder(3, 3, 3, 3));
067: barPanel.add(progress, BorderLayout.CENTER);
068: JPanel progressPanel = new JPanel(new BorderLayout(3, 0));
069: progressPanel.setOpaque(false);
070: progressPanel.add(progressStatus, BorderLayout.LINE_START);
071: progressPanel.add(barPanel, BorderLayout.CENTER);
072: mainPanel.add(progressPanel, BorderLayout.CENTER);
073: mainPanel.add(cancel, BorderLayout.LINE_END);
074: setCancelListener(null);
075: return mainPanel;
076: }
077:
078: private Component createStatus() {
079: status = new JLabel();
080: status.setVerticalAlignment(1);
081: status.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 4));
082: return status;
083: }
084:
085: public void setStatus(final String string) {
086: if (SwingUtilities.isEventDispatchThread()) {
087: status.setText(string);
088: showStatus();
089: return;
090: }
091: Runnable runnable = new Runnable() {
092: public void run() {
093: StatusDisplay.this .status.setText(string);
094: showStatus();
095: }
096: };
097: SwingUtilities.invokeLater(runnable);
098: }
099:
100: public void setStatusIcon(final Icon icon) {
101: if (SwingUtilities.isEventDispatchThread()) {
102: status.setIcon(icon);
103: showStatus();
104: return;
105: }
106: Runnable runnable = new Runnable() {
107: public void run() {
108: StatusDisplay.this .status.setIcon(icon);
109: showStatus();
110: }
111: };
112: SwingUtilities.invokeLater(runnable);
113: }
114:
115: public void setProgressStatus(final String string) {
116: if (SwingUtilities.isEventDispatchThread()) {
117: showProgress();
118: progressStatus.setText(string);
119: return;
120: }
121: Runnable runnable = new Runnable() {
122: public void run() {
123: showProgress();
124: StatusDisplay.this .progressStatus.setText(string);
125: }
126: };
127: SwingUtilities.invokeLater(runnable);
128: }
129:
130: /**
131: * @param amount A value between 0 and 100 (inclusive)
132: */
133: public void setProgress(final int amount) {
134: if (SwingUtilities.isEventDispatchThread()) {
135: showProgress();
136: progress.setValue(amount);
137: return;
138: }
139: Runnable runnable = new Runnable() {
140: public void run() {
141: showProgress();
142: StatusDisplay.this .progress.setValue(amount);
143: }
144: };
145: SwingUtilities.invokeLater(runnable);
146: }
147:
148: public void setCancelListener(CancelListener c) {
149: if (c == null) {
150: cancel.setVisible(false);
151: } else {
152: cancel.setVisible(true);
153: }
154: cancelListener = c;
155: }
156:
157: public String getItemName() {
158: return "Status";
159: }
160:
161: public Dimension getPreferredSize() {
162: return new Dimension(200, super .getPreferredSize().height);
163: }
164:
165: public void setIndeterminate(boolean bool) {
166: progress.setIndeterminate(bool);
167: }
168:
169: public void setProgressBarWidth(int i) {
170: progress.setMaximumSize(new Dimension(i, progress
171: .getPreferredSize().height));
172: }
173: }
|