001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.server.core.partition.impl.btree.gui;
021    
022    
023    import java.util.ArrayList;
024    
025    import javax.swing.table.AbstractTableModel;
026    
027    import org.apache.directory.server.i18n.I18n;
028    import org.apache.directory.shared.ldap.entry.EntryAttribute;
029    import org.apache.directory.shared.ldap.entry.ServerEntry;
030    import org.apache.directory.shared.ldap.entry.Value;
031    
032    
033    /**
034     * A general purpose table model for entry attributes.
035     * 
036     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037     * @version $Rev: 927146 $
038     */
039    public class AttributesTableModel extends AbstractTableModel
040    {
041        private static final long serialVersionUID = 3256443603340310841L;
042        /** name for the key column */
043        public static final String KEY_COL = "Keys";
044        /** name for the values column */
045        public static final String VAL_COL = "Values";
046    
047        /** list of attribute ids */
048        private final transient ArrayList<Object> keyList;
049        /** list of attribute values */
050        private final transient ArrayList<Object> valList;
051    
052        /** the attributes for the entry */
053        private final ServerEntry entry;
054        /** the unique id of the entry  */
055        private final Long id;
056        /** the distinguished name of the entry */
057        private final String dn;
058        /** whether or not the model is mutable */
059        private boolean isMutable = true;
060    
061    
062        /**
063         * Creates a table model for entry attributes.
064         *
065         * @param entry the entry to create a model for
066         * @param id the id for the entry
067         * @param dn the distinguished name of the entry
068         * @param isMutable whether or not the model can be changed
069         */
070        public AttributesTableModel( ServerEntry entry, Long id, String dn, boolean isMutable)
071        {
072            this.dn = dn;
073            this.id = id;
074            this.entry = entry;
075            this.isMutable = isMutable;
076    
077            int rowCount = 0;
078    
079            for ( EntryAttribute attribute:entry )
080            {
081                String attrId = attribute.getId();
082                rowCount = rowCount + entry.get( attrId ).size();
083            }
084    
085            keyList = new ArrayList<Object>( rowCount );
086            valList = new ArrayList<Object>( rowCount );
087    
088            for ( EntryAttribute attribute:entry )
089            {
090                String key = attribute.getId();
091    
092                for ( Value<?> value:attribute )
093                {
094                    keyList.add( attribute.getId() );
095                    valList.add( value.get() );
096                }
097            }
098        }
099    
100    
101        /**
102         * @see AbstractTableModel#getColumnName(int)
103         */
104        public String getColumnName( int col )
105        {
106            if ( col == 0 )
107            {
108                return KEY_COL;
109            }
110            else if ( col == 1 )
111            {
112                return VAL_COL;
113            }
114            else
115            {
116                throw new RuntimeException( I18n.err( I18n.ERR_728 ) );
117            }
118        }
119    
120    
121        /**
122         * @see javax.swing.table.AbstractTableModel#getRowCount()
123         */
124        public int getRowCount()
125        {
126            return keyList.size();
127        }
128    
129    
130        /**
131         * @see javax.swing.table.AbstractTableModel#getColumnCount()
132         */
133        public int getColumnCount()
134        {
135            return 2;
136        }
137    
138    
139        /**
140         * @see AbstractTableModel#getColumnClass(int)
141         */
142        public Class<String> getColumnClass( int c )
143        {
144            return String.class;
145        }
146    
147    
148        /**
149         * @see AbstractTableModel#isCellEditable(int, int)
150         */
151        public boolean isCellEditable( int row, int col )
152        {
153            return isMutable;
154        }
155    
156    
157        /**
158         * @see AbstractTableModel#getValueAt(int, int)
159         */
160        public Object getValueAt( int row, int col )
161        {
162            if ( row >= keyList.size() )
163            {
164                return ( "NULL" );
165            }
166    
167            if ( getColumnName( col ).equals( KEY_COL ) )
168            {
169                return keyList.get( row );
170            }
171            else if ( getColumnName( col ).equals( VAL_COL ) )
172            {
173                return valList.get( row );
174            }
175            else
176            {
177                throw new RuntimeException( I18n.err( I18n.ERR_729 ) );
178            }
179        }
180    
181    
182        /**
183         * @see AbstractTableModel#setValueAt(Object, int, int)
184         */
185        public void setValue( Object val, int row, int col )
186        {
187            ArrayList<Object> list = null;
188    
189            if ( col > 1 || col < 0 )
190            {
191                return;
192            }
193            else if ( col == 0 )
194            {
195                list = keyList;
196            }
197            else
198            {
199                list = valList;
200            }
201    
202            if ( row >= keyList.size() )
203            {
204                return;
205            }
206    
207            list.set( row, val );
208            fireTableCellUpdated( row, col );
209        }
210    
211    
212        /**
213         * Gets the distinguished name of the entry.
214         *
215         * @return the distinguished name of the entry
216         */
217        public String getEntryDn()
218        {
219            return dn;
220        }
221    
222    
223        /**
224         * Gets the unique id for the entry.
225         *
226         * @return the unique id for the entry
227         */
228        public Long getEntryId()
229        {
230            return id;
231        }
232    
233    
234        /**
235         * Deletes a row within the table model.
236         *
237         * @param row the row index to delete
238         */
239        public void delete( int row )
240        {
241            if ( row >= keyList.size() || row < 0 )
242            {
243                return;
244            }
245    
246            keyList.remove( row );
247            valList.remove( row );
248            fireTableRowsDeleted( row, row );
249        }
250    
251    
252        /**
253         * Inserts an attribute key/value into the table model.
254         *
255         * @param row the row index to insert into
256         * @param key the key of the attr to insert
257         * @param val the value of the attr to insert
258         */
259        public void insert( int row, Object key, Object val )
260        {
261            if ( row >= keyList.size() || row < 0 )
262            {
263                return;
264            }
265    
266            keyList.add( row, key );
267            valList.add( row, val );
268            fireTableRowsInserted( row, row );
269        }
270    }