KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > gui > SpoonModelTree


1 package spoon.support.gui;
2
3 import java.awt.GridLayout JavaDoc;
4 import java.awt.event.ActionEvent JavaDoc;
5 import java.awt.event.ActionListener JavaDoc;
6 import java.awt.event.KeyEvent JavaDoc;
7 import java.awt.event.KeyListener JavaDoc;
8 import java.awt.event.MouseEvent JavaDoc;
9 import java.awt.event.MouseListener JavaDoc;
10 import java.io.FileOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.util.Enumeration JavaDoc;
13
14 import javax.swing.JFileChooser JavaDoc;
15 import javax.swing.JFrame JavaDoc;
16 import javax.swing.JMenuItem JavaDoc;
17 import javax.swing.JOptionPane JavaDoc;
18 import javax.swing.JPanel JavaDoc;
19 import javax.swing.JPopupMenu JavaDoc;
20 import javax.swing.JScrollPane JavaDoc;
21 import javax.swing.JTree JavaDoc;
22 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
23 import javax.swing.tree.TreePath JavaDoc;
24
25 import spoon.reflect.Factory;
26 import spoon.reflect.declaration.CtPackage;
27 import spoon.support.SerializationModelStreamer;
28
29
30 public class SpoonModelTree extends JFrame JavaDoc implements KeyListener JavaDoc,
31         MouseListener JavaDoc {
32
33     private static final long serialVersionUID = 1L;
34
35     Enumeration JavaDoc enume;
36
37     private Factory factory;
38
39     private JPanel JavaDoc jContentPane = null;
40
41     private JScrollPane JavaDoc jScrollPane = null;
42
43     private JTree JavaDoc jTree = null;
44
45     JPopupMenu JavaDoc menu;
46
47     private DefaultMutableTreeNode JavaDoc root; // @jve:decl-index=0:visual-constraint="207,57"
48

49     String JavaDoc searchValue;
50
51     /**
52      * This is the default constructor
53      */

54     public SpoonModelTree(Factory factory) {
55         super();
56         SpoonTreeBuilder cst = new SpoonTreeBuilder();
57         for (CtPackage p : factory.Package().getAllRoots())
58             cst.scan(p);
59         this.factory = factory;
60         this.root = cst.getRoot();
61         initialize();
62     }
63
64     /**
65      * This method initializes jContentPane
66      *
67      * @return javax.swing.JPanel
68      */

69     private JPanel JavaDoc getJContentPane() {
70         if (jContentPane == null) {
71             GridLayout JavaDoc gridLayout = new GridLayout JavaDoc();
72             gridLayout.setRows(1);
73             jContentPane = new JPanel JavaDoc();
74             jContentPane.setLayout(gridLayout);
75             jContentPane.add(getJScrollPane(), null);
76         }
77         return jContentPane;
78     }
79
80     /**
81      * This method initializes jScrollPane
82      *
83      * @return javax.swing.JScrollPane
84      */

85     private JScrollPane JavaDoc getJScrollPane() {
86         if (jScrollPane == null) {
87             jScrollPane = new JScrollPane JavaDoc();
88             jScrollPane.setViewportView(getJTree());
89         }
90         return jScrollPane;
91     }
92
93     /**
94      * This method initializes jTree
95      *
96      * @return javax.swing.JTree
97      */

98     private JTree JavaDoc getJTree() {
99         if (jTree == null) {
100             jTree = new JTree JavaDoc(root);
101             jTree.addKeyListener(this);
102             jTree.addMouseListener(this);
103         }
104         return jTree;
105     }
106
107     private JPopupMenu JavaDoc getMenu() {
108         if (menu == null) {
109             menu = new JPopupMenu JavaDoc();
110
111             JMenuItem JavaDoc item = new JMenuItem JavaDoc("Save");
112             item.addActionListener(new ActionListener JavaDoc() {
113                 public void actionPerformed(ActionEvent JavaDoc e) {
114
115                     JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
116                     chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
117                     boolean cont = chooser.showSaveDialog(SpoonModelTree.this) == JFileChooser.APPROVE_OPTION;
118                     if (cont) {
119                         SerializationModelStreamer ser = new SerializationModelStreamer();
120                         try {
121                             ser.save(factory, new FileOutputStream JavaDoc(chooser
122                                     .getSelectedFile()));
123                         } catch (IOException JavaDoc e1) {
124                             e1.printStackTrace();
125                         }
126                     }
127                 }
128             });
129
130             menu.add(item);
131             menu.addSeparator();
132
133             // show reflect table
134
item = new JMenuItem JavaDoc("Reflect");
135             item.addActionListener(new ActionListener JavaDoc() {
136                 public void actionPerformed(ActionEvent JavaDoc e) {
137                     DefaultMutableTreeNode JavaDoc node = (DefaultMutableTreeNode JavaDoc) jTree
138                             .getLastSelectedPathComponent();
139                     if (node == null) {
140                         node = root;
141                     }
142                     new SpoonObjectFieldsTable(node.getUserObject());
143                 }
144             });
145             menu.add(item);
146
147             menu.addSeparator();
148
149             // Search value
150
item = new JMenuItem JavaDoc("Search");
151             item.addActionListener(new ActionListener JavaDoc() {
152                 public void actionPerformed(ActionEvent JavaDoc e) {
153                     search();
154                 }
155             });
156             menu.add(item);
157
158             // Search value
159
item = new JMenuItem JavaDoc("Search next");
160             item.addActionListener(new ActionListener JavaDoc() {
161                 public void actionPerformed(ActionEvent JavaDoc e) {
162                     next();
163                 }
164             });
165             menu.add(item);
166         }
167         return menu;
168     }
169
170     /**
171      * This method initializes this
172      *
173      * @return void
174      */

175     private void initialize() {
176         this.setSize(640, 480);
177         this.setLocation((getGraphicsConfiguration().getDevice()
178                 .getDisplayMode().getWidth() - getWidth()) / 2,
179                 (getGraphicsConfiguration().getDevice().getDisplayMode()
180                         .getHeight() - getHeight()) / 2);
181
182         this.setContentPane(getJContentPane());
183         this.setTitle("Spoon");
184         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
185         this.setVisible(true);
186     }
187
188     public void keyPressed(KeyEvent JavaDoc e) {
189
190     }
191
192     public void keyReleased(KeyEvent JavaDoc e) {
193
194     }
195
196     public void keyTyped(KeyEvent JavaDoc e) {
197         switch (e.getKeyChar()) {
198         case ('s'):
199
200             break;
201         case ('n'):
202             next();
203             break;
204         case ('o'):
205             if (jTree.getLastSelectedPathComponent() != null)
206                 new SpoonObjectFieldsTable(((DefaultMutableTreeNode JavaDoc) jTree
207                         .getLastSelectedPathComponent()).getUserObject());
208             break;
209         }
210     }
211
212     private void maybeShowPopup(MouseEvent JavaDoc e) {
213         if (e.isPopupTrigger()) {
214             getMenu().show(e.getComponent(), e.getX(), e.getY());
215         }
216     }
217
218     public void mouseClicked(MouseEvent JavaDoc e) {
219
220     }
221
222     public void mouseEntered(MouseEvent JavaDoc e) {
223     }
224
225     public void mouseExited(MouseEvent JavaDoc e) {
226     }
227
228     public void mousePressed(MouseEvent JavaDoc e) {
229         getJTree().setSelectionRow(
230                 getJTree().getClosestRowForLocation(e.getX(), e.getY()));
231         maybeShowPopup(e);
232     }
233
234     public void mouseReleased(MouseEvent JavaDoc e) {
235         maybeShowPopup(e);
236     }
237
238     public DefaultMutableTreeNode JavaDoc next() {
239         DefaultMutableTreeNode JavaDoc current = null;
240         while (enume != null && enume.hasMoreElements()) {
241             current = (DefaultMutableTreeNode JavaDoc) enume.nextElement();
242             if (current.getUserObject() != null
243                     && current.getUserObject().toString().contains(searchValue)) {
244                 setVisible(current);
245                 return current;
246             }
247         }
248         return null;
249     }
250
251     public DefaultMutableTreeNode JavaDoc search() {
252         searchValue = JOptionPane.showInputDialog(this,
253                 "Enter value to search:", "Search");
254
255         DefaultMutableTreeNode JavaDoc node = (DefaultMutableTreeNode JavaDoc) jTree
256                 .getLastSelectedPathComponent();
257         if (node == null) {
258             node = root;
259         }
260         enume = node.depthFirstEnumeration();
261
262         if (searchValue != null)
263             return next();
264         else
265             return null;
266     }
267
268     public void setVisible(DefaultMutableTreeNode JavaDoc node) {
269         TreePath JavaDoc path = new TreePath JavaDoc(node.getPath());
270         getJTree().scrollPathToVisible(path);
271         getJTree().setSelectionPath(path);
272     }
273
274 }
275
Popular Tags