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-2009 Sun Microsystems, Inc.
015 * Portions Copyright 2015 ForgeRock AS.
016 */
017
018package org.opends.guitools.controlpanel.datamodel;
019
020import static org.opends.messages.AdminToolMessages.*;
021
022import java.net.InetAddress;
023import java.util.ArrayList;
024import java.util.Comparator;
025import java.util.HashSet;
026import java.util.Set;
027import java.util.TreeSet;
028
029/**
030 * The table model used by the table that displays the connection handlers.
031 *
032 */
033public class ConnectionHandlerTableModel extends SortableTableModel
034implements Comparator<ConnectionHandlerDescriptor>
035{
036  private static final long serialVersionUID = -1121308303480078376L;
037  private Set<ConnectionHandlerDescriptor> data = new HashSet<>();
038  private ArrayList<String[]> dataArray = new ArrayList<>();
039  private String[] COLUMN_NAMES;
040  private int sortColumn;
041  private boolean sortAscending = true;
042
043  /**
044   * Constructor for this table model.
045   */
046  public ConnectionHandlerTableModel()
047  {
048    this(true);
049  }
050
051  /**
052   * Constructor for this table model.
053   * @param wrapHeader whether to wrap the headers or not.
054   * monitoring information or not.
055   */
056  public ConnectionHandlerTableModel(boolean wrapHeader)
057  {
058    if (wrapHeader)
059    {
060      COLUMN_NAMES = new String[] {
061          getHeader(INFO_ADDRESS_PORT_COLUMN.get()),
062          getHeader(INFO_PROTOCOL_COLUMN.get()),
063          getHeader(INFO_STATE_COLUMN.get())
064      };
065    }
066    else
067    {
068      COLUMN_NAMES = new String[] {
069          INFO_ADDRESS_PORT_COLUMN.get().toString(),
070          INFO_PROTOCOL_COLUMN.get().toString(),
071          INFO_STATE_COLUMN.get().toString()
072      };
073    }
074  }
075
076  /**
077   * Sets the data for this table model.
078   * @param newData the data for this table model.
079   */
080  public void setData(Set<ConnectionHandlerDescriptor> newData)
081  {
082    if (!newData.equals(data))
083    {
084      data.clear();
085      data.addAll(newData);
086      updateDataArray();
087      fireTableDataChanged();
088    }
089  }
090
091  /**
092   * Updates the table model contents and sorts its contents depending on the
093   * sort options set by the user.
094   */
095  public void forceResort()
096  {
097    updateDataArray();
098    fireTableDataChanged();
099  }
100
101  /**
102   * Comparable implementation.
103   * @param desc1 the first listener descriptor to compare.
104   * @param desc2 the second listener descriptor to compare.
105   * @return 1 if according to the sorting options set by the user the first
106   * listener descriptor must be put before the second descriptor, 0 if they
107   * are equivalent in terms of sorting and -1 if the second descriptor must
108   * be put before the first descriptor.
109   */
110  public int compare(ConnectionHandlerDescriptor desc1,
111      ConnectionHandlerDescriptor desc2)
112  {
113    int result = 0;
114    if (sortColumn == 0)
115    {
116      if (desc1.getAddresses().equals(desc2.getAddresses()))
117      {
118        Integer port1 = Integer.valueOf(desc1.getPort());
119        Integer port2 = Integer.valueOf(desc2.getPort());
120        result = port1.compareTo(port2);
121      }
122      else
123      {
124        result = getAddressPortString(desc1).compareTo(
125            getAddressPortString(desc2));
126      }
127      if (result == 0)
128      {
129        result = getProtocolString(desc1).compareTo(
130            getProtocolString(desc2));
131      }
132
133      if (result == 0)
134      {
135        result = desc1.getState().compareTo(desc2.getState());
136      }
137    }
138    else if (sortColumn == 1)
139    {
140      result = getProtocolString(desc1).compareTo(
141          getProtocolString(desc2));
142
143      if (result == 0)
144      {
145        result = getAddressPortString(desc1).compareTo(
146            getAddressPortString(desc2));
147      }
148
149      if (result == 0)
150      {
151        result = desc1.getState().compareTo(desc2.getState());
152      }
153    }
154    else
155    {
156      result = desc1.getState().compareTo(desc2.getState());
157
158      if (result == 0)
159      {
160        result = getAddressPortString(desc1).compareTo(
161            getAddressPortString(desc2));
162      }
163
164      if (result == 0)
165      {
166        result = getProtocolString(desc1).compareTo(
167            getProtocolString(desc2));
168      }
169    }
170
171    if (!sortAscending)
172    {
173      result = -result;
174    }
175
176    return result;
177  }
178
179  /** {@inheritDoc} */
180  public int getColumnCount()
181  {
182    return 3;
183  }
184
185  /** {@inheritDoc} */
186  public int getRowCount()
187  {
188    return dataArray.size();
189  }
190
191  /** {@inheritDoc} */
192  public Object getValueAt(int row, int col)
193  {
194    return dataArray.get(row)[col];
195  }
196
197  /** {@inheritDoc} */
198  public String getColumnName(int col) {
199    return COLUMN_NAMES[col];
200  }
201
202
203  /**
204   * Returns whether the sort is ascending or descending.
205   * @return <CODE>true</CODE> if the sort is ascending and <CODE>false</CODE>
206   * otherwise.
207   */
208  public boolean isSortAscending()
209  {
210    return sortAscending;
211  }
212
213  /**
214   * Sets whether to sort ascending of descending.
215   * @param sortAscending whether to sort ascending or descending.
216   */
217  public void setSortAscending(boolean sortAscending)
218  {
219    this.sortAscending = sortAscending;
220  }
221
222  /**
223   * Returns the column index used to sort.
224   * @return the column index used to sort.
225   */
226  public int getSortColumn()
227  {
228    return sortColumn;
229  }
230
231  /**
232   * Sets the column index used to sort.
233   * @param sortColumn column index used to sort..
234   */
235  public void setSortColumn(int sortColumn)
236  {
237    this.sortColumn = sortColumn;
238  }
239
240  private String getAddressPortString(ConnectionHandlerDescriptor desc)
241  {
242    Set<InetAddress> addresses = desc.getAddresses();
243    if (!addresses.isEmpty())
244    {
245      StringBuilder buf = new StringBuilder();
246      buf.append("<html>");
247      boolean added = false;
248      for (InetAddress address : addresses)
249      {
250        if (added)
251        {
252          buf.append("<br>");
253        }
254        buf.append(address.getHostAddress());
255        added = true;
256        if (desc.getPort() > 0)
257        {
258          buf.append(":").append(desc.getPort());
259        }
260      }
261      return buf.toString();
262    }
263
264    if (desc.getPort() > 0)
265    {
266      return String.valueOf(desc.getPort());
267    }
268    return INFO_NOT_APPLICABLE_LABEL.get().toString();
269  }
270
271  private String getProtocolString(ConnectionHandlerDescriptor desc)
272  {
273    String returnValue;
274    switch (desc.getProtocol())
275    {
276    case OTHER:
277      returnValue = desc.getName();
278      break;
279    default:
280      returnValue = desc.getProtocol().getDisplayMessage().toString();
281      break;
282    }
283    return returnValue;
284  }
285
286  private void updateDataArray()
287  {
288    TreeSet<ConnectionHandlerDescriptor> sortedSet = new TreeSet<>(this);
289    sortedSet.addAll(data);
290    dataArray.clear();
291    for (ConnectionHandlerDescriptor desc : sortedSet)
292    {
293      String[] s = new String[3];
294      s[0] = getAddressPortString(desc);
295      s[1] = getProtocolString(desc);
296
297      switch (desc.getState())
298      {
299      case ENABLED:
300        s[2] = INFO_ENABLED_LABEL.get().toString();
301        break;
302
303      case DISABLED:
304        s[2] = INFO_DISABLED_LABEL.get().toString();
305        break;
306
307      case UNKNOWN:
308        s[2] = INFO_UNKNOWN_LABEL.get().toString();
309        break;
310
311      default:
312        throw new RuntimeException("Unknown state: "+desc.getState());
313      }
314      dataArray.add(s);
315    }
316  }
317}