File Source: UpdateTableModel.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.dialogs.updater.UpdateTableModel__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc.addons.ui_swing.dialogs.updater;
24
25 import com.dmdirc.updater.UpdateListener;
26 import com.dmdirc.updater.Update;
27 import com.dmdirc.updater.UpdateComponent;
28 import com.dmdirc.updater.UpdateStatus;
29
30 import java.text.NumberFormat;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 import javax.swing.table.AbstractTableModel;
37
38 /**
39 * Update table model.
40 */
41 public class UpdateTableModel extends AbstractTableModel implements UpdateListener {
42
43 /**
44 * A version number for this class. It should be changed whenever the class
45 * structure is changed (or anything else that would prevent serialized
46 * objects being unserialized with the new class).
47 */
48 private static final long serialVersionUID = 3;
49 /** Data list. */
50 private List<Update> updates;
51 /** Enabled list. */
52 private Map<Update, Boolean> enabled;
53 /** Number formatter. */
54 private NumberFormat formatter;
55
56 /** Creates a new instance of UpdateTableModel. */
57 public UpdateTableModel() {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.updater.UpdateTableModel()
*
* Presumptions:
* java.text.NumberFormat:getNumberInstance(...)@70 != null
*
* Postconditions:
* this.enabled == &new HashMap(setUpdates#2)
* this.formatter != null
* this.updates == &new ArrayList(setUpdates#1)
* new ArrayList(setUpdates#1) num objects == 1
* new HashMap(setUpdates#2) num objects == 1
*/
58 this(new ArrayList<Update>());
59 }
60
61 /**
62 * Creates a new instance of UpdateTableModel.
63 *
64 * @param updates List of updates
65 */
66 public UpdateTableModel(final List<Update> updates) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.updater.UpdateTableModel(List)
*
* Preconditions:
* updates != null
*
* Presumptions:
* java.text.NumberFormat:getNumberInstance(...)@70 != null
*
* Postconditions:
* this.enabled == &new HashMap(setUpdates#2)
* this.formatter != null
* this.updates == &new ArrayList(setUpdates#1)
* new ArrayList(setUpdates#1) num objects == 1
* new HashMap(setUpdates#2) num objects == 1
*/
67 super();
68
69 setUpdates(updates);
70 formatter = NumberFormat.getNumberInstance();
71 formatter.setMaximumFractionDigits(1);
72 formatter.setMinimumFractionDigits(1);
73 }
74
75 /**
76 * Sets the updates list.
77 *
78 * @param updates List of updates
79 */
80 public void setUpdates(final List<Update> updates) {
/*
P/P * Method: void setUpdates(List)
*
* Preconditions:
* updates != null
*
* Presumptions:
* java.util.Iterator:next(...)@84 != null
*
* Postconditions:
* this.enabled == &new HashMap(setUpdates#2)
* this.updates == &new ArrayList(setUpdates#1)
* new ArrayList(setUpdates#1) num objects == 1
* new HashMap(setUpdates#2) num objects == 1
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@84: {0}, {1}
*/
81 this.updates = new ArrayList<Update>(updates);
82 this.enabled = new HashMap<Update, Boolean>();
83
84 for (Update update : updates) {
85 update.addUpdateListener(this);
86 enabled.put(update, true);
87 }
88
89 fireTableDataChanged();
90 }
91
92 /** {@inheritDoc} */
93 @Override
94 public int getRowCount() {
/*
P/P * Method: int getRowCount()
*
* Preconditions:
* this.updates != null
*
* Postconditions:
* init'ed(return_value)
*/
95 return updates.size();
96 }
97
98 /** {@inheritDoc} */
99 @Override
100 public int getColumnCount() {
/*
P/P * Method: int getColumnCount()
*
* Postconditions:
* return_value == 4
*/
101 return 4;
102 }
103
104 /** {@inheritDoc} */
105 @Override
106 public String getColumnName(final int columnIndex) {
/*
P/P * Method: String getColumnName(int)
*
* Preconditions:
* columnIndex in {0..3}
*
* Postconditions:
* return_value in Addr_Set{&"Update?",&"Component",&"New version",&"Status"}
*
* Test Vectors:
* columnIndex: {0}, {1}, {2}, {3}
*/
107 switch (columnIndex) {
108 case 0:
109 return "Update?";
110 case 1:
111 return "Component";
112 case 2:
113 return "New version";
114 case 3:
115 return "Status";
116 default:
117 throw new IllegalArgumentException("Unknown column: " +
118 columnIndex);
119 }
120 }
121
122 /** {@inheritDoc} */
123 @Override
124 public Class<?> getColumnClass(final int columnIndex) {
/*
P/P * Method: Class getColumnClass(int)
*
* Preconditions:
* columnIndex in {0..3}
*
* Test Vectors:
* columnIndex: {0}, {1}, {2}, {3}
*/
125 switch (columnIndex) {
126 case 0:
127 return Boolean.class;
128 case 1:
129 return UpdateComponent.class;
130 case 2:
131 return String.class;
132 case 3:
133 return UpdateStatus.class;
134 default:
135 throw new IllegalArgumentException("Unknown column: "
136 + columnIndex);
137 }
138 }
139
140 /** {@inheritDoc} */
141 @Override
142 public boolean isCellEditable(final int rowIndex, final int columnIndex) {
/*
P/P * Method: bool isCellEditable(int, int)
*
* Postconditions:
* init'ed(return_value)
*/
143 return columnIndex == 0;
144 }
145
146 /** {@inheritDoc} */
147 @Override
148 public Object getValueAt(final int rowIndex, final int columnIndex) {
/*
P/P * Method: Object getValueAt(int, int)
*
* Preconditions:
* columnIndex in {0..3}
* rowIndex in {0..232-2}
* this.updates != null
* (soft) this.enabled != null
* (soft) this.formatter != null
*
* Presumptions:
* com.dmdirc.updater.Update:getStatus(...)@164 != null
* init'ed(com.dmdirc.updater.UpdateStatus.DOWNLOADING)
* java.util.List:get(...)@160 != null
* java.util.List:get(...)@162 != null
* java.util.List:get(...)@164 != null
* ...
*
* Postconditions:
* java.lang.StringBuilder:toString(...)._tainted == 0
* init'ed(return_value)
*
* Test Vectors:
* columnIndex: {0}, {1}, {2}, {3}
* com.dmdirc.updater.UpdateStatus:equals(...)@164: {0}, {1}
*/
149 if (updates.size() <= rowIndex) {
150 throw new IndexOutOfBoundsException(rowIndex + " >= "
151 + updates.size());
152 }
153 if (rowIndex < 0) {
154 throw new IllegalArgumentException("Must specify a positive integer");
155 }
156 switch (columnIndex) {
157 case 0:
158 return enabled.get(updates.get(rowIndex));
159 case 1:
160 return updates.get(rowIndex).getComponent();
161 case 2:
162 return updates.get(rowIndex).getRemoteVersion();
163 case 3:
164 if (updates.get(rowIndex).getStatus().equals(UpdateStatus.DOWNLOADING)) {
165 return updates.get(rowIndex).getStatus() + " ("
166 + formatter.format(updates.get(rowIndex).getProgress()) + "%)";
167 } else {
168 return updates.get(rowIndex).getStatus();
169 }
170 default:
171 throw new IllegalArgumentException("Unknown column: "
172 + columnIndex);
173 }
174 }
175
176 /** {@inheritDoc} */
177 @Override
178 public void setValueAt(final Object aValue, final int rowIndex,
179 final int columnIndex) {
/*
P/P * Method: void setValueAt(Object, int, int)
*
* Preconditions:
* columnIndex == 0
* rowIndex in {0..232-2}
* this.enabled != null
* this.updates != null
*
* Presumptions:
* java.util.List:size(...)@180 >= 1
* java.util.List:size(...)@180 - rowIndex in {1..232-1}
*/
180 if (updates.size() <= rowIndex) {
181 throw new IndexOutOfBoundsException(rowIndex + " >= " +
182 updates.size());
183 }
184 if (rowIndex < 0) {
185 throw new IllegalArgumentException("Must specify a positive integer");
186 }
187 switch (columnIndex) {
188 case 0:
189 enabled.put(updates.get(rowIndex), (Boolean) aValue);
190 break;
191 default:
192 throw new IllegalArgumentException("Unknown column: "
193 + columnIndex);
194 }
195 fireTableCellUpdated(rowIndex, columnIndex);
196 }
197
198 /**
199 * Gets the update at the specified row.
200 *
201 * @param rowIndex Row to retrieve
202 *
203 * @return Specified Update
204 */
205 public Update getUpdate(final int rowIndex) {
/*
P/P * Method: Update getUpdate(int)
*
* Preconditions:
* this.updates != null
*
* Postconditions:
* init'ed(return_value)
*/
206 return updates.get(rowIndex);
207 }
208
209 /**
210 * Gets a list of all updates.
211 *
212 * @return Update list
213 */
214 public List<Update> getUpdates() {
/*
P/P * Method: List getUpdates()
*
* Preconditions:
* init'ed(this.updates)
*
* Postconditions:
* return_value == &new ArrayList(getUpdates#1)
* new ArrayList(getUpdates#1) num objects == 1
*/
215 return new ArrayList<Update>(updates);
216 }
217
218 /**
219 * Is the specified component to be updated?
220 *
221 * @param update Component to check
222 *
223 * @return true iif the component needs to be updated
224 */
225 public boolean isEnabled(final Update update) {
/*
P/P * Method: bool isEnabled(Update)
*
* Preconditions:
* this.enabled != null
*
* Presumptions:
* java.util.Map:get(...)@226 != null
*
* Postconditions:
* init'ed(return_value)
*/
226 return enabled.get(update);
227 }
228
229 /**
230 * Is the component at the specified index to be updated?
231 *
232 * @param rowIndex Component index to check
233 *
234 * @return true iif the component needs to be updated
235 */
236 public boolean isEnabled(final int rowIndex) {
/*
P/P * Method: bool isEnabled(int)
*
* Preconditions:
* this.enabled != null
* this.updates != null
*
* Postconditions:
* init'ed(return_value)
*/
237 return isEnabled(updates.get(rowIndex));
238 }
239
240 /**
241 * Adds an update to the list.
242 *
243 * @param update Update to add
244 */
245 public void addRow(final Update update) {
/*
P/P * Method: void addRow(Update)
*
* Preconditions:
* this.updates != null
* update != null
*/
246 updates.add(update);
247 update.addUpdateListener(this);
248 fireTableRowsInserted(updates.indexOf(update), updates.indexOf(update));
249 }
250
251 /**
252 * Removes a specified row from the list.
253 *
254 * @param row Row to remove
255 */
256 public void removeRow(final int row) {
/*
P/P * Method: void removeRow(int)
*
* Preconditions:
* this.updates != null
*
* Presumptions:
* java.util.List:get(...)@257 != null
*/
257 updates.get(row).removeUpdateListener(this);
258 updates.remove(row);
259 fireTableRowsDeleted(row, row);
260 }
261
262 /**
263 * Returns the index of the specified update.
264 *
265 * @param update Update to get index of
266 *
267 * @return Index of the update or -1 if not found.
268 */
269 public int indexOf(final Update update) {
/*
P/P * Method: int indexOf(Update)
*
* Preconditions:
* this.updates != null
*
* Postconditions:
* init'ed(return_value)
*/
270 return updates.indexOf(update);
271 }
272
273 /** {@inheritDoc} */
274 @Override
275 public void updateStatusChange(final Update update, final UpdateStatus status) {
/*
P/P * Method: void updateStatusChange(Update, UpdateStatus)
*
* Preconditions:
* this.updates != null
*/
276 fireTableCellUpdated(updates.indexOf(update), 3);
277 }
278
279 /** {@inheritDoc} */
280 @Override
281 public void updateProgressChange(final Update update, final float progress) {
/*
P/P * Method: void updateProgressChange(Update, float)
*
* Preconditions:
* this.updates != null
*/
282 fireTableCellUpdated(updates.indexOf(update), 3);
283 }
284 }
285
SofCheck Inspector Build Version : 2.17854
| UpdateTableModel.java |
2009-Jun-25 01:54:24 |
| UpdateTableModel.class |
2009-Sep-02 17:04:16 |