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-2016 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> type of the elements in this list model
035 */
036public class SortableListModel<T> extends AbstractListModel<T>
037{
038  private static final long serialVersionUID = 3241258779190228463L;
039  private SortedSet<T> data = new TreeSet<>();
040
041  @Override
042  public int getSize()
043  {
044    return data.size();
045  }
046
047  /**
048   * Sets the comparator to be used to sort the list.
049   * @param comp the comparator.
050   */
051  public void setComparator(Comparator<T> comp)
052  {
053    SortedSet<T> copy = data;
054    data = new TreeSet<>(comp);
055    data.addAll(copy);
056  }
057
058  @Override
059  public T getElementAt(int i)
060  {
061    int index = 0;
062    for (T element : data)
063    {
064      if (index == i)
065      {
066        return element;
067      }
068      index++;
069    }
070    throw new ArrayIndexOutOfBoundsException(
071        "The index "+i+" is bigger than the maximum size: "+getSize());
072  }
073
074  /**
075   * Adds a value to the list model.
076   * @param value the value to be added.
077   */
078  public void add(T value)
079  {
080    data.add(value);
081  }
082
083  /**
084   * Removes a value from the list model.
085   * @param value the value to be removed.
086   * @return <CODE>true</CODE> if the element was on the list and
087   * <CODE>false</CODE> otherwise.
088   */
089  public boolean remove(T value)
090  {
091    return data.remove(value);
092  }
093
094  /** Clears the list model. */
095  public void clear()
096  {
097    data.clear();
098  }
099
100  /**
101   * Adds all the elements in the collection to the list model.
102   * @param newData the collection containing the elements to be added.
103   */
104  public void addAll(Collection<T> newData)
105  {
106    data.addAll(newData);
107  }
108
109  @Override
110  public void fireContentsChanged(Object source, int index0, int index1)
111  {
112    super.fireContentsChanged(source, index0, index1);
113  }
114
115  /**
116   * Returns the data in this list model ordered.
117   * @return the data in this list model ordered.
118   */
119  public SortedSet<T> getData()
120  {
121    return new TreeSet<>(data);
122  }
123}