From: David ‘Bombe’ Roden Date: Wed, 14 May 2008 13:28:15 +0000 (+0000) Subject: add table model X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=cef853e1dfc369abafa2626e9f4d657df22b180c;p=jSite2.git add table model git-svn-id: http://trooper/svn/projects/jSite/trunk@877 c3eda9e8-030b-0410-8277-bc7414b0a119 --- diff --git a/src/net/pterodactylus/jsite/gui/RequestTableModel.java b/src/net/pterodactylus/jsite/gui/RequestTableModel.java new file mode 100644 index 0000000..c238cb1 --- /dev/null +++ b/src/net/pterodactylus/jsite/gui/RequestTableModel.java @@ -0,0 +1,153 @@ +/* + * jSite2 - RequestTableModel.java + * Copyright © 2008 David Roden + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package net.pterodactylus.jsite.gui; + +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.logging.Logger; + +import javax.swing.table.AbstractTableModel; + +import net.pterodactylus.jsite.core.Request; +import net.pterodactylus.util.logging.Logging; + +/** + * The request table model is the table model backing the request table. It + * registers itself as a {@link PropertyChangeListener} with every + * {@link Request} that is added to it so that it can track progress of the + * requests on its own. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + * @version $Id$ + */ +public class RequestTableModel extends AbstractTableModel implements PropertyChangeListener { + + /** Logger. */ + private static final Logger logger = Logging.getLogger(RequestTableModel.class.getName()); + + /** The column names. */ + private static final String[] COLUMN_NAMES = new String[] { "ID", "Progress" }; + + /** The requests. */ + private final List requests = Collections.synchronizedList(new ArrayList()); + + // + // ACTIONS + // + + /** + * Adds a request to the model. + * + * @param request + * The request to add + */ + public void addRequest(Request request) { + requests.add(request); + request.addPropertyChangeListener(this); + fireTableRowsInserted(requests.size() - 1, requests.size() - 1); + } + + /** + * Removes a request from the model. + * + * @param request + * The request to remove + */ + public void removeRequest(Request request) { + int requestIndex = requests.indexOf(request); + request.removePropertyChangeListener(this); + if (requestIndex != -1) { + requests.remove(request); + fireTableRowsDeleted(requestIndex, requestIndex); + } + } + + // + // INTERFACE TableModel + // + + /** + * @see javax.swing.table.AbstractTableModel#getColumnName(int) + */ + @Override + public String getColumnName(int column) { + return COLUMN_NAMES[column]; + } + + /** + * @see javax.swing.table.TableModel#getColumnCount() + */ + public int getColumnCount() { + return COLUMN_NAMES.length; + } + + /** + * @see javax.swing.table.TableModel#getRowCount() + */ + public int getRowCount() { + return requests.size(); + } + + /** + * @see javax.swing.table.TableModel#getValueAt(int, int) + */ + public Object getValueAt(int rowIndex, int columnIndex) { + Request request = requests.get(rowIndex); + switch (columnIndex) { + case 0: + return request.getIdentifier(); + case 1: + if (request.getRequiredBlocks() == 0) { + return "unknown"; + } + return String.valueOf((request.getSuccessfulBlocks() * 10000 / request.getRequiredBlocks()) / 100.0) + "%"; + } + return null; + } + + // + // INTERFACE PropertyChangeListener + // + + /** + * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent) + */ + public void propertyChange(PropertyChangeEvent propertyChangeEvent) { + Object eventSource = propertyChangeEvent.getSource(); + if (eventSource instanceof Request) { + Request sourceRequest = (Request) eventSource; + int requestIndex = requests.indexOf(sourceRequest); + if (requestIndex == -1) { + logger.warning("got request without index: " + sourceRequest); + return; + } + String propertyName = propertyChangeEvent.getPropertyName(); + if (Request.PROPERTY_REQUIRED_BLOCKS.equals(propertyName)) { + fireTableCellUpdated(requestIndex, 1); + } else if (Request.PROPERTY_SUCCESSFUL_BLOCKS.equals(propertyName)) { + fireTableCellUpdated(requestIndex, 1); + } + } + } + +}