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-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027
028package org.opends.guitools.controlpanel.datamodel;
029
030import java.util.ArrayList;
031import java.util.Comparator;
032import java.util.HashSet;
033import java.util.Set;
034import java.util.TreeSet;
035
036import org.opends.messages.AdminToolMessages;
037import org.forgerock.i18n.LocalizableMessage;
038
039/**
040 * Table Model used to store information about indexes.  It is used basically
041 * by the tables that appear on the right side of the 'Manage Indexes...'
042 * dialog when the user clicks on 'Indexes' or 'VLV Indexes'.
043 *
044 */
045public abstract class AbstractIndexTableModel extends SortableTableModel
046implements Comparator<AbstractIndexDescriptor>
047{
048  private static final long serialVersionUID = -5131878622200568636L;
049  private Set<AbstractIndexDescriptor> data = new HashSet<>();
050  private ArrayList<String[]> dataArray = new ArrayList<>();
051  private ArrayList<AbstractIndexDescriptor> indexArray = new ArrayList<>();
052  private final String[] COLUMN_NAMES = getColumnNames();
053  /**
054   * The sort column of the table.
055   */
056  protected int sortColumn;
057  /**
058   * Whether the sorting is ascending or descending.
059   */
060  protected boolean sortAscending = true;
061  private ControlPanelInfo info;
062
063
064  /**
065   * Sets the data for this table model.
066   * @param newData the data for this table model.
067   * @param info the control panel info.
068   */
069  public void setData(Set<AbstractIndexDescriptor> newData,
070      ControlPanelInfo info)
071  {
072    this.info = info;
073    data.clear();
074    data.addAll(newData);
075    updateDataArray();
076    fireTableDataChanged();
077  }
078
079  /**
080   * Updates the table model contents and sorts its contents depending on the
081   * sort options set by the user.
082   */
083  public void forceResort()
084  {
085    updateDataArray();
086    fireTableDataChanged();
087  }
088
089
090
091  /** {@inheritDoc} */
092  public int getColumnCount()
093  {
094    return COLUMN_NAMES.length;
095  }
096
097  /** {@inheritDoc} */
098  public int getRowCount()
099  {
100    return dataArray.size();
101  }
102
103  /** {@inheritDoc} */
104  public Object getValueAt(int row, int col)
105  {
106    return dataArray.get(row)[col];
107  }
108
109  /** {@inheritDoc} */
110  public String getColumnName(int col) {
111    return COLUMN_NAMES[col];
112  }
113
114
115  /**
116   * Returns whether the sort is ascending or descending.
117   * @return <CODE>true</CODE> if the sort is ascending and <CODE>false</CODE>
118   * otherwise.
119   */
120  public boolean isSortAscending()
121  {
122    return sortAscending;
123  }
124
125  /**
126   * Sets whether to sort ascending of descending.
127   * @param sortAscending whether to sort ascending or descending.
128   */
129  public void setSortAscending(boolean sortAscending)
130  {
131    this.sortAscending = sortAscending;
132  }
133
134  /**
135   * Returns the column index used to sort.
136   * @return the column index used to sort.
137   */
138  public int getSortColumn()
139  {
140    return sortColumn;
141  }
142
143  /**
144   * Sets the column index used to sort.
145   * @param sortColumn column index used to sort..
146   */
147  public void setSortColumn(int sortColumn)
148  {
149    this.sortColumn = sortColumn;
150  }
151
152  /**
153   * Returns the index in the specified row.
154   * @param row the row.
155   * @return the index in the specified row.
156   */
157  public AbstractIndexDescriptor getIndexAt(int row)
158  {
159    return indexArray.get(row);
160  }
161
162  /**
163   * Returns the message to be displayed in the cell if an index must be
164   * rebuilt.
165   * @param index the index to be analyzed.
166   * @return the message to be displayed in the cell if an index must be
167   * rebuilt.
168   */
169  protected LocalizableMessage getRebuildRequiredString(AbstractIndexDescriptor index)
170  {
171    if (info.mustReindex(index))
172    {
173      return AdminToolMessages.INFO_INDEX_MUST_BE_REBUILT_CELL_VALUE.get();
174    }
175    else
176    {
177      return AdminToolMessages.INFO_INDEX_MUST_NOT_BE_REBUILT_CELL_VALUE.get();
178    }
179  }
180
181  /**
182   * Compares the names of the two indexes.
183   * @param i1 the first index.
184   * @param i2 the second index.
185   * @return the alphabetical comparison of the names of both indexes.
186   */
187  protected int compareNames(AbstractIndexDescriptor i1,
188      AbstractIndexDescriptor i2)
189  {
190    return i1.getName().compareTo(i2.getName());
191  }
192
193  /**
194   * Compares the rebuild messages for the two indexes.
195   * @param i1 the first index.
196   * @param i2 the second index.
197   * @return the alphabetical comparison of the rebuild required message of both
198   * indexes.
199   */
200  protected int compareRebuildRequired(AbstractIndexDescriptor i1,
201      AbstractIndexDescriptor i2)
202  {
203    return getRebuildRequiredString(i1).compareTo(getRebuildRequiredString(i2));
204  }
205
206  /**
207   * Updates the array data.  This includes resorting it.
208   */
209  private void updateDataArray()
210  {
211    TreeSet<AbstractIndexDescriptor> sortedSet = new TreeSet<>(this);
212    sortedSet.addAll(data);
213    dataArray.clear();
214    indexArray.clear();
215    for (AbstractIndexDescriptor index : sortedSet)
216    {
217      String[] s = getLine(index);
218      dataArray.add(s);
219      indexArray.add(index);
220    }
221  }
222
223
224  /**
225   * Returns the column names of the table.
226   * @return the column names of the table.
227   */
228  protected abstract String[] getColumnNames();
229  /**
230   * Returns the different cell values for a given index in a String array.
231   * @param index the index.
232   * @return the different cell values for a given index in a String array.
233   */
234  protected abstract String[] getLine(AbstractIndexDescriptor index);
235}
236