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.jsite.core.Request.Type;
33 import net.pterodactylus.util.logging.Logging;
36 * The request table model is the table model backing the request table. It
37 * registers itself as a {@link PropertyChangeListener} with every
38 * {@link Request} that is added to it so that it can track progress of the
39 * requests on its own.
41 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
44 public class RequestTableModel extends AbstractTableModel implements PropertyChangeListener {
47 private static final Logger logger = Logging.getLogger(RequestTableModel.class.getName());
49 /** The column names. */
50 private static final String[] COLUMN_NAMES = new String[] { "ID", "Progress", "Type" };
53 private final List<Request> requests = Collections.synchronizedList(new ArrayList<Request>());
60 * Adds a request to the model.
65 public void addRequest(Request request) {
66 requests.add(request);
67 request.addPropertyChangeListener(this);
68 fireTableRowsInserted(requests.size() - 1, requests.size() - 1);
72 * Removes a request from the model.
75 * The request to remove
77 public void removeRequest(Request request) {
78 int requestIndex = requests.indexOf(request);
79 request.removePropertyChangeListener(this);
80 if (requestIndex != -1) {
81 requests.remove(request);
82 fireTableRowsDeleted(requestIndex, requestIndex);
87 // INTERFACE TableModel
91 * @see javax.swing.table.AbstractTableModel#getColumnName(int)
94 public String getColumnName(int column) {
95 return COLUMN_NAMES[column];
99 * @see javax.swing.table.TableModel#getColumnCount()
101 public int getColumnCount() {
102 return COLUMN_NAMES.length;
106 * @see javax.swing.table.TableModel#getRowCount()
108 public int getRowCount() {
109 return requests.size();
113 * @see javax.swing.table.TableModel#getValueAt(int, int)
115 public Object getValueAt(int rowIndex, int columnIndex) {
116 Request request = requests.get(rowIndex);
117 switch (columnIndex) {
119 return request.getIdentifier();
121 if (request.getRequiredBlocks() == 0) {
124 return String.valueOf((request.getSuccessfulBlocks() * 10000 / request.getRequiredBlocks()) / 100.0) + "%";
126 return ((request.getType() != null) ? request.getType() : Type.unknown).name();
132 // INTERFACE PropertyChangeListener
136 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
138 public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
139 Object eventSource = propertyChangeEvent.getSource();
140 if (eventSource instanceof Request) {
141 Request sourceRequest = (Request) eventSource;
142 int requestIndex = requests.indexOf(sourceRequest);
143 if (requestIndex == -1) {
144 logger.warning("got request without index: " + sourceRequest);
147 String propertyName = propertyChangeEvent.getPropertyName();
148 if (Request.PROPERTY_REQUIRED_BLOCKS.equals(propertyName)) {
149 fireTableCellUpdated(requestIndex, 1);
150 } else if (Request.PROPERTY_SUCCESSFUL_BLOCKS.equals(propertyName)) {
151 fireTableCellUpdated(requestIndex, 1);