1 20 package com.puppycrawl.tools.checkstyle.gui; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Component ; 24 import java.awt.GridLayout ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.KeyEvent ; 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.util.TooManyListenersException ; 30 31 import javax.swing.AbstractAction ; 32 import javax.swing.Action ; 33 import javax.swing.JButton ; 34 import javax.swing.JFileChooser ; 35 import javax.swing.JOptionPane ; 36 import javax.swing.JPanel ; 37 import javax.swing.JScrollPane ; 38 import javax.swing.SwingUtilities ; 39 import javax.swing.filechooser.FileFilter ; 40 import javax.swing.JTextArea ; 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 56 public class ParseTreeInfoPanel extends JPanel 57 { 58 private JTreeTable mTreeTable; 59 private ParseTreeModel mParseTreeModel; 60 private JTextArea mJTextArea; 61 private File mLastDirectory = null; 62 private File mCurrentFile = null; 63 private final Action reloadAction; 64 65 private static class JavaFileFilter extends FileFilter 66 { 67 public boolean accept(File f) 68 { 69 if (f == null) { 70 return false; 71 } 72 return f.isDirectory() || f.getName().endsWith(".java"); 73 } 74 75 public String getDescription() 76 { 77 return "Java Source Code"; 78 } 79 } 80 81 private class FileSelectionAction extends AbstractAction 82 { 83 public FileSelectionAction() 84 { 85 super("Select Java File"); 86 putValue(Action.MNEMONIC_KEY, new Integer (KeyEvent.VK_S)); 87 } 88 89 public void actionPerformed(ActionEvent e) 90 { 91 final JFileChooser fc = new JFileChooser ( mLastDirectory ); 92 final FileFilter filter = new JavaFileFilter(); 93 fc.setFileFilter(filter); 94 final Component parent = 95 SwingUtilities.getRoot(ParseTreeInfoPanel.this); 96 fc.showDialog(parent, "Open"); 97 final File file = fc.getSelectedFile(); 98 openFile(file, parent); 99 100 } 101 } 102 103 private class ReloadAction extends AbstractAction 104 { 105 public ReloadAction() 106 { 107 super("Reload Java File"); 108 putValue(Action.MNEMONIC_KEY, new Integer (KeyEvent.VK_R)); 109 } 110 111 public void actionPerformed(ActionEvent e) 112 { 113 final Component 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 mSp; 123 124 public void filesDropped(File [] files) 125 { 126 if ((files != null) && (files.length > 0)) 127 { 128 final File file = files[0]; 129 openFile(file, mSp); 130 } 131 } 132 133 public FileDropListener(JScrollPane aSp) 134 { 135 mSp = aSp; 136 } 137 } 138 139 140 public void openFile(File aFile, final Component 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 [] sourceLines = Utils.getLines(aFile.getAbsolutePath()); 152 if (mJTextArea.getText().length() != 0) { 154 mJTextArea.replaceRange("", 0, mJTextArea.getText() 155 .length()); 156 } 157 158 for (int i = 0; i < sourceLines.length; i++) { 160 mJTextArea.append(sourceLines[i] + "\n"); 161 } 162 163 mJTextArea.moveCaretPosition(0); 165 } 166 catch (final IOException 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 186 public static DetailAST parseFile(String aFileName) 187 throws IOException , ANTLRException 188 { 189 final String [] lines = Utils.getLines(aFileName); 190 final FileContents contents = new FileContents(aFileName, lines); 191 return TreeWalker.parse(contents); 192 } 193 194 197 public ParseTreeInfoPanel() 198 { 199 setLayout(new BorderLayout ()); 200 201 final DetailAST treeRoot = null; 202 mParseTreeModel = new ParseTreeModel(treeRoot); 203 mTreeTable = new JTreeTable(mParseTreeModel); 204 final JScrollPane sp = new JScrollPane (mTreeTable); 205 this.add(sp, BorderLayout.NORTH); 206 207 final JButton fileSelectionButton = 208 new JButton (new FileSelectionAction()); 209 210 reloadAction = new ReloadAction(); 211 reloadAction.setEnabled(false); 212 final JButton reloadButton = new JButton (reloadAction); 213 214 mJTextArea = new JTextArea (20, 15); 215 mJTextArea.setEditable(false); 216 217 final JScrollPane sp2 = new JScrollPane (mJTextArea); 218 this.add(sp2, BorderLayout.CENTER); 219 220 final JPanel p = new JPanel (new GridLayout (1,2)); 221 this.add(p, BorderLayout.SOUTH); 222 p.add(fileSelectionButton); 223 p.add(reloadButton); 224 225 try { 226 new FileDrop(sp, new FileDropListener(sp)); 229 } 230 catch (final TooManyListenersException ex) 231 { 232 showErrorDialog(null, "Cannot initialize Drag and Drop support"); 233 } 234 235 } 236 237 private void showErrorDialog(final Component parent, final String msg) 238 { 239 final Runnable showError = new Runnable () 240 { 241 public void run() 242 { 243 JOptionPane.showMessageDialog(parent, msg); 244 } 245 }; 246 SwingUtilities.invokeLater(showError); 247 } 248 249 } 250 | Popular Tags |