001 /* $Id: SortedSetListModel.java 12 2007-07-27 09:48:53Z ringler $
002 * This class is part of the de.mospace.swing library.
003 * Copyright (C) 2005-2006 Moritz Ringler
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License
007 * as published by the Free Software Foundation; either version 2
008 * of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018 */
019 package de.mospace.swing;
020
021 import javax.swing.*;
022 import java.util.*;
023
024
025 /** Wraps a sorted set as a ListModel.
026 *
027 * @version $Revision: 12 $ ($Date: 2007-07-27 11:48:53 +0200 (Fr, 27 Jul 2007) $)
028 * @author Moritz Ringler
029 **/
030 public class SortedSetListModel extends AbstractListModel {
031 private final SortedSet set;
032
033 public SortedSetListModel(){
034 set = new TreeSet();
035 }
036
037 public SortedSetListModel(SortedSet s){
038 set = s;
039 }
040
041 public int getSize(){
042 return set.size();
043 }
044
045 public synchronized Object getElementAt(int index){
046 if(index < 0 || index >= getSize()){
047 throw new ArrayIndexOutOfBoundsException(index);
048 }
049 Iterator it = set.iterator();
050 for(int i=0; i<index; i++){
051 it.next();
052 }
053 return it.next();
054 }
055
056 public synchronized void add(Object element){
057 if(set.add(element)){
058 fireContentsChanged(this, 0, getSize());
059 }
060 }
061
062 public synchronized void remove(Object element){
063 if(set.remove(element)){
064 fireContentsChanged(this, 0, getSize());
065 }
066 }
067
068 public synchronized void clear(){
069 set.clear();
070 fireContentsChanged(this, 0, getSize());
071 }
072
073 }