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