View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.core.partition.impl.btree.gui;
21  
22  
23  import java.util.ArrayList;
24  
25  import javax.swing.table.AbstractTableModel;
26  
27  import org.apache.directory.server.core.entry.ServerEntry;
28  import org.apache.directory.shared.ldap.entry.EntryAttribute;
29  import org.apache.directory.shared.ldap.entry.Value;
30  
31  
32  /**
33   * A general purpose table model for entry attributes.
34   * 
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   * @version $Rev: 655126 $
37   */
38  public class AttributesTableModel extends AbstractTableModel
39  {
40      private static final long serialVersionUID = 3256443603340310841L;
41      /** name for the key column */
42      public static final String KEY_COL = "Keys";
43      /** name for the values column */
44      public static final String VAL_COL = "Values";
45  
46      /** list of attribute ids */
47      private final transient ArrayList<Object> keyList;
48      /** list of attribute values */
49      private final transient ArrayList<Object> valList;
50  
51      /** the attributes for the entry */
52      private final ServerEntry entry;
53      /** the unique id of the entry  */
54      private final Long id;
55      /** the distinguished name of the entry */
56      private final String dn;
57      /** whether or not the model is mutable */
58      private boolean isMutable = true;
59  
60  
61      /**
62       * Creates a table model for entry attributes.
63       *
64       * @param entry the entry to create a model for
65       * @param id the id for the entry
66       * @param dn the distinguished name of the entry
67       * @param isMutable whether or not the model can be changed
68       */
69      public AttributesTableModel( ServerEntry entry, Long id, String dn, boolean isMutable)
70      {
71          this.dn = dn;
72          this.id = id;
73          this.entry = entry;
74          this.isMutable = isMutable;
75  
76          int rowCount = 0;
77  
78          for ( EntryAttribute attribute:entry )
79          {
80              String attrId = attribute.getId();
81              rowCount = rowCount + entry.get( attrId ).size();
82          }
83  
84          keyList = new ArrayList<Object>( rowCount );
85          valList = new ArrayList<Object>( rowCount );
86  
87          for ( EntryAttribute attribute:entry )
88          {
89              String key = attribute.getId();
90  
91              for ( Value<?> value:attribute )
92              {
93                  keyList.add( attribute.getId() );
94                  valList.add( value.get() );
95              }
96          }
97      }
98  
99  
100     /**
101      * @see AbstractTableModel#getColumnName(int)
102      */
103     public String getColumnName( int col )
104     {
105         if ( col == 0 )
106         {
107             return KEY_COL;
108         }
109         else if ( col == 1 )
110         {
111             return VAL_COL;
112         }
113         else
114         {
115             throw new RuntimeException( "There can only be 2 columns at index " + "0 and at 1" );
116         }
117     }
118 
119 
120     /**
121      * @see javax.swing.table.AbstractTableModel#getRowCount()
122      */
123     public int getRowCount()
124     {
125         return keyList.size();
126     }
127 
128 
129     /**
130      * @see javax.swing.table.AbstractTableModel#getColumnCount()
131      */
132     public int getColumnCount()
133     {
134         return 2;
135     }
136 
137 
138     /**
139      * @see AbstractTableModel#getColumnClass(int)
140      */
141     public Class<String> getColumnClass( int c )
142     {
143         return String.class;
144     }
145 
146 
147     /**
148      * @see AbstractTableModel#isCellEditable(int, int)
149      */
150     public boolean isCellEditable( int row, int col )
151     {
152         return isMutable;
153     }
154 
155 
156     /**
157      * @see AbstractTableModel#getValueAt(int, int)
158      */
159     public Object getValueAt( int row, int col )
160     {
161         if ( row >= keyList.size() )
162         {
163             return ( "NULL" );
164         }
165 
166         if ( getColumnName( col ).equals( KEY_COL ) )
167         {
168             return keyList.get( row );
169         }
170         else if ( getColumnName( col ).equals( VAL_COL ) )
171         {
172             return valList.get( row );
173         }
174         else
175         {
176             throw new RuntimeException( "You didn't correctly set col names" );
177         }
178     }
179 
180 
181     /**
182      * @see AbstractTableModel#setValueAt(Object, int, int)
183      */
184     public void setValue( Object val, int row, int col )
185     {
186         ArrayList<Object> list = null;
187 
188         if ( col > 1 || col < 0 )
189         {
190             return;
191         }
192         else if ( col == 0 )
193         {
194             list = keyList;
195         }
196         else
197         {
198             list = valList;
199         }
200 
201         if ( row >= keyList.size() )
202         {
203             return;
204         }
205 
206         list.set( row, val );
207         fireTableCellUpdated( row, col );
208     }
209 
210 
211     /**
212      * Gets the distinguished name of the entry.
213      *
214      * @return the distinguished name of the entry
215      */
216     public String getEntryDn()
217     {
218         return dn;
219     }
220 
221 
222     /**
223      * Gets the unique id for the entry.
224      *
225      * @return the unique id for the entry
226      */
227     public Long getEntryId()
228     {
229         return id;
230     }
231 
232 
233     /**
234      * Deletes a row within the table model.
235      *
236      * @param row the row index to delete
237      */
238     public void delete( int row )
239     {
240         if ( row >= keyList.size() || row < 0 )
241         {
242             return;
243         }
244 
245         keyList.remove( row );
246         valList.remove( row );
247         fireTableRowsDeleted( row, row );
248     }
249 
250 
251     /**
252      * Inserts an attribute key/value into the table model.
253      *
254      * @param row the row index to insert into
255      * @param key the key of the attr to insert
256      * @param val the value of the attr to insert
257      */
258     public void insert( int row, Object key, Object val )
259     {
260         if ( row >= keyList.size() || row < 0 )
261         {
262             return;
263         }
264 
265         keyList.add( row, key );
266         valList.add( row, val );
267         fireTableRowsInserted( row, row );
268     }
269 }