2 * jSite2 - RequestTableModel.java
3 * Copyright © 2008 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package net.pterodactylus.jsite.gui;
22 import java.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.logging.Logger;
29 import javax.swing.table.AbstractTableModel;
31 import net.pterodactylus.jsite.core.Request;
32 import net.pterodactylus.util.logging.Logging;
35 * The request table model is the table model backing the request table. It
36 * registers itself as a {@link PropertyChangeListener} with every
37 * {@link Request} that is added to it so that it can track progress of the
38 * requests on its own.
40 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
43 public class RequestTableModel extends AbstractTableModel implements PropertyChangeListener {
46 private static final Logger logger = Logging.getLogger(RequestTableModel.class.getName());
48 /** The column names. */
49 private static final String[] COLUMN_NAMES = new String[] { "ID", "Progress" };
52 private final List<Request> requests = Collections.synchronizedList(new ArrayList<Request>());
59 * Adds a request to the model.
64 public void addRequest(Request request) {
65 requests.add(request);
66 request.addPropertyChangeListener(this);
67 fireTableRowsInserted(requests.size() - 1, requests.size() - 1);
71 * Removes a request from the model.
74 * The request to remove
76 public void removeRequest(Request request) {
77 int requestIndex = requests.indexOf(request);
78 request.removePropertyChangeListener(this);
79 if (requestIndex != -1) {
80 requests.remove(request);
81 fireTableRowsDeleted(requestIndex, requestIndex);
86 // INTERFACE TableModel
90 * @see javax.swing.table.AbstractTableModel#getColumnName(int)
93 public String getColumnName(int column) {
94 return COLUMN_NAMES[column];
98 * @see javax.swing.table.TableModel#getColumnCount()
100 public int getColumnCount() {
101 return COLUMN_NAMES.length;
105 * @see javax.swing.table.TableModel#getRowCount()
107 public int getRowCount() {
108 return requests.size();
112 * @see javax.swing.table.TableModel#getValueAt(int, int)
114 public Object getValueAt(int rowIndex, int columnIndex) {
115 Request request = requests.get(rowIndex);
116 switch (columnIndex) {
118 return request.getIdentifier();
120 if (request.getRequiredBlocks() == 0) {
123 return String.valueOf((request.getSuccessfulBlocks() * 10000 / request.getRequiredBlocks()) / 100.0) + "%";
129 // INTERFACE PropertyChangeListener
133 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
135 public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
136 Object eventSource = propertyChangeEvent.getSource();
137 if (eventSource instanceof Request) {
138 Request sourceRequest = (Request) eventSource;
139 int requestIndex = requests.indexOf(sourceRequest);
140 if (requestIndex == -1) {
141 logger.warning("got request without index: " + sourceRequest);
144 String propertyName = propertyChangeEvent.getPropertyName();
145 if (Request.PROPERTY_REQUIRED_BLOCKS.equals(propertyName)) {
146 fireTableCellUpdated(requestIndex, 1);
147 } else if (Request.PROPERTY_SUCCESSFUL_BLOCKS.equals(propertyName)) {
148 fireTableCellUpdated(requestIndex, 1);