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 ForgeRock AS
026 */
027
028package org.opends.guitools.controlpanel.datamodel;
029
030
031import static com.forgerock.opendj.cli.Utils.wrapText;
032
033import javax.swing.table.AbstractTableModel;
034
035import org.opends.guitools.controlpanel.ui.ColorAndFontConstants;
036import org.opends.guitools.controlpanel.util.Utilities;
037import org.forgerock.i18n.LocalizableMessage;
038import org.opends.server.util.ServerConstants;
039
040/**
041 * A generic interface that must implement table models that are sortable.
042 */
043public abstract class SortableTableModel extends AbstractTableModel
044{
045  private static final long serialVersionUID = 123129879083L;
046
047  /**
048   * Returns whether the sort is ascending or descending.
049   * @return <CODE>true</CODE> if the sort is ascending and <CODE>false</CODE>
050   * otherwise.
051   */
052  public abstract boolean isSortAscending();
053
054  /**
055   * Sets whether to sort ascending of descending.
056   * @param sortAscending whether to sort ascending or descending.
057   */
058  public abstract void setSortAscending(boolean sortAscending);
059
060  /**
061   * Returns the column index used to sort.
062   * @return the column index used to sort.
063   */
064  public abstract int getSortColumn();
065
066  /**
067   * Sets the column index used to sort.
068   * @param sortColumn column index used to sort..
069   */
070  public abstract void setSortColumn(int sortColumn);
071
072  /**
073   * Updates the table model contents and sorts its contents depending on the
074   * sort options set by the user.
075   */
076  public abstract void forceResort();
077
078
079  /**
080   * Returns the header wrapped with the default line width.
081   * @param msg the header message value (with no HTML formatting).
082   * @return the header wrapped with the default line width.
083   */
084  protected String getHeader(LocalizableMessage msg)
085  {
086    return getHeader(msg, 15);
087  }
088
089  /**
090   * Returns the header wrapped with a certain line width.
091   * @param msg the header message value (with no HTML formatting).
092   * @param wrap the maximum line width before wrapping.
093   * @return the header wrapped with the specified line width.
094   */
095  protected String getHeader(LocalizableMessage msg, int wrap)
096  {
097    String text = msg.toString();
098    String wrappedText = wrapText(text, wrap);
099    wrappedText = wrappedText.replaceAll(ServerConstants.EOL, "<br>");
100    return "<html>"+Utilities.applyFont(wrappedText,
101        ColorAndFontConstants.headerFont);
102  }
103}