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