KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > ir > gui > IRBrowser


1 package org.jacorb.ir.gui;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import javax.swing.*;
24 import javax.swing.table.*;
25 import javax.swing.tree.*;
26 import javax.swing.event.*;
27
28 import java.awt.*;
29 import java.awt.event.*;
30
31 import org.jacorb.ir.gui.typesystem.*;
32
33 /**
34  * @author (c) Joerg von Frantzius, Gerald Brose, FU Berlin
35  * @version $Id: IRBrowser.java,v 1.8 2004/05/06 12:39:59 nicolas Exp $
36  */

37
38 class IRBrowser
39     extends JFrame
40     implements java.awt.event.WindowListener JavaDoc,
41                java.awt.event.MouseListener JavaDoc,
42                ListSelectionListener,
43                TreeSelectionListener,
44                ActionListener
45 {
46
47     JTable contentTable;
48     JSplitPane splitPane;
49     JTree treeView;
50     JTextArea textArea;
51     TypeSystem typeSystem;
52     TreeModel treeModel;
53     JMenuItem followTypeMenu;
54     private static final String JavaDoc title = "IRBrowser";
55
56     /**
57      * Constructor
58      */

59
60     public IRBrowser()
61     {
62     super();
63     typeSystem = new org.jacorb.ir.gui.typesystem.remote.RemoteTypeSystem();
64     initialize();
65     }
66
67     /**
68      * @param repositoryIOR java.lang.String
69      */

70
71     public IRBrowser (String JavaDoc repositoryIOR)
72     {
73     super();
74     typeSystem =
75             new org.jacorb.ir.gui.typesystem.remote.RemoteTypeSystem(repositoryIOR);
76     initialize();
77     }
78
79     /**
80      * @param event java.awt.ActionEvent
81      */

82
83     public void actionPerformed (java.awt.event.ActionEvent JavaDoc event)
84     {
85     NodeMapper nodeMapper =
86             (NodeMapper)contentTable.getModel().getValueAt(contentTable.getSelectedRow(),0);
87     TypeSystemNode typeSystemNode = nodeMapper.getNode();
88     followTypeOf(typeSystemNode);
89     System.out.println("following type of "+typeSystemNode);
90     }
91
92     /**
93      * conn0: (IRBrowser.window.windowClosing(java.awt.event.WindowEvent) --> IRBrowser.dispose())
94      * @param arg1 java.awt.event.WindowEvent
95      */

96
97     private void conn0(java.awt.event.WindowEvent JavaDoc arg1)
98     {
99     try
100         {
101             // user code begin {1}
102
// user code end
103
this.dispose();
104             // user code begin {2}
105
// user code end
106
}
107         catch (java.lang.Throwable JavaDoc ivjExc)
108         {
109             // user code begin {3}
110
// user code end
111
handleException(ivjExc);
112     }
113     }
114
115     /**
116      * @param typeSystemNode typesystem.TypeSystemNode
117      */

118
119     public void followTypeOf(TypeSystemNode typeSystemNode)
120     {
121     DefaultMutableTreeNode treeNode=null;
122     
123     if ( typeSystemNode instanceof TypeAssociator )
124         {
125             TypeSystemNode assTypeNode =
126                 ((TypeAssociator)typeSystemNode).getAssociatedTypeSystemNode();
127             if (assTypeNode.getModelRepresentant(treeModel)!=null)
128             {
129                 treeNode =
130                     (DefaultMutableTreeNode)assTypeNode.getModelRepresentant(treeModel);
131             }
132     }
133     if ( treeNode != null )
134         {
135             // wenn Node ein AbstractContainer ist oder eine assoziierte
136
// TypeSystemNode besitzt, jeweils im treeView dorthin springen
137
DefaultTreeModel treeModel =
138                 (DefaultTreeModel)treeView.getModel();
139             TreePath fullTreePath =
140                 new TreePath(treeModel.getPathToRoot(treeNode));
141     
142             treeView.scrollPathToVisible(fullTreePath);
143             // Selection auf node setzen
144
treeView.setSelectionPath(fullTreePath);
145             treeView.validate();
146     }
147     }
148
149     /**
150      * Called whenever the part throws an exception.
151      * @param exception java.lang.Throwable
152      */

153
154     private void handleException(Throwable JavaDoc exception)
155     {
156         exception.printStackTrace();
157     }
158
159     /**
160      * Initializes connections
161      */

162
163     private void initConnections()
164     {
165     this.addWindowListener(this);
166     }
167
168
169     /**
170      * Initialize class
171      */

172
173     public void initialize()
174     {
175         // setBackground(java.awt.Color.lightGray);
176
setTitle(title);
177
178     splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
179
180     // Create the table
181
DefaultTableModel tableModel = typeSystem.getTableModel(null);
182     contentTable = new JTable(tableModel);
183     contentTable.setAutoCreateColumnsFromModel(true);
184         // contentTable.setModel(tableModel);
185
contentTable.setColumnSelectionAllowed(false);
186     contentTable.setRowSelectionAllowed(true);
187     contentTable.setCellSelectionEnabled(false);
188     contentTable.removeEditor();
189     contentTable.setShowGrid(false);
190     contentTable.setTableHeader(new javax.swing.table.JTableHeader JavaDoc(contentTable.getColumnModel()));
191     contentTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
192     // contentTable.setBackground(java.awt.Color.white);
193
// contentTable.setMinimumSize(new java.awt.Dimension(100,contentTable.getMinimumSize().height));
194
contentTable.setMinimumSize(new Dimension(100,100));
195     contentTable.getSelectionModel().addListSelectionListener(this);
196
197
198     this.treeModel = typeSystem.getTreeModel();
199         // this.treeModel = typeSystem.createTreeModelRoot();
200
treeView = new JTree(treeModel);
201         // treeView.addTreeExpansionListener(typeSystem.getTreeExpansionListener(this.treeModel));
202
treeView.setRootVisible(true);
203     treeView.setShowsRootHandles(true);
204         // treeView.setBackground(java.awt.Color.lightGray);
205
// treeView.setMinimumSize(new java.awt.Dimension(200,100));
206
treeView.setMinimumSize(new Dimension(100,100));
207     treeView.addTreeSelectionListener(this);
208
209     JScrollPane tableScrollPane = JTable.createScrollPaneForTable(contentTable);
210     JScrollPane treeScrollPane = new JScrollPane(treeView);
211         // tableScrollPane.setBackground(java.awt.Color.white);
212
tableScrollPane.setMinimumSize(new java.awt.Dimension JavaDoc(100,100));
213     treeScrollPane.setMinimumSize(new java.awt.Dimension JavaDoc(100,100));
214     tableScrollPane.setPreferredSize(new java.awt.Dimension JavaDoc(100,100));
215     treeScrollPane.setPreferredSize(new java.awt.Dimension JavaDoc(100,100));
216
217     textArea = new JTextArea("Hallo, hallo!");
218     textArea.setEditable(false);
219     textArea.setFont(new java.awt.Font JavaDoc("sansserif",java.awt.Font.PLAIN,10));
220     textArea.setRows(4);
221     textArea.setTabSize(15);
222     textArea.setBorder(BorderFactory.createLoweredBevelBorder());
223     textArea.setMinimumSize(new java.awt.Dimension JavaDoc(100,100));
224     textArea.setPreferredSize(new Dimension(600,90));
225         // textArea.setBackground(java.awt.Color.lightGray);
226

227     splitPane.setLeftComponent(new JScrollPane(treeView));
228     splitPane.setRightComponent(tableScrollPane);
229         // splitPane.setRightComponent(new JScrollPane(contentTable));
230
splitPane.setDividerLocation(300);
231
232     // Hinzufügen der Komponenten
233
Container contentPane = getContentPane();
234         // contentPane.setBackground(java.awt.Color.lightGray);
235
/* getContentPane().setLayout(new java.awt.BorderLayout());
236                 getContentPane().add(splitPane, java.awt.BorderLayout.NORTH);
237                 getContentPane().add(textArea, java.awt.BorderLayout.SOUTH);
238         */

239         // contentPane.setLayout(new BorderLayout());
240
/* Container north = new JPanel();
241                 north.setLayout(new SpringLayout());
242                 Container south = new JPanel();
243                 south.setLayout(new SpringLayout());
244                 north.add(splitPane, SpringLayout.HEIGHT_WIDTH_SPRING);
245                 south.add(textArea);
246                 contentPane.add(north,BorderLayout.NORTH);
247                 contentPane.add(south,BorderLayout.SOUTH);
248         */

249         // contentPane.add(splitPane,"North");
250
// contentPane.add(textArea,"South");
251
splitPane.setBounds(0,0,600,400-textArea.getPreferredSize().height);
252     textArea.setBounds(0,splitPane.bounds().height,600,textArea.getPreferredSize().height);
253         // contentPane.setLayout(layout);
254
contentPane.setLayout(new BorderLayout());
255     contentPane.add("Center",splitPane);
256
257         // contentPane.add(textArea);
258
contentPane.add("South",textArea);
259     contentPane.setBackground(Color.white);
260
261     resize(600,400);
262         // contentPane.setBounds();
263

264         // JPopupMenu popup = new JPopupMenu(treeView);
265
// popup.add(new JMenuItem("Hallo?"));
266
// popup.addPopupMenuListener(new TreePopupMenuListener());
267
// treeView.add(popup);
268

269     JMenuBar menuBar = new JMenuBar();
270     JMenu menu = new JMenu("Navigate");
271     menuBar.add(menu);
272     followTypeMenu = new JMenuItem("Follow Type");
273     followTypeMenu.setEnabled(false);
274     followTypeMenu.addActionListener(this);
275     menu.add(followTypeMenu);
276     setJMenuBar(menuBar);
277
278     validate();
279     treeView.expandPath(new TreePath(((DefaultMutableTreeNode)treeModel.getRoot()).getPath()));
280     
281     treeView.addMouseListener((MouseListener)this);
282     contentTable.addMouseListener((MouseListener)this);
283         // pack();
284
// validate();
285
// validate();
286
// user code end
287
setName("IRBrowser");
288     setName("IRBrowser");
289     initConnections();
290     // user code begin {2}
291
// user code end
292
}
293
294     private static void usage()
295     {
296         System.err.println("usage: IRBrowser [ IOR | -f <ior_file ]");
297         System.exit(1);
298     }
299
300
301     /**
302      * @param args java.lang.String[]
303      */

304
305     public static void main(String JavaDoc args[])
306     {
307         IRBrowser test = null;
308         String JavaDoc ior = null;
309
310         if( args.length > 2 )
311         {
312             usage();
313         }
314         else if( args.length == 2 && args[0].equals("-f"))
315         {
316             try
317             {
318                 java.io.BufferedReader JavaDoc in =
319                     new java.io.BufferedReader JavaDoc(new java.io.FileReader JavaDoc( args[1] ) );
320                 ior = in.readLine();
321                 while (ior.indexOf("IOR:") != 0)
322                     ior = in.readLine();
323                 in.close();
324             }
325             catch ( java.io.IOException JavaDoc io )
326             {
327                 io.printStackTrace();
328                 usage();
329             }
330             test = new IRBrowser(ior);
331         }
332         else if( args.length == 1 )
333         {
334             test = new IRBrowser(args[0]);
335         }
336         else
337         {
338             test = new IRBrowser();
339         }
340         test.show();
341         return;
342     }
343
344     public void mouseClicked(MouseEvent event)
345     {
346     javax.swing.tree.DefaultMutableTreeNode JavaDoc treeNode = null;
347     // bei Doppelklick auf contentTable den treeView auf entsprechende TypeSystemNode setzen
348
if ( event.getComponent() == contentTable &&
349              event.getClickCount() > 1 &&
350              contentTable.getSelectedRow() != -1)
351         {
352             System.out.println("contentTable doubleClick");
353             // im TableModel steckt in jeder Zelle ein NodeMapper, von dem die dazugehörige treeNode zu erfahren ist
354

355             NodeMapper nodeMapper =
356                 (NodeMapper)contentTable.getModel().getValueAt(contentTable.getSelectedRow(),0);
357             TypeSystemNode typeSystemNode = nodeMapper.getNode();
358
359             if (typeSystemNode instanceof AbstractContainer)
360             {
361                 treeNode =
362                     (DefaultMutableTreeNode)typeSystemNode.getModelRepresentant(treeModel);
363             }
364
365             if (typeSystemNode instanceof TypeAssociator)
366             {
367                 TypeSystemNode assTypeNode =
368                     ((TypeAssociator)typeSystemNode).getAssociatedTypeSystemNode();
369                 if (assTypeNode.getModelRepresentant(treeModel) != null)
370                 {
371                     treeNode =
372                         (DefaultMutableTreeNode)assTypeNode.getModelRepresentant(treeModel);
373                 }
374             }
375             if ( treeNode!=null )
376             {
377                 // wenn Node ein AbstractContainer ist oder eine assoziierte TypeSystemNode besitzt, jeweils im treeView dorthin springen
378

379                 System.out.println("expanding Tree: "+treeNode);
380                 DefaultTreeModel treeModel =
381                     (DefaultTreeModel)treeView.getModel();
382                 TreePath fullTreePath =
383                     new TreePath(treeModel.getPathToRoot(treeNode));
384         
385                 treeView.scrollPathToVisible(fullTreePath);
386                 // Selection auf node setzen
387
treeView.setSelectionPath(fullTreePath);
388                 treeView.validate();
389             }
390     }
391     }
392
393     public void mouseEntered(MouseEvent event) {}
394     public void mouseExited(MouseEvent event){}
395     public void mousePressed(MouseEvent event){}
396     public void mouseReleased(MouseEvent event){}
397
398     /**
399      * Setze Titel des Frames und enable/disable Menüs je nach
400      * selektierter Node (Node kann in TableView oder in TreeView
401      * selektiert worden sein)
402      * @param node typesystem.TypeSystemNode
403      */

404
405     public void setSelectedNode (TypeSystemNode node )
406     {
407     // Node kann TableView oder TreeView selektiert worden sein
408
setTitle(title + " - " + node.getAbsoluteName());
409     textArea.setText(node.description());
410     if (node instanceof TypeAssociator)
411         {
412             followTypeMenu.setEnabled(true);
413     }
414     else
415         {
416             followTypeMenu.setEnabled(false);
417     }
418     }
419
420
421     /**
422      */

423
424     public void valueChanged (ListSelectionEvent e )
425     {
426     // contentTable nur bei einfacher Selection ändern
427
// System.out.println("valueChanged (Table...)");
428
TypeSystemNode node;
429     if (contentTable.getSelectedRow() != -1)
430         {
431             NodeMapper nodeMapper =
432                 (NodeMapper)contentTable.getModel().getValueAt(contentTable.getSelectedRow(),0);
433
434             if ((node = ((TypeSystemNode)nodeMapper.getNode())) != null)
435             {
436                 setSelectedNode(node);
437             }
438     }
439     }
440
441     /**
442      */

443
444     public void valueChanged (TreeSelectionEvent e )
445     {
446         // contentTable nur bei einfacher Selection ändern
447
DefaultMutableTreeNode treeNode =
448             (DefaultMutableTreeNode)e.getPath().getLastPathComponent();
449
450         TypeSystemNode node =
451             (TypeSystemNode)treeNode.getUserObject();
452
453         contentTable.setModel(typeSystem.getTableModel(treeNode));
454         contentTable.clearSelection();
455         TableColumnModel tabColMod = contentTable.getColumnModel();
456
457         for (int i=0; i<contentTable.getColumnCount(); i++)
458         {
459             TableColumn tabCol = tabColMod.getColumn(i);
460             tabCol.setCellEditor( null );
461             // otherwise columns would be editable
462
}
463         setSelectedNode(node);
464         contentTable.validate();
465         treeView.validate();
466     }
467
468     /**
469      * Method to handle events for the WindowListener interface.
470      * @param e java.awt.event.WindowEvent
471      */

472
473     public void windowActivated(java.awt.event.WindowEvent JavaDoc e) {
474     // user code begin {1}
475
// user code end
476
// user code begin {2}
477
// user code end
478
}
479
480     /**
481      * Method to handle events for the WindowListener interface.
482      * @param e java.awt.event.WindowEvent
483      */

484
485     /* WARNING: THIS METHOD WILL BE REGENERATED. */
486
487     public void windowClosed(java.awt.event.WindowEvent JavaDoc e)
488     {
489     // user code begin {1}
490
System.exit(0);
491     // user code end
492
// user code begin {2}
493
// user code end
494

495     }
496
497     /**
498      * Method to handle events for the WindowListener interface.
499      * @param e java.awt.event.WindowEvent
500      */

501     /* WARNING: THIS METHOD WILL BE REGENERATED. */
502
503     public void windowClosing(java.awt.event.WindowEvent JavaDoc e)
504     {
505     // user code begin {1}
506
// user code end
507
if ((e.getSource() == this) )
508         {
509             conn0(e);
510     }
511     // user code begin {2}
512
// user code end
513
}
514
515     /**
516      * Method to handle events for the WindowListener interface.
517      * @param e java.awt.event.WindowEvent
518      */

519     /* WARNING: THIS METHOD WILL BE REGENERATED. */
520
521     public void windowDeactivated(java.awt.event.WindowEvent JavaDoc e) {
522     // user code begin {1}
523
// user code end
524
// user code begin {2}
525
// user code end
526
}
527
528     /**
529      * Method to handle events for the WindowListener interface.
530      * @param e java.awt.event.WindowEvent
531      */

532     /* WARNING: THIS METHOD WILL BE REGENERATED. */
533
534     public void windowDeiconified(java.awt.event.WindowEvent JavaDoc e) {
535     // user code begin {1}
536
// user code end
537
// user code begin {2}
538
// user code end
539
}
540
541     /**
542      * Method to handle events for the WindowListener interface.
543      * @param e java.awt.event.WindowEvent
544      */

545     /* WARNING: THIS METHOD WILL BE REGENERATED. */
546
547     public void windowIconified(java.awt.event.WindowEvent JavaDoc e) {
548     // user code begin {1}
549
// user code end
550
// user code begin {2}
551
// user code end
552
}
553
554     /**
555      * Method to handle events for the WindowListener interface.
556      * @param e java.awt.event.WindowEvent
557      */

558     /* WARNING: THIS METHOD WILL BE REGENERATED. */
559
560     public void windowOpened(java.awt.event.WindowEvent JavaDoc e)
561     {
562     // user code begin {1}
563
// user code end
564
// user code begin {2}
565
// user code end
566
}
567 }
568
569
Popular Tags