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.awt.Color;
24 import java.awt.Font;
25 import java.awt.Frame;
26 import java.awt.Panel;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29
30 import javax.swing.BorderFactory;
31 import javax.swing.BoxLayout;
32 import javax.swing.ButtonGroup;
33 import javax.swing.JButton;
34 import javax.swing.JDialog;
35 import javax.swing.JLabel;
36 import javax.swing.JOptionPane;
37 import javax.swing.JPanel;
38 import javax.swing.JRadioButton;
39 import javax.swing.JScrollPane;
40 import javax.swing.JTabbedPane;
41 import javax.swing.JTable;
42 import javax.swing.JTextArea;
43 import javax.swing.JTextField;
44 import javax.swing.border.TitledBorder;
45 import javax.swing.table.DefaultTableModel;
46
47 import org.apache.directory.server.xdbm.IndexEntry;
48 import org.apache.directory.server.core.cursor.Cursor;
49 import org.apache.directory.server.xdbm.ForwardIndexEntry;
50 import org.apache.directory.server.xdbm.Index;
51 import org.apache.directory.shared.ldap.util.ExceptionUtils;
52 import org.apache.directory.shared.ldap.NotImplementedException;
53
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57
58
59
60
61
62
63
64 public class IndexDialog<K,O> extends JDialog
65 {
66 private static final Logger LOG = LoggerFactory.getLogger( IndexDialog.class );
67
68 private static final long serialVersionUID = 3689917253680445238L;
69
70 public static final String DEFAULT_CURSOR = "Default";
71 public static final String EQUALITY_CURSOR = "Equality";
72 public static final String GREATER_CURSOR = "Greater";
73 public static final String LESS_CURSOR = "Less";
74 public static final String REGEX_CURSOR = "Regex";
75
76 private Panel mainPnl = new Panel();
77 private JTabbedPane tabbedPane = new JTabbedPane();
78 private JPanel listPnl = new JPanel();
79 private JPanel cursorPnl = new JPanel();
80 private JPanel resultsPnl = new JPanel();
81 private JScrollPane jScrollPane2 = new JScrollPane();
82 private JTable resultsTbl = new JTable();
83 private JPanel buttonPnl = new JPanel();
84 private JButton doneBut = new JButton();
85 private JLabel jLabel1 = new JLabel();
86 private JTextField keyText = new JTextField();
87 private JLabel jLabel2 = new JLabel();
88 private JButton scanBut = new JButton();
89
90 private Index<K,O> index = null;
91
92
93 public IndexDialog( Frame parent, boolean modal, Index<K,O> index )
94 {
95 super( parent, modal );
96 this.index = index;
97 initGUI();
98 }
99
100
101 public IndexDialog( Index<K,O> index )
102 {
103 super();
104 this.index = index;
105 initGUI();
106 }
107
108
109
110
111
112
113 private void initGUI()
114 {
115 addWindowListener( new java.awt.event.WindowAdapter()
116 {
117 public void windowClosing( java.awt.event.WindowEvent evt )
118 {
119 closeDialog();
120 }
121 } );
122
123 pack();
124 setTitle( "Index On Attribute '" + index.getAttribute().getName() + "'" );
125 setBounds( new java.awt.Rectangle( 0, 0, 512, 471 ) );
126 getContentPane().add( mainPnl, java.awt.BorderLayout.CENTER );
127 mainPnl.setLayout( new java.awt.BorderLayout() );
128 mainPnl.add( tabbedPane, java.awt.BorderLayout.CENTER );
129 tabbedPane.add( listPnl, "Listing" );
130 listPnl.setLayout( new java.awt.GridBagLayout() );
131
132 RadioButtonListener radioListener = new RadioButtonListener();
133 JRadioButton radioDefault = new JRadioButton( DEFAULT_CURSOR );
134 radioDefault.setActionCommand( DEFAULT_CURSOR );
135 radioDefault.setSelected( true );
136 radioDefault.addActionListener( radioListener );
137
138 JRadioButton radioEquality = new JRadioButton( EQUALITY_CURSOR );
139 radioEquality.setActionCommand( EQUALITY_CURSOR );
140 radioEquality.addActionListener( radioListener );
141
142 JRadioButton radioGreater = new JRadioButton( GREATER_CURSOR );
143 radioGreater.setActionCommand( GREATER_CURSOR );
144 radioGreater.addActionListener( radioListener );
145
146 JRadioButton radioLess = new JRadioButton( LESS_CURSOR );
147 radioLess.setActionCommand( LESS_CURSOR );
148 radioLess.addActionListener( radioListener );
149
150 JRadioButton radioRegex = new JRadioButton( REGEX_CURSOR );
151 radioRegex.setActionCommand( REGEX_CURSOR );
152 radioRegex.addActionListener( radioListener );
153
154 ButtonGroup group = new ButtonGroup();
155 group.add( radioDefault );
156 group.add( radioEquality );
157 group.add( radioGreater );
158 group.add( radioLess );
159 group.add( radioRegex );
160
161 JPanel radioPanel = new JPanel();
162 radioPanel.setLayout( new BoxLayout( radioPanel, BoxLayout.X_AXIS ) );
163 radioPanel.add( radioDefault );
164 radioPanel.add( radioEquality );
165 radioPanel.add( radioGreater );
166 radioPanel.add( radioLess );
167 radioPanel.add( radioRegex );
168
169 listPnl.add( cursorPnl, new java.awt.GridBagConstraints( 0, 0, 1, 1, 1.0, 0.15,
170 java.awt.GridBagConstraints.NORTH, java.awt.GridBagConstraints.BOTH, new java.awt.Insets( 15, 0, 30, 0 ),
171 0, 0 ) );
172 listPnl.add( resultsPnl, new java.awt.GridBagConstraints( 0, 1, 1, 1, 1.0, 0.8,
173 java.awt.GridBagConstraints.CENTER, java.awt.GridBagConstraints.BOTH, new java.awt.Insets( 0, 0, 0, 0 ), 0,
174 0 ) );
175 listPnl.add( buttonPnl, new java.awt.GridBagConstraints( 0, 2, 1, 1, 1.0, 0.05,
176 java.awt.GridBagConstraints.CENTER, java.awt.GridBagConstraints.BOTH, new java.awt.Insets( 0, 0, 0, 0 ), 0,
177 0 ) );
178 cursorPnl.setLayout( new java.awt.GridBagLayout() );
179 cursorPnl.setBorder( javax.swing.BorderFactory.createTitledBorder( javax.swing.BorderFactory.createLineBorder(
180 new java.awt.Color( 153, 153, 153 ), 1 ), "Display Cursor Constraints",
181 javax.swing.border.TitledBorder.LEADING, javax.swing.border.TitledBorder.TOP, new java.awt.Font(
182 "SansSerif", 0, 14 ), new java.awt.Color( 60, 60, 60 ) ) );
183 cursorPnl.add( jLabel1, new java.awt.GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0,
184 java.awt.GridBagConstraints.WEST, java.awt.GridBagConstraints.NONE, new java.awt.Insets( 0, 15, 0, 10 ), 0,
185 0 ) );
186 cursorPnl.add( keyText, new java.awt.GridBagConstraints( 1, 1, 1, 1, 0.4, 0.0,
187 java.awt.GridBagConstraints.WEST, java.awt.GridBagConstraints.BOTH, new java.awt.Insets( 5, 5, 5, 236 ), 0,
188 0 ) );
189 cursorPnl.add( jLabel2, new java.awt.GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0,
190 java.awt.GridBagConstraints.WEST, java.awt.GridBagConstraints.NONE, new java.awt.Insets( 0, 15, 0, 10 ), 0,
191 0 ) );
192 cursorPnl.add( radioPanel,
193 new java.awt.GridBagConstraints( 1, 0, 1, 1, 0.4, 0.0, java.awt.GridBagConstraints.WEST,
194 java.awt.GridBagConstraints.NONE, new java.awt.Insets( 5, 5, 5, 0 ), 0, 0 ) );
195 resultsPnl.setLayout( new java.awt.BorderLayout() );
196 resultsPnl.setBorder( javax.swing.BorderFactory.createTitledBorder( javax.swing.BorderFactory.createLineBorder(
197 new java.awt.Color( 153, 153, 153 ), 1 ), "Scan Results", javax.swing.border.TitledBorder.LEADING,
198 javax.swing.border.TitledBorder.TOP, new java.awt.Font( "SansSerif", 0, 14 ), new java.awt.Color( 60, 60,
199 60 ) ) );
200 resultsPnl.add( jScrollPane2, java.awt.BorderLayout.CENTER );
201 jScrollPane2.getViewport().add( resultsTbl );
202 buttonPnl.setLayout( new java.awt.FlowLayout( java.awt.FlowLayout.CENTER, 15, 5 ) );
203 buttonPnl.add( doneBut );
204 buttonPnl.add( scanBut );
205 doneBut.setText( "Done" );
206 doneBut.addActionListener( new ActionListener()
207 {
208 public void actionPerformed( ActionEvent e )
209 {
210 closeDialog();
211 }
212 } );
213
214 jLabel1.setText( "Key Constraint:" );
215 keyText.setText( "" );
216 keyText.setMinimumSize( new java.awt.Dimension( 130, 20 ) );
217 keyText.setPreferredSize( new java.awt.Dimension( 130, 20 ) );
218 keyText.setMaximumSize( new java.awt.Dimension( 130, 20 ) );
219 keyText.setFont( new java.awt.Font( "SansSerif", java.awt.Font.PLAIN, 14 ) );
220 keyText.setSize( new java.awt.Dimension( 130, 20 ) );
221 jLabel2.setText( "Cursor Type:" );
222
223 scanBut.setText( "Scan" );
224 scanBut.addActionListener( new ActionListener()
225 {
226 public void actionPerformed( ActionEvent e )
227 {
228
229 doScan( ( K ) keyText.getText(), selectedCursorType );
230 }
231 } );
232
233 doScan( null, DEFAULT_CURSOR );
234 }
235
236 private String selectedCursorType = DEFAULT_CURSOR;
237
238 class RadioButtonListener implements ActionListener
239 {
240 public void actionPerformed( ActionEvent e )
241 {
242 if ( e.getActionCommand().equals( DEFAULT_CURSOR ) )
243 {
244 selectedCursorType = DEFAULT_CURSOR;
245 }
246 else if ( e.getActionCommand().equals( EQUALITY_CURSOR ) )
247 {
248 selectedCursorType = EQUALITY_CURSOR;
249 }
250 else if ( e.getActionCommand().equals( GREATER_CURSOR ) )
251 {
252 selectedCursorType = GREATER_CURSOR;
253 }
254 else if ( e.getActionCommand().equals( LESS_CURSOR ) )
255 {
256 selectedCursorType = LESS_CURSOR;
257 }
258 else if ( e.getActionCommand().equals( REGEX_CURSOR ) )
259 {
260 selectedCursorType = REGEX_CURSOR;
261 }
262 }
263 }
264
265
266 private void closeDialog()
267 {
268 setVisible( false );
269 dispose();
270 }
271
272
273 public boolean doScan( K key, String scanType )
274 {
275 if ( key == null && ! scanType.equals( DEFAULT_CURSOR ) )
276 {
277 JOptionPane.showMessageDialog( null, "Cannot use a " + scanType + " scan type with a null key constraint.",
278 "Missing Key Constraint", JOptionPane.ERROR_MESSAGE );
279 return false;
280 }
281
282 Object[] cols = new Object[2];
283 Object[] row;
284 cols[0] = "Keys ( Attribute Value )";
285 cols[1] = "Values ( Entry Id )";
286 DefaultTableModel model = new DefaultTableModel( cols, 0 );
287 int count = 0;
288
289 try
290 {
291 Cursor<IndexEntry<K, O>> list;
292
293 if ( scanType.equals( EQUALITY_CURSOR ) )
294 {
295 list = index.forwardCursor( key );
296 list.beforeFirst();
297 while ( list.next() )
298 {
299 IndexEntry<K,O> rec = list.get();
300 row = new Object[2];
301 row[0] = rec.getValue();
302 row[1] = rec.getId();
303 model.addRow( row );
304 count++;
305 }
306 }
307 else if ( scanType.equals( GREATER_CURSOR ) )
308 {
309 list = index.forwardCursor();
310 ForwardIndexEntry<K, O> entry = new ForwardIndexEntry<K, O>();
311 entry.setValue( key );
312 list.before( entry );
313 while ( list.next() )
314 {
315 IndexEntry<K,O> rec = list.get();
316 row = new Object[2];
317 row[0] = rec.getValue();
318 row[1] = rec.getId();
319 model.addRow( row );
320 count++;
321 }
322 }
323 else if ( scanType.equals( LESS_CURSOR ) )
324 {
325 list = index.forwardCursor();
326 ForwardIndexEntry<K, O> entry = new ForwardIndexEntry<K, O>();
327 entry.setValue( key );
328 list.after( entry );
329 while ( list.previous() )
330 {
331 IndexEntry<K,O> rec = list.get();
332 row = new Object[2];
333 row[0] = rec.getValue();
334 row[1] = rec.getId();
335 model.addRow( row );
336 count++;
337 }
338 }
339 else if ( scanType.equals( REGEX_CURSOR ) )
340 {
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357 throw new NotImplementedException();
358 }
359 else
360 {
361 list = index.forwardCursor();
362 while ( list.next() )
363 {
364 IndexEntry<K,O> rec = list.get();
365 row = new Object[2];
366 row[0] = rec.getValue();
367 row[1] = rec.getId();
368 model.addRow( row );
369 count++;
370 }
371 }
372
373 resultsTbl.setModel( model );
374 resultsPnl.setBorder( BorderFactory.createTitledBorder( BorderFactory.createLineBorder( new Color( 153,
375 153, 153 ), 1 ), "Scan Results: " + count, TitledBorder.LEADING, TitledBorder.TOP, new Font(
376 "SansSerif", 0, 14 ), new Color( 60, 60, 60 ) ) );
377
378 if ( isVisible() )
379 {
380 validate();
381 }
382 }
383 catch ( Exception e )
384 {
385 String msg = ExceptionUtils.getStackTrace( e );
386
387 if ( msg.length() > 1024 )
388 {
389 msg = msg.substring( 0, 1024 ) + "\n. . . TRUNCATED . . .";
390 }
391
392 msg = "Error while scanning index " + "on attribute " + index.getAttribute() + " using a " + scanType
393 + " cursor type with a key constraint of '" + key + "':\n" + msg;
394
395 LOG.error( msg, e );
396 JTextArea area = new JTextArea();
397 area.setText( msg );
398 JOptionPane.showMessageDialog( null, area, "Index Scan Error", JOptionPane.ERROR_MESSAGE );
399 return false;
400 }
401
402 return true;
403 }
404
405
406 public static void show( Index index )
407 {
408
409 IndexDialog dialog = new IndexDialog( index );
410 dialog.setVisible( true );
411 }
412 }