PageRenderTime 2023ms CodeModel.GetById 356ms app.highlight 563ms RepoModel.GetById 247ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/TaskMonitor.java

#
Java | 282 lines | 224 code | 34 blank | 24 comment | 11 complexity | f4b2415d02bc6200d71c1fb7284a74c5 MD5 | raw file
  1/*
  2 * TaskMonitor
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright Š 2010 Matthieu Casanova
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 * This program is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 * GNU General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU General Public License
 18 * along with this program; if not, write to the Free Software
 19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 20 */
 21
 22package org.gjt.sp.jedit.gui;
 23
 24import org.gjt.sp.jedit.GUIUtilities;
 25import org.gjt.sp.jedit.jEdit;
 26import org.gjt.sp.util.Task;
 27import org.gjt.sp.util.TaskListener;
 28import org.gjt.sp.util.TaskManager;
 29import org.gjt.sp.util.ThreadUtilities;
 30
 31import javax.swing.*;
 32import javax.swing.event.TableModelEvent;
 33import javax.swing.event.TableModelListener;
 34import javax.swing.table.AbstractTableModel;
 35import javax.swing.table.TableCellEditor;
 36import javax.swing.table.TableCellRenderer;
 37import java.awt.*;
 38import java.awt.event.ActionEvent;
 39import java.awt.event.ActionListener;
 40import java.util.ArrayList;
 41
 42/**
 43 * @author Matthieu Casanova
 44 */
 45public class TaskMonitor extends JPanel implements TaskListener
 46{
 47	private final TaskTableModel model;
 48	private final JTable table;
 49	private final JLabel remainingCount;
 50
 51	public TaskMonitor()
 52	{
 53		super(new BorderLayout());
 54		JPanel panel = new JPanel(new BorderLayout());
 55		remainingCount = new JLabel();
 56		panel.add(remainingCount, BorderLayout.NORTH);
 57
 58		model = new TaskTableModel();
 59		model.addTableModelListener(new TableModelListener()
 60		{
 61			public void tableChanged(TableModelEvent e)
 62			{
 63				if (e.getType() == TableModelEvent.INSERT ||
 64					e.getType() == TableModelEvent.DELETE)
 65				{
 66					updateTasksCount();
 67				}
 68			}
 69		});
 70		table = new JTable(model);
 71		table.setDefaultRenderer(Object.class, new TaskCellRenderer());
 72		table.getTableHeader().setVisible(false);
 73		table.setDefaultEditor(Object.class, new TaskTableEditor());
 74		table.getColumnModel().getColumn(1).setMaxWidth(16);
 75		table.getColumnModel().getColumn(1).setMinWidth(16);
 76		JScrollPane scroll = new JScrollPane(table);
 77		panel.add(scroll);
 78		updateTasksCount();
 79
 80		add(new IOProgressMonitor(), BorderLayout.NORTH);
 81		add(panel);
 82	}
 83
 84	@Override
 85	public void addNotify()
 86	{
 87		TaskManager.instance.visit(new TaskManager.TaskVisitor() 
 88		{
 89			public void visit(Task task)
 90			{
 91				model.addTask(task);
 92			}
 93		});
 94		TaskManager.instance.addTaskListener(this);
 95		super.addNotify();
 96	}
 97
 98	@Override
 99	public void removeNotify()
100	{
101		TaskManager.instance.removeTaskListener(this);
102		super.removeNotify();
103		model.removeAll();
104	}
105
106	public void waiting(Task task)
107	{
108		model.addTask(task);
109	}
110
111	public void running(Task task)
112	{
113		repaint();
114	}
115
116	public void done(Task task)
117	{
118		model.removeTask(task);
119	}
120
121	public void statusUpdated(Task task)
122	{
123		repaint();
124	}
125
126	public void maximumUpdated(Task task)
127	{
128		repaint();
129	}
130
131	public void valueUpdated(Task task)
132	{
133		repaint();
134	}
135
136	private void updateTasksCount()
137	{
138		remainingCount.setText(jEdit.getProperty("taskmanager.remainingtasks.label",
139						new Object[]{model.getRowCount()}));
140	}
141
142	private static class TaskCellRenderer implements TableCellRenderer
143	{
144		private final JProgressBar progress;
145		private final JButton button;
146		private TaskCellRenderer()
147		{
148			progress = new JProgressBar();
149			button = new JButton(GUIUtilities.loadIcon(jEdit.getProperty("close-buffer.icon")));
150			progress.setStringPainted(true);
151		}
152
153		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
154							       boolean hasFocus, int row, int column)
155		{
156			Task task = (Task) value;
157			if (column == 0)
158			{
159				if (task.getMaximum() == 0L)
160				{
161					progress.setIndeterminate(true);
162				}
163				else
164				{
165					progress.setIndeterminate(false);
166					long max = task.getMaximum();
167					long val = task.getValue();
168					if (max > Integer.MAX_VALUE)
169					{
170						max >>= 10L;
171						val >>= 10L;
172
173					}
174					progress.setMaximum((int) max);
175					progress.setValue((int) val);
176				}
177				progress.setToolTipText(task.getLabel());
178				progress.setString(task.getStatus());
179				return progress;
180			}
181
182			return button;
183		}
184	}
185
186	private class TaskTableEditor extends AbstractCellEditor implements TableCellEditor
187	{
188		private final JButton button;
189
190		private Task task;
191
192		private TaskTableEditor()
193		{
194			button = new JButton(GUIUtilities.loadIcon(jEdit.getProperty("close-buffer.icon")));
195			button.addActionListener(new ActionListener()
196			{
197				public void actionPerformed(ActionEvent e)
198				{
199					task.cancel();
200					stopCellEditing();
201				}
202			});
203		}
204
205		public Object getCellEditorValue()
206		{
207			return null;
208		}
209
210		//{{{ getTableCellEditorComponent() method
211		public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
212		{
213			task = (Task) value;
214			return button;
215		} //}}}
216	}
217
218	private static class TaskTableModel extends AbstractTableModel
219	{
220		private final java.util.List<Task> tasks;
221
222		private TaskTableModel()
223		{
224			tasks = new ArrayList<Task>();
225		}
226
227		public int getRowCount()
228		{
229			return tasks.size();
230		}
231
232		public int getColumnCount()
233		{
234			return 2;
235		}
236
237		@Override
238		public boolean isCellEditable(int rowIndex, int columnIndex)
239		{
240			return columnIndex == 1;
241		}
242
243		public Object getValueAt(int rowIndex, int columnIndex)
244		{
245			return tasks.get(rowIndex);
246		}
247
248		void addTask(final Task task)
249		{
250			ThreadUtilities.runInDispatchThread(new Runnable()
251			{
252				public void run()
253				{
254					tasks.add(task);
255					fireTableRowsInserted(tasks.size()-1, tasks.size()-1);
256				}
257			});
258		}
259
260		void removeTask(final Task task)
261		{
262			ThreadUtilities.runInDispatchThread(new Runnable()
263			{
264				public void run()
265				{
266					int index = tasks.indexOf(task);
267					if (index != -1)
268					{
269						tasks.remove(index);
270						fireTableRowsDeleted(index,index);
271					}
272				}
273			});
274		}
275
276		public void removeAll()
277		{
278			tasks.clear();
279			fireTableDataChanged();
280		}
281	}
282}