001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018:
019: package org.columba.mail.gui.util;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Dimension;
023: import java.awt.HeadlessException;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026:
027: import javax.swing.BorderFactory;
028: import javax.swing.JButton;
029: import javax.swing.JDialog;
030: import javax.swing.JFrame;
031: import javax.swing.JLabel;
032: import javax.swing.JPanel;
033: import javax.swing.JProgressBar;
034:
035: import org.columba.api.command.IWorkerStatusChangeListener;
036: import org.columba.api.command.IWorkerStatusController;
037: import org.columba.api.command.WorkerStatusChangedEvent;
038: import org.columba.core.command.Command;
039: import org.columba.core.gui.base.ButtonWithMnemonic;
040: import org.columba.mail.resourceloader.MailImageLoader;
041:
042: /**
043: * Dialog shows progress while sending message.
044: * <p>
045: * Additionally offers the possibility to cancel the operation.
046: * <p>
047: * This is the first example of watching the progress for a specific worker,
048: * using the timestamp attribute, which is created by {@link DefaultProcessor}
049: * when executing the {@link Command}.
050: *
051: * @author fdietz
052: */
053: public class SendMessageDialog extends JDialog implements
054: IWorkerStatusChangeListener, ActionListener {
055: private JProgressBar progressBar;
056:
057: private JButton cancelButton;
058:
059: private JLabel label;
060:
061: private IWorkerStatusController worker;
062:
063: /**
064: * @param arg0
065: * @throws java.awt.HeadlessException
066: */
067: public SendMessageDialog(IWorkerStatusController worker)
068: throws HeadlessException {
069: super (new JFrame(), "Sending message...", false);
070:
071: setWorker(worker);
072:
073: initComponents();
074: layoutComponents();
075: pack();
076: setLocationRelativeTo(null);
077: setVisible(true);
078: }
079:
080: protected void initComponents() {
081: label = new JLabel("Sending message...");
082: label.setIcon(MailImageLoader.getIcon("send.png"));
083:
084: progressBar = new JProgressBar();
085: progressBar.setPreferredSize(new Dimension(300, 20));
086:
087: cancelButton = new ButtonWithMnemonic("&Cancel");
088: cancelButton.setActionCommand("CANCEL");
089: cancelButton.addActionListener(this );
090: }
091:
092: protected void layoutComponents() {
093: JPanel panel = new JPanel();
094: panel.setLayout(new BorderLayout());
095: panel
096: .setBorder(BorderFactory.createEmptyBorder(12, 12, 12,
097: 12));
098:
099: getContentPane().add(panel);
100:
101: JPanel top = new JPanel();
102: top.setBorder(BorderFactory.createEmptyBorder(0, 12, 6, 0));
103: top.setLayout(new BorderLayout());
104: top.add(label, BorderLayout.WEST);
105:
106: panel.add(top, BorderLayout.NORTH);
107:
108: panel.add(progressBar, BorderLayout.CENTER);
109:
110: JPanel bottom = new JPanel();
111: bottom.setBorder(BorderFactory.createEmptyBorder(12, 0, 0, 0));
112: bottom.add(cancelButton);
113:
114: panel.add(bottom, BorderLayout.SOUTH);
115: }
116:
117: /** ********************** WorkerStatusListener ************************** */
118: public void workerStatusChanged(WorkerStatusChangedEvent e) {
119: int ts = e.getTimeStamp();
120: final WorkerStatusChangedEvent event = e;
121:
122: // only update if timestamp is equal
123: if (worker.getTimeStamp() == ts) {
124: switch (e.getType()) {
125: case WorkerStatusChangedEvent.DISPLAY_TEXT_CHANGED:
126: javax.swing.SwingUtilities.invokeLater(new Runnable() {
127: public void run() {
128: label.setText((String) event.getNewValue());
129: }
130: });
131:
132: break;
133:
134: case WorkerStatusChangedEvent.DISPLAY_TEXT_CLEARED:
135:
136: // implemented for completeness.
137: // Time-out for clearing text is ignored here.
138: javax.swing.SwingUtilities.invokeLater(new Runnable() {
139: public void run() {
140: label.setText("");
141: }
142: });
143:
144: break;
145:
146: case WorkerStatusChangedEvent.PROGRESSBAR_MAX_CHANGED: {
147: javax.swing.SwingUtilities.invokeLater(new Runnable() {
148: public void run() {
149: progressBar.setMaximum(((Integer) event
150: .getNewValue()).intValue());
151: }
152: });
153:
154: break;
155: }
156:
157: case WorkerStatusChangedEvent.PROGRESSBAR_VALUE_CHANGED:
158: javax.swing.SwingUtilities.invokeLater(new Runnable() {
159: public void run() {
160: progressBar.setValue(((Integer) event
161: .getNewValue()).intValue());
162: }
163: });
164:
165: break;
166:
167: /*
168: * case WorkerStatusChangedEvent.FINISHED : setVisible(false);
169: * break;
170: */
171: }
172: }
173: }
174:
175: /*
176: * (non-Javadoc)
177: *
178: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
179: */
180: public void actionPerformed(ActionEvent arg0) {
181: if (arg0.getActionCommand().equals("CANCEL")) {
182: // send cancel event to worker
183: worker.cancel();
184: setVisible(false);
185: }
186: }
187:
188: /**
189: * @param worker
190: * The worker to set.
191: */
192: public void setWorker(IWorkerStatusController worker) {
193: this .worker = worker;
194:
195: worker.addWorkerStatusChangeListener(this );
196: }
197:
198: /**
199: * @see java.awt.Component#setVisible(boolean)
200: */
201: public void setVisible(boolean b) {
202: super.setVisible(b);
203:
204: if (!b && worker != null) {
205: worker.removeWorkerStatusChangeListener(this);
206: }
207: }
208: }
|