KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > gui > ParseTreeInfoPanel


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2002 Oliver Burn
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation; either
8
// version 2.1 of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
// Lesser General Public License for more details.
14
//
15
// You should have received a copy of the GNU Lesser General Public
16
// License along with this library; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
////////////////////////////////////////////////////////////////////////////////
19

20 package com.puppycrawl.tools.checkstyle.gui;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.GridLayout JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.KeyEvent JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.TooManyListenersException JavaDoc;
30
31 import javax.swing.AbstractAction JavaDoc;
32 import javax.swing.Action JavaDoc;
33 import javax.swing.JButton JavaDoc;
34 import javax.swing.JFileChooser JavaDoc;
35 import javax.swing.JOptionPane JavaDoc;
36 import javax.swing.JPanel JavaDoc;
37 import javax.swing.JScrollPane JavaDoc;
38 import javax.swing.SwingUtilities JavaDoc;
39 import javax.swing.filechooser.FileFilter JavaDoc;
40 import javax.swing.JTextArea JavaDoc;
41
42 import antlr.ANTLRException;
43
44 import com.puppycrawl.tools.checkstyle.TreeWalker;
45 import com.puppycrawl.tools.checkstyle.api.DetailAST;
46 import com.puppycrawl.tools.checkstyle.api.FileContents;
47 import com.puppycrawl.tools.checkstyle.api.Utils;
48
49 /**
50  * Displays information about a parse tree.
51  * The user can change the file that is parsed and displayed
52  * through a JFileChooser.
53  *
54  * @author Lars Kühne
55  */

56 public class ParseTreeInfoPanel extends JPanel JavaDoc
57 {
58     private JTreeTable mTreeTable;
59     private ParseTreeModel mParseTreeModel;
60     private JTextArea JavaDoc mJTextArea;
61     private File JavaDoc mLastDirectory = null;
62     private File JavaDoc mCurrentFile = null;
63     private final Action JavaDoc reloadAction;
64
65     private static class JavaFileFilter extends FileFilter JavaDoc
66     {
67         public boolean accept(File JavaDoc f)
68         {
69             if (f == null) {
70                 return false;
71             }
72             return f.isDirectory() || f.getName().endsWith(".java");
73         }
74
75         public String JavaDoc getDescription()
76         {
77             return "Java Source Code";
78         }
79     }
80
81     private class FileSelectionAction extends AbstractAction JavaDoc
82     {
83         public FileSelectionAction()
84         {
85             super("Select Java File");
86             putValue(Action.MNEMONIC_KEY, new Integer JavaDoc(KeyEvent.VK_S));
87         }
88
89         public void actionPerformed(ActionEvent JavaDoc e)
90         {
91             final JFileChooser JavaDoc fc = new JFileChooser JavaDoc( mLastDirectory );
92             final FileFilter JavaDoc filter = new JavaFileFilter();
93             fc.setFileFilter(filter);
94             final Component JavaDoc parent =
95                 SwingUtilities.getRoot(ParseTreeInfoPanel.this);
96             fc.showDialog(parent, "Open");
97             final File JavaDoc file = fc.getSelectedFile();
98             openFile(file, parent);
99
100         }
101     }
102
103     private class ReloadAction extends AbstractAction JavaDoc
104     {
105         public ReloadAction()
106         {
107             super("Reload Java File");
108             putValue(Action.MNEMONIC_KEY, new Integer JavaDoc(KeyEvent.VK_R));
109         }
110
111         public void actionPerformed(ActionEvent JavaDoc e)
112         {
113             final Component JavaDoc parent =
114                 SwingUtilities.getRoot(ParseTreeInfoPanel.this);
115             openFile(mCurrentFile, parent);
116         }
117     }
118
119
120     private class FileDropListener implements FileDrop.Listener
121     {
122         private final JScrollPane JavaDoc mSp;
123
124         public void filesDropped(File JavaDoc[] files)
125         {
126             if ((files != null) && (files.length > 0))
127             {
128                 final File JavaDoc file = files[0];
129                 openFile(file, mSp);
130             }
131         }
132
133         public FileDropListener(JScrollPane JavaDoc aSp)
134         {
135             mSp = aSp;
136         }
137     }
138
139
140     public void openFile(File JavaDoc aFile, final Component JavaDoc aParent)
141     {
142         if (aFile != null) {
143             try {
144                 Main.frame.setTitle("Checkstyle : " + aFile.getName());
145                 final DetailAST parseTree = parseFile(aFile.getAbsolutePath());
146                 mParseTreeModel.setParseTree(parseTree);
147                 mCurrentFile = aFile;
148                 mLastDirectory = aFile.getParentFile();
149                 reloadAction.setEnabled(true);
150
151                 final String JavaDoc[] sourceLines = Utils.getLines(aFile.getAbsolutePath());
152                 //clean the text area before inserting the lines of the new file
153
if (mJTextArea.getText().length() != 0) {
154                     mJTextArea.replaceRange("", 0, mJTextArea.getText()
155                             .length());
156                 }
157
158                 // insert the contents of the file to the text area
159
for (int i = 0; i < sourceLines.length; i++) {
160                     mJTextArea.append(sourceLines[i] + "\n");
161                 }
162
163                 // move back to the top of the file
164
mJTextArea.moveCaretPosition(0);
165             }
166             catch (final IOException JavaDoc ex) {
167                 showErrorDialog(
168                         aParent,
169                         "Could not open " + aFile + ": " + ex.getMessage());
170             }
171             catch (final ANTLRException ex) {
172                 showErrorDialog(
173                         aParent,
174                         "Could not parse " + aFile + ": " + ex.getMessage());
175             }
176         }
177     }
178
179     /**
180      * Parses a file and returns the parse tree.
181      * @param aFileName the file to parse
182      * @return the root node of the parse tree
183      * @throws IOException if the file cannot be opened
184      * @throws ANTLRException if the file is not a Java source
185      */

186     public static DetailAST parseFile(String JavaDoc aFileName)
187         throws IOException JavaDoc, ANTLRException
188     {
189         final String JavaDoc[] lines = Utils.getLines(aFileName);
190         final FileContents contents = new FileContents(aFileName, lines);
191         return TreeWalker.parse(contents);
192     }
193
194     /**
195      * Create a new ParseTreeInfoPanel instance.
196      */

197     public ParseTreeInfoPanel()
198     {
199         setLayout(new BorderLayout JavaDoc());
200
201         final DetailAST treeRoot = null;
202         mParseTreeModel = new ParseTreeModel(treeRoot);
203         mTreeTable = new JTreeTable(mParseTreeModel);
204         final JScrollPane JavaDoc sp = new JScrollPane JavaDoc(mTreeTable);
205         this.add(sp, BorderLayout.NORTH);
206
207         final JButton JavaDoc fileSelectionButton =
208             new JButton JavaDoc(new FileSelectionAction());
209
210         reloadAction = new ReloadAction();
211         reloadAction.setEnabled(false);
212         final JButton JavaDoc reloadButton = new JButton JavaDoc(reloadAction);
213
214         mJTextArea = new JTextArea JavaDoc(20, 15);
215         mJTextArea.setEditable(false);
216
217         final JScrollPane JavaDoc sp2 = new JScrollPane JavaDoc(mJTextArea);
218         this.add(sp2, BorderLayout.CENTER);
219
220         final JPanel JavaDoc p = new JPanel JavaDoc(new GridLayout JavaDoc(1,2));
221         this.add(p, BorderLayout.SOUTH);
222         p.add(fileSelectionButton);
223         p.add(reloadButton);
224
225         try {
226             // TODO: creating an object for the side effect of the constructor
227
// and then ignoring the object looks strange.
228
new FileDrop(sp, new FileDropListener(sp));
229         }
230         catch (final TooManyListenersException JavaDoc ex)
231         {
232            showErrorDialog(null, "Cannot initialize Drag and Drop support");
233         }
234
235     }
236
237     private void showErrorDialog(final Component JavaDoc parent, final String JavaDoc msg)
238     {
239         final Runnable JavaDoc showError = new Runnable JavaDoc()
240         {
241             public void run()
242             {
243                 JOptionPane.showMessageDialog(parent, msg);
244             }
245         };
246         SwingUtilities.invokeLater(showError);
247     }
248
249 }
250
Popular Tags