You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UpdateTableModel.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.dialogs.updater;
  18. import com.dmdirc.updater.UpdateComponent;
  19. import com.dmdirc.updater.manager.CachingUpdateManager;
  20. import com.dmdirc.updater.manager.UpdateStatus;
  21. import com.dmdirc.updater.manager.UpdateStatusListener;
  22. import java.text.NumberFormat;
  23. import java.util.ArrayList;
  24. import java.util.Collections;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import javax.swing.table.AbstractTableModel;
  29. /**
  30. * Update table model.
  31. */
  32. public class UpdateTableModel extends AbstractTableModel implements UpdateStatusListener {
  33. /** A version number for this class. */
  34. private static final long serialVersionUID = 3;
  35. /** The update manager to use. */
  36. private final CachingUpdateManager updateManager;
  37. /** Data list. */
  38. private final List<UpdateComponent> updates = new ArrayList<>();
  39. /** Enabled list. */
  40. private final List<UpdateComponent> enabled = new ArrayList<>();
  41. /** Cached progress for each component. */
  42. private final Map<UpdateComponent, Double> progress = new HashMap<>();
  43. /** Number formatter. */
  44. private final NumberFormat formatter;
  45. /**
  46. * Creates a new instance of UpdateTableModel.
  47. *
  48. * @param updateManager The update manager to use
  49. * @param updates List of components to display
  50. */
  51. public UpdateTableModel(final CachingUpdateManager updateManager,
  52. final List<UpdateComponent> updates) {
  53. this.updateManager = updateManager;
  54. this.updateManager.addUpdateStatusListener(this);
  55. setUpdates(updates);
  56. formatter = NumberFormat.getNumberInstance();
  57. formatter.setMaximumFractionDigits(1);
  58. formatter.setMinimumFractionDigits(1);
  59. }
  60. /**
  61. * Sets the updates list.
  62. *
  63. * @param updates List of updates
  64. */
  65. public void setUpdates(final List<UpdateComponent> updates) {
  66. this.updates.clear();
  67. this.updates.addAll(updates);
  68. this.enabled.clear();
  69. this.enabled.addAll(updates);
  70. this.progress.clear();
  71. fireTableDataChanged();
  72. }
  73. @Override
  74. public int getRowCount() {
  75. return updates.size();
  76. }
  77. @Override
  78. public int getColumnCount() {
  79. return 4;
  80. }
  81. @Override
  82. public String getColumnName(final int columnIndex) {
  83. switch (columnIndex) {
  84. case 0:
  85. return "Update?";
  86. case 1:
  87. return "Component";
  88. case 2:
  89. return "New version";
  90. case 3:
  91. return "Status";
  92. default:
  93. throw new IllegalArgumentException("Unknown column: " + columnIndex);
  94. }
  95. }
  96. @Override
  97. public Class<?> getColumnClass(final int columnIndex) {
  98. switch (columnIndex) {
  99. case 0:
  100. return Boolean.class;
  101. case 1:
  102. return UpdateComponent.class;
  103. case 2:
  104. return String.class;
  105. case 3:
  106. return UpdateStatus.class;
  107. default:
  108. throw new IllegalArgumentException("Unknown column: "
  109. + columnIndex);
  110. }
  111. }
  112. @Override
  113. public boolean isCellEditable(final int rowIndex, final int columnIndex) {
  114. return columnIndex == 0;
  115. }
  116. @Override
  117. public Object getValueAt(final int rowIndex, final int columnIndex) {
  118. if (updates.size() <= rowIndex) {
  119. throw new IndexOutOfBoundsException(rowIndex + " >= "
  120. + updates.size());
  121. }
  122. if (rowIndex < 0) {
  123. throw new IllegalArgumentException("Must specify a positive integer");
  124. }
  125. final UpdateComponent component = updates.get(rowIndex);
  126. switch (columnIndex) {
  127. case 0:
  128. return enabled.contains(component);
  129. case 1:
  130. return component;
  131. case 2:
  132. return updateManager.getCheckResult(component).getUpdatedVersionName();
  133. case 3:
  134. final UpdateStatus status = updateManager.getStatus(component);
  135. final boolean hasProgress = progress.containsKey(component)
  136. && progress.get(component) > 0;
  137. return status.getDescription() + (hasProgress
  138. ? " (" + formatter.format(progress.get(component)) + "%)"
  139. : "");
  140. default:
  141. throw new IllegalArgumentException("Unknown column: "
  142. + columnIndex);
  143. }
  144. }
  145. @Override
  146. public void setValueAt(final Object aValue, final int rowIndex,
  147. final int columnIndex) {
  148. if (updates.size() <= rowIndex) {
  149. throw new IndexOutOfBoundsException(rowIndex + " >= " + updates.size());
  150. }
  151. if (rowIndex < 0) {
  152. throw new IllegalArgumentException("Must specify a positive integer");
  153. }
  154. switch (columnIndex) {
  155. case 0:
  156. enabled.add(updates.get(rowIndex));
  157. break;
  158. default:
  159. throw new IllegalArgumentException("Unknown column: "
  160. + columnIndex);
  161. }
  162. fireTableCellUpdated(rowIndex, columnIndex);
  163. }
  164. /**
  165. * Gets the update at the specified row.
  166. *
  167. * @param rowIndex Row to retrieve
  168. *
  169. * @return Specified Update
  170. */
  171. public UpdateComponent getUpdate(final int rowIndex) {
  172. return updates.get(rowIndex);
  173. }
  174. /**
  175. * Gets a list of all updates.
  176. *
  177. * @return Update list
  178. */
  179. public List<UpdateComponent> getUpdates() {
  180. return Collections.unmodifiableList(updates);
  181. }
  182. /**
  183. * Is the specified component to be updated?
  184. *
  185. * @param update Component to check
  186. *
  187. * @return true iif the component needs to be updated
  188. */
  189. public boolean isEnabled(final UpdateComponent update) {
  190. return enabled.contains(update);
  191. }
  192. /**
  193. * Is the component at the specified index to be updated?
  194. *
  195. * @param rowIndex Component index to check
  196. *
  197. * @return true iif the component needs to be updated
  198. */
  199. public boolean isEnabled(final int rowIndex) {
  200. return isEnabled(updates.get(rowIndex));
  201. }
  202. /**
  203. * Adds an update to the list.
  204. *
  205. * @param update Update to add
  206. */
  207. public void addRow(final UpdateComponent update) {
  208. updates.add(update);
  209. fireTableRowsInserted(updates.indexOf(update), updates.indexOf(update));
  210. }
  211. /**
  212. * Removes a specified row from the list.
  213. *
  214. * @param row Row to remove
  215. */
  216. public void removeRow(final int row) {
  217. updates.remove(row);
  218. fireTableRowsDeleted(row, row);
  219. }
  220. /**
  221. * Returns the index of the specified update.
  222. *
  223. * @param update Update to get index of
  224. *
  225. * @return Index of the update or -1 if not found.
  226. */
  227. public int indexOf(final UpdateComponent update) {
  228. return updates.indexOf(update);
  229. }
  230. @Override
  231. public void updateStatusChanged(final UpdateComponent component,
  232. final UpdateStatus status, final double progress) {
  233. this.progress.put(component, progress);
  234. fireTableCellUpdated(updates.indexOf(component), 3);
  235. }
  236. }