001: /*
002: * Spoon - http://spoon.gforge.inria.fr/
003: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
004: *
005: * This software is governed by the CeCILL-C License under French law and
006: * abiding by the rules of distribution of free software. You can use, modify
007: * and/or redistribute the software under the terms of the CeCILL-C license as
008: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
009: *
010: * This program is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
013: *
014: * The fact that you are presently reading this means that you have had
015: * knowledge of the CeCILL-C license and that you accept its terms.
016: */
017:
018: package spoon.support.gui;
019:
020: import java.awt.GridLayout;
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.awt.event.KeyEvent;
024: import java.awt.event.KeyListener;
025: import java.awt.event.MouseEvent;
026: import java.awt.event.MouseListener;
027: import java.io.FileOutputStream;
028: import java.io.IOException;
029: import java.util.Enumeration;
030:
031: import javax.swing.JFileChooser;
032: import javax.swing.JFrame;
033: import javax.swing.JMenuItem;
034: import javax.swing.JOptionPane;
035: import javax.swing.JPanel;
036: import javax.swing.JPopupMenu;
037: import javax.swing.JScrollPane;
038: import javax.swing.JTree;
039: import javax.swing.tree.DefaultMutableTreeNode;
040: import javax.swing.tree.TreePath;
041:
042: import spoon.reflect.Factory;
043: import spoon.reflect.declaration.CtPackage;
044: import spoon.support.SerializationModelStreamer;
045:
046: public class SpoonModelTree extends JFrame implements KeyListener,
047: MouseListener {
048:
049: private static final long serialVersionUID = 1L;
050:
051: Enumeration<?> enume;
052:
053: private Factory factory;
054:
055: private JPanel jContentPane = null;
056:
057: private JScrollPane jScrollPane = null;
058:
059: private JTree jTree = null;
060:
061: JPopupMenu menu;
062:
063: private DefaultMutableTreeNode root; // @jve:decl-index=0:visual-constraint="207,57"
064:
065: String searchValue;
066:
067: /**
068: * This is the default constructor
069: */
070: public SpoonModelTree(Factory factory) {
071: super ();
072: SpoonTreeBuilder cst = new SpoonTreeBuilder();
073: for (CtPackage p : factory.Package().getAllRoots()) {
074: cst.scan(p);
075: }
076: this .factory = factory;
077: root = cst.getRoot();
078: initialize();
079: }
080:
081: /**
082: * This method initializes jContentPane
083: *
084: * @return javax.swing.JPanel
085: */
086: private JPanel getJContentPane() {
087: if (jContentPane == null) {
088: GridLayout gridLayout = new GridLayout();
089: gridLayout.setRows(1);
090: jContentPane = new JPanel();
091: jContentPane.setLayout(gridLayout);
092: jContentPane.add(getJScrollPane(), null);
093: }
094: return jContentPane;
095: }
096:
097: /**
098: * This method initializes jScrollPane
099: *
100: * @return javax.swing.JScrollPane
101: */
102: private JScrollPane getJScrollPane() {
103: if (jScrollPane == null) {
104: jScrollPane = new JScrollPane();
105: jScrollPane.setViewportView(getJTree());
106: }
107: return jScrollPane;
108: }
109:
110: /**
111: * This method initializes jTree
112: *
113: * @return javax.swing.JTree
114: */
115: private JTree getJTree() {
116: if (jTree == null) {
117: jTree = new JTree(root);
118: jTree.addKeyListener(this );
119: jTree.addMouseListener(this );
120: }
121: return jTree;
122: }
123:
124: private JPopupMenu getMenu() {
125: if (menu == null) {
126: menu = new JPopupMenu();
127:
128: JMenuItem item = new JMenuItem("Save");
129: item.addActionListener(new ActionListener() {
130: public void actionPerformed(ActionEvent e) {
131:
132: JFileChooser chooser = new JFileChooser();
133: chooser
134: .setFileSelectionMode(JFileChooser.FILES_ONLY);
135: boolean cont = chooser
136: .showSaveDialog(SpoonModelTree.this ) == JFileChooser.APPROVE_OPTION;
137: if (cont) {
138: SerializationModelStreamer ser = new SerializationModelStreamer();
139: try {
140: ser.save(factory, new FileOutputStream(
141: chooser.getSelectedFile()));
142: } catch (IOException e1) {
143: e1.printStackTrace();
144: }
145: }
146: }
147: });
148:
149: menu.add(item);
150: menu.addSeparator();
151:
152: // show reflect table
153: item = new JMenuItem("Reflect");
154: item.addActionListener(new ActionListener() {
155: public void actionPerformed(ActionEvent e) {
156: DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree
157: .getLastSelectedPathComponent();
158: if (node == null) {
159: node = root;
160: }
161: new SpoonObjectFieldsTable(node.getUserObject());
162: }
163: });
164: menu.add(item);
165:
166: menu.addSeparator();
167:
168: // Search value
169: item = new JMenuItem("Search");
170: item.addActionListener(new ActionListener() {
171: public void actionPerformed(ActionEvent e) {
172: search();
173: }
174: });
175: menu.add(item);
176:
177: // Search value
178: item = new JMenuItem("Search next");
179: item.addActionListener(new ActionListener() {
180: public void actionPerformed(ActionEvent e) {
181: next();
182: }
183: });
184: menu.add(item);
185: }
186: return menu;
187: }
188:
189: /**
190: * This method initializes this
191: *
192: * @return void
193: */
194: private void initialize() {
195: this .setSize(640, 480);
196: this
197: .setLocation(
198: (getGraphicsConfiguration().getDevice()
199: .getDisplayMode().getWidth() - getWidth()) / 2,
200: (getGraphicsConfiguration().getDevice()
201: .getDisplayMode().getHeight() - getHeight()) / 2);
202:
203: setContentPane(getJContentPane());
204: setTitle("Spoon");
205: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
206: this .setVisible(true);
207: }
208:
209: public void keyPressed(KeyEvent e) {
210:
211: }
212:
213: public void keyReleased(KeyEvent e) {
214:
215: }
216:
217: public void keyTyped(KeyEvent e) {
218: switch (e.getKeyChar()) {
219: case ('s'):
220:
221: break;
222: case ('n'):
223: next();
224: break;
225: case ('o'):
226: if (jTree.getLastSelectedPathComponent() != null) {
227: new SpoonObjectFieldsTable(
228: ((DefaultMutableTreeNode) jTree
229: .getLastSelectedPathComponent())
230: .getUserObject());
231: }
232: break;
233: }
234: }
235:
236: private void maybeShowPopup(MouseEvent e) {
237: if (e.isPopupTrigger()) {
238: getMenu().show(e.getComponent(), e.getX(), e.getY());
239: }
240: }
241:
242: public void mouseClicked(MouseEvent e) {
243:
244: }
245:
246: public void mouseEntered(MouseEvent e) {
247: }
248:
249: public void mouseExited(MouseEvent e) {
250: }
251:
252: public void mousePressed(MouseEvent e) {
253: getJTree()
254: .setSelectionRow(
255: getJTree().getClosestRowForLocation(e.getX(),
256: e.getY()));
257: maybeShowPopup(e);
258: }
259:
260: public void mouseReleased(MouseEvent e) {
261: maybeShowPopup(e);
262: }
263:
264: public DefaultMutableTreeNode next() {
265: DefaultMutableTreeNode current = null;
266: while ((enume != null) && enume.hasMoreElements()) {
267: current = (DefaultMutableTreeNode) enume.nextElement();
268: if ((current.getUserObject() != null)
269: && current.getUserObject().toString().contains(
270: searchValue)) {
271: setVisible(current);
272: return current;
273: }
274: }
275: return null;
276: }
277:
278: public DefaultMutableTreeNode search() {
279: searchValue = JOptionPane.showInputDialog(this ,
280: "Enter value to search:", "Search");
281:
282: DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree
283: .getLastSelectedPathComponent();
284: if (node == null) {
285: node = root;
286: }
287: enume = node.depthFirstEnumeration();
288:
289: if (searchValue != null) {
290: return next();
291: }
292: return null;
293: }
294:
295: public void setVisible(DefaultMutableTreeNode node) {
296: TreePath path = new TreePath(node.getPath());
297: getJTree().scrollPathToVisible(path);
298: getJTree().setSelectionPath(path);
299: }
300:
301: }
|