KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ldap > server > db > gui > IndexDialog


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.db.gui ;
18
19
20 import org.apache.ldap.common.util.ExceptionUtils;
21 import org.apache.ldap.common.util.StringTools;
22 import org.apache.ldap.server.db.Index;
23 import org.apache.ldap.server.db.IndexRecord;
24 import org.apache.regexp.RE;
25
26 import javax.naming.NamingEnumeration JavaDoc;
27 import javax.swing.*;
28 import javax.swing.table.DefaultTableModel JavaDoc;
29 import java.awt.*;
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.awt.event.ActionListener JavaDoc;
32
33
34 /**
35  * A dialog showing index values.
36  *
37  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
38  * @version $Rev: 169198 $
39  */

40 public class IndexDialog extends JDialog
41 {
42     private static final long serialVersionUID = 3689917253680445238L;
43
44     public static final String JavaDoc DEFAULT_CURSOR = "Default" ;
45     public static final String JavaDoc EQUALITY_CURSOR = "Equality" ;
46     public static final String JavaDoc GREATER_CURSOR = "Greater" ;
47     public static final String JavaDoc LESS_CURSOR = "Less" ;
48     public static final String JavaDoc REGEX_CURSOR = "Regex" ;
49
50     private Panel m_mainPnl = new Panel();
51     private JTabbedPane m_tabbedPane = new JTabbedPane();
52     private JPanel m_listPnl = new JPanel();
53     private JPanel m_cursorPnl = new JPanel();
54     private JPanel m_resultsPnl = new JPanel();
55     private JScrollPane jScrollPane2 = new JScrollPane();
56     private JTable m_resultsTbl = new JTable();
57     private JPanel m_buttonPnl = new JPanel();
58     private JButton m_doneBut = new JButton();
59     private JLabel jLabel1 = new JLabel();
60     private JTextField m_keyText = new JTextField();
61     private JLabel jLabel2 = new JLabel();
62     private JComboBox m_cursorType = new JComboBox();
63     private JButton m_scanBut = new JButton();
64     private Index m_index = null ;
65
66     /** Creates new form JDialog */
67     public IndexDialog( Frame parent, boolean modal, Index a_index )
68     {
69         super ( parent, modal ) ;
70         m_index = a_index ;
71         initGUI() ;
72     }
73
74     /**
75      * This method is called from within the constructor to initialize the
76      * form.
77      */

78     private void initGUI()
79     {
80         addWindowListener(new java.awt.event.WindowAdapter JavaDoc()
81         {
82             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt)
83             {
84                 closeDialog();
85             }
86         });
87
88         pack();
89         setTitle("Index On Attribute '" + m_index.getAttribute() + "'");
90         setBounds(new java.awt.Rectangle JavaDoc(0, 0, 512, 471));
91         getContentPane().add(m_mainPnl, java.awt.BorderLayout.CENTER);
92         m_mainPnl.setLayout(new java.awt.BorderLayout JavaDoc());
93         m_mainPnl.add(m_tabbedPane, java.awt.BorderLayout.CENTER);
94         m_tabbedPane.add(m_listPnl, "Listing");
95         m_listPnl.setLayout(new java.awt.GridBagLayout JavaDoc());
96         m_listPnl.add(
97             m_cursorPnl,
98             new java.awt.GridBagConstraints JavaDoc(
99                 0,
100                 0,
101                 1,
102                 1,
103                 1.0,
104                 0.15,
105                 java.awt.GridBagConstraints.NORTH,
106                 java.awt.GridBagConstraints.BOTH,
107                 new java.awt.Insets JavaDoc(15, 0, 30, 0),
108                 0,
109                 0));
110         m_listPnl.add(
111             m_resultsPnl,
112             new java.awt.GridBagConstraints JavaDoc(
113                 0,
114                 1,
115                 1,
116                 1,
117                 1.0,
118                 0.8,
119                 java.awt.GridBagConstraints.CENTER,
120                 java.awt.GridBagConstraints.BOTH,
121                 new java.awt.Insets JavaDoc(0, 0, 0, 0),
122                 0,
123                 0));
124         m_listPnl.add(
125             m_buttonPnl,
126             new java.awt.GridBagConstraints JavaDoc(
127                 0,
128                 2,
129                 1,
130                 1,
131                 1.0,
132                 0.05,
133                 java.awt.GridBagConstraints.CENTER,
134                 java.awt.GridBagConstraints.BOTH,
135                 new java.awt.Insets JavaDoc(0, 0, 0, 0),
136                 0,
137                 0));
138         m_cursorPnl.setLayout(new java.awt.GridBagLayout JavaDoc());
139         m_cursorPnl.setBorder(
140             javax.swing.BorderFactory.createTitledBorder(
141                 javax.swing.BorderFactory.createLineBorder(
142                     new java.awt.Color JavaDoc(153, 153, 153),
143                     1),
144                 "Display Cursor Constraints",
145                 javax.swing.border.TitledBorder.LEADING,
146                 javax.swing.border.TitledBorder.TOP,
147                 new java.awt.Font JavaDoc("SansSerif", 0, 14),
148                 new java.awt.Color JavaDoc(60, 60, 60)));
149         m_cursorPnl.add(
150             jLabel1,
151             new java.awt.GridBagConstraints JavaDoc(
152                 0,
153                 1,
154                 1,
155                 1,
156                 0.0,
157                 0.0,
158                 java.awt.GridBagConstraints.WEST,
159                 java.awt.GridBagConstraints.NONE,
160                 new java.awt.Insets JavaDoc(0, 15, 0, 10),
161                 0,
162                 0));
163         m_cursorPnl.add(
164             m_keyText,
165             new java.awt.GridBagConstraints JavaDoc(
166                 1,
167                 1,
168                 1,
169                 1,
170                 0.4,
171                 0.0,
172                 java.awt.GridBagConstraints.WEST,
173                 java.awt.GridBagConstraints.BOTH,
174                 new java.awt.Insets JavaDoc(5, 5, 5, 236),
175                 0,
176                 0));
177         m_cursorPnl.add(
178             jLabel2,
179             new java.awt.GridBagConstraints JavaDoc(
180                 0,
181                 0,
182                 1,
183                 1,
184                 0.0,
185                 0.0,
186                 java.awt.GridBagConstraints.WEST,
187                 java.awt.GridBagConstraints.NONE,
188                 new java.awt.Insets JavaDoc(0, 15, 0, 10),
189                 0,
190                 0));
191         m_cursorPnl.add(
192             m_cursorType,
193             new java.awt.GridBagConstraints JavaDoc(
194                 1,
195                 0,
196                 1,
197                 1,
198                 0.4,
199                 0.0,
200                 java.awt.GridBagConstraints.WEST,
201                 java.awt.GridBagConstraints.NONE,
202                 new java.awt.Insets JavaDoc(5, 5, 5, 0),
203                 0,
204                 0));
205         m_resultsPnl.setLayout(new java.awt.BorderLayout JavaDoc());
206         m_resultsPnl.setBorder(
207             javax.swing.BorderFactory.createTitledBorder(
208                 javax.swing.BorderFactory.createLineBorder(
209                     new java.awt.Color JavaDoc(153, 153, 153),
210                     1),
211                 "Scan Results",
212                 javax.swing.border.TitledBorder.LEADING,
213                 javax.swing.border.TitledBorder.TOP,
214                 new java.awt.Font JavaDoc("SansSerif", 0, 14),
215                 new java.awt.Color JavaDoc(60, 60, 60)));
216         m_resultsPnl.add(jScrollPane2, java.awt.BorderLayout.CENTER);
217         jScrollPane2.getViewport().add(m_resultsTbl);
218         m_buttonPnl.setLayout(
219             new java.awt.FlowLayout JavaDoc(java.awt.FlowLayout.CENTER, 15, 5));
220         m_buttonPnl.add(m_doneBut);
221         m_buttonPnl.add(m_scanBut);
222         m_doneBut.setText("Done");
223         m_doneBut.addActionListener(new ActionListener JavaDoc()
224         {
225             public void actionPerformed(ActionEvent JavaDoc e)
226             {
227                 closeDialog();
228             }
229         });
230
231         jLabel1.setText("Key Constraint:");
232         m_keyText.setText("");
233         m_keyText.setMinimumSize(new java.awt.Dimension JavaDoc(130, 20));
234         m_keyText.setPreferredSize(new java.awt.Dimension JavaDoc(130, 20));
235         m_keyText.setMaximumSize(new java.awt.Dimension JavaDoc(130, 20));
236         m_keyText.setFont(
237             new java.awt.Font JavaDoc("SansSerif", java.awt.Font.PLAIN, 14));
238         m_keyText.setSize(new java.awt.Dimension JavaDoc(130, 20));
239         jLabel2.setText("Cursor Type:");
240         m_cursorType.setMaximumSize(new java.awt.Dimension JavaDoc(32767, 20));
241         m_cursorType.setMinimumSize(new java.awt.Dimension JavaDoc(126, 20));
242         m_cursorType.setPreferredSize(new java.awt.Dimension JavaDoc(130, 20));
243         DefaultComboBoxModel l_comboModel = new DefaultComboBoxModel();
244         l_comboModel.addElement(DEFAULT_CURSOR);
245         l_comboModel.addElement(EQUALITY_CURSOR);
246         l_comboModel.addElement(GREATER_CURSOR);
247         l_comboModel.addElement(LESS_CURSOR);
248         l_comboModel.addElement(REGEX_CURSOR);
249         m_cursorType.setModel(l_comboModel);
250         m_cursorType.setMaximumRowCount(5);
251         m_scanBut.setText("Scan");
252         m_scanBut.addActionListener(new ActionListener JavaDoc()
253         {
254             public void actionPerformed(ActionEvent JavaDoc e)
255             {
256                 doScan(
257                     m_keyText.getText(),
258                     (String JavaDoc) m_cursorType.getSelectedItem());
259             }
260         });
261
262         doScan(null, DEFAULT_CURSOR);
263     }
264
265
266     private void closeDialog()
267     {
268         setVisible( false ) ;
269         dispose() ;
270     }
271
272
273     public boolean doScan( String JavaDoc a_key, String JavaDoc a_scanType )
274     {
275         if ( a_key == null || a_key.trim().equals( "" ) )
276         {
277             a_key = null ;
278         }
279
280         if ( a_key == null && a_scanType != DEFAULT_CURSOR )
281         {
282             JOptionPane.showMessageDialog( null, "Cannot use a " +
283                 a_scanType + " scan type with a null key constraint.",
284                 "Missing Key Constraint", JOptionPane.ERROR_MESSAGE ) ;
285             return false ;
286         }
287
288         try
289         {
290             NamingEnumeration JavaDoc l_list = null ;
291
292             if ( a_scanType == EQUALITY_CURSOR )
293             {
294                 l_list = m_index.listIndices( a_key ) ;
295             }
296             else if ( a_scanType == GREATER_CURSOR )
297             {
298                 l_list = m_index.listIndices( a_key, true ) ;
299             }
300             else if ( a_scanType == LESS_CURSOR )
301             {
302                 l_list = m_index.listIndices( a_key, false ) ;
303             }
304             else if ( a_scanType == REGEX_CURSOR )
305             {
306                 RE l_regex = StringTools.getRegex( a_key ) ;
307                 int l_starIndex = a_key.indexOf( '*' ) ;
308
309                 if ( l_starIndex > 0 )
310                 {
311                     String JavaDoc l_prefix = a_key.substring( 0, l_starIndex ) ;
312                     System.out.println( "Regex prefix = " + l_prefix ) ;
313                     l_list = m_index.listIndices( l_regex, l_prefix ) ;
314                 }
315                 else
316                 {
317                     l_list = m_index.listIndices( l_regex ) ;
318                 }
319             }
320             else
321             {
322                 l_list = m_index.listIndices() ;
323             }
324
325             Object JavaDoc [] l_cols = new Object JavaDoc [2] ;
326             Object JavaDoc [] l_row = null ;
327             l_cols[0] = "Keys ( Attribute Value )" ;
328             l_cols[1] = "Values ( Entry Id )" ;
329             DefaultTableModel JavaDoc l_model = new DefaultTableModel JavaDoc( l_cols, 0 ) ;
330             int l_count = 0 ;
331             while( l_list.hasMore() )
332             {
333                 IndexRecord l_rec = ( IndexRecord ) l_list.next() ;
334                 l_row = new Object JavaDoc [2] ;
335                 l_row[0] = l_rec.getIndexKey() ;
336                 l_row[1] = l_rec.getEntryId() ;
337                 l_model.addRow( l_row ) ;
338                 l_count++ ;
339             }
340
341             m_resultsTbl.setModel( l_model ) ;
342             m_resultsPnl.setBorder(
343                 javax.swing.BorderFactory.createTitledBorder(
344                 javax.swing.BorderFactory.createLineBorder(
345                 new java.awt.Color JavaDoc( 153, 153, 153 ), 1 ),
346                 "Scan Results: " + l_count,
347                 javax.swing.border.TitledBorder.LEADING,
348                 javax.swing.border.TitledBorder.TOP,
349                 new java.awt.Font JavaDoc( "SansSerif", 0, 14 ),
350                 new java.awt.Color JavaDoc( 60, 60, 60 ) ) ) ;
351
352             if ( isVisible() )
353             {
354                 validate() ;
355             }
356         }
357         catch ( Exception JavaDoc e )
358         {
359             String JavaDoc l_msg = ExceptionUtils.getStackTrace( e );
360
361             if ( l_msg.length() > 1024 )
362             {
363                 l_msg = l_msg.substring( 0, 1024 )
364                     + "\n. . . TRUNCATED . . ." ;
365             }
366
367             l_msg = "Error while scanning index "
368                 + "on attribute " + m_index.getAttribute() + " using a "
369                 + a_scanType + " cursor type with a key constraint of '"
370                 + a_key + "':\n" + l_msg ;
371                 
372             JTextArea l_area = new JTextArea() ;
373             l_area.setText( l_msg ) ;
374             JOptionPane.showMessageDialog( null, l_area, "Index Scan Error",
375                     JOptionPane.ERROR_MESSAGE ) ;
376             return false ;
377         }
378
379         return true ;
380     }
381 }
382
Popular Tags