001 /*
002 * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003 *
004 * This file is part of the Java Data Mining Package (JDMP).
005 * See the NOTICE file distributed with this work for additional
006 * information regarding copyright ownership and licensing.
007 *
008 * JDMP is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation; either version 2
011 * of the License, or (at your option) any later version.
012 *
013 * JDMP is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU Lesser General Public License for more details.
017 *
018 * You should have received a copy of the GNU Lesser General Public
019 * License along with JDMP; if not, write to the
020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021 * Boston, MA 02110-1301 USA
022 */
023
024 package org.jdmp.core.util;
025
026 import java.util.ArrayList;
027 import java.util.Collection;
028 import java.util.Iterator;
029 import java.util.List;
030
031 import javax.swing.AbstractListModel;
032
033 public class DefaultObservableList<V> extends AbstractListModel implements ObservableList<V> {
034 private static final long serialVersionUID = -2636828198788691727L;
035
036 private List<V> values = null;
037
038 public DefaultObservableList() {
039 values = new ArrayList<V>();
040 }
041
042 public DefaultObservableList(List<V> values) {
043 this.values = values;
044 }
045
046 public synchronized V getElementAt(int index) {
047 if (index >= 0 && index < values.size()) {
048 return values.get(index);
049 } else {
050 return null;
051 }
052 }
053
054 public synchronized void clear() {
055 int size = values.size();
056 values.clear();
057 fireIntervalRemoved(this, 0, size - 1);
058 }
059
060 public synchronized int getSize() {
061 return values.size();
062 }
063
064 public synchronized int indexOf(V value) {
065 return values.indexOf(value);
066 }
067
068 public synchronized void add(V value) {
069 values.add(value);
070 fireIntervalAdded(this, values.size() - 1, values.size() - 1);
071 }
072
073 public void addAll(Collection<V> values) {
074 for (V v : values) {
075 add(v);
076 }
077 }
078
079 public synchronized boolean remove(V value) {
080 int index = values.indexOf(value);
081 boolean b = values.remove(value);
082 if (index >= 0) {
083 fireIntervalRemoved(this, index, index);
084 }
085 return b;
086 }
087
088 public synchronized Iterator<V> iterator() {
089 return values.iterator();
090 }
091
092 public boolean isEmpty() {
093 return values.isEmpty();
094 }
095
096 public Collection<V> toCollection() {
097 return values;
098 }
099
100 public String toString() {
101 return values.toString();
102 }
103
104 @Override
105 public void fireContentsChanged() {
106 fireContentsChanged(this, -1, -1);
107 }
108
109 }