123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- /*
- * Copyright (c) 2006-2013 DMDirc Developers
- *
- * 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.components;
-
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.List;
-
- import javax.swing.AbstractListModel;
-
- /**
- * Generic list model, closely following the DefaultListModel API.
- *
- * @param <T> Generic type of the list
- */
- public class GenericListModel<T> extends AbstractListModel {
-
- /**
- * 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 = -4227892376992714545L;
- /** Data stored in list. */
- private final List<T> list;
-
- /**
- * Instantiates an empty generic list model.
- */
- public GenericListModel() {
- super();
-
- this.list = Collections.synchronizedList(new ArrayList<T>());
- }
-
- /**
- * Instantiates a list model containing the specified list items.
- *
- * @param list Data to be included in the model
- */
- public GenericListModel(final List<T> list) {
- super();
-
- this.list = Collections.synchronizedList(new ArrayList<T>(list));
- }
-
- /** {@inheritDoc} */
- @Override
- public int getSize() {
- return list.size();
- }
-
- /**
- * Returns the specified index in the list.
- *
- * @param index Index of the item to return
- *
- * @return Item at index
- */
- public T get(final int index) {
- return list.get(index);
- }
-
- /**
- * Checks whether the list mode is empty.
- *
- * @return true iif the is the list if empty
- */
- public boolean isEmpty() {
- return list.isEmpty();
- }
-
- /**
- * Checks whether this list model contains the specified object.
- *
- * @param object Object to check for in the model
- *
- * @return true iif the model object is in this model
- */
- public boolean contains(final T object) {
- return list.contains(object);
- }
-
- /**
- * Checks the first index of the speficied option.
- *
- * @param object Object to check the index of
- *
- * @return first index of the object or -1 if not found
- */
- public int indexOf(final T object) {
- return list.indexOf(object);
- }
-
- /**
- * Checks the last index of the speficied option.
- *
- * @param object Object to check the index of
- *
- * @return last index of the object or -1 if not found
- */
- public int lastIndexOf(final T object) {
- return list.lastIndexOf(object);
- }
-
- /**
- * Sets the object at the specified index to be the specified object.
- *
- * @param index Index of the object
- * @param object Object to set
- */
- public void set(final int index, final T object) {
- list.set(index, object);
- fireContentsChanged(this, index, index);
- }
-
- /**
- * Removes the object at the specified index.
- *
- * @param index Index to remove
- */
- public void remove(final int index) {
- list.remove(index);
- fireIntervalRemoved(this, index, index);
- }
-
- /**
- * Adds the specified object at the specified index.
- *
- * @param index Index to insert object at
- * @param object Object to insert
- */
- public void add(final int index, final T object) {
- list.add(index, object);
- fireIntervalAdded(this, index, index);
- }
-
- /**
- * Adds the specified object.
- *
- * @param object Object to add
- */
- public void add(final T object) {
- final int index = list.size();
- list.add(object);
- fireIntervalAdded(this, index, index);
- }
-
- /**
- * Removes the specified object.
- *
- * @param obj Object to remove
- *
- * @return true iif object was removed
- */
- public boolean remove(final T obj) {
- final int index = indexOf(obj);
- final boolean succes = list.remove(obj);
- if (index >= 0) {
- fireIntervalRemoved(this, index, index);
- }
- return succes;
- }
-
- /**
- * Clears this model of all items.
- */
- public void clear() {
- final int lastIndex = list.size() - 1;
- list.clear();
- if (lastIndex >= 0) {
- fireIntervalRemoved(this, 0, lastIndex);
- }
- }
-
- /** {@inheritDoc} */
- @Override
- public String toString() {
- return list.toString();
- }
-
- /**
- * Removes the specified range of items from the list.
- *
- * @param start Index to start removing
- * @param end Index to stop removing
- */
- public void removeRange(final int start, final int end) {
- if (start > end) {
- throw new IllegalArgumentException("start must be greater than "
- + "or equal to end");
- }
- for (int i = end; i >= start; i--) {
- list.remove(i);
- }
- fireIntervalRemoved(this, start, end);
- }
-
- /**
- * Adds all the objects in the specified collection to this list.
- *
- * @param collection Collection to add
- */
- public void addAll(final Collection<T> collection) {
- list.addAll(collection);
- }
-
- /**
- * Adds all the objects in the specified collection to this list at the
- * specified index.
- *
- * @param index Index to add the items
- * @param collection Collection to add
- */
- public void addAll(final int index, final Collection<T> collection) {
- list.addAll(index, collection);
- }
-
- /** {@inheritDoc} */
- @Override
- public Object getElementAt(final int index) {
- return get(index);
- }
- }
|