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 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) 2006-2010 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. package com.dmdirc.addons.ui_swing.dialogs.prefs;
  23. import com.dmdirc.updater.UpdateChecker;
  24. import com.dmdirc.updater.UpdateComponent;
  25. import java.util.ArrayList;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. import javax.swing.table.AbstractTableModel;
  30. /**
  31. * Update component table model
  32. */
  33. public class UpdateTableModel extends AbstractTableModel {
  34. /**
  35. * A version number for this class. It should be changed whenever the class
  36. * structure is changed (or anything else that would prevent serialized
  37. * objects being unserialized with the new class).
  38. */
  39. private static final long serialVersionUID = 3;
  40. /** Update component list. */
  41. private final List<UpdateComponent> updates;
  42. /** Enabled list. */
  43. private Map<UpdateComponent, Boolean> enabled;
  44. /**
  45. * Instantiates a new table model.
  46. */
  47. public UpdateTableModel() {
  48. this(new ArrayList<UpdateComponent>());
  49. }
  50. /**
  51. * Instantiates a new table model.
  52. *
  53. * @param updates Update components to show
  54. */
  55. public UpdateTableModel(final List<UpdateComponent> updates) {
  56. this.updates = new ArrayList<UpdateComponent>(updates);
  57. this.enabled = new HashMap<UpdateComponent, Boolean>();
  58. for (UpdateComponent update : this.updates) {
  59. enabled.put(update, UpdateChecker.isEnabled(update));
  60. }
  61. }
  62. /** {@inheritDoc} */
  63. @Override
  64. public int getRowCount() {
  65. synchronized (updates) {
  66. return updates.size();
  67. }
  68. }
  69. /** {@inheritDoc} */
  70. @Override
  71. public int getColumnCount() {
  72. return 3;
  73. }
  74. /** {@inheritDoc} */
  75. @Override
  76. public String getColumnName(final int columnIndex) {
  77. switch (columnIndex) {
  78. case 0:
  79. return "Update Component";
  80. case 1:
  81. return "Enabled?";
  82. case 2:
  83. return "Version";
  84. default:
  85. throw new IllegalArgumentException("Unknown column: " +
  86. columnIndex);
  87. }
  88. }
  89. /** {@inheritDoc} */
  90. @Override
  91. public Class<?> getColumnClass(final int columnIndex) {
  92. switch (columnIndex) {
  93. case 0:
  94. return String.class;
  95. case 1:
  96. return Boolean.class;
  97. case 2:
  98. return Integer.class;
  99. default:
  100. throw new IllegalArgumentException("Unknown column: " +
  101. columnIndex);
  102. }
  103. }
  104. /** {@inheritDoc} */
  105. @Override
  106. public boolean isCellEditable(final int rowIndex, final int columnIndex) {
  107. return columnIndex == 1;
  108. }
  109. /** {@inheritDoc} */
  110. @Override
  111. public Object getValueAt(int rowIndex, int columnIndex) {
  112. synchronized (updates) {
  113. if (updates.size() <= rowIndex) {
  114. throw new IndexOutOfBoundsException(rowIndex + " >= " +
  115. updates.size());
  116. }
  117. if (rowIndex < 0) {
  118. throw new IllegalArgumentException("Must specify a positive integer");
  119. }
  120. switch (columnIndex) {
  121. case 0:
  122. return updates.get(rowIndex).getFriendlyName();
  123. case 1:
  124. return enabled.get(updates.get(rowIndex));
  125. case 2:
  126. return updates.get(rowIndex).getFriendlyVersion();
  127. default:
  128. throw new IllegalArgumentException("Unknown column: " +
  129. columnIndex);
  130. }
  131. }
  132. }
  133. /** {@inheritDoc} */
  134. @Override
  135. public void setValueAt(final Object aValue, final int rowIndex,
  136. final int columnIndex) {
  137. synchronized (updates) {
  138. if (updates.size() <= rowIndex) {
  139. throw new IndexOutOfBoundsException(rowIndex + " >= " +
  140. updates.size());
  141. }
  142. if (rowIndex < 0) {
  143. throw new IllegalArgumentException("Must specify a positive integer");
  144. }
  145. switch (columnIndex) {
  146. case 1:
  147. enabled.put(updates.get(rowIndex), (Boolean) aValue);
  148. break;
  149. default:
  150. throw new IllegalArgumentException("Unknown column: " +
  151. columnIndex);
  152. }
  153. fireTableCellUpdated(rowIndex, columnIndex);
  154. }
  155. }
  156. /**
  157. * Adds a update component to the model.
  158. *
  159. * @param component update component to add
  160. */
  161. public void add(final UpdateComponent component) {
  162. synchronized (updates) {
  163. updates.add(component);
  164. fireTableRowsInserted(updates.size() - 1, updates.size() - 1);
  165. }
  166. }
  167. /**
  168. * Removes a update component to the model.
  169. *
  170. * @param component update component to remove
  171. */
  172. public void remove(final UpdateComponent component) {
  173. synchronized (updates) {
  174. remove(updates.indexOf(component));
  175. }
  176. }
  177. /**
  178. * Removes a update component to the model.
  179. *
  180. * @param index Index of the update component to remove
  181. */
  182. public void remove(final int index) {
  183. synchronized (updates) {
  184. if (index != -1) {
  185. updates.remove(index);
  186. fireTableRowsDeleted(index, index);
  187. }
  188. }
  189. }
  190. /**
  191. * Retrieves the update component that corresponds to the specified row.
  192. *
  193. * @since 0.6.3m2
  194. * @param row The row number
  195. * @return The corresponding update component
  196. */
  197. public UpdateComponent getComponent(final int row) {
  198. return updates.get(row);
  199. }
  200. }