KickJava   Java API By Example, From Geeks To Geeks.

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


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.filter.ExprNode;
21 import org.apache.ldap.common.filter.FilterParser;
22 import org.apache.ldap.common.filter.FilterParserImpl;
23 import org.apache.ldap.common.ldif.LdifIterator;
24 import org.apache.ldap.common.ldif.LdifParser;
25 import org.apache.ldap.common.ldif.LdifParserImpl;
26 import org.apache.ldap.common.message.DerefAliasesEnum;
27 import org.apache.ldap.common.message.LockableAttributesImpl;
28 import org.apache.ldap.common.name.LdapName;
29 import org.apache.ldap.common.util.StringTools;
30 import org.apache.ldap.server.db.Database;
31 import org.apache.ldap.server.db.Index;
32 import org.apache.ldap.server.db.IndexRecord;
33 import org.apache.ldap.server.db.SearchEngine;
34
35 import javax.naming.NamingEnumeration JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37 import javax.naming.directory.Attributes JavaDoc;
38 import javax.naming.directory.SearchControls JavaDoc;
39 import javax.swing.*;
40 import javax.swing.event.TreeSelectionEvent JavaDoc;
41 import javax.swing.event.TreeSelectionListener JavaDoc;
42 import javax.swing.table.DefaultTableModel JavaDoc;
43 import javax.swing.tree.DefaultTreeModel JavaDoc;
44 import javax.swing.tree.TreeModel JavaDoc;
45 import javax.swing.tree.TreeNode JavaDoc;
46 import javax.swing.tree.TreePath JavaDoc;
47 import java.awt.*;
48 import java.awt.event.ActionEvent JavaDoc;
49 import java.awt.event.ActionListener JavaDoc;
50 import java.io.File JavaDoc;
51 import java.io.FileNotFoundException JavaDoc;
52 import java.io.FileReader JavaDoc;
53 import java.io.IOException JavaDoc;
54 import java.math.BigInteger JavaDoc;
55 import java.util.HashMap JavaDoc;
56 import java.util.Hashtable JavaDoc;
57 import java.util.Iterator JavaDoc;
58 import java.util.Stack JavaDoc;
59
60
61 /**
62  * The frame for the database.
63  *
64  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
65  * @version $Rev: 169198 $
66  */

67 public class MainFrame extends JFrame
68 {
69     private static final long serialVersionUID = 4049353102291513657L;
70
71     // Swing Stuff
72
private JLabel statusBar = new JLabel( "Ready" );
73     private JPanel mainPnl = new JPanel();
74     private JSplitPane splitPane = new JSplitPane();
75     private JTabbedPane tabbedPane = new JTabbedPane();
76     private JPanel entryPnl = new JPanel();
77     private JPanel idxPnl = new JPanel();
78     private JScrollPane treePane = new JScrollPane();
79     private JTree tree = new JTree();
80     private JScrollPane entryPane = new JScrollPane();
81     private JTable entryTbl = new JTable();
82     private JScrollPane idxPane = new JScrollPane();
83     private JTable idxTbl = new JTable();
84     private JMenu searchMenu = new JMenu();
85     private JMenuItem annotate = new JMenuItem();
86     private JMenuItem run = new JMenuItem();
87     private JMenuItem debug = new JMenuItem();
88     private JMenu indices = new JMenu();
89
90     // Non Swing Stuff
91
private Database database = null;
92     private boolean doCleanUp = false;
93     private HashMap JavaDoc nodes = new HashMap JavaDoc();
94     private EntryNode root = null;
95     private SearchEngine eng = null;
96
97
98     /**
99      * Creates new form JFrame
100      */

101     public MainFrame( Database db, SearchEngine eng )
102         throws NamingException JavaDoc
103     {
104         database = db;
105         this.eng = eng;
106
107         initGUI();
108         buildIndicesMenu( database );
109         pack();
110         load();
111     }
112
113
114     /**
115      * This method is called from within the constructor to initialize the form
116      */

117     private void initGUI()
118     {
119         mainPnl.setBorder( null );
120         mainPnl.setLayout( new java.awt.BorderLayout JavaDoc() );
121         mainPnl.add( splitPane, java.awt.BorderLayout.CENTER );
122         splitPane.add( tabbedPane, javax.swing.JSplitPane.RIGHT );
123         splitPane.add( treePane, javax.swing.JSplitPane.LEFT );
124         tabbedPane.add( entryPnl, "Entry Attributes" );
125         tabbedPane.add( idxPnl, "Entry Indices" );
126
127         entryPnl.setLayout( new java.awt.BorderLayout JavaDoc() );
128         entryPnl.add( entryPane, java.awt.BorderLayout.CENTER );
129
130         idxPnl.setLayout( new java.awt.BorderLayout JavaDoc() );
131         idxPnl.add( idxPane, java.awt.BorderLayout.CENTER );
132
133         getContentPane().setLayout( new java.awt.BorderLayout JavaDoc() );
134         JPanel content = new JPanel();
135         content.setPreferredSize( new java.awt.Dimension JavaDoc( 798, 461 ) );
136         content.setLayout( new java.awt.BorderLayout JavaDoc() );
137         content.setBorder( javax.swing.BorderFactory.createEtchedBorder() );
138         content.add( mainPnl, java.awt.BorderLayout.NORTH );
139         getContentPane().add( content, BorderLayout.CENTER );
140         // set title
141
setTitle( "Backend DB Viewer" );
142         // add status bar
143
getContentPane().add( statusBar, BorderLayout.SOUTH );
144         // add menu bar
145
JMenuBar menuBar = new JMenuBar();
146
147         // --------------------------------------------------------------------
148
// 'Backend' Menu
149
// --------------------------------------------------------------------
150

151         JMenu backendMenu = new JMenu( "Backend" );
152         backendMenu.setText( "Backend" );
153         backendMenu.setBackground( new java.awt.Color JavaDoc( 205, 205, 205 ) );
154         backendMenu.setMnemonic( 'B' );
155
156         // create Import menu item
157
JMenuItem add = new JMenuItem( "Add" );
158         backendMenu.add( add );
159         add.setMnemonic( 'A' );
160         add.setBackground( new java.awt.Color JavaDoc( 205, 205, 205 ) );
161         add.addActionListener( new ActionListener JavaDoc()
162         {
163             public void actionPerformed( ActionEvent JavaDoc e )
164             {
165                 doAddDialog();
166             }
167         } );
168
169         // create Import menu item
170
JMenuItem importItem = new JMenuItem( "Import" );
171         backendMenu.add( importItem );
172         importItem.setMnemonic( 'I' );
173         importItem.setBackground( new java.awt.Color JavaDoc( 205, 205, 205 ) );
174         importItem.addActionListener( new ActionListener JavaDoc()
175         {
176             public void actionPerformed( ActionEvent JavaDoc e )
177             {
178                 doImport();
179             }
180         } );
181
182         // create Exit menu item
183
JMenuItem exit = new JMenuItem( "Exit" );
184         backendMenu.add( exit );
185         exit.setMnemonic( 'E' );
186         exit.setBackground( new java.awt.Color JavaDoc( 205, 205, 205 ) );
187         exit.addActionListener( new ActionListener JavaDoc()
188         {
189             public void actionPerformed( ActionEvent JavaDoc e )
190             {
191                 exitForm();
192             }
193         } );
194
195         // create About menu item
196
JMenu helpMenu = new JMenu( "Help" );
197         helpMenu.setMnemonic( 'H' );
198         JMenuItem about = new JMenuItem( "About" );
199         about.setMnemonic( 'A' );
200         about.setBackground( new java.awt.Color JavaDoc( 205,205,205 ) );
201         about.addActionListener( new ActionListener JavaDoc()
202         {
203             public void actionPerformed( ActionEvent JavaDoc e )
204             {
205                 AboutDialog aboutDialog =
206                     new AboutDialog ( MainFrame.this, true );
207                 MainFrame.this.centerOnScreen( aboutDialog );
208                 aboutDialog.setVisible( true );
209             }
210         } );
211         helpMenu.setBackground( new java.awt.Color JavaDoc( 205,205,205 ) );
212         helpMenu.add( about );
213         
214         // create Save menu item
215
// create Print menu item
216
menuBar.setBackground( new java.awt.Color JavaDoc( 196,197,203 ) );
217         menuBar.add( backendMenu );
218         menuBar.add( searchMenu );
219         menuBar.add( indices );
220         menuBar.add( helpMenu );
221         // sets menu bar
222
setJMenuBar( menuBar );
223         setBounds( new java.awt.Rectangle JavaDoc( 0, 0, 802, 515 ) );
224         setSize( new java.awt.Dimension JavaDoc( 802,515 ) );
225         setResizable( true );
226         
227         addWindowListener( new java.awt.event.WindowAdapter JavaDoc()
228         {
229             public void windowClosing( java.awt.event.WindowEvent JavaDoc evt )
230             {
231                 exitForm();
232             }
233         } );
234         
235         treePane.getViewport().add( tree );
236         tree.setBounds( new java.awt.Rectangle JavaDoc( 6,184,82,80 ) );
237         tree.setShowsRootHandles( true );
238         tree.setToolTipText( "DB DIT" );
239         tree.setScrollsOnExpand( true );
240         tree.getSelectionModel().addTreeSelectionListener(
241             new TreeSelectionListener JavaDoc()
242         {
243             public void valueChanged( TreeSelectionEvent JavaDoc e )
244             {
245                 TreePath JavaDoc path = e.getNewLeadSelectionPath();
246
247                 if ( path == null )
248                 {
249                     return;
250                 }
251
252                 Object JavaDoc last = path.getLastPathComponent();
253                 try
254                 {
255                     if ( last instanceof EntryNode )
256                     {
257                         displayEntry( ( ( EntryNode ) last ).getEntryId(),
258                         ( ( EntryNode ) last).getLdapEntry() );
259                     }
260                 }
261                 catch( Exception JavaDoc ex )
262                 {
263                     ex.printStackTrace();
264                 }
265             }
266         } );
267
268         entryPane.getViewport().add( entryTbl );
269         entryTbl.setBounds( new java.awt.Rectangle JavaDoc( 321,103,32,32 ) );
270
271         idxPane.getViewport().add( idxTbl );
272         idxTbl.setBounds( new java.awt.Rectangle JavaDoc( 429,134,32,32 ) );
273
274         treePane.setSize( new java.awt.Dimension JavaDoc( 285, 435 ) );
275         treePane.setPreferredSize( new java.awt.Dimension JavaDoc( 285, 403 ) );
276         searchMenu.setText( "Search" );
277         searchMenu.setBackground( new java.awt.Color JavaDoc( 205,205,205 ) );
278         searchMenu.add( run );
279         searchMenu.add( debug );
280         searchMenu.add( annotate );
281
282         ActionListener JavaDoc searchHandler = new ActionListener JavaDoc()
283         {
284             public void actionPerformed( ActionEvent JavaDoc an_event )
285             {
286                 System.out.println( "action command = "
287                     + an_event.getActionCommand() );
288                 doFilterDialog( an_event.getActionCommand() );
289             }
290         };
291
292         annotate.setText( FilterDialog.ANNOTATE_MODE );
293         annotate.setActionCommand( FilterDialog.ANNOTATE_MODE );
294         annotate.setBackground( new java.awt.Color JavaDoc( 205,205,205 ) );
295         annotate.addActionListener( searchHandler );
296
297         run.setText( FilterDialog.RUN_MODE );
298         run.setActionCommand( FilterDialog.RUN_MODE );
299         run.setBackground( new java.awt.Color JavaDoc( 205,205,205 ) );
300         run.addActionListener( searchHandler );
301
302         debug.setText( FilterDialog.DEBUG_MODE );
303         debug.setActionCommand( FilterDialog.DEBUG_MODE );
304         debug.setBackground( new java.awt.Color JavaDoc( 205,205,205 ) );
305         debug.addActionListener( searchHandler );
306
307         indices.setText( "Indices" );
308         indices.setBackground( new java.awt.Color JavaDoc( 205, 205, 205 ) );
309     }
310
311
312     private void centerOnScreen( Window window )
313     {
314         Dimension frameSize = window.getSize();
315         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
316         
317         frameSize.height = ( ( frameSize.height > screenSize.height )
318             ? screenSize.height : frameSize.height );
319         frameSize.width = ( ( frameSize.width > screenSize.width )
320             ? screenSize.width : frameSize.width );
321         window.setLocation( ( screenSize.width - frameSize.width ) / 2,
322             ( screenSize.height - frameSize.height ) / 2 );
323     }
324
325     
326     /**
327      * Displays a entry addition dialog.
328      */

329     public void doAddDialog()
330     {
331         try
332         {
333             TreePath JavaDoc path = tree.getSelectionModel().getSelectionPath();
334             String JavaDoc parentDn = database.getSuffix().toString();
335
336             if ( null != path )
337             {
338                 Object JavaDoc last = path.getLastPathComponent();
339
340                 if( last instanceof EntryNode )
341                 {
342                     parentDn = ( ( EntryNode ) last ).getEntryDn();
343                 }
344             }
345             
346             if ( null == parentDn )
347             {
348                 JOptionPane.showMessageDialog( this,
349                     "Must select a parent entry to add a child to!" );
350                 return;
351             }
352         
353             AddEntryDialog dialog = new AddEntryDialog( this, false );
354             dialog.setParentDn( parentDn );
355             
356             centerOnScreen( dialog );
357             dialog.setEnabled( true );
358             dialog.setVisible( true );
359         }
360         catch ( Exception JavaDoc e )
361         {
362             e.printStackTrace();
363         }
364     }
365     
366
367     /**
368      * Gets the DN of the DIT node selected in the tree view.
369      *
370      * @return the DN of the selected tree node or the root Dn of the tree if
371      * nothing has been selected yet.
372      */

373     public String JavaDoc getSelectedDn()
374     {
375         TreePath JavaDoc path = tree.getSelectionModel().getSelectionPath();
376         
377         if ( null == path )
378         {
379             return database.getSuffix().toString();
380         }
381         
382         Object JavaDoc last = path.getLastPathComponent();
383         String JavaDoc base = null;
384         
385         if( last instanceof EntryNode )
386         {
387             try
388             {
389                 base = ( ( EntryNode ) last ).getEntryDn();
390             }
391             catch ( NamingException JavaDoc e )
392             {
393                 e.printStackTrace();
394             }
395         }
396         else
397         {
398             base = database.getSuffix().toString();
399         }
400         
401         return base;
402     }
403     
404
405     public void doImport()
406     {
407         FileReader JavaDoc in = null;
408         LdifIterator list = null;
409         LdifParser parser = new LdifParserImpl();
410         JFileChooser chooser = new JFileChooser();
411         int choice = chooser.showOpenDialog( this );
412         File JavaDoc selected = chooser.getSelectedFile();
413         
414         if ( JFileChooser.APPROVE_OPTION != choice )
415         {
416             return;
417         }
418
419         try
420         {
421             in = new FileReader JavaDoc( selected );
422             list = new LdifIterator( in );
423             
424             while ( list.hasNext() )
425             {
426                 String JavaDoc dif = ( String JavaDoc ) list.next();
427                 LockableAttributesImpl attrs = new LockableAttributesImpl();
428                 parser.parse( attrs, dif );
429                 String JavaDoc updn = ( String JavaDoc ) attrs.get( "dn" ).get();
430                 LdapName ndn =
431                     new LdapName( StringTools.deepTrimToLower( updn ) );
432                 attrs.remove( "dn" );
433
434                 if ( null == database.getEntryId( ndn.toString() ) )
435                 {
436                     database.add( updn, ndn, attrs );
437                     load();
438                 }
439             }
440         }
441         catch( NamingException JavaDoc e )
442         {
443             // @todo display popup with error here!
444
e.printStackTrace();
445             return;
446         }
447         catch( FileNotFoundException JavaDoc e )
448         {
449             // @todo display popup with error here!
450
e.printStackTrace();
451             return;
452         }
453         catch( IOException JavaDoc e )
454         {
455             // @todo display popup with error here!
456
e.printStackTrace();
457             return;
458         }
459         catch( Exception JavaDoc e )
460         {
461             // @todo display popup with error here!
462
e.printStackTrace();
463             return;
464         }
465     }
466
467
468     /**
469      * Exit the Application
470      */

471     private void exitForm()
472     {
473         setEnabled( false );
474         setVisible( false );
475         dispose();
476
477         if ( doCleanUp && database != null )
478         {
479             try
480             {
481                 database.sync();
482                 database.close();
483             }
484             catch ( NamingException JavaDoc e )
485             {
486                 e.printStackTrace();
487             }
488
489             System.exit( 0 );
490         }
491     }
492     
493     
494     public void doRunDebugAnnotate( FilterDialog dialog, String JavaDoc mode )
495     {
496         try
497         {
498             if ( mode == FilterDialog.RUN_MODE )
499             {
500                 doRun( dialog.getFilter(), dialog.getScope(),
501                     dialog.getBase(), dialog.getLimit() );
502             }
503             else if ( mode == FilterDialog.DEBUG_MODE )
504             {
505                 doDebug( dialog.getFilter(), dialog.getScope(),
506                      dialog.getBase(), dialog.getLimit() );
507             }
508             else if ( mode == FilterDialog.ANNOTATE_MODE )
509             {
510                 if ( doAnnotate( dialog.getFilter() ) )
511                 {
512                     // continue
513
}
514                 else
515                 {
516                     // We failed don't loose users filter buf
517
// allow user to make edits.
518
return;
519                 }
520             }
521             else
522             {
523                 throw new RuntimeException JavaDoc( "Unrecognized mode." );
524             }
525         }
526         catch ( Exception JavaDoc e )
527         {
528             // @todo show error popup here!
529
e.printStackTrace();
530         }
531     }
532
533
534     public void doFilterDialog( final String JavaDoc mode )
535     {
536         final FilterDialog dialog = new FilterDialog( mode, this, true );
537
538         if ( tree.getSelectionModel().getSelectionPath() != null )
539         {
540             dialog.setBase( getSelectedDn() );
541         }
542         else
543         {
544             dialog.setBase( database.getSuffix().toString() );
545         }
546
547         dialog.addActionListener( new ActionListener JavaDoc()
548         {
549             public void actionPerformed( ActionEvent JavaDoc an_event )
550             {
551                 String JavaDoc cmd = an_event.getActionCommand();
552
553                 if ( cmd.equals( FilterDialog.SEARCH_CMD ) )
554                 {
555                     doRunDebugAnnotate( dialog, mode );
556                 }
557                 else if ( cmd.equals(FilterDialog.CANCEL_CMD ) )
558                 {
559                     // Do nothing! Just exit dialog.
560
}
561                 else
562                 {
563                     throw new RuntimeException JavaDoc(
564                         "Unrecognized FilterDialog command: " + cmd );
565                 }
566
567                 dialog.setVisible( false );
568                 dialog.dispose();
569             }
570         } );
571
572         //Center the frame on screen
573
dialog.setSize( 456, 256 );
574         centerOnScreen( dialog );
575         dialog.setEnabled( true );
576         dialog.setVisible( true );
577     }
578
579
580     public boolean doRun( String JavaDoc filter, String JavaDoc scope, String JavaDoc base,
581         String JavaDoc limit )
582         throws Exception JavaDoc
583     {
584         System.out.println( "Search attempt using filter '" + filter + "' "
585             + "with scope '" + scope + "' and a return limit of '" + limit
586             + "'" );
587         FilterParser parser = new FilterParserImpl();
588         ExprNode root = null;
589
590         try
591         {
592             root = parser.parse( filter );
593         }
594         catch ( Exception JavaDoc e )
595         {
596             e.printStackTrace();
597             JTextArea text = new JTextArea();
598             String JavaDoc msg = e.getMessage();
599
600             if ( msg.length() > 1024 )
601             {
602                 msg = msg.substring( 0, 1024 ) + "\n. . . truncated . . .";
603             }
604
605             text.setText( msg );
606             text.setEnabled( false );
607             JOptionPane.showMessageDialog( null, text, "Syntax Error",
608                 JOptionPane.ERROR_MESSAGE );
609             return false;
610         }
611
612         SearchControls JavaDoc ctls = new SearchControls JavaDoc();
613
614         if ( scope == FilterDialog.BASE_SCOPE )
615         {
616             ctls.setSearchScope( SearchControls.OBJECT_SCOPE );
617         }
618         else if ( scope == FilterDialog.SINGLE_SCOPE )
619         {
620             ctls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
621         }
622         else if ( scope == FilterDialog.SUBTREE_SCOPE )
623         {
624             ctls.setSearchScope( SearchControls.SUBTREE_SCOPE );
625         } else {
626             throw new RuntimeException JavaDoc( "Unexpected scope parameter: " +
627                 scope );
628         }
629
630         int limitMax = Integer.MAX_VALUE;
631         if ( ! limit.equals( FilterDialog.UNLIMITED ) )
632         {
633             limitMax = Integer.parseInt( limit );
634         }
635
636         Hashtable JavaDoc env = new Hashtable JavaDoc();
637
638         env.put( DerefAliasesEnum.JNDI_PROP,
639             DerefAliasesEnum.DEREFALWAYS_NAME );
640
641         NamingEnumeration JavaDoc cursor = eng.search( new LdapName( base ),
642                 env, root, ctls );
643         String JavaDoc [] cols = new String JavaDoc [2];
644         cols[0] = "id";
645         cols[1] = "dn";
646         DefaultTableModel JavaDoc tableModel = new DefaultTableModel JavaDoc( cols, 0 );
647         Object JavaDoc [] row = new Object JavaDoc[2];
648         int count = 0;
649         while ( cursor.hasMore() && count < limitMax )
650         {
651             IndexRecord rec = ( IndexRecord ) cursor.next();
652             row[0] = rec.getEntryId();
653             row[1] = database.getEntryDn( ( BigInteger JavaDoc ) row[0] );
654             tableModel.addRow( row );
655             count++;
656         }
657
658         SearchResultDialog results = new SearchResultDialog( this, false );
659         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
660         buf.append( "base: " );
661         buf.append( base );
662         buf.append( "\n" );
663         buf.append( "scope: " );
664         buf.append( scope );
665         buf.append( "\n" );
666         buf.append( "limit: " );
667         buf.append( limit );
668         buf.append( "\n" );
669         buf.append( "total: " );
670         buf.append( count );
671         buf.append( "\n" );
672         buf.append( "filter:\n" );
673         buf.append( filter );
674         buf.append( "\n" );
675         results.setFilter( buf.toString() );
676
677         TreeNode JavaDoc astRoot = new ASTNode( null, root );
678         TreeModel JavaDoc treeModel = new DefaultTreeModel JavaDoc( astRoot, true );
679         results.setTreeModel( treeModel );
680         results.setTableModel( tableModel );
681         centerOnScreen( results );
682         results.setVisible( true );
683         return true;
684     }
685
686
687     public void doDebug( String JavaDoc filter, String JavaDoc scope, String JavaDoc base,
688         String JavaDoc limit )
689     {
690         System.out.println( "Search attempt using filter '" + filter + "' "
691             + "with scope '" + scope + "' and a return limit of '" + limit
692             + "'" );
693     }
694
695
696     public void selectTreeNode( BigInteger JavaDoc id )
697     {
698         Stack JavaDoc stack = new Stack JavaDoc();
699         Object JavaDoc [] comps = null;
700         TreeNode JavaDoc parent = ( EntryNode ) nodes.get( id );
701
702         while ( parent != null && ( parent != parent.getParent() ) )
703         {
704             stack.push( parent );
705             parent = parent.getParent();
706         }
707
708         if ( stack.size() == 0 )
709         {
710             comps = new Object JavaDoc[1];
711             comps[0] = root;
712         }
713         else
714         {
715             comps = new Object JavaDoc[stack.size()];
716         }
717
718         for ( int ii = 0; stack.size() > 0 && ii < comps.length; ii++ )
719         {
720             comps[ii] = stack.pop();
721         }
722
723         TreePath JavaDoc path = new TreePath JavaDoc( comps );
724         tree.scrollPathToVisible( path );
725         tree.getSelectionModel().setSelectionPath( path );
726         tree.validate();
727     }
728
729
730     public boolean doAnnotate( String JavaDoc filter )
731         throws Exception JavaDoc
732     {
733         FilterParser parser = new FilterParserImpl();
734         ExprNode root = null;
735
736         try
737         {
738             root = parser.parse( filter );
739         }
740         catch( Exception JavaDoc e )
741         {
742             JTextArea text = new JTextArea();
743             String JavaDoc msg = e.getMessage();
744
745             if ( msg.length() > 1024 )
746             {
747                 msg = msg.substring( 0, 1024 ) + "\n. . . truncated . . .";
748             }
749
750             text.setText( msg );
751             text.setEnabled( false );
752             JOptionPane.showMessageDialog( null, text, "Syntax Error",
753                 JOptionPane.ERROR_MESSAGE );
754             return false;
755         }
756
757         AnnotatedFilterTreeDialog treeDialog = new
758             AnnotatedFilterTreeDialog( MainFrame.this, false );
759         treeDialog.setFilter( filter );
760
761         eng.getOptimizer().annotate( root );
762         TreeNode JavaDoc astRoot = new ASTNode( null, root );
763         TreeModel JavaDoc model = new DefaultTreeModel JavaDoc( astRoot, true );
764         treeDialog.setModel( model );
765         treeDialog.setVisible( true );
766         return true;
767     }
768
769
770     /**
771      * Shows a dialog to display and scan indices.
772      *
773      * @param idxAttr the name of the index or its attribute
774      * @throws Exception if the indices cannot be accessed
775      */

776     public void showIndexDialog( String JavaDoc idxAttr )
777         throws Exception JavaDoc
778     {
779         Index index = null;
780         boolean isSystem = database.hasSystemIndexOn( idxAttr );
781         
782         if ( isSystem )
783         {
784             index = database.getSystemIndex( idxAttr );
785         }
786         else
787         {
788             index = database.getUserIndex( idxAttr );
789         }
790
791         if ( index != null )
792         {
793             IndexDialog dialog = new IndexDialog( this, false, index );
794             centerOnScreen( dialog );
795             dialog.setEnabled( true );
796             dialog.setVisible( true );
797         }
798     }
799
800
801     public void buildIndicesMenu( Database database )
802     {
803         JMenuItem item = null;
804         
805         ActionListener JavaDoc listener = new ActionListener JavaDoc()
806         {
807             public void actionPerformed( ActionEvent JavaDoc event )
808             {
809                 try
810                 {
811                     showIndexDialog( event.getActionCommand() );
812                 }
813                 catch ( Exception JavaDoc e )
814                 {
815                     e.printStackTrace();
816                 }
817             }
818         };
819
820         Iterator list = database.getSystemIndices();
821         while ( list.hasNext() )
822         {
823             String JavaDoc idx = ( String JavaDoc ) list.next();
824             item = new JMenuItem();
825             item.setBackground( new java.awt.Color JavaDoc( 205, 205, 205 ) );
826             indices.add( item );
827             item.setText( idx );
828             item.setActionCommand( idx );
829             item.addActionListener( listener );
830         }
831
832         indices.add( new JSeparator() );
833         list = database.getUserIndices();
834         while ( list.hasNext() )
835         {
836             String JavaDoc idx = ( String JavaDoc ) list.next();
837             item = new JMenuItem();
838             item.setBackground( new java.awt.Color JavaDoc( 205, 205, 205 ) );
839             indices.add( item );
840             item.setText( idx );
841             item.setActionCommand( idx );
842             item.addActionListener( listener );
843         }
844     }
845
846
847     void displayEntry( BigInteger JavaDoc id, Attributes JavaDoc entry )
848         throws Exception JavaDoc
849     {
850         String JavaDoc dn = database.getEntryUpdn( id );
851         AttributesTableModel model =
852             new AttributesTableModel( entry, id, dn, false );
853         entryTbl.setModel( model );
854
855         model = new AttributesTableModel(
856             database.getIndices( id ), id, dn, false );
857         idxTbl.setModel( model );
858
859         validate();
860     }
861
862
863     private void load() throws NamingException JavaDoc
864     {
865         // boolean doFiltered = false;
866
nodes = new HashMap JavaDoc();
867
868         Attributes JavaDoc suffix = database.getSuffixEntry();
869         BigInteger JavaDoc id = database.getEntryId(
870             database.getSuffix().toString() );
871         root = new EntryNode( id, null, database, suffix, nodes );
872
873         /*
874         int option = JOptionPane.showConfirmDialog( null,
875             "Would you like to filter leaf nodes on load?", "Use Filter?",
876             JOptionPane.OK_CANCEL_OPTION );
877         doFiltered = option == JOptionPane.OK_OPTION;
878
879         if(doFiltered) {
880             SearchEngine engine = new SearchEngine();
881             final FilterDialog dialog =
882                 new FilterDialog(FilterDialog.LOAD_MODE, this, true);
883             dialog.addActionListener(new ActionListener() {
884                 public void actionPerformed(ActionEvent e) {
885                     dialog.setVisible(false);
886                     dialog.dispose();
887                 }
888             });
889
890             dialog.setBase(database.getSuffix().toString());
891             dialog.setScope(FilterDialog.SUBTREE_SCOPE);
892
893             //Center the frame on screen
894             dialog.setSize(456, 256);
895             this.centerOnScreen( dialog );
896             dialog.setEnabled(true);
897             dialog.setVisible(true);
898
899             FilterParser parser = new FilterParserImpl();
900             parser.enableLogging(logger);
901             ExprNode exprNode = parser.parse(dialog.getFilter());
902
903             int scope = -1;
904             String scopeStr = dialog.getScope();
905             if(scopeStr == FilterDialog.BASE_SCOPE) {
906                 scope = Backend.BASE_SCOPE;
907             } else if(scopeStr == FilterDialog.SINGLE_SCOPE) {
908                 scope = Backend.SINGLE_SCOPE;
909             } else if(scopeStr == FilterDialog.SUBTREE_SCOPE) {
910                 scope = Backend.SUBTREE_SCOPE;
911             } else {
912                 throw new RuntimeException("Unrecognized scope");
913             }
914
915             exprNode =
916                 engine.addScopeNode(exprNode, dialog.getBase(), scope);
917             root = new EntryNode(null, database,
918                 database.getSuffixEntry(), nodes, exprNode, engine);
919         } else {
920             root = new EntryNode(null, database,
921                 database.getSuffixEntry(), nodes);
922         }
923         */

924
925         DefaultTreeModel JavaDoc model = new DefaultTreeModel JavaDoc( root );
926         tree.setModel( model );
927
928         if ( isVisible() )
929         {
930             tree.validate();
931         }
932     }
933 }
934
Popular Tags