001: package org.acm.seguin.ide.common;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Color;
005: import java.awt.Component;
006: import java.awt.Frame;
007: import java.awt.GridBagConstraints;
008: import java.awt.GridBagLayout;
009: import java.awt.Insets;
010: import java.awt.Toolkit;
011: import java.awt.event.ActionEvent;
012: import java.awt.event.ActionListener;
013: import java.io.BufferedReader;
014: import java.io.File;
015: import java.io.FileReader;
016: import java.io.FileWriter;
017: import java.io.IOException;
018: import java.io.PrintStream;
019: import java.io.StringReader;
020: import java.util.Iterator;
021:
022: import javax.swing.BorderFactory;
023: import javax.swing.JButton;
024: import javax.swing.JFrame;
025: import javax.swing.JLabel;
026: import javax.swing.JPanel;
027: import javax.swing.JPanel;
028: import javax.swing.JScrollPane;
029: import javax.swing.JSplitPane;
030: import javax.swing.JTextArea;
031: import javax.swing.JTextPane;
032: import net.sourceforge.jrefactory.parser.JavaParser;
033: import net.sourceforge.jrefactory.parser.ParseException;
034:
035: import net.sourceforge.jrefactory.ast.ASTCompilationUnit;
036: import net.sourceforge.jrefactory.ast.SimpleNode;
037: import org.acm.seguin.pmd.jaxen.DocumentNavigator;
038:
039: import org.jaxen.BaseXPath;
040: import org.jaxen.JaxenException;
041: import org.jaxen.XPath;
042:
043: /**
044: * Description of the Class
045: *
046: *@author Mike Atkinson
047: */
048: public class ASTViewerPane extends JPanel {
049:
050: private JTextArea astArea = new JTextArea();
051: private JTextArea xpathResultArea = new JTextArea();
052: private JTextArea xpathQueryArea = new JTextArea(6, 40);
053:
054: private JSplitPane upperSplitPane;
055: private Frame view;
056:
057: private final static String SETTINGS_FILE_NAME = System
058: .getProperty("user.home")
059: + System.getProperty("file.separator") + ".pmd_astviewer";
060:
061: /**
062: * Constructor for the ASTViewerPane object
063: *
064: *@param aView Description of Parameter
065: */
066: public ASTViewerPane(Frame aView) {
067: this .view = aView;
068: setLayout(new BorderLayout());
069:
070: JSmartPanel astPanel = new JSmartPanel();
071: astArea.setRows(20);
072: astArea.setColumns(20);
073: JScrollPane astScrollPane = new JScrollPane(astArea);
074: astPanel.add(astScrollPane, 0, 0, 1, 1, 1.0, 1.0,
075: GridBagConstraints.NORTH, GridBagConstraints.BOTH,
076: new Insets(0, 0, 0, 0));
077:
078: JSmartPanel xpathResultPanel = new JSmartPanel();
079: xpathResultArea.setRows(20);
080: xpathResultArea.setColumns(20);
081: JScrollPane xpathResultScrollPane = new JScrollPane(
082: xpathResultArea);
083: xpathResultPanel.add(xpathResultScrollPane, 0, 0, 1, 1, 1.0,
084: 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH,
085: new Insets(0, 0, 0, 0));
086:
087: JButton goButton = new JButton("Go");
088: goButton.setMnemonic('g');
089: goButton.addActionListener(new ShowListener());
090: goButton.addActionListener(new XPathListener());
091:
092: JPanel controlPanel = new JPanel();
093: controlPanel.setLayout(new BorderLayout());
094: controlPanel.add(new JLabel("XPath Query (if any) ->"),
095: BorderLayout.NORTH);
096: xpathQueryArea.setBorder(BorderFactory
097: .createLineBorder(Color.black));
098: controlPanel.add(new JScrollPane(xpathQueryArea));
099: controlPanel.add(goButton, BorderLayout.EAST);
100:
101: upperSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
102: astPanel, xpathResultPanel);
103:
104: add(upperSplitPane);
105: add(controlPanel, BorderLayout.SOUTH);
106: }
107:
108: public void setView(Frame view) {
109: this .view = view;
110: }
111:
112: /** Description of the Method */
113: public void initDividers() {
114: upperSplitPane.setDividerLocation(2 * upperSplitPane
115: .getMaximumDividerLocation() / 3);
116: }
117:
118: /**
119: * Description of the Class
120: *
121: *@author Mike Atkinson
122: */
123: private class ShowListener implements ActionListener {
124: /**
125: * Description of the Method
126: *
127: *@param ae Description of Parameter
128: */
129: public void actionPerformed(ActionEvent ae) {
130: //Buffer buffer = view.getBuffer();
131: //StringReader sr = new StringReader(buffer.getText(0, buffer.getLength()));
132: StringReader sr = new StringReader(IDEPlugin.getText(view,
133: null));
134: JavaParser parser = new JavaParser(sr);
135: //MyPrintStream ps = new MyPrintStream();
136: //System.setOut(ps);
137: try {
138: ASTCompilationUnit c = parser.CompilationUnit();
139: //c.dump("");
140: astArea.setText(c.dumpString("\r\n"));
141: } catch (ParseException pe) {
142: astArea.setText(pe.fillInStackTrace().getMessage());
143: }
144: }
145: }
146:
147: /**
148: * Description of the Class
149: *
150: *@author Mike Atkinson
151: */
152: private class XPathListener implements ActionListener {
153: /**
154: * Description of the Method
155: *
156: *@param ae Description of Parameter
157: */
158: public void actionPerformed(ActionEvent ae) {
159: if (xpathQueryArea.getText().length() == 0) {
160: xpathResultArea.setText("XPath query field is empty");
161: return;
162: }
163: //Buffer buffer = view.getBuffer();
164: //StringReader sr = new StringReader(buffer.getText(0, buffer.getLength()));
165: StringReader sr = new StringReader(IDEPlugin.getText(view,
166: null));
167: JavaParser parser = new JavaParser(sr);
168: try {
169: XPath xpath = new BaseXPath(xpathQueryArea.getText(),
170: new DocumentNavigator());
171: ASTCompilationUnit c = parser.CompilationUnit();
172: StringBuffer sb = new StringBuffer();
173: for (Iterator iter = xpath.selectNodes(c).iterator(); iter
174: .hasNext();) {
175: SimpleNode node = (SimpleNode) iter.next();
176: String name = node.getClass().getName()
177: .substring(
178: node.getClass().getName()
179: .lastIndexOf('.') + 1);
180: String line = " at line "
181: + String.valueOf(node.getBeginLine());
182: sb.append(name).append(line).append(
183: System.getProperty("line.separator"));
184: }
185: xpathResultArea.setText(sb.toString());
186: if (sb.length() == 0) {
187: xpathResultArea.setText("No results returned "
188: + System.currentTimeMillis());
189: }
190: } catch (ParseException pe) {
191: xpathResultArea.setText(pe.fillInStackTrace()
192: .getMessage());
193: } catch (JaxenException je) {
194: xpathResultArea.setText(je.fillInStackTrace()
195: .getMessage());
196: }
197: xpathQueryArea.requestFocus();
198: }
199: }
200:
201: /**
202: * Description of the Class
203: *
204: *@author Mike Atkinson
205: */
206: public static class JSmartPanel extends JPanel {
207:
208: private GridBagConstraints constraints = new GridBagConstraints();
209:
210: /** Constructor for the JSmartPanel object */
211: public JSmartPanel() {
212: super (new GridBagLayout());
213: }
214:
215: /**
216: * Description of the Method
217: *
218: *@param comp Description of Parameter
219: *@param gridx Description of Parameter
220: *@param gridy Description of Parameter
221: *@param gridwidth Description of Parameter
222: *@param gridheight Description of Parameter
223: *@param weightx Description of Parameter
224: *@param weighty Description of Parameter
225: *@param anchor Description of Parameter
226: *@param fill Description of Parameter
227: *@param insets Description of Parameter
228: */
229: public void add(Component comp, int gridx, int gridy,
230: int gridwidth, int gridheight, double weightx,
231: double weighty, int anchor, int fill, Insets insets) {
232: constraints.gridx = gridx;
233: constraints.gridy = gridy;
234: constraints.gridwidth = gridwidth;
235: constraints.gridheight = gridheight;
236: constraints.weightx = weightx;
237: constraints.weighty = weighty;
238: constraints.anchor = anchor;
239: constraints.fill = fill;
240: constraints.insets = insets;
241:
242: add(comp, constraints);
243: }
244: }
245:
246: /**
247: * Description of the Class
248: *
249: *@author Mike Atkinson
250: */
251: private static class MyPrintStream extends PrintStream {
252:
253: private StringBuffer buf = new StringBuffer();
254:
255: /** Constructor for the MyPrintStream object */
256: public MyPrintStream() {
257: super (System.out);
258: }
259:
260: /**
261: * Gets the String attribute of the MyPrintStream object
262: *
263: *@return The String value
264: */
265: public String getString() {
266: return buf.toString();
267: }
268:
269: /**
270: * Description of the Method
271: *
272: *@param s Description of Parameter
273: */
274: public void println(String s) {
275: super .println(s);
276: buf.append(s);
277: buf.append(System.getProperty("line.separator"));
278: }
279: }
280:
281: }
|