KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > idltree > IdlTreePanel


1 /***************************************************************************/
2 /* COACH: Component Based Open Source Architecture for */
3 /* Distributed Telecom Applications */
4 /* See: http://www.objectweb.org/ */
5 /* */
6 /* Copyright (C) 2003 Lucent Technologies Nederland BV */
7 /* Bell Labs Advanced Technologies - EMEA */
8 /* */
9 /* Initial developer(s): Harold Batteram */
10 /* */
11 /* This library is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public */
13 /* License as published by the Free Software Foundation; either */
14 /* version 2.1 of the License, or (at your option) any later version. */
15 /* */
16 /* This library is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
19 /* Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public */
22 /* License along with this library; if not, write to the Free Software */
23 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 /***************************************************************************/
25 package org.coach.idltree;
26
27 import java.awt.*;
28 import javax.swing.*;
29 import javax.swing.event.*;
30 import javax.swing.tree.*;
31 import java.awt.event.*;
32 import java.util.*;
33 import java.io.*;
34
35 public class IdlTreePanel extends JPanel {
36     private TreePath selection;
37     private IdlModel model;
38     private IdlNode nodeCopy;
39     private BorderLayout borderLayout1 = new BorderLayout();
40     private JScrollPane TreeScrollPane = new JScrollPane();
41     private JTree typeTree = new JTree();
42     private JPopupMenu popupMenu = new JPopupMenu();
43     private JMenuItem copyMenuItem = new JMenuItem();
44     private JMenuItem pasteMenuItem = new JMenuItem();
45     private JMenuItem saveMenuItem = new JMenuItem();
46     private JMenuItem cloneMenuItem = new JMenuItem();
47     private JFileChooser chooser;
48     private JMenuItem openMenuItem = new JMenuItem();
49     private TypeSelectorDialog typeSelectorDialog;
50     private Frame parentFrame;
51     private ObjectListener objectListener;
52
53     /** the list of listeners for the ModelChangeEvent */
54     protected Vector modelListeners = new Vector();
55
56     public void addModelListener(ModelChangeListener listener) {
57         if (!modelListeners.contains(listener)) {
58             modelListeners.addElement(listener);
59         }
60     }
61
62     public void removeModelListener(ModelChangeListener listener) {
63         modelListeners.removeElement(listener);
64     }
65     
66     public void addObjectListener(ObjectListener objectListener) {
67         this.objectListener = objectListener;
68     }
69
70     public void removeObjectListener(ObjectListener objectListener) {
71         this.objectListener = null;
72     }
73
74     public void notifyModelListeners() {
75         ModelChangeEvent evt = new ModelChangeEvent(model, this);
76         ModelChangeListener listener;
77         for (int i = 0; i < modelListeners.size(); i++) {
78             listener = (ModelChangeListener)modelListeners.elementAt(i);
79             listener.modelChanged(evt);
80         }
81     }
82     
83     public IdlTreePanel() {
84         try {
85             jbInit();
86         }
87         catch(Exception JavaDoc ex) {
88             ex.printStackTrace();
89         }
90     }
91
92     public IdlTreePanel(IdlModel m) {
93         try {
94             jbInit();
95             setModel(m);
96             IdList.getTree();
97         }
98         catch(Exception JavaDoc ex) {
99             ex.printStackTrace();
100         }
101     }
102     
103     public boolean isReply() {
104         if (model != null && model.getRoot() instanceof IdlReply) {
105             return true;
106         }
107         return false;
108     }
109                 
110     private Frame getFrame() {
111         if (parentFrame != null) {
112             return parentFrame;
113         }
114         Component c = this;
115         while (c != null && !(c instanceof Frame)) {
116             c = c.getParent();
117         }
118         parentFrame = (Frame)c;
119         return parentFrame;
120     }
121     
122     private TypeSelectorDialog getTypeSelectorDialog() {
123         if (typeSelectorDialog != null) {
124             return typeSelectorDialog;
125         }
126         typeSelectorDialog = new TypeSelectorDialog();
127         return typeSelectorDialog;
128     }
129
130     public void setModel(IdlModel tm) {
131         model = tm;
132         typeTree.setModel(model);
133         typeTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
134         typeTree.addTreeSelectionListener(new TreeSelectionListener() {
135             public void valueChanged(TreeSelectionEvent e) {
136                 TreePath selection = e.getNewLeadSelectionPath();
137                 model.setSelection(selection);
138             }
139         });
140
141         typeTree.setEditable(true);
142         TreeCellEditor editor = new TypeNodeEditor(model);
143         typeTree.setCellEditor(editor);
144         typeTree.validate();
145     }
146     
147     private void updateModel(IdlModel tm) {
148         setModel(tm);
149         notifyModelListeners();
150     }
151     
152     private void jbInit() throws Exception JavaDoc {
153         this.setLayout(borderLayout1);
154         chooser = new JFileChooser(".");
155         chooser.setDialogTitle("Save as XML file");
156         chooser.setApproveButtonText("Save");
157         chooser.setApproveButtonToolTipText("Saving File");
158         chooser.setAutoscrolls(true);
159         chooser.setDialogType(chooser.SAVE_DIALOG);
160         chooser.addChoosableFileFilter(new XML_FileFilter());
161         copyMenuItem.setText("copy");
162         copyMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
163
164             public void actionPerformed(ActionEvent e) {
165                 copyMenuItem_actionPerformed(e);
166             }
167         });
168         pasteMenuItem.setText("paste");
169         pasteMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
170
171             public void actionPerformed(ActionEvent e) {
172                 pasteMenuItem_actionPerformed(e);
173             }
174         });
175         saveMenuItem.setText("save");
176         saveMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
177
178             public void actionPerformed(ActionEvent e) {
179                 saveMenuItem_actionPerformed(e);
180             }
181         });
182         typeTree.addMouseListener(new java.awt.event.MouseAdapter JavaDoc() {
183
184             public void mouseClicked(MouseEvent e) {
185                 showPopup(e);
186             }
187
188             public void mousePressed(MouseEvent e) {
189                 showPopup(e);
190             }
191
192             public void mouseReleased(MouseEvent e) {
193                 showPopup(e);
194             }
195         });
196         cloneMenuItem.setText("clone");
197         cloneMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
198
199             public void actionPerformed(ActionEvent e) {
200                 cloneMenuItem_actionPerformed(e);
201             }
202         });
203
204         openMenuItem.setText("open");
205         openMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
206
207             public void actionPerformed(ActionEvent e) {
208                 openMenuItem_actionPerformed(e);
209             }
210         });
211         this.add(TreeScrollPane, BorderLayout.CENTER);
212         TreeScrollPane.getViewport().add(typeTree, null);
213         popupMenu.add(copyMenuItem);
214         popupMenu.add(pasteMenuItem);
215         popupMenu.add(saveMenuItem);
216         popupMenu.add(openMenuItem);
217         popupMenu.add(cloneMenuItem);
218     }
219
220     void copyMenuItem_actionPerformed(ActionEvent e) {
221         IdlNode node = (IdlNode)model.getSelection().getLastPathComponent();
222         nodeCopy = node.copy();
223     }
224
225     void pasteMenuItem_actionPerformed(ActionEvent e) {
226         if (nodeCopy != null && model.getSelection() != null) {
227             IdlNode node = (IdlNode)model.getSelection().getLastPathComponent();
228             IdlNode parent = (IdlNode)node.getParent();
229             if (node.canPaste(nodeCopy)) {
230                 node.paste(nodeCopy);
231                 try {
232                     model.nodeStructureChanged(node);
233                 } catch(Exception JavaDoc ex) {
234                     JOptionPane.showMessageDialog(null, ex.toString());
235 ex.printStackTrace();
236                 }
237             } else {
238                 JOptionPane.showMessageDialog(null, "type mismatch");
239             }
240         }
241     }
242
243     void saveMenuItem_actionPerformed(ActionEvent e) {
244         IdlNode node = (IdlNode)model.getRoot();
245         int state = chooser.showSaveDialog(null);
246         File file = chooser.getSelectedFile();
247
248         if (file != null && state == JFileChooser.APPROVE_OPTION) {
249             try {
250                 XmlWriter w = new XmlWriter();
251                 XmlNode.write(node, w);
252                 PrintWriter p = new PrintWriter(new FileWriter(file));
253                 p.println(w.toString());
254                 p.close();
255 // System.out.println(w.toString());
256
} catch (Exception JavaDoc ex) {
257                 ex.printStackTrace();
258                 JOptionPane.showMessageDialog(null, ex.toString());
259             }
260         }
261     }
262
263     void showPopup(MouseEvent e) {
264         if (e.isPopupTrigger()) {
265             popupMenu.show(typeTree, e.getX(), e.getY());
266         }
267     }
268
269     void cloneMenuItem_actionPerformed(ActionEvent e) {
270         IdlNode node = (IdlNode)model.getSelection().getLastPathComponent();
271         IdlNode clone = (IdlNode)node.clone();
272         if (clone != null) {
273 // TypeTreeFrame frame = new TypeTreeFrame(new IdlModel(clone));
274
// GUIApp.launch(frame,"AnyTree", 300, 300, 450, 400);
275
}
276     }
277
278     void openMenuItem_actionPerformed(ActionEvent e) {
279         IdlNode node = (IdlNode)model.getRoot();
280         int state = chooser.showOpenDialog(null);
281         File file = chooser.getSelectedFile();
282
283         if (file != null && state == JFileChooser.APPROVE_OPTION) {
284             IdlNode t = null;
285             try {
286 // t = node.fromAnyFile(file.getPath());
287
} catch (Exception JavaDoc ex) {
288                 JOptionPane.showMessageDialog(null, ex.toString());
289 ex.printStackTrace();
290             }
291             if (t != null) {
292                 try {
293 // updateModel(new TypeModel(t));
294
} catch(Exception JavaDoc ex) {
295                     JOptionPane.showMessageDialog(null, ex.toString());
296 ex.printStackTrace();
297                 }
298             }
299         }
300     }
301
302     class XML_FileFilter extends javax.swing.filechooser.FileFilter JavaDoc{
303         public boolean accept(File f){
304             String JavaDoc path = f.getPath().toLowerCase();
305             if (path.endsWith(".xml")){
306                 return true;
307             }
308             return false;
309         }
310
311        public String JavaDoc getDescription(){
312             return "XML files (*.xml)";
313         }
314     }
315
316     /**
317      * This class handles the presentation of a node.
318      */

319     class TypeNodeRenderer extends DefaultTreeCellRenderer {
320         private Component strut = Box.createHorizontalStrut(5);
321         private Hashtable nodes = new Hashtable();
322     
323         public TypeNodeRenderer() {
324             super();
325         }
326     
327         public Component getTreeCellRendererComponent(JTree tree, Object JavaDoc value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
328             JPanel panel;
329             IdlNode node = (IdlNode)value;
330             Object JavaDoc key = node.getUserObject();
331     
332             Component cmp = null;
333             // to make a node uneditable set cmp to null. The default is a text field
334
if (node instanceof IdlUnion) {
335                 cmp = createComboBox(((IdlUnion)node).getTags(), ((IdlUnion)node).getIndex());
336             } else if (node instanceof IdlEnum) {
337                 cmp = createComboBox(((IdlEnum)node).getEnums(), ((IdlEnum)node).getIndex());
338             } else if (node instanceof IdlOperation) {
339                 cmp = null;
340             } else if (node instanceof IdlParameter) {
341                 cmp = null;
342             } else if (node instanceof IdlStruct) {
343                 cmp = null;
344             } else if (node instanceof IdlException) {
345                 cmp = null;
346             } else if (node instanceof IdlReply) {
347                 String JavaDoc[] exceptions = ((IdlReply)node).getExceptions();
348                 if (exceptions.length > 0) {
349                     // create a combo box with all the exception names plus a "no exception" choice.
350
String JavaDoc[] choices = new String JavaDoc[exceptions.length + 1];
351                     choices[0] = "no exception";
352                     for (int i = 0; i < exceptions.length; i++) {
353                         choices[i + 1] = exceptions[i];
354                     }
355                     cmp = createComboBox(choices, 0);
356                 } else {
357                     cmp = null;
358                 }
359             } else if (node.getType().equals("boolean")) {
360                 cmp = createComboBox(new String JavaDoc[] {"FALSE", "TRUE"}, (node.getValue().equals("TRUE") ? 1 : 0));
361             } else if (node instanceof IdlAny) {
362                 cmp = null;
363
364             } else if (node instanceof IdlSequence) {
365                 if (((IdlSequence)node).isBounded()) {
366                     cmp = null;
367                 } else {
368                     JTextField t = new JTextField();
369                     t.setColumns(5);
370                     t.setText("" + ((IdlSequence)node).getSize());
371                     cmp = t;
372                 }
373             } else if (node instanceof IdlObject) {
374                 JTextField t = new JTextField();
375                 final String JavaDoc v = node.getValue();
376                 t.setColumns(10);
377                 t.setText(v);
378                 t.setCaretPosition(0);
379                 cmp = t;
380                 if (isReply()) {
381                     objectAction(v);
382                 }
383             } else {
384                 JTextField t = new JTextField();
385                 String JavaDoc v = node.getValue();
386                 t.setColumns(10);
387                 t.setText(v);
388                 t.setCaretPosition(0);
389                 cmp = t;
390             }
391             if (cmp != null) {
392                 nodes.put(key, cmp);
393             }
394             Component x = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
395     
396             JLabel label = new JLabel(node.toString());
397             panel = new JPanel();
398             setOpaque(false);
399             panel.setOpaque(false);
400             panel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
401             panel.add(x);
402             if (cmp != null) {
403                 panel.add(strut);
404                 cmp.setVisible(false);
405                 panel.add(cmp);
406             }
407             return panel;
408         }
409         
410         private void objectAction(String JavaDoc ior) {
411             if (objectListener != null) {
412                 objectListener.objectAction(ior);
413             }
414         }
415     
416         public Component getEdit(Object JavaDoc value) {
417             Object JavaDoc key = ((IdlNode)value).getUserObject();
418             return (Component)nodes.get(key);
419         }
420     
421         private JComboBox createComboBox(String JavaDoc[] list, int idx) {
422             JComboBox c = new JComboBox();
423             for (int i = 0; i < list.length; i++) {
424                 c.addItem(list[i]);
425             }
426             c.setSelectedIndex(idx);
427             return c;
428         }
429     }
430
431     /**
432      * This class handles editing of a node.
433      */

434     class TypeNodeEditor extends AbstractCellEditor {
435         private DefaultTreeModel model;
436         private TypeNodeRenderer renderer;
437         private IdlNode node;
438         private IdlNode parent;
439     
440         public TypeNodeEditor(DefaultTreeModel m) {
441             model = m;
442             renderer = new TypeNodeRenderer();
443         }
444     
445         public Component getTreeCellEditorComponent(JTree tree, Object JavaDoc value, boolean selected, boolean expanded, boolean leaf, int row) {
446             node = (IdlNode)value;
447     
448             Component c = renderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, true);
449     
450             final Component cmp = renderer.getEdit(value);
451             final JComboBox combo;
452             final JTextField text;
453             final JTree idtree;
454
455             if (node instanceof IdlUnion) {
456                 final IdlUnion union = (IdlUnion)node;
457                 combo = (JComboBox)cmp;
458                 combo.addActionListener(new ActionListener() {
459                     public void actionPerformed(ActionEvent e) {
460                         JComboBox src = (JComboBox)e.getSource();
461                         int idx = src.getSelectedIndex();
462                         try {
463                             union.setIndex(idx);
464                             model.nodeStructureChanged(union.getParent());
465                         } catch (Exception JavaDoc ex) {
466                             JOptionPane.showMessageDialog(null, ex.toString());
467 ex.printStackTrace();
468                         }
469                         stopCellEditing();
470                     }
471                 });
472             } else if (node instanceof IdlReply) {
473                 final IdlReply reply = (IdlReply)node;
474                 String JavaDoc[] exceptions = reply.getExceptions();
475                 if (exceptions.length > 0) {
476                     combo = (JComboBox)cmp;
477                     combo.addActionListener(new ActionListener() {
478                         public void actionPerformed(ActionEvent e) {
479                             JComboBox src = (JComboBox)e.getSource();
480                             int idx = src.getSelectedIndex();
481                             try {
482                                 reply.setException(idx - 1);
483                                 model.nodeStructureChanged(reply);
484                             } catch (Exception JavaDoc ex) {
485                                 JOptionPane.showMessageDialog(null, ex.toString());
486 ex.printStackTrace();
487                             }
488                             stopCellEditing();
489                         }
490                     });
491                 }
492             } else if (node.getType().equals("boolean")) {
493                 combo = (JComboBox)cmp;
494                 combo.addActionListener(new ActionListener() {
495                     public void actionPerformed(ActionEvent e) {
496                         JComboBox src = (JComboBox)e.getSource();
497                         int idx = src.getSelectedIndex();
498                         try {
499                             node.setValue(idx == 0 ? "FALSE" : "TRUE");
500                         } catch (Exception JavaDoc ex) {
501                             JOptionPane.showMessageDialog(null, ex.toString());
502 ex.printStackTrace();
503                         }
504                         stopCellEditing();
505                     }
506                 });
507             } else if (node instanceof IdlEnum) {
508                 final IdlEnum idlEnum = (IdlEnum)node;
509                 combo = (JComboBox)cmp;
510                 combo.addActionListener(new ActionListener() {
511                     public void actionPerformed(ActionEvent e) {
512                         JComboBox src = (JComboBox)e.getSource();
513                         int idx = src.getSelectedIndex();
514                         try {
515                             idlEnum.setIndex(idx);
516                         } catch (Exception JavaDoc ex) {
517                             JOptionPane.showMessageDialog(null, ex.toString());
518 ex.printStackTrace();
519                         }
520                         stopCellEditing();
521                     }
522                 });
523             } else if (node instanceof IdlSequence) {
524                 final IdlSequence seq = (IdlSequence)node;
525                 if (!seq.isBounded()) {
526                     text = (JTextField)cmp;
527                     text.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
528                         public void focusLost(FocusEvent e) {
529                             JTextField src = (JTextField)e.getSource();
530                             String JavaDoc size = src.getText();
531                             try {
532                                 seq.setSize(Integer.parseInt(size));
533                                 model.nodeStructureChanged(seq.getParent());
534                             } catch (Exception JavaDoc ex) {
535                                 JOptionPane.showMessageDialog(null, ex.toString());
536 ex.printStackTrace();
537                             }
538                             stopCellEditing();
539                         }
540                     });
541                 } else {
542                     stopCellEditing();
543                 }
544             } else if (node instanceof IdlAny) {
545                 final IdlAny any = (IdlAny)node;
546                 TypeSelectorDialog d = getTypeSelectorDialog();
547                 d.show();
548                 String JavaDoc id = d.getSelection();
549                 if (!id.equals("")) {
550                     try {
551                         any.setContent(id);
552                         model.nodeStructureChanged(any.getParent());
553                     } catch (Exception JavaDoc ex) {
554                         JOptionPane.showMessageDialog(null, ex.toString());
555 ex.printStackTrace();
556                     }
557                 }
558                 stopCellEditing();
559             } else {
560                 text = (JTextField)cmp;
561                 if (text != null) {
562                     text.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
563                         public void focusLost(FocusEvent e) {
564                             JTextField src = (JTextField)e.getSource();
565                             String JavaDoc v = src.getText();
566                             try {
567                                 node.setValue(v);
568                             } catch (Exception JavaDoc ex) {
569                                 JOptionPane.showMessageDialog(null, ex.toString());
570 ex.printStackTrace();
571                             }
572                             stopCellEditing();
573                         }
574                     });
575                 }
576             }
577             if (cmp != null) {
578                 cmp.setVisible(true);
579             }
580             return c;
581         }
582     
583         public Object JavaDoc getCellEditorValue() {
584             return node.getUserObject();
585         }
586     }
587  
588     class TypeSelectorDialog extends JDialog {
589         private TypeSelector typeSelector;
590         
591         public TypeSelectorDialog() {
592             super(getFrame(), "Select id", true);
593             setLocationRelativeTo(getFrame());
594             typeSelector = new TypeSelector(this);
595             setContentPane(typeSelector);
596             pack();
597         }
598
599         public String JavaDoc getSelection() {
600             return typeSelector.getSelection();
601         }
602     }
603
604     class TypeSelector extends JPanel {
605         private String JavaDoc selection = "";
606         private JButton cancelButton = new JButton("Cancel");
607         private JButton okButton = new JButton("Ok");
608         private JLabel selectionLabel = new JLabel(" ");
609         private JDialog dialog;
610
611         public TypeSelector(JDialog d) {
612             dialog = d;
613             final JTree idTree = IdList.getTree();
614             setLayout(new BorderLayout());
615             JScrollPane typeTreeScrollPane = new JScrollPane();
616             add(typeTreeScrollPane, BorderLayout.CENTER);
617             typeTreeScrollPane.getViewport().add(idTree, null);
618             JPanel buttonPanel = new JPanel();
619             buttonPanel.add(okButton);
620             buttonPanel.add(cancelButton);
621             add(buttonPanel, BorderLayout.SOUTH);
622             add(selectionLabel, BorderLayout.NORTH);
623             
624             cancelButton.addActionListener(new ActionListener() {
625                 public void actionPerformed(ActionEvent e) {
626                     selection = "";
627                     selectionLabel.setText(" ");
628                     dialog.dispose();
629                 }
630             });
631             
632             okButton.addActionListener(new ActionListener() {
633                 public void actionPerformed(ActionEvent e) {
634                     dialog.dispose();
635                 }
636             });
637                                    
638             idTree.addTreeSelectionListener(new TreeSelectionListener() {
639                 public void valueChanged(TreeSelectionEvent e) {
640                     DefaultMutableTreeNode node = (DefaultMutableTreeNode)idTree.getLastSelectedPathComponent();
641                     if (node == null || !node.isLeaf()) {
642                         selection = "";
643                         selectionLabel.setText(" ");
644                         return;
645                     }
646                     TreeNode[] p = node.getPath();
647                     String JavaDoc id = "";
648                     String JavaDoc sep = "";
649                     for (int i = 1; i < p.length; i++) {
650                         id += sep + (String JavaDoc)((DefaultMutableTreeNode)p[i]).getUserObject();
651                         sep = "/";
652                     }
653                     selection = id;
654                     selectionLabel.setText(selection);
655                 }
656             });
657         }
658         
659         public String JavaDoc getSelection() {
660             return selection;
661         }
662     }
663 }
664
Popular Tags