1 package net.sourceforge.pmd.util.viewer.gui; 2 3 import net.sourceforge.pmd.TargetJDK1_3; 4 import net.sourceforge.pmd.TargetJDK1_4; 5 import net.sourceforge.pmd.TargetJDK1_5; 6 import net.sourceforge.pmd.TargetJDK1_6; 7 import net.sourceforge.pmd.TargetJDKVersion; 8 import net.sourceforge.pmd.ast.ParseException; 9 import net.sourceforge.pmd.util.viewer.model.ViewerModel; 10 import net.sourceforge.pmd.util.viewer.model.ViewerModelEvent; 11 import net.sourceforge.pmd.util.viewer.model.ViewerModelListener; 12 import net.sourceforge.pmd.util.viewer.util.NLS; 13 14 import javax.swing.*; 15 import java.awt.BorderLayout ; 16 import java.awt.FlowLayout ; 17 import java.awt.event.ActionEvent ; 18 import java.awt.event.ActionListener ; 19 20 21 27 28 public class MainFrame 29 extends JFrame 30 implements ActionListener , ActionCommands, ViewerModelListener { 31 private ViewerModel model; 32 private SourceCodePanel sourcePanel; 33 private ASTPanel astPanel; 34 private XPathPanel xPathPanel; 35 private JButton compileBtn; 36 private JButton evalBtn; 37 private JLabel statusLbl; 38 private JRadioButtonMenuItem jdk13MenuItem; 39 private JRadioButtonMenuItem jdk14MenuItem; 40 private JRadioButtonMenuItem jdk15MenuItem; 41 private JRadioButtonMenuItem jdk16MenuItem; 42 43 46 public MainFrame() { 47 super(NLS.nls("MAIN.FRAME.TITLE")); 48 init(); 49 } 50 51 private void init() { 52 model = new ViewerModel(); 53 model.addViewerModelListener(this); 54 sourcePanel = new SourceCodePanel(model); 55 astPanel = new ASTPanel(model); 56 xPathPanel = new XPathPanel(model); 57 getContentPane().setLayout(new BorderLayout ()); 58 JSplitPane editingPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sourcePanel, astPanel); 59 editingPane.setResizeWeight(0.5d); 60 JPanel interactionsPane = new JPanel(new BorderLayout ()); 61 interactionsPane.add(xPathPanel, BorderLayout.SOUTH); 62 interactionsPane.add(editingPane, BorderLayout.CENTER); 63 getContentPane().add(interactionsPane, BorderLayout.CENTER); 64 compileBtn = new JButton(NLS.nls("MAIN.FRAME.COMPILE_BUTTON.TITLE")); 65 compileBtn.setActionCommand(COMPILE_ACTION); 66 compileBtn.addActionListener(this); 67 evalBtn = new JButton(NLS.nls("MAIN.FRAME.EVALUATE_BUTTON.TITLE")); 68 evalBtn.setActionCommand(EVALUATE_ACTION); 69 evalBtn.addActionListener(this); 70 evalBtn.setEnabled(false); 71 statusLbl = new JLabel(); 72 statusLbl.setHorizontalAlignment(SwingConstants.RIGHT); 73 JPanel btnPane = new JPanel(new FlowLayout (FlowLayout.LEFT)); 74 btnPane.add(compileBtn); 75 btnPane.add(evalBtn); 76 btnPane.add(statusLbl); 77 getContentPane().add(btnPane, BorderLayout.SOUTH); 78 79 JMenuBar menuBar = new JMenuBar(); 80 JMenu menu = new JMenu("JDK"); 81 ButtonGroup group = new ButtonGroup(); 82 jdk13MenuItem = new JRadioButtonMenuItem("JDK 1.3"); 83 jdk13MenuItem.setSelected(false); 84 group.add(jdk13MenuItem); 85 menu.add(jdk13MenuItem); 86 jdk14MenuItem = new JRadioButtonMenuItem("JDK 1.4"); 87 jdk14MenuItem.setSelected(true); 88 group.add(jdk14MenuItem); 89 menu.add(jdk14MenuItem); 90 jdk15MenuItem = new JRadioButtonMenuItem("JDK 1.5"); 91 jdk15MenuItem.setSelected(false); 92 group.add(jdk15MenuItem); 93 menu.add(jdk15MenuItem); 94 jdk16MenuItem = new JRadioButtonMenuItem("JDK 1.6"); 95 jdk16MenuItem.setSelected(false); 96 group.add(jdk16MenuItem); 97 menu.add(jdk16MenuItem); 98 menuBar.add(menu); 99 setJMenuBar(menuBar); 100 101 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 102 pack(); 103 setSize(800, 600); 104 setVisible(true); 105 } 106 107 private TargetJDKVersion createJDKVersion() { 108 if (jdk14MenuItem.isSelected()) { 109 return new TargetJDK1_4(); 110 } else if (jdk13MenuItem.isSelected()) { 111 return new TargetJDK1_3(); 112 } else if (jdk16MenuItem.isSelected()) { 113 return new TargetJDK1_6(); 114 } 115 return new TargetJDK1_5(); 116 } 117 118 121 public void actionPerformed(ActionEvent e) { 122 String command = e.getActionCommand(); 123 long t0, t1; 124 if (command.equals(COMPILE_ACTION)) { 125 try { 126 t0 = System.currentTimeMillis(); 127 model.commitSource(sourcePanel.getSourceCode(), createJDKVersion()); 128 t1 = System.currentTimeMillis(); 129 setStatus(NLS.nls("MAIN.FRAME.COMPILATION.TOOK") + " " + (t1 - t0) + " ms"); 130 } catch (ParseException exc) { 131 setStatus(NLS.nls("MAIN.FRAME.COMPILATION.PROBLEM") + " " + exc.toString()); 132 new ParseExceptionHandler(this, exc); 133 } 134 } else if (command.equals(EVALUATE_ACTION)) { 135 try { 136 t0 = System.currentTimeMillis(); 137 model.evaluateXPathExpression(xPathPanel.getXPathExpression(), this); 138 t1 = System.currentTimeMillis(); 139 setStatus(NLS.nls("MAIN.FRAME.EVALUATION.TOOK") + " " + (t1 - t0) + " ms"); 140 } catch (Exception exc) { 141 setStatus(NLS.nls("MAIN.FRAME.EVALUATION.PROBLEM") + " " + exc.toString()); 142 new ParseExceptionHandler(this, exc); 143 } 144 } 145 } 146 147 152 private void setStatus(String string) { 153 statusLbl.setText(string == null ? "" : string); 154 } 155 156 159 public void viewerModelChanged(ViewerModelEvent e) { 160 evalBtn.setEnabled(model.hasCompiledTree()); 161 } 162 } 163 | Popular Tags |