001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016:
017: package org.columba.core.gui.statusbar;
018:
019: import java.awt.BorderLayout;
020: import java.awt.Color;
021: import java.awt.Component;
022: import java.awt.Dimension;
023: import java.awt.GridBagConstraints;
024: import java.awt.GridBagLayout;
025: import java.awt.GridLayout;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import java.awt.event.KeyEvent;
029:
030: import javax.swing.BorderFactory;
031: import javax.swing.Box;
032: import javax.swing.DefaultListModel;
033: import javax.swing.JButton;
034: import javax.swing.JComponent;
035: import javax.swing.JDialog;
036: import javax.swing.JFrame;
037: import javax.swing.JList;
038: import javax.swing.JPanel;
039: import javax.swing.JScrollPane;
040: import javax.swing.KeyStroke;
041: import javax.swing.SwingConstants;
042: import javax.swing.SwingUtilities;
043: import javax.swing.event.ListSelectionEvent;
044: import javax.swing.event.ListSelectionListener;
045:
046: import org.columba.api.command.IWorkerStatusChangeListener;
047: import org.columba.api.command.WorkerStatusChangedEvent;
048: import org.columba.core.command.TaskManager;
049: import org.columba.core.command.TaskManagerEvent;
050: import org.columba.core.command.TaskManagerListener;
051: import org.columba.core.command.Worker;
052: import org.columba.core.gui.base.ButtonWithMnemonic;
053: import org.columba.core.gui.base.SingleSideEtchedBorder;
054: import org.columba.core.help.HelpManager;
055: import org.columba.core.resourceloader.GlobalResourceLoader;
056:
057: /**
058: * Dialog showing all running tasks.
059: * <p>
060: * Lets the user cancel or kill tasks.
061: *
062: * @author fdietz
063: */
064: public class TaskManagerDialog extends JDialog implements
065: TaskManagerListener, ActionListener,
066: IWorkerStatusChangeListener, ListSelectionListener {
067:
068: private static TaskManagerDialog instance;
069:
070: private TaskManager taskManager;
071:
072: private JButton cancelButton;
073:
074: private JButton killButton;
075:
076: private JList list;
077:
078: public TaskManagerDialog(TaskManager tm) {
079: super ((JFrame) null, "Task Manager", false);
080: this .taskManager = tm;
081:
082: initComponents();
083: pack();
084: setLocationRelativeTo(null);
085:
086: tm.addTaskManagerListener(this );
087: }
088:
089: public static TaskManagerDialog createInstance() {
090: if (instance == null) {
091: instance = new TaskManagerDialog(TaskManager.getInstance());
092: }
093:
094: if (!instance.isVisible()) {
095: instance.setVisible(true);
096: }
097: instance.toFront();
098:
099: return instance;
100: }
101:
102: public void initComponents() {
103: JPanel mainPanel = new JPanel(new BorderLayout());
104: mainPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12,
105: 12));
106: getContentPane().add(mainPanel);
107:
108: cancelButton = new ButtonWithMnemonic("Cancel &Task");
109: cancelButton.setEnabled(false);
110: cancelButton.setActionCommand("CANCEL");
111: cancelButton.addActionListener(this );
112:
113: killButton = new ButtonWithMnemonic("Kill");
114: killButton.setActionCommand("KILL");
115: killButton.setEnabled(false);
116: killButton.addActionListener(this );
117:
118: GridBagLayout gridBagLayout = new GridBagLayout();
119: GridBagConstraints c = new GridBagConstraints();
120:
121: JPanel eastPanel = new JPanel(gridBagLayout);
122: eastPanel.setLayout(gridBagLayout);
123: mainPanel.add(eastPanel, BorderLayout.EAST);
124:
125: c.fill = GridBagConstraints.HORIZONTAL;
126: c.weightx = 1.0;
127: c.gridwidth = GridBagConstraints.REMAINDER;
128: gridBagLayout.setConstraints(cancelButton, c);
129: eastPanel.add(cancelButton);
130:
131: // disabled Kill button, because feature is not supported
132: /*
133: * Component strut1 = Box.createRigidArea(new Dimension(30, 6));
134: * gridBagLayout.setConstraints(strut1, c); eastPanel.add(strut1);
135: *
136: * gridBagLayout.setConstraints(killButton, c);
137: * eastPanel.add(killButton);
138: */
139:
140: Component glue = Box.createVerticalGlue();
141: glue = Box.createVerticalGlue();
142: c.fill = GridBagConstraints.BOTH;
143: c.weighty = 1.0;
144: gridBagLayout.setConstraints(glue, c);
145: eastPanel.add(glue);
146:
147: // centerpanel
148: JPanel centerPanel = new JPanel(new BorderLayout());
149: centerPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0,
150: 6));
151: DefaultListModel model = new DefaultListModel();
152: Worker[] workers = taskManager.getWorkers();
153: for (int i = 0; i < workers.length; i++) {
154: model.addElement(workers[i]);
155: }
156: list = new JList(model);
157: list.setCellRenderer(new TaskRenderer());
158: list.getSelectionModel().addListSelectionListener(this );
159:
160: JScrollPane scrollPane = new JScrollPane(list);
161: scrollPane.setPreferredSize(new Dimension(300, 250));
162: scrollPane.getViewport().setBackground(Color.white);
163: centerPanel.add(scrollPane);
164:
165: mainPanel.add(centerPanel);
166:
167: JPanel bottomPanel = new JPanel(new BorderLayout());
168: bottomPanel.setBorder(new SingleSideEtchedBorder(
169: SwingConstants.TOP));
170:
171: JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 6, 0));
172: buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12,
173: 12, 12));
174:
175: ButtonWithMnemonic closeButton = new ButtonWithMnemonic(
176: GlobalResourceLoader.getString("", "global", "close"));
177: closeButton.setActionCommand("CLOSE"); //$NON-NLS-1$
178: closeButton.addActionListener(this );
179: buttonPanel.add(closeButton);
180:
181: ButtonWithMnemonic helpButton = new ButtonWithMnemonic(
182: GlobalResourceLoader.getString("", "global", "help"));
183: buttonPanel.add(helpButton);
184: bottomPanel.add(buttonPanel, BorderLayout.EAST);
185: getContentPane().add(bottomPanel, BorderLayout.SOUTH);
186: getRootPane().setDefaultButton(closeButton);
187: getRootPane().registerKeyboardAction(this , "CLOSE",
188: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
189: JComponent.WHEN_IN_FOCUSED_WINDOW);
190:
191: // associate with JavaHelp
192: HelpManager.getInstance().enableHelpOnButton(helpButton,
193: "organising_and_managing_your_email_3");
194: HelpManager.getInstance().enableHelpKey(getRootPane(),
195: "organising_and_managing_your_email_3");
196: }
197:
198: public void actionPerformed(ActionEvent e) {
199: String action = e.getActionCommand();
200:
201: if (action.equals("CLOSE")) {
202: setVisible(false);
203: } else if (action.equals("CANCEL")) {
204: Worker worker = (Worker) list.getSelectedValue();
205: worker.setCancel(true);
206:
207: } else if (action.equals("KILL")) {
208:
209: }
210: }
211:
212: public void workerAdded(final TaskManagerEvent e) {
213: SwingUtilities.invokeLater(new Runnable() {
214: public void run() {
215: Worker worker = e.getWorker();
216:
217: worker
218: .addWorkerStatusChangeListener(TaskManagerDialog.this );
219:
220: ((DefaultListModel) list.getModel()).addElement(e
221: .getWorker());
222: }
223: });
224: }
225:
226: public void workerRemoved(final TaskManagerEvent e) {
227: SwingUtilities.invokeLater(new Runnable() {
228: public void run() {
229: Worker worker = e.getWorker();
230:
231: ((DefaultListModel) list.getModel())
232: .removeElement(worker);
233: }
234: });
235: }
236:
237: public void workerStatusChanged(WorkerStatusChangedEvent e) {
238: final WorkerStatusChangedEvent event = e;
239:
240: switch (e.getType()) {
241:
242: case WorkerStatusChangedEvent.DISPLAY_TEXT_CHANGED:
243: break;
244:
245: case WorkerStatusChangedEvent.DISPLAY_TEXT_CLEARED:
246: break;
247:
248: case WorkerStatusChangedEvent.PROGRESSBAR_MAX_CHANGED:
249: break;
250:
251: case WorkerStatusChangedEvent.PROGRESSBAR_VALUE_CHANGED:
252: break;
253:
254: case WorkerStatusChangedEvent.FINISHED:
255: }
256:
257: SwingUtilities.invokeLater(new Runnable() {
258: public void run() {
259: // force repaint
260: DefaultListModel m = ((DefaultListModel) list
261: .getModel());
262: int index = m.indexOf(event.getSource());
263: if (index != -1)
264: m.setElementAt(event.getSource(), index);
265:
266: }
267: });
268:
269: }
270:
271: /**
272: * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
273: */
274: public void valueChanged(ListSelectionEvent ev) {
275: if (ev.getValueIsAdjusting())
276: return;
277:
278: if (list.getSelectedIndex() != -1)
279: cancelButton.setEnabled(true);
280: else
281: cancelButton.setEnabled(false);
282:
283: }
284: }
|