1 package spoon.support.gui; 2 3 import java.awt.GridLayout ; 4 import java.awt.event.ActionEvent ; 5 import java.awt.event.ActionListener ; 6 import java.awt.event.KeyEvent ; 7 import java.awt.event.KeyListener ; 8 import java.awt.event.MouseEvent ; 9 import java.awt.event.MouseListener ; 10 import java.io.FileOutputStream ; 11 import java.io.IOException ; 12 import java.util.Enumeration ; 13 14 import javax.swing.JFileChooser ; 15 import javax.swing.JFrame ; 16 import javax.swing.JMenuItem ; 17 import javax.swing.JOptionPane ; 18 import javax.swing.JPanel ; 19 import javax.swing.JPopupMenu ; 20 import javax.swing.JScrollPane ; 21 import javax.swing.JTree ; 22 import javax.swing.tree.DefaultMutableTreeNode ; 23 import javax.swing.tree.TreePath ; 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 implements KeyListener , 31 MouseListener { 32 33 private static final long serialVersionUID = 1L; 34 35 Enumeration enume; 36 37 private Factory factory; 38 39 private JPanel jContentPane = null; 40 41 private JScrollPane jScrollPane = null; 42 43 private JTree jTree = null; 44 45 JPopupMenu menu; 46 47 private DefaultMutableTreeNode root; 49 String searchValue; 50 51 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 69 private JPanel getJContentPane() { 70 if (jContentPane == null) { 71 GridLayout gridLayout = new GridLayout (); 72 gridLayout.setRows(1); 73 jContentPane = new JPanel (); 74 jContentPane.setLayout(gridLayout); 75 jContentPane.add(getJScrollPane(), null); 76 } 77 return jContentPane; 78 } 79 80 85 private JScrollPane getJScrollPane() { 86 if (jScrollPane == null) { 87 jScrollPane = new JScrollPane (); 88 jScrollPane.setViewportView(getJTree()); 89 } 90 return jScrollPane; 91 } 92 93 98 private JTree getJTree() { 99 if (jTree == null) { 100 jTree = new JTree (root); 101 jTree.addKeyListener(this); 102 jTree.addMouseListener(this); 103 } 104 return jTree; 105 } 106 107 private JPopupMenu getMenu() { 108 if (menu == null) { 109 menu = new JPopupMenu (); 110 111 JMenuItem item = new JMenuItem ("Save"); 112 item.addActionListener(new ActionListener () { 113 public void actionPerformed(ActionEvent e) { 114 115 JFileChooser chooser = new JFileChooser (); 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 (chooser 122 .getSelectedFile())); 123 } catch (IOException e1) { 124 e1.printStackTrace(); 125 } 126 } 127 } 128 }); 129 130 menu.add(item); 131 menu.addSeparator(); 132 133 item = new JMenuItem ("Reflect"); 135 item.addActionListener(new ActionListener () { 136 public void actionPerformed(ActionEvent e) { 137 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) 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 item = new JMenuItem ("Search"); 151 item.addActionListener(new ActionListener () { 152 public void actionPerformed(ActionEvent e) { 153 search(); 154 } 155 }); 156 menu.add(item); 157 158 item = new JMenuItem ("Search next"); 160 item.addActionListener(new ActionListener () { 161 public void actionPerformed(ActionEvent e) { 162 next(); 163 } 164 }); 165 menu.add(item); 166 } 167 return menu; 168 } 169 170 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 e) { 189 190 } 191 192 public void keyReleased(KeyEvent e) { 193 194 } 195 196 public void keyTyped(KeyEvent 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 ) jTree 207 .getLastSelectedPathComponent()).getUserObject()); 208 break; 209 } 210 } 211 212 private void maybeShowPopup(MouseEvent e) { 213 if (e.isPopupTrigger()) { 214 getMenu().show(e.getComponent(), e.getX(), e.getY()); 215 } 216 } 217 218 public void mouseClicked(MouseEvent e) { 219 220 } 221 222 public void mouseEntered(MouseEvent e) { 223 } 224 225 public void mouseExited(MouseEvent e) { 226 } 227 228 public void mousePressed(MouseEvent e) { 229 getJTree().setSelectionRow( 230 getJTree().getClosestRowForLocation(e.getX(), e.getY())); 231 maybeShowPopup(e); 232 } 233 234 public void mouseReleased(MouseEvent e) { 235 maybeShowPopup(e); 236 } 237 238 public DefaultMutableTreeNode next() { 239 DefaultMutableTreeNode current = null; 240 while (enume != null && enume.hasMoreElements()) { 241 current = (DefaultMutableTreeNode ) 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 search() { 252 searchValue = JOptionPane.showInputDialog(this, 253 "Enter value to search:", "Search"); 254 255 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) 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 node) { 269 TreePath path = new TreePath (node.getPath()); 270 getJTree().scrollPathToVisible(path); 271 getJTree().setSelectionPath(path); 272 } 273 274 } 275 | Popular Tags |