c238cb1b8ec1ea74a792a55c90540fe434b1bdbf
[jSite2.git] / src / net / pterodactylus / jsite / gui / RequestTableModel.java
1 /*
2  * jSite2 - RequestTableModel.java
3  * Copyright © 2008 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package net.pterodactylus.jsite.gui;
21
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;
28
29 import javax.swing.table.AbstractTableModel;
30
31 import net.pterodactylus.jsite.core.Request;
32 import net.pterodactylus.util.logging.Logging;
33
34 /**
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.
39  * 
40  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
41  * @version $Id$
42  */
43 public class RequestTableModel extends AbstractTableModel implements PropertyChangeListener {
44
45         /** Logger. */
46         private static final Logger logger = Logging.getLogger(RequestTableModel.class.getName());
47
48         /** The column names. */
49         private static final String[] COLUMN_NAMES = new String[] { "ID", "Progress" };
50
51         /** The requests. */
52         private final List<Request> requests = Collections.synchronizedList(new ArrayList<Request>());
53
54         //
55         // ACTIONS
56         //
57
58         /**
59          * Adds a request to the model.
60          * 
61          * @param request
62          *            The request to add
63          */
64         public void addRequest(Request request) {
65                 requests.add(request);
66                 request.addPropertyChangeListener(this);
67                 fireTableRowsInserted(requests.size() - 1, requests.size() - 1);
68         }
69
70         /**
71          * Removes a request from the model.
72          * 
73          * @param request
74          *            The request to remove
75          */
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);
82                 }
83         }
84
85         //
86         // INTERFACE TableModel
87         //
88
89         /**
90          * @see javax.swing.table.AbstractTableModel#getColumnName(int)
91          */
92         @Override
93         public String getColumnName(int column) {
94                 return COLUMN_NAMES[column];
95         }
96
97         /**
98          * @see javax.swing.table.TableModel#getColumnCount()
99          */
100         public int getColumnCount() {
101                 return COLUMN_NAMES.length;
102         }
103
104         /**
105          * @see javax.swing.table.TableModel#getRowCount()
106          */
107         public int getRowCount() {
108                 return requests.size();
109         }
110
111         /**
112          * @see javax.swing.table.TableModel#getValueAt(int, int)
113          */
114         public Object getValueAt(int rowIndex, int columnIndex) {
115                 Request request = requests.get(rowIndex);
116                 switch (columnIndex) {
117                         case 0:
118                                 return request.getIdentifier();
119                         case 1:
120                                 if (request.getRequiredBlocks() == 0) {
121                                         return "unknown";
122                                 }
123                                 return String.valueOf((request.getSuccessfulBlocks() * 10000 / request.getRequiredBlocks()) / 100.0) + "%";
124                 }
125                 return null;
126         }
127
128         //
129         // INTERFACE PropertyChangeListener
130         //
131
132         /**
133          * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
134          */
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);
142                                 return;
143                         }
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);
149                         }
150                 }
151         }
152
153 }