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-2015 ForgeRock AS.
016 */
017
018package org.opends.guitools.controlpanel.datamodel;
019
020import java.util.ArrayList;
021import java.util.Comparator;
022import java.util.HashSet;
023import java.util.Set;
024import java.util.TreeSet;
025
026import org.opends.messages.AdminToolMessages;
027import org.forgerock.i18n.LocalizableMessage;
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 *
034 */
035public abstract class AbstractIndexTableModel extends SortableTableModel
036implements Comparator<AbstractIndexDescriptor>
037{
038  private static final long serialVersionUID = -5131878622200568636L;
039  private Set<AbstractIndexDescriptor> data = new HashSet<>();
040  private ArrayList<String[]> dataArray = new ArrayList<>();
041  private ArrayList<AbstractIndexDescriptor> indexArray = new ArrayList<>();
042  private final String[] COLUMN_NAMES = getColumnNames();
043  /**
044   * The sort column of the table.
045   */
046  protected int sortColumn;
047  /**
048   * Whether the sorting is ascending or descending.
049   */
050  protected boolean sortAscending = true;
051  private ControlPanelInfo info;
052
053
054  /**
055   * Sets the data for this table model.
056   * @param newData the data for this table model.
057   * @param info the control panel info.
058   */
059  public void setData(Set<AbstractIndexDescriptor> newData,
060      ControlPanelInfo info)
061  {
062    this.info = info;
063    data.clear();
064    data.addAll(newData);
065    updateDataArray();
066    fireTableDataChanged();
067  }
068
069  /**
070   * Updates the table model contents and sorts its contents depending on the
071   * sort options set by the user.
072   */
073  public void forceResort()
074  {
075    updateDataArray();
076    fireTableDataChanged();
077  }
078
079
080
081  /** {@inheritDoc} */
082  public int getColumnCount()
083  {
084    return COLUMN_NAMES.length;
085  }
086
087  /** {@inheritDoc} */
088  public int getRowCount()
089  {
090    return dataArray.size();
091  }
092
093  /** {@inheritDoc} */
094  public Object getValueAt(int row, int col)
095  {
096    return dataArray.get(row)[col];
097  }
098
099  /** {@inheritDoc} */
100  public String getColumnName(int col) {
101    return COLUMN_NAMES[col];
102  }
103
104
105  /**
106   * Returns whether the sort is ascending or descending.
107   * @return <CODE>true</CODE> if the sort is ascending and <CODE>false</CODE>
108   * otherwise.
109   */
110  public boolean isSortAscending()
111  {
112    return sortAscending;
113  }
114
115  /**
116   * Sets whether to sort ascending of descending.
117   * @param sortAscending whether to sort ascending or descending.
118   */
119  public void setSortAscending(boolean sortAscending)
120  {
121    this.sortAscending = sortAscending;
122  }
123
124  /**
125   * Returns the column index used to sort.
126   * @return the column index used to sort.
127   */
128  public int getSortColumn()
129  {
130    return sortColumn;
131  }
132
133  /**
134   * Sets the column index used to sort.
135   * @param sortColumn column index used to sort..
136   */
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  /**
197   * Updates the array data.  This includes resorting it.
198   */
199  private void updateDataArray()
200  {
201    TreeSet<AbstractIndexDescriptor> sortedSet = new TreeSet<>(this);
202    sortedSet.addAll(data);
203    dataArray.clear();
204    indexArray.clear();
205    for (AbstractIndexDescriptor index : sortedSet)
206    {
207      String[] s = getLine(index);
208      dataArray.add(s);
209      indexArray.add(index);
210    }
211  }
212
213
214  /**
215   * Returns the column names of the table.
216   * @return the column names of the table.
217   */
218  protected abstract String[] getColumnNames();
219  /**
220   * Returns the different cell values for a given index in a String array.
221   * @param index the index.
222   * @return the different cell values for a given index in a String array.
223   */
224  protected abstract String[] getLine(AbstractIndexDescriptor index);
225}
226