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