001/* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt 010 * or http://forgerock.org/license/CDDLv1.0.html. 011 * See the License for the specific language governing permissions 012 * and limitations under the License. 013 * 014 * When distributing Covered Code, include this CDDL HEADER in each 015 * file and include the License file at legal-notices/CDDLv1_0.txt. 016 * If applicable, add the following below this CDDL HEADER, with the 017 * fields enclosed by brackets "[]" replaced with your own identifying 018 * information: 019 * Portions Copyright [yyyy] [name of copyright owner] 020 * 021 * CDDL HEADER END 022 * 023 * 024 * Copyright 2008 Sun Microsystems, Inc. 025 * Portions Copyright 2015 ForgeRock AS. 026 */ 027 028package org.opends.guitools.controlpanel.datamodel; 029 030import java.util.Collection; 031import java.util.Comparator; 032import java.util.SortedSet; 033import java.util.TreeSet; 034 035import javax.swing.AbstractListModel; 036 037/** 038 * Note: this implementation does not call automatically fireContentsChanged, 039 * its up to the caller of the different methods of this implementation to 040 * call it explicitly. This is done because in general there is a series 041 * of calls to the add/remove methods and a single call to notify that 042 * things have changed is enough. 043 * 044 * @param <T> 045 */ 046public class SortableListModel<T> extends AbstractListModel 047{ 048 private static final long serialVersionUID = 3241258779190228463L; 049 private SortedSet<T> data = new TreeSet<>(); 050 051 /** 052 * Returns the size of the list model. 053 * @return the size of the list model. 054 */ 055 public int getSize() 056 { 057 return data.size(); 058 } 059 060 /** 061 * Sets the comparator to be used to sort the list. 062 * @param comp the comparator. 063 */ 064 public void setComparator(Comparator<T> comp) 065 { 066 SortedSet<T> copy = data; 067 data = new TreeSet<>(comp); 068 data.addAll(copy); 069 } 070 071 /** 072 * Returns the element at the specified index. 073 * @param i the index of the element. 074 * @return the element at the specified index. 075 */ 076 public T getElementAt(int i) 077 { 078 int index = 0; 079 for (T element : data) 080 { 081 if (index == i) 082 { 083 return element; 084 } 085 index++; 086 } 087 throw new ArrayIndexOutOfBoundsException( 088 "The index "+i+" is bigger than the maximum size: "+getSize()); 089 } 090 091 /** 092 * Adds a value to the list model. 093 * @param value the value to be added. 094 */ 095 public void add(T value) 096 { 097 data.add(value); 098 } 099 100 /** 101 * Removes a value from the list model. 102 * @param value the value to be removed. 103 * @return <CODE>true</CODE> if the element was on the list and 104 * <CODE>false</CODE> otherwise. 105 */ 106 public boolean remove(T value) 107 { 108 return data.remove(value); 109 } 110 111 /** 112 * Clears the list model. 113 * 114 */ 115 public void clear() 116 { 117 data.clear(); 118 } 119 120 /** 121 * Adds all the elements in the collection to the list model. 122 * @param newData the collection containing the elements to be added. 123 */ 124 public void addAll(Collection<T> newData) 125 { 126 data.addAll(newData); 127 } 128 129 /** {@inheritDoc} */ 130 public void fireContentsChanged(Object source, int index0, int index1) 131 { 132 super.fireContentsChanged(source, index0, index1); 133 } 134 135 /** 136 * Returns the data in this list model ordered. 137 * @return the data in this list model ordered. 138 */ 139 public SortedSet<T> getData() 140 { 141 return new TreeSet<>(data); 142 } 143}