1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
35
36
37
38 public class AttributesTableModel extends AbstractTableModel
39 {
40 private static final long serialVersionUID = 3256443603340310841L;
41
42 public static final String KEY_COL = "Keys";
43
44 public static final String VAL_COL = "Values";
45
46
47 private final transient ArrayList<Object> keyList;
48
49 private final transient ArrayList<Object> valList;
50
51
52 private final ServerEntry entry;
53
54 private final Long id;
55
56 private final String dn;
57
58 private boolean isMutable = true;
59
60
61
62
63
64
65
66
67
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
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
122
123 public int getRowCount()
124 {
125 return keyList.size();
126 }
127
128
129
130
131
132 public int getColumnCount()
133 {
134 return 2;
135 }
136
137
138
139
140
141 public Class<String> getColumnClass( int c )
142 {
143 return String.class;
144 }
145
146
147
148
149
150 public boolean isCellEditable( int row, int col )
151 {
152 return isMutable;
153 }
154
155
156
157
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
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
213
214
215
216 public String getEntryDn()
217 {
218 return dn;
219 }
220
221
222
223
224
225
226
227 public Long getEntryId()
228 {
229 return id;
230 }
231
232
233
234
235
236
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
253
254
255
256
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 }