123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- /*
- * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.addons.ui_swing.dialogs.prefs;
-
- import com.dmdirc.updater.UpdateChecker;
- import com.dmdirc.updater.UpdateComponent;
-
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import javax.swing.table.AbstractTableModel;
-
- /**
- * Update component table model
- */
- public class UpdateTableModel extends AbstractTableModel {
-
- /**
- * A version number for this class. It should be changed whenever the class
- * structure is changed (or anything else that would prevent serialized
- * objects being unserialized with the new class).
- */
- private static final long serialVersionUID = 3;
- /** Update component list. */
- private final List<UpdateComponent> updates;
- /** Enabled list. */
- private Map<UpdateComponent, Boolean> enabled;
-
- /**
- * Instantiates a new table model.
- */
- public UpdateTableModel() {
- this(new ArrayList<UpdateComponent>());
- }
-
- /**
- * Instantiates a new table model.
- *
- * @param updates Update components to show
- */
- public UpdateTableModel(final List<UpdateComponent> updates) {
- this.updates = new ArrayList<UpdateComponent>(updates);
- this.enabled = new HashMap<UpdateComponent, Boolean>();
-
- for (UpdateComponent update : this.updates) {
- enabled.put(update, UpdateChecker.isEnabled(update));
- }
- }
-
- /** {@inheritDoc} */
- @Override
- public int getRowCount() {
- synchronized (updates) {
- return updates.size();
- }
- }
-
- /** {@inheritDoc} */
- @Override
- public int getColumnCount() {
- return 3;
- }
-
- /** {@inheritDoc} */
- @Override
- public String getColumnName(final int columnIndex) {
- switch (columnIndex) {
- case 0:
- return "Update Component";
- case 1:
- return "Enabled?";
- case 2:
- return "Version";
- default:
- throw new IllegalArgumentException("Unknown column: " +
- columnIndex);
- }
- }
-
- /** {@inheritDoc} */
- @Override
- public Class<?> getColumnClass(final int columnIndex) {
- switch (columnIndex) {
- case 0:
- return String.class;
- case 1:
- return Boolean.class;
- case 2:
- return Integer.class;
- default:
- throw new IllegalArgumentException("Unknown column: " +
- columnIndex);
- }
- }
-
- /** {@inheritDoc} */
- @Override
- public boolean isCellEditable(final int rowIndex, final int columnIndex) {
- return columnIndex == 1;
- }
-
- /** {@inheritDoc} */
- @Override
- public Object getValueAt(int rowIndex, int columnIndex) {
- synchronized (updates) {
- if (updates.size() <= rowIndex) {
- throw new IndexOutOfBoundsException(rowIndex + " >= " +
- updates.size());
- }
- if (rowIndex < 0) {
- throw new IllegalArgumentException("Must specify a positive integer");
- }
- switch (columnIndex) {
- case 0:
- return updates.get(rowIndex).getFriendlyName();
- case 1:
- return enabled.get(updates.get(rowIndex));
- case 2:
- return updates.get(rowIndex).getFriendlyVersion();
- default:
- throw new IllegalArgumentException("Unknown column: " +
- columnIndex);
- }
- }
- }
-
- /** {@inheritDoc} */
- @Override
- public void setValueAt(final Object aValue, final int rowIndex,
- final int columnIndex) {
- synchronized (updates) {
- if (updates.size() <= rowIndex) {
- throw new IndexOutOfBoundsException(rowIndex + " >= " +
- updates.size());
- }
- if (rowIndex < 0) {
- throw new IllegalArgumentException("Must specify a positive integer");
- }
- switch (columnIndex) {
- case 1:
- enabled.put(updates.get(rowIndex), (Boolean) aValue);
- break;
- default:
- throw new IllegalArgumentException("Unknown column: " +
- columnIndex);
- }
- fireTableCellUpdated(rowIndex, columnIndex);
- }
- }
-
- /**
- * Adds a update component to the model.
- *
- * @param component update component to add
- */
- public void add(final UpdateComponent component) {
- synchronized (updates) {
- updates.add(component);
- fireTableRowsInserted(updates.size() - 1, updates.size() - 1);
- }
- }
-
- /**
- * Removes a update component to the model.
- *
- * @param component update component to remove
- */
- public void remove(final UpdateComponent component) {
- synchronized (updates) {
- remove(updates.indexOf(component));
- }
- }
-
- /**
- * Removes a update component to the model.
- *
- * @param index Index of the update component to remove
- */
- public void remove(final int index) {
- synchronized (updates) {
- if (index != -1) {
- updates.remove(index);
- fireTableRowsDeleted(index, index);
- }
- }
- }
-
- /**
- * Retrieves the update component that corresponds to the specified row.
- *
- * @since 0.6.3m2
- * @param row The row number
- * @return The corresponding update component
- */
- public UpdateComponent getComponent(final int row) {
- return updates.get(row);
- }
- }
|