1 17 package org.apache.ldap.server.db.gui; 18 19 20 import javax.naming.NamingEnumeration ; 21 import javax.naming.NamingException ; 22 import javax.naming.directory.Attribute ; 23 import javax.naming.directory.Attributes ; 24 import javax.swing.table.AbstractTableModel ; 25 import java.math.BigInteger ; 26 import java.util.ArrayList ; 27 28 29 35 public class AttributesTableModel extends AbstractTableModel 36 { 37 private static final long serialVersionUID = 3256443603340310841L; 38 39 public static final String KEY_COL = "Keys"; 40 41 public static final String VAL_COL = "Values"; 42 43 44 private final transient ArrayList keyList; 45 46 private final transient ArrayList valList; 47 48 49 private final Attributes entry; 50 51 private final BigInteger id; 52 53 private final String dn; 54 55 private boolean isMutable = true; 56 57 58 66 public AttributesTableModel( Attributes entry, BigInteger id, String dn, 67 boolean isMutable ) 68 { 69 this.dn = dn; 70 this.id = id; 71 this.entry = entry; 72 this.isMutable = isMutable; 73 74 NamingEnumeration list = entry.getIDs(); 75 int rowCount = 0; 76 77 while( list.hasMoreElements() ) 78 { 79 String attrId = ( String ) list.nextElement(); 80 rowCount = rowCount + entry.get( attrId ).size(); 81 } 82 83 keyList = new ArrayList ( rowCount ); 84 valList = new ArrayList ( rowCount ); 85 86 list = this.entry.getIDs(); 87 while ( list.hasMoreElements() ) 88 { 89 String l_key = ( String ) list.nextElement(); 90 Attribute l_attr = this.entry.get( l_key ); 91 92 for ( int ii = 0; ii < l_attr.size(); ii++ ) 93 { 94 try 95 { 96 keyList.add( l_attr.getID() ); 97 valList.add( l_attr.get( ii ) ); 98 } 99 catch ( NamingException e ) 100 { 101 e.printStackTrace(); 102 } 103 } 104 } 105 } 106 107 108 111 public String getColumnName( int col ) 112 { 113 if ( col == 0 ) 114 { 115 return KEY_COL; 116 } 117 else if ( col == 1 ) 118 { 119 return VAL_COL; 120 } 121 else 122 { 123 throw new RuntimeException ("There can only be 2 columns at index " 124 + "0 and at 1"); 125 } 126 } 127 128 129 132 public int getRowCount() 133 { 134 return keyList.size(); 135 } 136 137 138 141 public int getColumnCount() 142 { 143 return 2; 144 } 145 146 147 150 public Class getColumnClass( int c ) 151 { 152 return String .class; 153 } 154 155 156 159 public boolean isCellEditable( int row, int col ) 160 { 161 return isMutable; 162 } 163 164 165 168 public Object getValueAt( int row, int col ) 169 { 170 if ( row >= keyList.size() ) 171 { 172 return ( "NULL" ); 173 } 174 175 if ( getColumnName( col ).equals( KEY_COL ) ) 176 { 177 return keyList.get( row ); 178 } 179 else if ( getColumnName( col ).equals( VAL_COL ) ) 180 { 181 return valList.get( row ); 182 } 183 else 184 { 185 throw new RuntimeException ( "You didn't correctly set col names" ); 186 } 187 } 188 189 190 193 public void setValue( Object val, int row, int col ) 194 { 195 ArrayList list = null; 196 197 if ( col > 1 || col < 0 ) 198 { 199 return; 200 } 201 else if ( col == 0 ) 202 { 203 list = keyList; 204 } 205 else 206 { 207 list = valList; 208 } 209 210 if ( row >= keyList.size() ) 211 { 212 return; 213 } 214 215 list.set( row, val ); 216 fireTableCellUpdated( row, col ); 217 } 218 219 220 225 public String getEntryDn() 226 { 227 return dn; 228 } 229 230 231 236 public BigInteger getEntryId() 237 { 238 return id; 239 } 240 241 242 247 public void delete( int row ) 248 { 249 if ( row >= keyList.size() || row < 0 ) 250 { 251 return; 252 } 253 254 keyList.remove( row ); 255 valList.remove( row ); 256 fireTableRowsDeleted( row, row ); 257 } 258 259 260 267 public void insert( int row, Object key, Object val ) 268 { 269 if ( row >= keyList.size() || row < 0 ) 270 { 271 return; 272 } 273 274 keyList.add( row, key ); 275 valList.add( row, val ); 276 fireTableRowsInserted( row, row ); 277 } 278 } 279 | Popular Tags |