View Javadoc

1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
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  /***
30   * A general purpose table model for entry attributes.
31   * 
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   * @version $Rev: 159259 $
34   */
35  public class AttributesTableModel extends AbstractTableModel
36  {
37      private static final long serialVersionUID = 3256443603340310841L;
38      /*** name for the key column */
39      public static final String KEY_COL = "Keys";
40      /*** name for the values column */
41      public static final String VAL_COL = "Values";
42  
43      /*** list of attribute ids */
44      private final transient ArrayList keyList;
45      /*** list of attribute values */
46      private final transient ArrayList valList;
47  
48      /*** the attributes for the entry */
49      private final Attributes entry;
50      /*** the unique id of the entry  */
51      private final BigInteger id;
52      /*** the distinguished name of the entry */
53      private final String dn;
54      /*** whether or not the model is mutable */
55      private boolean isMutable = true;
56  
57  
58      /***
59       * Creates a table model for entry attributes.
60       *
61       * @param entry the entry to create a model for
62       * @param id the id for the entry
63       * @param dn the distinguished name of the entry
64       * @param isMutable whether or not the model can be changed
65       */
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     /***
109      * @see AbstractTableModel#getColumnName(int)
110      */
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     /***
130      * @see javax.swing.table.AbstractTableModel#getRowCount()
131      */
132     public int getRowCount()
133     {
134         return keyList.size();
135     }
136 
137 
138     /***
139      * @see javax.swing.table.AbstractTableModel#getColumnCount()
140      */
141     public int getColumnCount()
142     {
143         return 2;
144     }
145 
146 
147     /***
148      * @see AbstractTableModel#getColumnClass(int)
149      */
150     public Class getColumnClass( int c )
151     {
152         return String.class;
153     }
154 
155 
156     /***
157      * @see AbstractTableModel#isCellEditable(int, int)
158      */
159     public boolean isCellEditable( int row, int col )
160     {
161         return isMutable;
162     }
163 
164 
165     /***
166      * @see AbstractTableModel#getValueAt(int, int)
167      */
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     /***
191      * @see AbstractTableModel#setValueAt(Object, int, int)
192      */
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     /***
221      * Gets the distinguished name of the entry.
222      *
223      * @return the distinguished name of the entry
224      */
225     public String getEntryDn()
226     {
227         return dn;
228     }
229     
230     
231     /***
232      * Gets the unique id for the entry.
233      *
234      * @return the unique id for the entry
235      */
236     public BigInteger getEntryId()
237     {
238         return id;
239     }
240 
241 
242     /***
243      * Deletes a row within the table model.
244      *
245      * @param row the row index to delete
246      */
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     /***
261      * Inserts an attribute key/value into the table model.
262      *
263      * @param row the row index to insert into
264      * @param key the key of the attr to insert
265      * @param val the value of the attr to insert
266      */
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 }