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.partition.impl.btree.gui;
18  
19  
20  import java.math.BigInteger;
21  import java.util.ArrayList;
22  
23  import javax.naming.NamingEnumeration;
24  import javax.naming.NamingException;
25  import javax.naming.directory.Attribute;
26  import javax.naming.directory.Attributes;
27  import javax.swing.table.AbstractTableModel;
28  
29  
30  /***
31   * A general purpose table model for entry attributes.
32   * 
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   * @version $Rev: 264732 $
35   */
36  public class AttributesTableModel extends AbstractTableModel
37  {
38      private static final long serialVersionUID = 3256443603340310841L;
39      /*** name for the key column */
40      public static final String KEY_COL = "Keys";
41      /*** name for the values column */
42      public static final String VAL_COL = "Values";
43  
44      /*** list of attribute ids */
45      private final transient ArrayList keyList;
46      /*** list of attribute values */
47      private final transient ArrayList valList;
48  
49      /*** the attributes for the entry */
50      private final Attributes entry;
51      /*** the unique id of the entry  */
52      private final BigInteger id;
53      /*** the distinguished name of the entry */
54      private final String dn;
55      /*** whether or not the model is mutable */
56      private boolean isMutable = true;
57  
58  
59      /***
60       * Creates a table model for entry attributes.
61       *
62       * @param entry the entry to create a model for
63       * @param id the id for the entry
64       * @param dn the distinguished name of the entry
65       * @param isMutable whether or not the model can be changed
66       */
67      public AttributesTableModel( Attributes entry, BigInteger id, String dn,
68                                   boolean isMutable )
69      {
70          this.dn = dn;
71          this.id = id;
72          this.entry = entry;
73          this.isMutable = isMutable;
74          
75          NamingEnumeration list = entry.getIDs();
76          int rowCount = 0;
77          
78          while( list.hasMoreElements() )
79          {
80              String attrId = ( String ) list.nextElement();
81              rowCount = rowCount + entry.get( attrId ).size();
82          }
83          
84          keyList = new ArrayList( rowCount );
85          valList = new ArrayList( rowCount );
86  
87          list = this.entry.getIDs();
88          while ( list.hasMoreElements() )
89          {
90              String l_key = ( String ) list.nextElement();
91              Attribute l_attr = this.entry.get( l_key );
92              
93              for ( int ii = 0; ii < l_attr.size(); ii++ )
94              {
95                  try 
96                  {
97                      keyList.add( l_attr.getID() );
98                      valList.add( l_attr.get( ii ) );
99                  }
100                 catch ( NamingException e )
101                 {
102                     e.printStackTrace();
103                 }
104             }
105         }
106     }
107 
108 
109     /***
110      * @see AbstractTableModel#getColumnName(int)
111      */
112     public String getColumnName( int col )
113     {
114         if ( col == 0 )
115         {
116             return KEY_COL;
117         } 
118         else if ( col == 1 )
119         {
120             return VAL_COL;
121         } 
122         else 
123         {
124             throw new RuntimeException("There can only be 2 columns at index "
125                 + "0 and at 1");
126         }
127     }
128 
129 
130     /***
131      * @see javax.swing.table.AbstractTableModel#getRowCount()
132      */
133     public int getRowCount()
134     {
135         return keyList.size();
136     }
137 
138 
139     /***
140      * @see javax.swing.table.AbstractTableModel#getColumnCount()
141      */
142     public int getColumnCount()
143     {
144         return 2;
145     }
146 
147 
148     /***
149      * @see AbstractTableModel#getColumnClass(int)
150      */
151     public Class getColumnClass( int c )
152     {
153         return String.class;
154     }
155 
156 
157     /***
158      * @see AbstractTableModel#isCellEditable(int, int)
159      */
160     public boolean isCellEditable( int row, int col )
161     {
162         return isMutable;
163     }
164 
165 
166     /***
167      * @see AbstractTableModel#getValueAt(int, int)
168      */
169     public Object getValueAt( int row, int col )
170     {
171 		if ( row >= keyList.size() )
172         {
173 			return ( "NULL" );
174 		}
175 
176         if ( getColumnName( col ).equals( KEY_COL ) )
177         {
178             return keyList.get( row );
179         } 
180         else if ( getColumnName( col ).equals( VAL_COL ) )
181         {
182             return valList.get( row );
183         } 
184         else 
185         {
186             throw new RuntimeException( "You didn't correctly set col names" );
187         }
188     }
189 
190 
191     /***
192      * @see AbstractTableModel#setValueAt(Object, int, int)
193      */
194     public void setValue( Object val, int row, int col )
195     {
196         ArrayList list = null;
197         
198         if ( col > 1 || col < 0 )
199         {
200             return;
201         }
202         else if ( col == 0 )
203         {
204             list = keyList;
205         }
206         else 
207         {
208             list = valList;
209         }
210         
211         if ( row >= keyList.size() )
212         {
213             return;
214         }
215 
216         list.set( row, val );
217         fireTableCellUpdated( row, col );
218     }
219 
220 
221     /***
222      * Gets the distinguished name of the entry.
223      *
224      * @return the distinguished name of the entry
225      */
226     public String getEntryDn()
227     {
228         return dn;
229     }
230     
231     
232     /***
233      * Gets the unique id for the entry.
234      *
235      * @return the unique id for the entry
236      */
237     public BigInteger getEntryId()
238     {
239         return id;
240     }
241 
242 
243     /***
244      * Deletes a row within the table model.
245      *
246      * @param row the row index to delete
247      */
248     public void delete( int row )
249     {
250         if ( row >= keyList.size() || row < 0 )
251         {
252             return;
253         }
254         
255         keyList.remove( row );
256         valList.remove( row );
257         fireTableRowsDeleted( row, row );
258     }
259 
260 
261     /***
262      * Inserts an attribute key/value into the table model.
263      *
264      * @param row the row index to insert into
265      * @param key the key of the attr to insert
266      * @param val the value of the attr to insert
267      */
268     public void insert( int row, Object key, Object val )
269     {
270         if ( row >= keyList.size() || row < 0 )
271         {
272             return;
273         }
274         
275         keyList.add( row, key );
276         valList.add( row, val );
277         fireTableRowsInserted( row, row );
278     }
279 }