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