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