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.awt.Frame;
21  import java.awt.Panel;
22  import java.awt.event.ActionEvent;
23  import java.awt.event.ActionListener;
24  
25  import javax.naming.NamingEnumeration;
26  import javax.swing.DefaultComboBoxModel;
27  import javax.swing.JButton;
28  import javax.swing.JComboBox;
29  import javax.swing.JDialog;
30  import javax.swing.JLabel;
31  import javax.swing.JOptionPane;
32  import javax.swing.JPanel;
33  import javax.swing.JScrollPane;
34  import javax.swing.JTabbedPane;
35  import javax.swing.JTable;
36  import javax.swing.JTextArea;
37  import javax.swing.JTextField;
38  import javax.swing.table.DefaultTableModel;
39  
40  import org.apache.ldap.common.util.ExceptionUtils;
41  import org.apache.ldap.common.util.StringTools;
42  import org.apache.ldap.server.partition.impl.btree.Index;
43  import org.apache.ldap.server.partition.impl.btree.IndexRecord;
44  import org.apache.regexp.RE;
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  
48  
49  /***
50   * A dialog showing index values.
51   *
52   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
53   * @version $Rev: 264732 $
54   */
55  public class IndexDialog extends JDialog
56  {
57      private static final Logger log = LoggerFactory.getLogger(IndexDialog.class);
58  
59      private static final long serialVersionUID = 3689917253680445238L;
60  
61      public static final String DEFAULT_CURSOR = "Default" ;
62      public static final String EQUALITY_CURSOR = "Equality" ;
63      public static final String GREATER_CURSOR = "Greater" ;
64      public static final String LESS_CURSOR = "Less" ;
65      public static final String REGEX_CURSOR = "Regex" ;
66  
67      private Panel m_mainPnl = new Panel();
68      private JTabbedPane m_tabbedPane = new JTabbedPane();
69      private JPanel m_listPnl = new JPanel();
70      private JPanel m_cursorPnl = new JPanel();
71      private JPanel m_resultsPnl = new JPanel();
72      private JScrollPane jScrollPane2 = new JScrollPane();
73      private JTable m_resultsTbl = new JTable();
74      private JPanel m_buttonPnl = new JPanel();
75      private JButton m_doneBut = new JButton();
76      private JLabel jLabel1 = new JLabel();
77      private JTextField m_keyText = new JTextField();
78      private JLabel jLabel2 = new JLabel();
79      private JComboBox m_cursorType = new JComboBox();
80      private JButton m_scanBut = new JButton();
81      private Index m_index = null ;
82  
83      /*** Creates new form JDialog */
84      public IndexDialog( Frame parent, boolean modal, Index a_index )
85      {
86          super ( parent, modal ) ;
87          m_index = a_index ;
88          initGUI() ;
89      }
90  
91      /***
92       * This method is called from within the constructor to initialize the
93       * form.
94       */
95      private void initGUI()
96      {
97          addWindowListener(new java.awt.event.WindowAdapter()
98          {
99              public void windowClosing(java.awt.event.WindowEvent evt)
100             {
101                 closeDialog();
102             }
103         });
104 
105         pack();
106         setTitle("Index On Attribute '" + m_index.getAttribute() + "'");
107         setBounds(new java.awt.Rectangle(0, 0, 512, 471));
108         getContentPane().add(m_mainPnl, java.awt.BorderLayout.CENTER);
109         m_mainPnl.setLayout(new java.awt.BorderLayout());
110         m_mainPnl.add(m_tabbedPane, java.awt.BorderLayout.CENTER);
111         m_tabbedPane.add(m_listPnl, "Listing");
112         m_listPnl.setLayout(new java.awt.GridBagLayout());
113         m_listPnl.add(
114             m_cursorPnl,
115             new java.awt.GridBagConstraints(
116                 0,
117                 0,
118                 1,
119                 1,
120                 1.0,
121                 0.15,
122                 java.awt.GridBagConstraints.NORTH,
123                 java.awt.GridBagConstraints.BOTH,
124                 new java.awt.Insets(15, 0, 30, 0),
125                 0,
126                 0));
127         m_listPnl.add(
128             m_resultsPnl,
129             new java.awt.GridBagConstraints(
130                 0,
131                 1,
132                 1,
133                 1,
134                 1.0,
135                 0.8,
136                 java.awt.GridBagConstraints.CENTER,
137                 java.awt.GridBagConstraints.BOTH,
138                 new java.awt.Insets(0, 0, 0, 0),
139                 0,
140                 0));
141         m_listPnl.add(
142             m_buttonPnl,
143             new java.awt.GridBagConstraints(
144                 0,
145                 2,
146                 1,
147                 1,
148                 1.0,
149                 0.05,
150                 java.awt.GridBagConstraints.CENTER,
151                 java.awt.GridBagConstraints.BOTH,
152                 new java.awt.Insets(0, 0, 0, 0),
153                 0,
154                 0));
155         m_cursorPnl.setLayout(new java.awt.GridBagLayout());
156         m_cursorPnl.setBorder(
157             javax.swing.BorderFactory.createTitledBorder(
158                 javax.swing.BorderFactory.createLineBorder(
159                     new java.awt.Color(153, 153, 153),
160                     1),
161                 "Display Cursor Constraints",
162                 javax.swing.border.TitledBorder.LEADING,
163                 javax.swing.border.TitledBorder.TOP,
164                 new java.awt.Font("SansSerif", 0, 14),
165                 new java.awt.Color(60, 60, 60)));
166         m_cursorPnl.add(
167             jLabel1,
168             new java.awt.GridBagConstraints(
169                 0,
170                 1,
171                 1,
172                 1,
173                 0.0,
174                 0.0,
175                 java.awt.GridBagConstraints.WEST,
176                 java.awt.GridBagConstraints.NONE,
177                 new java.awt.Insets(0, 15, 0, 10),
178                 0,
179                 0));
180         m_cursorPnl.add(
181             m_keyText,
182             new java.awt.GridBagConstraints(
183                 1,
184                 1,
185                 1,
186                 1,
187                 0.4,
188                 0.0,
189                 java.awt.GridBagConstraints.WEST,
190                 java.awt.GridBagConstraints.BOTH,
191                 new java.awt.Insets(5, 5, 5, 236),
192                 0,
193                 0));
194         m_cursorPnl.add(
195             jLabel2,
196             new java.awt.GridBagConstraints(
197                 0,
198                 0,
199                 1,
200                 1,
201                 0.0,
202                 0.0,
203                 java.awt.GridBagConstraints.WEST,
204                 java.awt.GridBagConstraints.NONE,
205                 new java.awt.Insets(0, 15, 0, 10),
206                 0,
207                 0));
208         m_cursorPnl.add(
209             m_cursorType,
210             new java.awt.GridBagConstraints(
211                 1,
212                 0,
213                 1,
214                 1,
215                 0.4,
216                 0.0,
217                 java.awt.GridBagConstraints.WEST,
218                 java.awt.GridBagConstraints.NONE,
219                 new java.awt.Insets(5, 5, 5, 0),
220                 0,
221                 0));
222         m_resultsPnl.setLayout(new java.awt.BorderLayout());
223         m_resultsPnl.setBorder(
224             javax.swing.BorderFactory.createTitledBorder(
225                 javax.swing.BorderFactory.createLineBorder(
226                     new java.awt.Color(153, 153, 153),
227                     1),
228                 "Scan Results",
229                 javax.swing.border.TitledBorder.LEADING,
230                 javax.swing.border.TitledBorder.TOP,
231                 new java.awt.Font("SansSerif", 0, 14),
232                 new java.awt.Color(60, 60, 60)));
233         m_resultsPnl.add(jScrollPane2, java.awt.BorderLayout.CENTER);
234         jScrollPane2.getViewport().add(m_resultsTbl);
235         m_buttonPnl.setLayout(
236             new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 15, 5));
237         m_buttonPnl.add(m_doneBut);
238         m_buttonPnl.add(m_scanBut);
239         m_doneBut.setText("Done");
240         m_doneBut.addActionListener(new ActionListener()
241         {
242             public void actionPerformed(ActionEvent e)
243             {
244                 closeDialog();
245             }
246         });
247 
248         jLabel1.setText("Key Constraint:");
249         m_keyText.setText("");
250         m_keyText.setMinimumSize(new java.awt.Dimension(130, 20));
251         m_keyText.setPreferredSize(new java.awt.Dimension(130, 20));
252         m_keyText.setMaximumSize(new java.awt.Dimension(130, 20));
253         m_keyText.setFont(
254             new java.awt.Font("SansSerif", java.awt.Font.PLAIN, 14));
255         m_keyText.setSize(new java.awt.Dimension(130, 20));
256         jLabel2.setText("Cursor Type:");
257         m_cursorType.setMaximumSize(new java.awt.Dimension(32767, 20));
258         m_cursorType.setMinimumSize(new java.awt.Dimension(126, 20));
259         m_cursorType.setPreferredSize(new java.awt.Dimension(130, 20));
260         DefaultComboBoxModel l_comboModel = new DefaultComboBoxModel();
261         l_comboModel.addElement(DEFAULT_CURSOR);
262         l_comboModel.addElement(EQUALITY_CURSOR);
263         l_comboModel.addElement(GREATER_CURSOR);
264         l_comboModel.addElement(LESS_CURSOR);
265         l_comboModel.addElement(REGEX_CURSOR);
266         m_cursorType.setModel(l_comboModel);
267         m_cursorType.setMaximumRowCount(5);
268         m_scanBut.setText("Scan");
269         m_scanBut.addActionListener(new ActionListener()
270         {
271             public void actionPerformed(ActionEvent e)
272             {
273                 doScan(
274                     m_keyText.getText(),
275                     (String) m_cursorType.getSelectedItem());
276             }
277         });
278 
279         doScan(null, DEFAULT_CURSOR);
280     }
281 
282 
283     private void closeDialog()
284     {
285         setVisible( false ) ;
286         dispose() ;
287     }
288 
289 
290     public boolean doScan( String a_key, String a_scanType )
291     {
292         if ( a_key == null || a_key.trim().equals( "" ) ) 
293         {
294             a_key = null ;
295         }
296 
297         if ( a_key == null && a_scanType != DEFAULT_CURSOR ) 
298         {
299             JOptionPane.showMessageDialog( null, "Cannot use a " +
300                 a_scanType + " scan type with a null key constraint.",
301                 "Missing Key Constraint", JOptionPane.ERROR_MESSAGE ) ;
302             return false ;
303         }
304 
305         try 
306         {
307             NamingEnumeration l_list = null ;
308 
309             if ( a_scanType == EQUALITY_CURSOR ) 
310             {
311                 l_list = m_index.listIndices( a_key ) ;
312             } 
313             else if ( a_scanType == GREATER_CURSOR ) 
314             {
315                 l_list = m_index.listIndices( a_key, true ) ;
316             } 
317             else if ( a_scanType == LESS_CURSOR ) 
318             {
319                 l_list = m_index.listIndices( a_key, false ) ;
320             } 
321             else if ( a_scanType == REGEX_CURSOR ) 
322             {
323                 RE l_regex = StringTools.getRegex( a_key ) ;
324                 int l_starIndex = a_key.indexOf( '*' ) ;
325 
326                 if ( l_starIndex > 0 ) 
327                 {
328                     String l_prefix = a_key.substring( 0, l_starIndex ) ;
329 
330                     if (log.isDebugEnabled())
331                         log.debug( "Regex prefix = " + l_prefix ) ;
332 
333                     l_list = m_index.listIndices( l_regex, l_prefix ) ;
334                 } 
335                 else 
336                 {
337                     l_list = m_index.listIndices( l_regex ) ;
338                 }
339             } 
340             else 
341             {
342                 l_list = m_index.listIndices() ;
343             }
344 
345             Object [] l_cols = new Object [2] ;
346             Object [] l_row = null ;
347             l_cols[0] = "Keys ( Attribute Value )" ;
348             l_cols[1] = "Values ( Entry Id )" ;
349             DefaultTableModel l_model = new DefaultTableModel( l_cols, 0 ) ;
350             int l_count = 0 ;
351             while( l_list.hasMore() )
352             {
353                 IndexRecord l_rec = ( IndexRecord ) l_list.next() ;
354                 l_row = new Object [2] ;
355                 l_row[0] = l_rec.getIndexKey() ;
356                 l_row[1] = l_rec.getEntryId() ;
357                 l_model.addRow( l_row ) ;
358                 l_count++ ;
359             }
360 
361             m_resultsTbl.setModel( l_model ) ;
362             m_resultsPnl.setBorder(
363                 javax.swing.BorderFactory.createTitledBorder(
364                 javax.swing.BorderFactory.createLineBorder(
365                 new java.awt.Color( 153, 153, 153 ), 1 ),
366                 "Scan Results: " + l_count,
367                 javax.swing.border.TitledBorder.LEADING,
368                 javax.swing.border.TitledBorder.TOP,
369                 new java.awt.Font( "SansSerif", 0, 14 ),
370                 new java.awt.Color( 60, 60, 60 ) ) ) ;
371 
372             if ( isVisible() ) 
373             {
374                 validate() ;
375             }
376         } 
377         catch ( Exception e ) 
378         {
379             String l_msg = ExceptionUtils.getStackTrace( e );
380 
381             if ( l_msg.length() > 1024 ) 
382             {
383                 l_msg = l_msg.substring( 0, 1024 )
384                     + "\n. . . TRUNCATED . . ." ;
385             }
386 
387             l_msg = "Error while scanning index "
388                 + "on attribute " + m_index.getAttribute() + " using a "
389                 + a_scanType + " cursor type with a key constraint of '"
390                 + a_key + "':\n" + l_msg ;
391                 
392             JTextArea l_area = new JTextArea() ;
393             l_area.setText( l_msg ) ;
394             JOptionPane.showMessageDialog( null, l_area, "Index Scan Error",
395                     JOptionPane.ERROR_MESSAGE ) ;
396             return false ;
397         }
398 
399         return true ;
400     }
401 }